]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/net/e1000/e1000_hw.c
e1000: updated whitespace and comments
[mv-sheeva.git] / drivers / net / e1000 / e1000_hw.c
1 /*******************************************************************************
2
3   Intel PRO/1000 Linux driver
4   Copyright(c) 1999 - 2006 Intel Corporation.
5
6   This program is free software; you can redistribute it and/or modify it
7   under the terms and conditions of the GNU General Public License,
8   version 2, as published by the Free Software Foundation.
9
10   This program is distributed in the hope it will be useful, but WITHOUT
11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13   more details.
14
15   You should have received a copy of the GNU General Public License along with
16   this program; if not, write to the Free Software Foundation, Inc.,
17   51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18
19   The full GNU General Public License is included in this distribution in
20   the file called "COPYING".
21
22   Contact Information:
23   Linux NICS <linux.nics@intel.com>
24   e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
25   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26
27  */
28
29 /* e1000_hw.c
30  * Shared functions for accessing and configuring the MAC
31  */
32
33 #include "e1000_hw.h"
34
35 static s32 e1000_check_downshift(struct e1000_hw *hw);
36 static s32 e1000_check_polarity(struct e1000_hw *hw,
37                                 e1000_rev_polarity *polarity);
38 static void e1000_clear_hw_cntrs(struct e1000_hw *hw);
39 static void e1000_clear_vfta(struct e1000_hw *hw);
40 static s32 e1000_config_dsp_after_link_change(struct e1000_hw *hw,
41                                               bool link_up);
42 static s32 e1000_config_fc_after_link_up(struct e1000_hw *hw);
43 static s32 e1000_detect_gig_phy(struct e1000_hw *hw);
44 static s32 e1000_get_auto_rd_done(struct e1000_hw *hw);
45 static s32 e1000_get_cable_length(struct e1000_hw *hw, u16 *min_length,
46                                   u16 *max_length);
47 static s32 e1000_get_phy_cfg_done(struct e1000_hw *hw);
48 static s32 e1000_id_led_init(struct e1000_hw *hw);
49 static void e1000_init_rx_addrs(struct e1000_hw *hw);
50 static s32 e1000_phy_igp_get_info(struct e1000_hw *hw,
51                                   struct e1000_phy_info *phy_info);
52 static s32 e1000_read_eeprom_eerd(struct e1000_hw *hw, u16 offset, u16 words,
53                                   u16 *data);
54 static s32 e1000_write_eeprom_eewr(struct e1000_hw *hw, u16 offset, u16 words,
55                                    u16 *data);
56 static s32 e1000_poll_eerd_eewr_done(struct e1000_hw *hw, int eerd);
57 static s32 e1000_phy_m88_get_info(struct e1000_hw *hw,
58                                   struct e1000_phy_info *phy_info);
59 static s32 e1000_set_d3_lplu_state(struct e1000_hw *hw, bool active);
60 static s32 e1000_wait_autoneg(struct e1000_hw *hw);
61 static void e1000_write_reg_io(struct e1000_hw *hw, u32 offset, u32 value);
62 static s32 e1000_set_phy_type(struct e1000_hw *hw);
63 static void e1000_phy_init_script(struct e1000_hw *hw);
64 static s32 e1000_setup_copper_link(struct e1000_hw *hw);
65 static s32 e1000_setup_fiber_serdes_link(struct e1000_hw *hw);
66 static s32 e1000_adjust_serdes_amplitude(struct e1000_hw *hw);
67 static s32 e1000_phy_force_speed_duplex(struct e1000_hw *hw);
68 static s32 e1000_config_mac_to_phy(struct e1000_hw *hw);
69 static void e1000_raise_mdi_clk(struct e1000_hw *hw, u32 *ctrl);
70 static void e1000_lower_mdi_clk(struct e1000_hw *hw, u32 *ctrl);
71 static void e1000_shift_out_mdi_bits(struct e1000_hw *hw, u32 data, u16 count);
72 static u16 e1000_shift_in_mdi_bits(struct e1000_hw *hw);
73 static s32 e1000_phy_reset_dsp(struct e1000_hw *hw);
74 static s32 e1000_write_eeprom_spi(struct e1000_hw *hw, u16 offset,
75                                   u16 words, u16 *data);
76 static s32 e1000_write_eeprom_microwire(struct e1000_hw *hw, u16 offset,
77                                         u16 words, u16 *data);
78 static s32 e1000_spi_eeprom_ready(struct e1000_hw *hw);
79 static void e1000_raise_ee_clk(struct e1000_hw *hw, u32 *eecd);
80 static void e1000_lower_ee_clk(struct e1000_hw *hw, u32 *eecd);
81 static void e1000_shift_out_ee_bits(struct e1000_hw *hw, u16 data, u16 count);
82 static s32 e1000_write_phy_reg_ex(struct e1000_hw *hw, u32 reg_addr,
83                                   u16 phy_data);
84 static s32 e1000_read_phy_reg_ex(struct e1000_hw *hw, u32 reg_addr,
85                                  u16 *phy_data);
86 static u16 e1000_shift_in_ee_bits(struct e1000_hw *hw, u16 count);
87 static s32 e1000_acquire_eeprom(struct e1000_hw *hw);
88 static void e1000_release_eeprom(struct e1000_hw *hw);
89 static void e1000_standby_eeprom(struct e1000_hw *hw);
90 static s32 e1000_set_vco_speed(struct e1000_hw *hw);
91 static s32 e1000_polarity_reversal_workaround(struct e1000_hw *hw);
92 static s32 e1000_set_phy_mode(struct e1000_hw *hw);
93 static s32 e1000_do_read_eeprom(struct e1000_hw *hw, u16 offset, u16 words,
94                                 u16 *data);
95 static s32 e1000_do_write_eeprom(struct e1000_hw *hw, u16 offset, u16 words,
96                                  u16 *data);
97
98 /* IGP cable length table */
99 static const
100 u16 e1000_igp_cable_length_table[IGP01E1000_AGC_LENGTH_TABLE_SIZE] = {
101         5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
102         5, 10, 10, 10, 10, 10, 10, 10, 20, 20, 20, 20, 20, 25, 25, 25,
103         25, 25, 25, 25, 30, 30, 30, 30, 40, 40, 40, 40, 40, 40, 40, 40,
104         40, 50, 50, 50, 50, 50, 50, 50, 60, 60, 60, 60, 60, 60, 60, 60,
105         60, 70, 70, 70, 70, 70, 70, 80, 80, 80, 80, 80, 80, 90, 90, 90,
106         90, 90, 90, 90, 90, 90, 100, 100, 100, 100, 100, 100, 100, 100, 100,
107             100,
108         100, 100, 100, 100, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110,
109             110, 110,
110         110, 110, 110, 110, 110, 110, 120, 120, 120, 120, 120, 120, 120, 120,
111             120, 120
112 };
113
114 static DEFINE_SPINLOCK(e1000_eeprom_lock);
115
116 /**
117  * e1000_set_phy_type - Set the phy type member in the hw struct.
118  * @hw: Struct containing variables accessed by shared code
119  */
120 static s32 e1000_set_phy_type(struct e1000_hw *hw)
121 {
122         DEBUGFUNC("e1000_set_phy_type");
123
124         if (hw->mac_type == e1000_undefined)
125                 return -E1000_ERR_PHY_TYPE;
126
127         switch (hw->phy_id) {
128         case M88E1000_E_PHY_ID:
129         case M88E1000_I_PHY_ID:
130         case M88E1011_I_PHY_ID:
131         case M88E1111_I_PHY_ID:
132                 hw->phy_type = e1000_phy_m88;
133                 break;
134         case IGP01E1000_I_PHY_ID:
135                 if (hw->mac_type == e1000_82541 ||
136                     hw->mac_type == e1000_82541_rev_2 ||
137                     hw->mac_type == e1000_82547 ||
138                     hw->mac_type == e1000_82547_rev_2) {
139                         hw->phy_type = e1000_phy_igp;
140                         break;
141                 }
142         default:
143                 /* Should never have loaded on this device */
144                 hw->phy_type = e1000_phy_undefined;
145                 return -E1000_ERR_PHY_TYPE;
146         }
147
148         return E1000_SUCCESS;
149 }
150
151 /**
152  * e1000_phy_init_script - IGP phy init script - initializes the GbE PHY
153  * @hw: Struct containing variables accessed by shared code
154  */
155 static void e1000_phy_init_script(struct e1000_hw *hw)
156 {
157         u32 ret_val;
158         u16 phy_saved_data;
159
160         DEBUGFUNC("e1000_phy_init_script");
161
162         if (hw->phy_init_script) {
163                 msleep(20);
164
165                 /* Save off the current value of register 0x2F5B to be restored at
166                  * the end of this routine. */
167                 ret_val = e1000_read_phy_reg(hw, 0x2F5B, &phy_saved_data);
168
169                 /* Disabled the PHY transmitter */
170                 e1000_write_phy_reg(hw, 0x2F5B, 0x0003);
171                 msleep(20);
172
173                 e1000_write_phy_reg(hw, 0x0000, 0x0140);
174                 msleep(5);
175
176                 switch (hw->mac_type) {
177                 case e1000_82541:
178                 case e1000_82547:
179                         e1000_write_phy_reg(hw, 0x1F95, 0x0001);
180                         e1000_write_phy_reg(hw, 0x1F71, 0xBD21);
181                         e1000_write_phy_reg(hw, 0x1F79, 0x0018);
182                         e1000_write_phy_reg(hw, 0x1F30, 0x1600);
183                         e1000_write_phy_reg(hw, 0x1F31, 0x0014);
184                         e1000_write_phy_reg(hw, 0x1F32, 0x161C);
185                         e1000_write_phy_reg(hw, 0x1F94, 0x0003);
186                         e1000_write_phy_reg(hw, 0x1F96, 0x003F);
187                         e1000_write_phy_reg(hw, 0x2010, 0x0008);
188                         break;
189
190                 case e1000_82541_rev_2:
191                 case e1000_82547_rev_2:
192                         e1000_write_phy_reg(hw, 0x1F73, 0x0099);
193                         break;
194                 default:
195                         break;
196                 }
197
198                 e1000_write_phy_reg(hw, 0x0000, 0x3300);
199                 msleep(20);
200
201                 /* Now enable the transmitter */
202                 e1000_write_phy_reg(hw, 0x2F5B, phy_saved_data);
203
204                 if (hw->mac_type == e1000_82547) {
205                         u16 fused, fine, coarse;
206
207                         /* Move to analog registers page */
208                         e1000_read_phy_reg(hw,
209                                            IGP01E1000_ANALOG_SPARE_FUSE_STATUS,
210                                            &fused);
211
212                         if (!(fused & IGP01E1000_ANALOG_SPARE_FUSE_ENABLED)) {
213                                 e1000_read_phy_reg(hw,
214                                                    IGP01E1000_ANALOG_FUSE_STATUS,
215                                                    &fused);
216
217                                 fine = fused & IGP01E1000_ANALOG_FUSE_FINE_MASK;
218                                 coarse =
219                                     fused & IGP01E1000_ANALOG_FUSE_COARSE_MASK;
220
221                                 if (coarse >
222                                     IGP01E1000_ANALOG_FUSE_COARSE_THRESH) {
223                                         coarse -=
224                                             IGP01E1000_ANALOG_FUSE_COARSE_10;
225                                         fine -= IGP01E1000_ANALOG_FUSE_FINE_1;
226                                 } else if (coarse ==
227                                            IGP01E1000_ANALOG_FUSE_COARSE_THRESH)
228                                         fine -= IGP01E1000_ANALOG_FUSE_FINE_10;
229
230                                 fused =
231                                     (fused & IGP01E1000_ANALOG_FUSE_POLY_MASK) |
232                                     (fine & IGP01E1000_ANALOG_FUSE_FINE_MASK) |
233                                     (coarse &
234                                      IGP01E1000_ANALOG_FUSE_COARSE_MASK);
235
236                                 e1000_write_phy_reg(hw,
237                                                     IGP01E1000_ANALOG_FUSE_CONTROL,
238                                                     fused);
239                                 e1000_write_phy_reg(hw,
240                                                     IGP01E1000_ANALOG_FUSE_BYPASS,
241                                                     IGP01E1000_ANALOG_FUSE_ENABLE_SW_CONTROL);
242                         }
243                 }
244         }
245 }
246
247 /**
248  * e1000_set_mac_type - Set the mac type member in the hw struct.
249  * @hw: Struct containing variables accessed by shared code
250  */
251 s32 e1000_set_mac_type(struct e1000_hw *hw)
252 {
253         DEBUGFUNC("e1000_set_mac_type");
254
255         switch (hw->device_id) {
256         case E1000_DEV_ID_82542:
257                 switch (hw->revision_id) {
258                 case E1000_82542_2_0_REV_ID:
259                         hw->mac_type = e1000_82542_rev2_0;
260                         break;
261                 case E1000_82542_2_1_REV_ID:
262                         hw->mac_type = e1000_82542_rev2_1;
263                         break;
264                 default:
265                         /* Invalid 82542 revision ID */
266                         return -E1000_ERR_MAC_TYPE;
267                 }
268                 break;
269         case E1000_DEV_ID_82543GC_FIBER:
270         case E1000_DEV_ID_82543GC_COPPER:
271                 hw->mac_type = e1000_82543;
272                 break;
273         case E1000_DEV_ID_82544EI_COPPER:
274         case E1000_DEV_ID_82544EI_FIBER:
275         case E1000_DEV_ID_82544GC_COPPER:
276         case E1000_DEV_ID_82544GC_LOM:
277                 hw->mac_type = e1000_82544;
278                 break;
279         case E1000_DEV_ID_82540EM:
280         case E1000_DEV_ID_82540EM_LOM:
281         case E1000_DEV_ID_82540EP:
282         case E1000_DEV_ID_82540EP_LOM:
283         case E1000_DEV_ID_82540EP_LP:
284                 hw->mac_type = e1000_82540;
285                 break;
286         case E1000_DEV_ID_82545EM_COPPER:
287         case E1000_DEV_ID_82545EM_FIBER:
288                 hw->mac_type = e1000_82545;
289                 break;
290         case E1000_DEV_ID_82545GM_COPPER:
291         case E1000_DEV_ID_82545GM_FIBER:
292         case E1000_DEV_ID_82545GM_SERDES:
293                 hw->mac_type = e1000_82545_rev_3;
294                 break;
295         case E1000_DEV_ID_82546EB_COPPER:
296         case E1000_DEV_ID_82546EB_FIBER:
297         case E1000_DEV_ID_82546EB_QUAD_COPPER:
298                 hw->mac_type = e1000_82546;
299                 break;
300         case E1000_DEV_ID_82546GB_COPPER:
301         case E1000_DEV_ID_82546GB_FIBER:
302         case E1000_DEV_ID_82546GB_SERDES:
303         case E1000_DEV_ID_82546GB_PCIE:
304         case E1000_DEV_ID_82546GB_QUAD_COPPER:
305         case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
306                 hw->mac_type = e1000_82546_rev_3;
307                 break;
308         case E1000_DEV_ID_82541EI:
309         case E1000_DEV_ID_82541EI_MOBILE:
310         case E1000_DEV_ID_82541ER_LOM:
311                 hw->mac_type = e1000_82541;
312                 break;
313         case E1000_DEV_ID_82541ER:
314         case E1000_DEV_ID_82541GI:
315         case E1000_DEV_ID_82541GI_LF:
316         case E1000_DEV_ID_82541GI_MOBILE:
317                 hw->mac_type = e1000_82541_rev_2;
318                 break;
319         case E1000_DEV_ID_82547EI:
320         case E1000_DEV_ID_82547EI_MOBILE:
321                 hw->mac_type = e1000_82547;
322                 break;
323         case E1000_DEV_ID_82547GI:
324                 hw->mac_type = e1000_82547_rev_2;
325                 break;
326         default:
327                 /* Should never have loaded on this device */
328                 return -E1000_ERR_MAC_TYPE;
329         }
330
331         switch (hw->mac_type) {
332         case e1000_82541:
333         case e1000_82547:
334         case e1000_82541_rev_2:
335         case e1000_82547_rev_2:
336                 hw->asf_firmware_present = true;
337                 break;
338         default:
339                 break;
340         }
341
342         /* The 82543 chip does not count tx_carrier_errors properly in
343          * FD mode
344          */
345         if (hw->mac_type == e1000_82543)
346                 hw->bad_tx_carr_stats_fd = true;
347
348         if (hw->mac_type > e1000_82544)
349                 hw->has_smbus = true;
350
351         return E1000_SUCCESS;
352 }
353
354 /**
355  * e1000_set_media_type - Set media type and TBI compatibility.
356  * @hw: Struct containing variables accessed by shared code
357  */
358 void e1000_set_media_type(struct e1000_hw *hw)
359 {
360         u32 status;
361
362         DEBUGFUNC("e1000_set_media_type");
363
364         if (hw->mac_type != e1000_82543) {
365                 /* tbi_compatibility is only valid on 82543 */
366                 hw->tbi_compatibility_en = false;
367         }
368
369         switch (hw->device_id) {
370         case E1000_DEV_ID_82545GM_SERDES:
371         case E1000_DEV_ID_82546GB_SERDES:
372                 hw->media_type = e1000_media_type_internal_serdes;
373                 break;
374         default:
375                 switch (hw->mac_type) {
376                 case e1000_82542_rev2_0:
377                 case e1000_82542_rev2_1:
378                         hw->media_type = e1000_media_type_fiber;
379                         break;
380                 default:
381                         status = er32(STATUS);
382                         if (status & E1000_STATUS_TBIMODE) {
383                                 hw->media_type = e1000_media_type_fiber;
384                                 /* tbi_compatibility not valid on fiber */
385                                 hw->tbi_compatibility_en = false;
386                         } else {
387                                 hw->media_type = e1000_media_type_copper;
388                         }
389                         break;
390                 }
391         }
392 }
393
394 /**
395  * e1000_reset_hw: reset the hardware completely
396  * @hw: Struct containing variables accessed by shared code
397  *
398  * Reset the transmit and receive units; mask and clear all interrupts.
399  */
400 s32 e1000_reset_hw(struct e1000_hw *hw)
401 {
402         u32 ctrl;
403         u32 ctrl_ext;
404         u32 icr;
405         u32 manc;
406         u32 led_ctrl;
407         s32 ret_val;
408
409         DEBUGFUNC("e1000_reset_hw");
410
411         /* For 82542 (rev 2.0), disable MWI before issuing a device reset */
412         if (hw->mac_type == e1000_82542_rev2_0) {
413                 DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
414                 e1000_pci_clear_mwi(hw);
415         }
416
417         /* Clear interrupt mask to stop board from generating interrupts */
418         DEBUGOUT("Masking off all interrupts\n");
419         ew32(IMC, 0xffffffff);
420
421         /* Disable the Transmit and Receive units.  Then delay to allow
422          * any pending transactions to complete before we hit the MAC with
423          * the global reset.
424          */
425         ew32(RCTL, 0);
426         ew32(TCTL, E1000_TCTL_PSP);
427         E1000_WRITE_FLUSH();
428
429         /* The tbi_compatibility_on Flag must be cleared when Rctl is cleared. */
430         hw->tbi_compatibility_on = false;
431
432         /* Delay to allow any outstanding PCI transactions to complete before
433          * resetting the device
434          */
435         msleep(10);
436
437         ctrl = er32(CTRL);
438
439         /* Must reset the PHY before resetting the MAC */
440         if ((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
441                 ew32(CTRL, (ctrl | E1000_CTRL_PHY_RST));
442                 msleep(5);
443         }
444
445         /* Issue a global reset to the MAC.  This will reset the chip's
446          * transmit, receive, DMA, and link units.  It will not effect
447          * the current PCI configuration.  The global reset bit is self-
448          * clearing, and should clear within a microsecond.
449          */
450         DEBUGOUT("Issuing a global reset to MAC\n");
451
452         switch (hw->mac_type) {
453         case e1000_82544:
454         case e1000_82540:
455         case e1000_82545:
456         case e1000_82546:
457         case e1000_82541:
458         case e1000_82541_rev_2:
459                 /* These controllers can't ack the 64-bit write when issuing the
460                  * reset, so use IO-mapping as a workaround to issue the reset */
461                 E1000_WRITE_REG_IO(hw, CTRL, (ctrl | E1000_CTRL_RST));
462                 break;
463         case e1000_82545_rev_3:
464         case e1000_82546_rev_3:
465                 /* Reset is performed on a shadow of the control register */
466                 ew32(CTRL_DUP, (ctrl | E1000_CTRL_RST));
467                 break;
468         default:
469                 ew32(CTRL, (ctrl | E1000_CTRL_RST));
470                 break;
471         }
472
473         /* After MAC reset, force reload of EEPROM to restore power-on settings to
474          * device.  Later controllers reload the EEPROM automatically, so just wait
475          * for reload to complete.
476          */
477         switch (hw->mac_type) {
478         case e1000_82542_rev2_0:
479         case e1000_82542_rev2_1:
480         case e1000_82543:
481         case e1000_82544:
482                 /* Wait for reset to complete */
483                 udelay(10);
484                 ctrl_ext = er32(CTRL_EXT);
485                 ctrl_ext |= E1000_CTRL_EXT_EE_RST;
486                 ew32(CTRL_EXT, ctrl_ext);
487                 E1000_WRITE_FLUSH();
488                 /* Wait for EEPROM reload */
489                 msleep(2);
490                 break;
491         case e1000_82541:
492         case e1000_82541_rev_2:
493         case e1000_82547:
494         case e1000_82547_rev_2:
495                 /* Wait for EEPROM reload */
496                 msleep(20);
497                 break;
498         default:
499                 /* Auto read done will delay 5ms or poll based on mac type */
500                 ret_val = e1000_get_auto_rd_done(hw);
501                 if (ret_val)
502                         return ret_val;
503                 break;
504         }
505
506         /* Disable HW ARPs on ASF enabled adapters */
507         if (hw->mac_type >= e1000_82540) {
508                 manc = er32(MANC);
509                 manc &= ~(E1000_MANC_ARP_EN);
510                 ew32(MANC, manc);
511         }
512
513         if ((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
514                 e1000_phy_init_script(hw);
515
516                 /* Configure activity LED after PHY reset */
517                 led_ctrl = er32(LEDCTL);
518                 led_ctrl &= IGP_ACTIVITY_LED_MASK;
519                 led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
520                 ew32(LEDCTL, led_ctrl);
521         }
522
523         /* Clear interrupt mask to stop board from generating interrupts */
524         DEBUGOUT("Masking off all interrupts\n");
525         ew32(IMC, 0xffffffff);
526
527         /* Clear any pending interrupt events. */
528         icr = er32(ICR);
529
530         /* If MWI was previously enabled, reenable it. */
531         if (hw->mac_type == e1000_82542_rev2_0) {
532                 if (hw->pci_cmd_word & PCI_COMMAND_INVALIDATE)
533                         e1000_pci_set_mwi(hw);
534         }
535
536         return E1000_SUCCESS;
537 }
538
539 /**
540  * e1000_init_hw: Performs basic configuration of the adapter.
541  * @hw: Struct containing variables accessed by shared code
542  *
543  * Assumes that the controller has previously been reset and is in a
544  * post-reset uninitialized state. Initializes the receive address registers,
545  * multicast table, and VLAN filter table. Calls routines to setup link
546  * configuration and flow control settings. Clears all on-chip counters. Leaves
547  * the transmit and receive units disabled and uninitialized.
548  */
549 s32 e1000_init_hw(struct e1000_hw *hw)
550 {
551         u32 ctrl;
552         u32 i;
553         s32 ret_val;
554         u32 mta_size;
555         u32 ctrl_ext;
556
557         DEBUGFUNC("e1000_init_hw");
558
559         /* Initialize Identification LED */
560         ret_val = e1000_id_led_init(hw);
561         if (ret_val) {
562                 DEBUGOUT("Error Initializing Identification LED\n");
563                 return ret_val;
564         }
565
566         /* Set the media type and TBI compatibility */
567         e1000_set_media_type(hw);
568
569         /* Disabling VLAN filtering. */
570         DEBUGOUT("Initializing the IEEE VLAN\n");
571         if (hw->mac_type < e1000_82545_rev_3)
572                 ew32(VET, 0);
573         e1000_clear_vfta(hw);
574
575         /* For 82542 (rev 2.0), disable MWI and put the receiver into reset */
576         if (hw->mac_type == e1000_82542_rev2_0) {
577                 DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
578                 e1000_pci_clear_mwi(hw);
579                 ew32(RCTL, E1000_RCTL_RST);
580                 E1000_WRITE_FLUSH();
581                 msleep(5);
582         }
583
584         /* Setup the receive address. This involves initializing all of the Receive
585          * Address Registers (RARs 0 - 15).
586          */
587         e1000_init_rx_addrs(hw);
588
589         /* For 82542 (rev 2.0), take the receiver out of reset and enable MWI */
590         if (hw->mac_type == e1000_82542_rev2_0) {
591                 ew32(RCTL, 0);
592                 E1000_WRITE_FLUSH();
593                 msleep(1);
594                 if (hw->pci_cmd_word & PCI_COMMAND_INVALIDATE)
595                         e1000_pci_set_mwi(hw);
596         }
597
598         /* Zero out the Multicast HASH table */
599         DEBUGOUT("Zeroing the MTA\n");
600         mta_size = E1000_MC_TBL_SIZE;
601         for (i = 0; i < mta_size; i++) {
602                 E1000_WRITE_REG_ARRAY(hw, MTA, i, 0);
603                 /* use write flush to prevent Memory Write Block (MWB) from
604                  * occurring when accessing our register space */
605                 E1000_WRITE_FLUSH();
606         }
607
608         /* Set the PCI priority bit correctly in the CTRL register.  This
609          * determines if the adapter gives priority to receives, or if it
610          * gives equal priority to transmits and receives.  Valid only on
611          * 82542 and 82543 silicon.
612          */
613         if (hw->dma_fairness && hw->mac_type <= e1000_82543) {
614                 ctrl = er32(CTRL);
615                 ew32(CTRL, ctrl | E1000_CTRL_PRIOR);
616         }
617
618         switch (hw->mac_type) {
619         case e1000_82545_rev_3:
620         case e1000_82546_rev_3:
621                 break;
622         default:
623                 /* Workaround for PCI-X problem when BIOS sets MMRBC incorrectly. */
624                 if (hw->bus_type == e1000_bus_type_pcix
625                     && e1000_pcix_get_mmrbc(hw) > 2048)
626                         e1000_pcix_set_mmrbc(hw, 2048);
627                 break;
628         }
629
630         /* Call a subroutine to configure the link and setup flow control. */
631         ret_val = e1000_setup_link(hw);
632
633         /* Set the transmit descriptor write-back policy */
634         if (hw->mac_type > e1000_82544) {
635                 ctrl = er32(TXDCTL);
636                 ctrl =
637                     (ctrl & ~E1000_TXDCTL_WTHRESH) |
638                     E1000_TXDCTL_FULL_TX_DESC_WB;
639                 ew32(TXDCTL, ctrl);
640         }
641
642         /* Clear all of the statistics registers (clear on read).  It is
643          * important that we do this after we have tried to establish link
644          * because the symbol error count will increment wildly if there
645          * is no link.
646          */
647         e1000_clear_hw_cntrs(hw);
648
649         if (hw->device_id == E1000_DEV_ID_82546GB_QUAD_COPPER ||
650             hw->device_id == E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3) {
651                 ctrl_ext = er32(CTRL_EXT);
652                 /* Relaxed ordering must be disabled to avoid a parity
653                  * error crash in a PCI slot. */
654                 ctrl_ext |= E1000_CTRL_EXT_RO_DIS;
655                 ew32(CTRL_EXT, ctrl_ext);
656         }
657
658         return ret_val;
659 }
660
661 /**
662  * e1000_adjust_serdes_amplitude - Adjust SERDES output amplitude based on EEPROM setting.
663  * @hw: Struct containing variables accessed by shared code.
664  */
665 static s32 e1000_adjust_serdes_amplitude(struct e1000_hw *hw)
666 {
667         u16 eeprom_data;
668         s32 ret_val;
669
670         DEBUGFUNC("e1000_adjust_serdes_amplitude");
671
672         if (hw->media_type != e1000_media_type_internal_serdes)
673                 return E1000_SUCCESS;
674
675         switch (hw->mac_type) {
676         case e1000_82545_rev_3:
677         case e1000_82546_rev_3:
678                 break;
679         default:
680                 return E1000_SUCCESS;
681         }
682
683         ret_val = e1000_read_eeprom(hw, EEPROM_SERDES_AMPLITUDE, 1,
684                                     &eeprom_data);
685         if (ret_val) {
686                 return ret_val;
687         }
688
689         if (eeprom_data != EEPROM_RESERVED_WORD) {
690                 /* Adjust SERDES output amplitude only. */
691                 eeprom_data &= EEPROM_SERDES_AMPLITUDE_MASK;
692                 ret_val =
693                     e1000_write_phy_reg(hw, M88E1000_PHY_EXT_CTRL, eeprom_data);
694                 if (ret_val)
695                         return ret_val;
696         }
697
698         return E1000_SUCCESS;
699 }
700
701 /**
702  * e1000_setup_link - Configures flow control and link settings.
703  * @hw: Struct containing variables accessed by shared code
704  *
705  * Determines which flow control settings to use. Calls the appropriate media-
706  * specific link configuration function. Configures the flow control settings.
707  * Assuming the adapter has a valid link partner, a valid link should be
708  * established. Assumes the hardware has previously been reset and the
709  * transmitter and receiver are not enabled.
710  */
711 s32 e1000_setup_link(struct e1000_hw *hw)
712 {
713         u32 ctrl_ext;
714         s32 ret_val;
715         u16 eeprom_data;
716
717         DEBUGFUNC("e1000_setup_link");
718
719         /* Read and store word 0x0F of the EEPROM. This word contains bits
720          * that determine the hardware's default PAUSE (flow control) mode,
721          * a bit that determines whether the HW defaults to enabling or
722          * disabling auto-negotiation, and the direction of the
723          * SW defined pins. If there is no SW over-ride of the flow
724          * control setting, then the variable hw->fc will
725          * be initialized based on a value in the EEPROM.
726          */
727         if (hw->fc == E1000_FC_DEFAULT) {
728                 ret_val = e1000_read_eeprom(hw, EEPROM_INIT_CONTROL2_REG,
729                                             1, &eeprom_data);
730                 if (ret_val) {
731                         DEBUGOUT("EEPROM Read Error\n");
732                         return -E1000_ERR_EEPROM;
733                 }
734                 if ((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) == 0)
735                         hw->fc = E1000_FC_NONE;
736                 else if ((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) ==
737                          EEPROM_WORD0F_ASM_DIR)
738                         hw->fc = E1000_FC_TX_PAUSE;
739                 else
740                         hw->fc = E1000_FC_FULL;
741         }
742
743         /* We want to save off the original Flow Control configuration just
744          * in case we get disconnected and then reconnected into a different
745          * hub or switch with different Flow Control capabilities.
746          */
747         if (hw->mac_type == e1000_82542_rev2_0)
748                 hw->fc &= (~E1000_FC_TX_PAUSE);
749
750         if ((hw->mac_type < e1000_82543) && (hw->report_tx_early == 1))
751                 hw->fc &= (~E1000_FC_RX_PAUSE);
752
753         hw->original_fc = hw->fc;
754
755         DEBUGOUT1("After fix-ups FlowControl is now = %x\n", hw->fc);
756
757         /* Take the 4 bits from EEPROM word 0x0F that determine the initial
758          * polarity value for the SW controlled pins, and setup the
759          * Extended Device Control reg with that info.
760          * This is needed because one of the SW controlled pins is used for
761          * signal detection.  So this should be done before e1000_setup_pcs_link()
762          * or e1000_phy_setup() is called.
763          */
764         if (hw->mac_type == e1000_82543) {
765                 ret_val = e1000_read_eeprom(hw, EEPROM_INIT_CONTROL2_REG,
766                                             1, &eeprom_data);
767                 if (ret_val) {
768                         DEBUGOUT("EEPROM Read Error\n");
769                         return -E1000_ERR_EEPROM;
770                 }
771                 ctrl_ext = ((eeprom_data & EEPROM_WORD0F_SWPDIO_EXT) <<
772                             SWDPIO__EXT_SHIFT);
773                 ew32(CTRL_EXT, ctrl_ext);
774         }
775
776         /* Call the necessary subroutine to configure the link. */
777         ret_val = (hw->media_type == e1000_media_type_copper) ?
778             e1000_setup_copper_link(hw) : e1000_setup_fiber_serdes_link(hw);
779
780         /* Initialize the flow control address, type, and PAUSE timer
781          * registers to their default values.  This is done even if flow
782          * control is disabled, because it does not hurt anything to
783          * initialize these registers.
784          */
785         DEBUGOUT
786             ("Initializing the Flow Control address, type and timer regs\n");
787
788         ew32(FCT, FLOW_CONTROL_TYPE);
789         ew32(FCAH, FLOW_CONTROL_ADDRESS_HIGH);
790         ew32(FCAL, FLOW_CONTROL_ADDRESS_LOW);
791
792         ew32(FCTTV, hw->fc_pause_time);
793
794         /* Set the flow control receive threshold registers.  Normally,
795          * these registers will be set to a default threshold that may be
796          * adjusted later by the driver's runtime code.  However, if the
797          * ability to transmit pause frames in not enabled, then these
798          * registers will be set to 0.
799          */
800         if (!(hw->fc & E1000_FC_TX_PAUSE)) {
801                 ew32(FCRTL, 0);
802                 ew32(FCRTH, 0);
803         } else {
804                 /* We need to set up the Receive Threshold high and low water marks
805                  * as well as (optionally) enabling the transmission of XON frames.
806                  */
807                 if (hw->fc_send_xon) {
808                         ew32(FCRTL, (hw->fc_low_water | E1000_FCRTL_XONE));
809                         ew32(FCRTH, hw->fc_high_water);
810                 } else {
811                         ew32(FCRTL, hw->fc_low_water);
812                         ew32(FCRTH, hw->fc_high_water);
813                 }
814         }
815         return ret_val;
816 }
817
818 /**
819  * e1000_setup_fiber_serdes_link - prepare fiber or serdes link
820  * @hw: Struct containing variables accessed by shared code
821  *
822  * Manipulates Physical Coding Sublayer functions in order to configure
823  * link. Assumes the hardware has been previously reset and the transmitter
824  * and receiver are not enabled.
825  */
826 static s32 e1000_setup_fiber_serdes_link(struct e1000_hw *hw)
827 {
828         u32 ctrl;
829         u32 status;
830         u32 txcw = 0;
831         u32 i;
832         u32 signal = 0;
833         s32 ret_val;
834
835         DEBUGFUNC("e1000_setup_fiber_serdes_link");
836
837         /* On adapters with a MAC newer than 82544, SWDP 1 will be
838          * set when the optics detect a signal. On older adapters, it will be
839          * cleared when there is a signal.  This applies to fiber media only.
840          * If we're on serdes media, adjust the output amplitude to value
841          * set in the EEPROM.
842          */
843         ctrl = er32(CTRL);
844         if (hw->media_type == e1000_media_type_fiber)
845                 signal = (hw->mac_type > e1000_82544) ? E1000_CTRL_SWDPIN1 : 0;
846
847         ret_val = e1000_adjust_serdes_amplitude(hw);
848         if (ret_val)
849                 return ret_val;
850
851         /* Take the link out of reset */
852         ctrl &= ~(E1000_CTRL_LRST);
853
854         /* Adjust VCO speed to improve BER performance */
855         ret_val = e1000_set_vco_speed(hw);
856         if (ret_val)
857                 return ret_val;
858
859         e1000_config_collision_dist(hw);
860
861         /* Check for a software override of the flow control settings, and setup
862          * the device accordingly.  If auto-negotiation is enabled, then software
863          * will have to set the "PAUSE" bits to the correct value in the Tranmsit
864          * Config Word Register (TXCW) and re-start auto-negotiation.  However, if
865          * auto-negotiation is disabled, then software will have to manually
866          * configure the two flow control enable bits in the CTRL register.
867          *
868          * The possible values of the "fc" parameter are:
869          *      0:  Flow control is completely disabled
870          *      1:  Rx flow control is enabled (we can receive pause frames, but
871          *          not send pause frames).
872          *      2:  Tx flow control is enabled (we can send pause frames but we do
873          *          not support receiving pause frames).
874          *      3:  Both Rx and TX flow control (symmetric) are enabled.
875          */
876         switch (hw->fc) {
877         case E1000_FC_NONE:
878                 /* Flow control is completely disabled by a software over-ride. */
879                 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD);
880                 break;
881         case E1000_FC_RX_PAUSE:
882                 /* RX Flow control is enabled and TX Flow control is disabled by a
883                  * software over-ride. Since there really isn't a way to advertise
884                  * that we are capable of RX Pause ONLY, we will advertise that we
885                  * support both symmetric and asymmetric RX PAUSE. Later, we will
886                  *  disable the adapter's ability to send PAUSE frames.
887                  */
888                 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
889                 break;
890         case E1000_FC_TX_PAUSE:
891                 /* TX Flow control is enabled, and RX Flow control is disabled, by a
892                  * software over-ride.
893                  */
894                 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_ASM_DIR);
895                 break;
896         case E1000_FC_FULL:
897                 /* Flow control (both RX and TX) is enabled by a software over-ride. */
898                 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
899                 break;
900         default:
901                 DEBUGOUT("Flow control param set incorrectly\n");
902                 return -E1000_ERR_CONFIG;
903                 break;
904         }
905
906         /* Since auto-negotiation is enabled, take the link out of reset (the link
907          * will be in reset, because we previously reset the chip). This will
908          * restart auto-negotiation.  If auto-negotiation is successful then the
909          * link-up status bit will be set and the flow control enable bits (RFCE
910          * and TFCE) will be set according to their negotiated value.
911          */
912         DEBUGOUT("Auto-negotiation enabled\n");
913
914         ew32(TXCW, txcw);
915         ew32(CTRL, ctrl);
916         E1000_WRITE_FLUSH();
917
918         hw->txcw = txcw;
919         msleep(1);
920
921         /* If we have a signal (the cable is plugged in) then poll for a "Link-Up"
922          * indication in the Device Status Register.  Time-out if a link isn't
923          * seen in 500 milliseconds seconds (Auto-negotiation should complete in
924          * less than 500 milliseconds even if the other end is doing it in SW).
925          * For internal serdes, we just assume a signal is present, then poll.
926          */
927         if (hw->media_type == e1000_media_type_internal_serdes ||
928             (er32(CTRL) & E1000_CTRL_SWDPIN1) == signal) {
929                 DEBUGOUT("Looking for Link\n");
930                 for (i = 0; i < (LINK_UP_TIMEOUT / 10); i++) {
931                         msleep(10);
932                         status = er32(STATUS);
933                         if (status & E1000_STATUS_LU)
934                                 break;
935                 }
936                 if (i == (LINK_UP_TIMEOUT / 10)) {
937                         DEBUGOUT("Never got a valid link from auto-neg!!!\n");
938                         hw->autoneg_failed = 1;
939                         /* AutoNeg failed to achieve a link, so we'll call
940                          * e1000_check_for_link. This routine will force the link up if
941                          * we detect a signal. This will allow us to communicate with
942                          * non-autonegotiating link partners.
943                          */
944                         ret_val = e1000_check_for_link(hw);
945                         if (ret_val) {
946                                 DEBUGOUT("Error while checking for link\n");
947                                 return ret_val;
948                         }
949                         hw->autoneg_failed = 0;
950                 } else {
951                         hw->autoneg_failed = 0;
952                         DEBUGOUT("Valid Link Found\n");
953                 }
954         } else {
955                 DEBUGOUT("No Signal Detected\n");
956         }
957         return E1000_SUCCESS;
958 }
959
960 /**
961  * e1000_copper_link_preconfig - early configuration for copper
962  * @hw: Struct containing variables accessed by shared code
963  *
964  * Make sure we have a valid PHY and change PHY mode before link setup.
965  */
966 static s32 e1000_copper_link_preconfig(struct e1000_hw *hw)
967 {
968         u32 ctrl;
969         s32 ret_val;
970         u16 phy_data;
971
972         DEBUGFUNC("e1000_copper_link_preconfig");
973
974         ctrl = er32(CTRL);
975         /* With 82543, we need to force speed and duplex on the MAC equal to what
976          * the PHY speed and duplex configuration is. In addition, we need to
977          * perform a hardware reset on the PHY to take it out of reset.
978          */
979         if (hw->mac_type > e1000_82543) {
980                 ctrl |= E1000_CTRL_SLU;
981                 ctrl &= ~(E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
982                 ew32(CTRL, ctrl);
983         } else {
984                 ctrl |=
985                     (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX | E1000_CTRL_SLU);
986                 ew32(CTRL, ctrl);
987                 ret_val = e1000_phy_hw_reset(hw);
988                 if (ret_val)
989                         return ret_val;
990         }
991
992         /* Make sure we have a valid PHY */
993         ret_val = e1000_detect_gig_phy(hw);
994         if (ret_val) {
995                 DEBUGOUT("Error, did not detect valid phy.\n");
996                 return ret_val;
997         }
998         DEBUGOUT1("Phy ID = %x \n", hw->phy_id);
999
1000         /* Set PHY to class A mode (if necessary) */
1001         ret_val = e1000_set_phy_mode(hw);
1002         if (ret_val)
1003                 return ret_val;
1004
1005         if ((hw->mac_type == e1000_82545_rev_3) ||
1006             (hw->mac_type == e1000_82546_rev_3)) {
1007                 ret_val =
1008                     e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
1009                 phy_data |= 0x00000008;
1010                 ret_val =
1011                     e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
1012         }
1013
1014         if (hw->mac_type <= e1000_82543 ||
1015             hw->mac_type == e1000_82541 || hw->mac_type == e1000_82547 ||
1016             hw->mac_type == e1000_82541_rev_2
1017             || hw->mac_type == e1000_82547_rev_2)
1018                 hw->phy_reset_disable = false;
1019
1020         return E1000_SUCCESS;
1021 }
1022
1023 /**
1024  * e1000_copper_link_igp_setup - Copper link setup for e1000_phy_igp series.
1025  * @hw: Struct containing variables accessed by shared code
1026  */
1027 static s32 e1000_copper_link_igp_setup(struct e1000_hw *hw)
1028 {
1029         u32 led_ctrl;
1030         s32 ret_val;
1031         u16 phy_data;
1032
1033         DEBUGFUNC("e1000_copper_link_igp_setup");
1034
1035         if (hw->phy_reset_disable)
1036                 return E1000_SUCCESS;
1037
1038         ret_val = e1000_phy_reset(hw);
1039         if (ret_val) {
1040                 DEBUGOUT("Error Resetting the PHY\n");
1041                 return ret_val;
1042         }
1043
1044         /* Wait 15ms for MAC to configure PHY from eeprom settings */
1045         msleep(15);
1046         /* Configure activity LED after PHY reset */
1047         led_ctrl = er32(LEDCTL);
1048         led_ctrl &= IGP_ACTIVITY_LED_MASK;
1049         led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
1050         ew32(LEDCTL, led_ctrl);
1051
1052         /* The NVM settings will configure LPLU in D3 for IGP2 and IGP3 PHYs */
1053         if (hw->phy_type == e1000_phy_igp) {
1054                 /* disable lplu d3 during driver init */
1055                 ret_val = e1000_set_d3_lplu_state(hw, false);
1056                 if (ret_val) {
1057                         DEBUGOUT("Error Disabling LPLU D3\n");
1058                         return ret_val;
1059                 }
1060         }
1061
1062         /* Configure mdi-mdix settings */
1063         ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, &phy_data);
1064         if (ret_val)
1065                 return ret_val;
1066
1067         if ((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
1068                 hw->dsp_config_state = e1000_dsp_config_disabled;
1069                 /* Force MDI for earlier revs of the IGP PHY */
1070                 phy_data &=
1071                     ~(IGP01E1000_PSCR_AUTO_MDIX |
1072                       IGP01E1000_PSCR_FORCE_MDI_MDIX);
1073                 hw->mdix = 1;
1074
1075         } else {
1076                 hw->dsp_config_state = e1000_dsp_config_enabled;
1077                 phy_data &= ~IGP01E1000_PSCR_AUTO_MDIX;
1078
1079                 switch (hw->mdix) {
1080                 case 1:
1081                         phy_data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
1082                         break;
1083                 case 2:
1084                         phy_data |= IGP01E1000_PSCR_FORCE_MDI_MDIX;
1085                         break;
1086                 case 0:
1087                 default:
1088                         phy_data |= IGP01E1000_PSCR_AUTO_MDIX;
1089                         break;
1090                 }
1091         }
1092         ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, phy_data);
1093         if (ret_val)
1094                 return ret_val;
1095
1096         /* set auto-master slave resolution settings */
1097         if (hw->autoneg) {
1098                 e1000_ms_type phy_ms_setting = hw->master_slave;
1099
1100                 if (hw->ffe_config_state == e1000_ffe_config_active)
1101                         hw->ffe_config_state = e1000_ffe_config_enabled;
1102
1103                 if (hw->dsp_config_state == e1000_dsp_config_activated)
1104                         hw->dsp_config_state = e1000_dsp_config_enabled;
1105
1106                 /* when autonegotiation advertisement is only 1000Mbps then we
1107                  * should disable SmartSpeed and enable Auto MasterSlave
1108                  * resolution as hardware default. */
1109                 if (hw->autoneg_advertised == ADVERTISE_1000_FULL) {
1110                         /* Disable SmartSpeed */
1111                         ret_val =
1112                             e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
1113                                                &phy_data);
1114                         if (ret_val)
1115                                 return ret_val;
1116                         phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
1117                         ret_val =
1118                             e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
1119                                                 phy_data);
1120                         if (ret_val)
1121                                 return ret_val;
1122                         /* Set auto Master/Slave resolution process */
1123                         ret_val =
1124                             e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_data);
1125                         if (ret_val)
1126                                 return ret_val;
1127                         phy_data &= ~CR_1000T_MS_ENABLE;
1128                         ret_val =
1129                             e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_data);
1130                         if (ret_val)
1131                                 return ret_val;
1132                 }
1133
1134                 ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_data);
1135                 if (ret_val)
1136                         return ret_val;
1137
1138                 /* load defaults for future use */
1139                 hw->original_master_slave = (phy_data & CR_1000T_MS_ENABLE) ?
1140                     ((phy_data & CR_1000T_MS_VALUE) ?
1141                      e1000_ms_force_master :
1142                      e1000_ms_force_slave) : e1000_ms_auto;
1143
1144                 switch (phy_ms_setting) {
1145                 case e1000_ms_force_master:
1146                         phy_data |= (CR_1000T_MS_ENABLE | CR_1000T_MS_VALUE);
1147                         break;
1148                 case e1000_ms_force_slave:
1149                         phy_data |= CR_1000T_MS_ENABLE;
1150                         phy_data &= ~(CR_1000T_MS_VALUE);
1151                         break;
1152                 case e1000_ms_auto:
1153                         phy_data &= ~CR_1000T_MS_ENABLE;
1154                 default:
1155                         break;
1156                 }
1157                 ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_data);
1158                 if (ret_val)
1159                         return ret_val;
1160         }
1161
1162         return E1000_SUCCESS;
1163 }
1164
1165 /**
1166  * e1000_copper_link_mgp_setup - Copper link setup for e1000_phy_m88 series.
1167  * @hw: Struct containing variables accessed by shared code
1168  */
1169 static s32 e1000_copper_link_mgp_setup(struct e1000_hw *hw)
1170 {
1171         s32 ret_val;
1172         u16 phy_data;
1173
1174         DEBUGFUNC("e1000_copper_link_mgp_setup");
1175
1176         if (hw->phy_reset_disable)
1177                 return E1000_SUCCESS;
1178
1179         /* Enable CRS on TX. This must be set for half-duplex operation. */
1180         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
1181         if (ret_val)
1182                 return ret_val;
1183
1184         phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
1185
1186         /* Options:
1187          *   MDI/MDI-X = 0 (default)
1188          *   0 - Auto for all speeds
1189          *   1 - MDI mode
1190          *   2 - MDI-X mode
1191          *   3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
1192          */
1193         phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
1194
1195         switch (hw->mdix) {
1196         case 1:
1197                 phy_data |= M88E1000_PSCR_MDI_MANUAL_MODE;
1198                 break;
1199         case 2:
1200                 phy_data |= M88E1000_PSCR_MDIX_MANUAL_MODE;
1201                 break;
1202         case 3:
1203                 phy_data |= M88E1000_PSCR_AUTO_X_1000T;
1204                 break;
1205         case 0:
1206         default:
1207                 phy_data |= M88E1000_PSCR_AUTO_X_MODE;
1208                 break;
1209         }
1210
1211         /* Options:
1212          *   disable_polarity_correction = 0 (default)
1213          *       Automatic Correction for Reversed Cable Polarity
1214          *   0 - Disabled
1215          *   1 - Enabled
1216          */
1217         phy_data &= ~M88E1000_PSCR_POLARITY_REVERSAL;
1218         if (hw->disable_polarity_correction == 1)
1219                 phy_data |= M88E1000_PSCR_POLARITY_REVERSAL;
1220         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
1221         if (ret_val)
1222                 return ret_val;
1223
1224         if (hw->phy_revision < M88E1011_I_REV_4) {
1225                 /* Force TX_CLK in the Extended PHY Specific Control Register
1226                  * to 25MHz clock.
1227                  */
1228                 ret_val =
1229                     e1000_read_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL,
1230                                        &phy_data);
1231                 if (ret_val)
1232                         return ret_val;
1233
1234                 phy_data |= M88E1000_EPSCR_TX_CLK_25;
1235
1236                 if ((hw->phy_revision == E1000_REVISION_2) &&
1237                     (hw->phy_id == M88E1111_I_PHY_ID)) {
1238                         /* Vidalia Phy, set the downshift counter to 5x */
1239                         phy_data &= ~(M88EC018_EPSCR_DOWNSHIFT_COUNTER_MASK);
1240                         phy_data |= M88EC018_EPSCR_DOWNSHIFT_COUNTER_5X;
1241                         ret_val = e1000_write_phy_reg(hw,
1242                                                       M88E1000_EXT_PHY_SPEC_CTRL,
1243                                                       phy_data);
1244                         if (ret_val)
1245                                 return ret_val;
1246                 } else {
1247                         /* Configure Master and Slave downshift values */
1248                         phy_data &= ~(M88E1000_EPSCR_MASTER_DOWNSHIFT_MASK |
1249                                       M88E1000_EPSCR_SLAVE_DOWNSHIFT_MASK);
1250                         phy_data |= (M88E1000_EPSCR_MASTER_DOWNSHIFT_1X |
1251                                      M88E1000_EPSCR_SLAVE_DOWNSHIFT_1X);
1252                         ret_val = e1000_write_phy_reg(hw,
1253                                                       M88E1000_EXT_PHY_SPEC_CTRL,
1254                                                       phy_data);
1255                         if (ret_val)
1256                                 return ret_val;
1257                 }
1258         }
1259
1260         /* SW Reset the PHY so all changes take effect */
1261         ret_val = e1000_phy_reset(hw);
1262         if (ret_val) {
1263                 DEBUGOUT("Error Resetting the PHY\n");
1264                 return ret_val;
1265         }
1266
1267         return E1000_SUCCESS;
1268 }
1269
1270 /**
1271  * e1000_copper_link_autoneg - setup auto-neg
1272  * @hw: Struct containing variables accessed by shared code
1273  *
1274  * Setup auto-negotiation and flow control advertisements,
1275  * and then perform auto-negotiation.
1276  */
1277 static s32 e1000_copper_link_autoneg(struct e1000_hw *hw)
1278 {
1279         s32 ret_val;
1280         u16 phy_data;
1281
1282         DEBUGFUNC("e1000_copper_link_autoneg");
1283
1284         /* Perform some bounds checking on the hw->autoneg_advertised
1285          * parameter.  If this variable is zero, then set it to the default.
1286          */
1287         hw->autoneg_advertised &= AUTONEG_ADVERTISE_SPEED_DEFAULT;
1288
1289         /* If autoneg_advertised is zero, we assume it was not defaulted
1290          * by the calling code so we set to advertise full capability.
1291          */
1292         if (hw->autoneg_advertised == 0)
1293                 hw->autoneg_advertised = AUTONEG_ADVERTISE_SPEED_DEFAULT;
1294
1295         DEBUGOUT("Reconfiguring auto-neg advertisement params\n");
1296         ret_val = e1000_phy_setup_autoneg(hw);
1297         if (ret_val) {
1298                 DEBUGOUT("Error Setting up Auto-Negotiation\n");
1299                 return ret_val;
1300         }
1301         DEBUGOUT("Restarting Auto-Neg\n");
1302
1303         /* Restart auto-negotiation by setting the Auto Neg Enable bit and
1304          * the Auto Neg Restart bit in the PHY control register.
1305          */
1306         ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
1307         if (ret_val)
1308                 return ret_val;
1309
1310         phy_data |= (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
1311         ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
1312         if (ret_val)
1313                 return ret_val;
1314
1315         /* Does the user want to wait for Auto-Neg to complete here, or
1316          * check at a later time (for example, callback routine).
1317          */
1318         if (hw->wait_autoneg_complete) {
1319                 ret_val = e1000_wait_autoneg(hw);
1320                 if (ret_val) {
1321                         DEBUGOUT
1322                             ("Error while waiting for autoneg to complete\n");
1323                         return ret_val;
1324                 }
1325         }
1326
1327         hw->get_link_status = true;
1328
1329         return E1000_SUCCESS;
1330 }
1331
1332 /**
1333  * e1000_copper_link_postconfig - post link setup
1334  * @hw: Struct containing variables accessed by shared code
1335  *
1336  * Config the MAC and the PHY after link is up.
1337  *   1) Set up the MAC to the current PHY speed/duplex
1338  *      if we are on 82543.  If we
1339  *      are on newer silicon, we only need to configure
1340  *      collision distance in the Transmit Control Register.
1341  *   2) Set up flow control on the MAC to that established with
1342  *      the link partner.
1343  *   3) Config DSP to improve Gigabit link quality for some PHY revisions.
1344  */
1345 static s32 e1000_copper_link_postconfig(struct e1000_hw *hw)
1346 {
1347         s32 ret_val;
1348         DEBUGFUNC("e1000_copper_link_postconfig");
1349
1350         if (hw->mac_type >= e1000_82544) {
1351                 e1000_config_collision_dist(hw);
1352         } else {
1353                 ret_val = e1000_config_mac_to_phy(hw);
1354                 if (ret_val) {
1355                         DEBUGOUT("Error configuring MAC to PHY settings\n");
1356                         return ret_val;
1357                 }
1358         }
1359         ret_val = e1000_config_fc_after_link_up(hw);
1360         if (ret_val) {
1361                 DEBUGOUT("Error Configuring Flow Control\n");
1362                 return ret_val;
1363         }
1364
1365         /* Config DSP to improve Giga link quality */
1366         if (hw->phy_type == e1000_phy_igp) {
1367                 ret_val = e1000_config_dsp_after_link_change(hw, true);
1368                 if (ret_val) {
1369                         DEBUGOUT("Error Configuring DSP after link up\n");
1370                         return ret_val;
1371                 }
1372         }
1373
1374         return E1000_SUCCESS;
1375 }
1376
1377 /**
1378  * e1000_setup_copper_link - phy/speed/duplex setting
1379  * @hw: Struct containing variables accessed by shared code
1380  *
1381  * Detects which PHY is present and sets up the speed and duplex
1382  */
1383 static s32 e1000_setup_copper_link(struct e1000_hw *hw)
1384 {
1385         s32 ret_val;
1386         u16 i;
1387         u16 phy_data;
1388
1389         DEBUGFUNC("e1000_setup_copper_link");
1390
1391         /* Check if it is a valid PHY and set PHY mode if necessary. */
1392         ret_val = e1000_copper_link_preconfig(hw);
1393         if (ret_val)
1394                 return ret_val;
1395
1396         if (hw->phy_type == e1000_phy_igp) {
1397                 ret_val = e1000_copper_link_igp_setup(hw);
1398                 if (ret_val)
1399                         return ret_val;
1400         } else if (hw->phy_type == e1000_phy_m88) {
1401                 ret_val = e1000_copper_link_mgp_setup(hw);
1402                 if (ret_val)
1403                         return ret_val;
1404         }
1405
1406         if (hw->autoneg) {
1407                 /* Setup autoneg and flow control advertisement
1408                  * and perform autonegotiation */
1409                 ret_val = e1000_copper_link_autoneg(hw);
1410                 if (ret_val)
1411                         return ret_val;
1412         } else {
1413                 /* PHY will be set to 10H, 10F, 100H,or 100F
1414                  * depending on value from forced_speed_duplex. */
1415                 DEBUGOUT("Forcing speed and duplex\n");
1416                 ret_val = e1000_phy_force_speed_duplex(hw);
1417                 if (ret_val) {
1418                         DEBUGOUT("Error Forcing Speed and Duplex\n");
1419                         return ret_val;
1420                 }
1421         }
1422
1423         /* Check link status. Wait up to 100 microseconds for link to become
1424          * valid.
1425          */
1426         for (i = 0; i < 10; i++) {
1427                 ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
1428                 if (ret_val)
1429                         return ret_val;
1430                 ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
1431                 if (ret_val)
1432                         return ret_val;
1433
1434                 if (phy_data & MII_SR_LINK_STATUS) {
1435                         /* Config the MAC and PHY after link is up */
1436                         ret_val = e1000_copper_link_postconfig(hw);
1437                         if (ret_val)
1438                                 return ret_val;
1439
1440                         DEBUGOUT("Valid link established!!!\n");
1441                         return E1000_SUCCESS;
1442                 }
1443                 udelay(10);
1444         }
1445
1446         DEBUGOUT("Unable to establish link!!!\n");
1447         return E1000_SUCCESS;
1448 }
1449
1450 /**
1451  * e1000_phy_setup_autoneg - phy settings
1452  * @hw: Struct containing variables accessed by shared code
1453  *
1454  * Configures PHY autoneg and flow control advertisement settings
1455  */
1456 s32 e1000_phy_setup_autoneg(struct e1000_hw *hw)
1457 {
1458         s32 ret_val;
1459         u16 mii_autoneg_adv_reg;
1460         u16 mii_1000t_ctrl_reg;
1461
1462         DEBUGFUNC("e1000_phy_setup_autoneg");
1463
1464         /* Read the MII Auto-Neg Advertisement Register (Address 4). */
1465         ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_ADV, &mii_autoneg_adv_reg);
1466         if (ret_val)
1467                 return ret_val;
1468
1469         /* Read the MII 1000Base-T Control Register (Address 9). */
1470         ret_val =
1471             e1000_read_phy_reg(hw, PHY_1000T_CTRL, &mii_1000t_ctrl_reg);
1472         if (ret_val)
1473                 return ret_val;
1474
1475         /* Need to parse both autoneg_advertised and fc and set up
1476          * the appropriate PHY registers.  First we will parse for
1477          * autoneg_advertised software override.  Since we can advertise
1478          * a plethora of combinations, we need to check each bit
1479          * individually.
1480          */
1481
1482         /* First we clear all the 10/100 mb speed bits in the Auto-Neg
1483          * Advertisement Register (Address 4) and the 1000 mb speed bits in
1484          * the  1000Base-T Control Register (Address 9).
1485          */
1486         mii_autoneg_adv_reg &= ~REG4_SPEED_MASK;
1487         mii_1000t_ctrl_reg &= ~REG9_SPEED_MASK;
1488
1489         DEBUGOUT1("autoneg_advertised %x\n", hw->autoneg_advertised);
1490
1491         /* Do we want to advertise 10 Mb Half Duplex? */
1492         if (hw->autoneg_advertised & ADVERTISE_10_HALF) {
1493                 DEBUGOUT("Advertise 10mb Half duplex\n");
1494                 mii_autoneg_adv_reg |= NWAY_AR_10T_HD_CAPS;
1495         }
1496
1497         /* Do we want to advertise 10 Mb Full Duplex? */
1498         if (hw->autoneg_advertised & ADVERTISE_10_FULL) {
1499                 DEBUGOUT("Advertise 10mb Full duplex\n");
1500                 mii_autoneg_adv_reg |= NWAY_AR_10T_FD_CAPS;
1501         }
1502
1503         /* Do we want to advertise 100 Mb Half Duplex? */
1504         if (hw->autoneg_advertised & ADVERTISE_100_HALF) {
1505                 DEBUGOUT("Advertise 100mb Half duplex\n");
1506                 mii_autoneg_adv_reg |= NWAY_AR_100TX_HD_CAPS;
1507         }
1508
1509         /* Do we want to advertise 100 Mb Full Duplex? */
1510         if (hw->autoneg_advertised & ADVERTISE_100_FULL) {
1511                 DEBUGOUT("Advertise 100mb Full duplex\n");
1512                 mii_autoneg_adv_reg |= NWAY_AR_100TX_FD_CAPS;
1513         }
1514
1515         /* We do not allow the Phy to advertise 1000 Mb Half Duplex */
1516         if (hw->autoneg_advertised & ADVERTISE_1000_HALF) {
1517                 DEBUGOUT
1518                     ("Advertise 1000mb Half duplex requested, request denied!\n");
1519         }
1520
1521         /* Do we want to advertise 1000 Mb Full Duplex? */
1522         if (hw->autoneg_advertised & ADVERTISE_1000_FULL) {
1523                 DEBUGOUT("Advertise 1000mb Full duplex\n");
1524                 mii_1000t_ctrl_reg |= CR_1000T_FD_CAPS;
1525         }
1526
1527         /* Check for a software override of the flow control settings, and
1528          * setup the PHY advertisement registers accordingly.  If
1529          * auto-negotiation is enabled, then software will have to set the
1530          * "PAUSE" bits to the correct value in the Auto-Negotiation
1531          * Advertisement Register (PHY_AUTONEG_ADV) and re-start auto-negotiation.
1532          *
1533          * The possible values of the "fc" parameter are:
1534          *      0:  Flow control is completely disabled
1535          *      1:  Rx flow control is enabled (we can receive pause frames
1536          *          but not send pause frames).
1537          *      2:  Tx flow control is enabled (we can send pause frames
1538          *          but we do not support receiving pause frames).
1539          *      3:  Both Rx and TX flow control (symmetric) are enabled.
1540          *  other:  No software override.  The flow control configuration
1541          *          in the EEPROM is used.
1542          */
1543         switch (hw->fc) {
1544         case E1000_FC_NONE:     /* 0 */
1545                 /* Flow control (RX & TX) is completely disabled by a
1546                  * software over-ride.
1547                  */
1548                 mii_autoneg_adv_reg &= ~(NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
1549                 break;
1550         case E1000_FC_RX_PAUSE: /* 1 */
1551                 /* RX Flow control is enabled, and TX Flow control is
1552                  * disabled, by a software over-ride.
1553                  */
1554                 /* Since there really isn't a way to advertise that we are
1555                  * capable of RX Pause ONLY, we will advertise that we
1556                  * support both symmetric and asymmetric RX PAUSE.  Later
1557                  * (in e1000_config_fc_after_link_up) we will disable the
1558                  *hw's ability to send PAUSE frames.
1559                  */
1560                 mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
1561                 break;
1562         case E1000_FC_TX_PAUSE: /* 2 */
1563                 /* TX Flow control is enabled, and RX Flow control is
1564                  * disabled, by a software over-ride.
1565                  */
1566                 mii_autoneg_adv_reg |= NWAY_AR_ASM_DIR;
1567                 mii_autoneg_adv_reg &= ~NWAY_AR_PAUSE;
1568                 break;
1569         case E1000_FC_FULL:     /* 3 */
1570                 /* Flow control (both RX and TX) is enabled by a software
1571                  * over-ride.
1572                  */
1573                 mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
1574                 break;
1575         default:
1576                 DEBUGOUT("Flow control param set incorrectly\n");
1577                 return -E1000_ERR_CONFIG;
1578         }
1579
1580         ret_val = e1000_write_phy_reg(hw, PHY_AUTONEG_ADV, mii_autoneg_adv_reg);
1581         if (ret_val)
1582                 return ret_val;
1583
1584         DEBUGOUT1("Auto-Neg Advertising %x\n", mii_autoneg_adv_reg);
1585
1586         ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL, mii_1000t_ctrl_reg);
1587         if (ret_val)
1588                 return ret_val;
1589
1590         return E1000_SUCCESS;
1591 }
1592
1593 /**
1594  * e1000_phy_force_speed_duplex - force link settings
1595  * @hw: Struct containing variables accessed by shared code
1596  *
1597  * Force PHY speed and duplex settings to hw->forced_speed_duplex
1598  */
1599 static s32 e1000_phy_force_speed_duplex(struct e1000_hw *hw)
1600 {
1601         u32 ctrl;
1602         s32 ret_val;
1603         u16 mii_ctrl_reg;
1604         u16 mii_status_reg;
1605         u16 phy_data;
1606         u16 i;
1607
1608         DEBUGFUNC("e1000_phy_force_speed_duplex");
1609
1610         /* Turn off Flow control if we are forcing speed and duplex. */
1611         hw->fc = E1000_FC_NONE;
1612
1613         DEBUGOUT1("hw->fc = %d\n", hw->fc);
1614
1615         /* Read the Device Control Register. */
1616         ctrl = er32(CTRL);
1617
1618         /* Set the bits to Force Speed and Duplex in the Device Ctrl Reg. */
1619         ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
1620         ctrl &= ~(DEVICE_SPEED_MASK);
1621
1622         /* Clear the Auto Speed Detect Enable bit. */
1623         ctrl &= ~E1000_CTRL_ASDE;
1624
1625         /* Read the MII Control Register. */
1626         ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &mii_ctrl_reg);
1627         if (ret_val)
1628                 return ret_val;
1629
1630         /* We need to disable autoneg in order to force link and duplex. */
1631
1632         mii_ctrl_reg &= ~MII_CR_AUTO_NEG_EN;
1633
1634         /* Are we forcing Full or Half Duplex? */
1635         if (hw->forced_speed_duplex == e1000_100_full ||
1636             hw->forced_speed_duplex == e1000_10_full) {
1637                 /* We want to force full duplex so we SET the full duplex bits in the
1638                  * Device and MII Control Registers.
1639                  */
1640                 ctrl |= E1000_CTRL_FD;
1641                 mii_ctrl_reg |= MII_CR_FULL_DUPLEX;
1642                 DEBUGOUT("Full Duplex\n");
1643         } else {
1644                 /* We want to force half duplex so we CLEAR the full duplex bits in
1645                  * the Device and MII Control Registers.
1646                  */
1647                 ctrl &= ~E1000_CTRL_FD;
1648                 mii_ctrl_reg &= ~MII_CR_FULL_DUPLEX;
1649                 DEBUGOUT("Half Duplex\n");
1650         }
1651
1652         /* Are we forcing 100Mbps??? */
1653         if (hw->forced_speed_duplex == e1000_100_full ||
1654             hw->forced_speed_duplex == e1000_100_half) {
1655                 /* Set the 100Mb bit and turn off the 1000Mb and 10Mb bits. */
1656                 ctrl |= E1000_CTRL_SPD_100;
1657                 mii_ctrl_reg |= MII_CR_SPEED_100;
1658                 mii_ctrl_reg &= ~(MII_CR_SPEED_1000 | MII_CR_SPEED_10);
1659                 DEBUGOUT("Forcing 100mb ");
1660         } else {
1661                 /* Set the 10Mb bit and turn off the 1000Mb and 100Mb bits. */
1662                 ctrl &= ~(E1000_CTRL_SPD_1000 | E1000_CTRL_SPD_100);
1663                 mii_ctrl_reg |= MII_CR_SPEED_10;
1664                 mii_ctrl_reg &= ~(MII_CR_SPEED_1000 | MII_CR_SPEED_100);
1665                 DEBUGOUT("Forcing 10mb ");
1666         }
1667
1668         e1000_config_collision_dist(hw);
1669
1670         /* Write the configured values back to the Device Control Reg. */
1671         ew32(CTRL, ctrl);
1672
1673         if (hw->phy_type == e1000_phy_m88) {
1674                 ret_val =
1675                     e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
1676                 if (ret_val)
1677                         return ret_val;
1678
1679                 /* Clear Auto-Crossover to force MDI manually. M88E1000 requires MDI
1680                  * forced whenever speed are duplex are forced.
1681                  */
1682                 phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
1683                 ret_val =
1684                     e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
1685                 if (ret_val)
1686                         return ret_val;
1687
1688                 DEBUGOUT1("M88E1000 PSCR: %x \n", phy_data);
1689
1690                 /* Need to reset the PHY or these changes will be ignored */
1691                 mii_ctrl_reg |= MII_CR_RESET;
1692
1693                 /* Disable MDI-X support for 10/100 */
1694         } else {
1695                 /* Clear Auto-Crossover to force MDI manually.  IGP requires MDI
1696                  * forced whenever speed or duplex are forced.
1697                  */
1698                 ret_val =
1699                     e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, &phy_data);
1700                 if (ret_val)
1701                         return ret_val;
1702
1703                 phy_data &= ~IGP01E1000_PSCR_AUTO_MDIX;
1704                 phy_data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
1705
1706                 ret_val =
1707                     e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, phy_data);
1708                 if (ret_val)
1709                         return ret_val;
1710         }
1711
1712         /* Write back the modified PHY MII control register. */
1713         ret_val = e1000_write_phy_reg(hw, PHY_CTRL, mii_ctrl_reg);
1714         if (ret_val)
1715                 return ret_val;
1716
1717         udelay(1);
1718
1719         /* The wait_autoneg_complete flag may be a little misleading here.
1720          * Since we are forcing speed and duplex, Auto-Neg is not enabled.
1721          * But we do want to delay for a period while forcing only so we
1722          * don't generate false No Link messages.  So we will wait here
1723          * only if the user has set wait_autoneg_complete to 1, which is
1724          * the default.
1725          */
1726         if (hw->wait_autoneg_complete) {
1727                 /* We will wait for autoneg to complete. */
1728                 DEBUGOUT("Waiting for forced speed/duplex link.\n");
1729                 mii_status_reg = 0;
1730
1731                 /* We will wait for autoneg to complete or 4.5 seconds to expire. */
1732                 for (i = PHY_FORCE_TIME; i > 0; i--) {
1733                         /* Read the MII Status Register and wait for Auto-Neg Complete bit
1734                          * to be set.
1735                          */
1736                         ret_val =
1737                             e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
1738                         if (ret_val)
1739                                 return ret_val;
1740
1741                         ret_val =
1742                             e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
1743                         if (ret_val)
1744                                 return ret_val;
1745
1746                         if (mii_status_reg & MII_SR_LINK_STATUS)
1747                                 break;
1748                         msleep(100);
1749                 }
1750                 if ((i == 0) && (hw->phy_type == e1000_phy_m88)) {
1751                         /* We didn't get link.  Reset the DSP and wait again for link. */
1752                         ret_val = e1000_phy_reset_dsp(hw);
1753                         if (ret_val) {
1754                                 DEBUGOUT("Error Resetting PHY DSP\n");
1755                                 return ret_val;
1756                         }
1757                 }
1758                 /* This loop will early-out if the link condition has been met.  */
1759                 for (i = PHY_FORCE_TIME; i > 0; i--) {
1760                         if (mii_status_reg & MII_SR_LINK_STATUS)
1761                                 break;
1762                         msleep(100);
1763                         /* Read the MII Status Register and wait for Auto-Neg Complete bit
1764                          * to be set.
1765                          */
1766                         ret_val =
1767                             e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
1768                         if (ret_val)
1769                                 return ret_val;
1770
1771                         ret_val =
1772                             e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
1773                         if (ret_val)
1774                                 return ret_val;
1775                 }
1776         }
1777
1778         if (hw->phy_type == e1000_phy_m88) {
1779                 /* Because we reset the PHY above, we need to re-force TX_CLK in the
1780                  * Extended PHY Specific Control Register to 25MHz clock.  This value
1781                  * defaults back to a 2.5MHz clock when the PHY is reset.
1782                  */
1783                 ret_val =
1784                     e1000_read_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL,
1785                                        &phy_data);
1786                 if (ret_val)
1787                         return ret_val;
1788
1789                 phy_data |= M88E1000_EPSCR_TX_CLK_25;
1790                 ret_val =
1791                     e1000_write_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL,
1792                                         phy_data);
1793                 if (ret_val)
1794                         return ret_val;
1795
1796                 /* In addition, because of the s/w reset above, we need to enable CRS on
1797                  * TX.  This must be set for both full and half duplex operation.
1798                  */
1799                 ret_val =
1800                     e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
1801                 if (ret_val)
1802                         return ret_val;
1803
1804                 phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
1805                 ret_val =
1806                     e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
1807                 if (ret_val)
1808                         return ret_val;
1809
1810                 if ((hw->mac_type == e1000_82544 || hw->mac_type == e1000_82543)
1811                     && (!hw->autoneg)
1812                     && (hw->forced_speed_duplex == e1000_10_full
1813                         || hw->forced_speed_duplex == e1000_10_half)) {
1814                         ret_val = e1000_polarity_reversal_workaround(hw);
1815                         if (ret_val)
1816                                 return ret_val;
1817                 }
1818         }
1819         return E1000_SUCCESS;
1820 }
1821
1822 /**
1823  * e1000_config_collision_dist - set collision distance register
1824  * @hw: Struct containing variables accessed by shared code
1825  *
1826  * Sets the collision distance in the Transmit Control register.
1827  * Link should have been established previously. Reads the speed and duplex
1828  * information from the Device Status register.
1829  */
1830 void e1000_config_collision_dist(struct e1000_hw *hw)
1831 {
1832         u32 tctl, coll_dist;
1833
1834         DEBUGFUNC("e1000_config_collision_dist");
1835
1836         if (hw->mac_type < e1000_82543)
1837                 coll_dist = E1000_COLLISION_DISTANCE_82542;
1838         else
1839                 coll_dist = E1000_COLLISION_DISTANCE;
1840
1841         tctl = er32(TCTL);
1842
1843         tctl &= ~E1000_TCTL_COLD;
1844         tctl |= coll_dist << E1000_COLD_SHIFT;
1845
1846         ew32(TCTL, tctl);
1847         E1000_WRITE_FLUSH();
1848 }
1849
1850 /**
1851  * e1000_config_mac_to_phy - sync phy and mac settings
1852  * @hw: Struct containing variables accessed by shared code
1853  * @mii_reg: data to write to the MII control register
1854  *
1855  * Sets MAC speed and duplex settings to reflect the those in the PHY
1856  * The contents of the PHY register containing the needed information need to
1857  * be passed in.
1858  */
1859 static s32 e1000_config_mac_to_phy(struct e1000_hw *hw)
1860 {
1861         u32 ctrl;
1862         s32 ret_val;
1863         u16 phy_data;
1864
1865         DEBUGFUNC("e1000_config_mac_to_phy");
1866
1867         /* 82544 or newer MAC, Auto Speed Detection takes care of
1868          * MAC speed/duplex configuration.*/
1869         if (hw->mac_type >= e1000_82544)
1870                 return E1000_SUCCESS;
1871
1872         /* Read the Device Control Register and set the bits to Force Speed
1873          * and Duplex.
1874          */
1875         ctrl = er32(CTRL);
1876         ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
1877         ctrl &= ~(E1000_CTRL_SPD_SEL | E1000_CTRL_ILOS);
1878
1879         /* Set up duplex in the Device Control and Transmit Control
1880          * registers depending on negotiated values.
1881          */
1882         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data);
1883         if (ret_val)
1884                 return ret_val;
1885
1886         if (phy_data & M88E1000_PSSR_DPLX)
1887                 ctrl |= E1000_CTRL_FD;
1888         else
1889                 ctrl &= ~E1000_CTRL_FD;
1890
1891         e1000_config_collision_dist(hw);
1892
1893         /* Set up speed in the Device Control register depending on
1894          * negotiated values.
1895          */
1896         if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_1000MBS)
1897                 ctrl |= E1000_CTRL_SPD_1000;
1898         else if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_100MBS)
1899                 ctrl |= E1000_CTRL_SPD_100;
1900
1901         /* Write the configured values back to the Device Control Reg. */
1902         ew32(CTRL, ctrl);
1903         return E1000_SUCCESS;
1904 }
1905
1906 /**
1907  * e1000_force_mac_fc - force flow control settings
1908  * @hw: Struct containing variables accessed by shared code
1909  *
1910  * Forces the MAC's flow control settings.
1911  * Sets the TFCE and RFCE bits in the device control register to reflect
1912  * the adapter settings. TFCE and RFCE need to be explicitly set by
1913  * software when a Copper PHY is used because autonegotiation is managed
1914  * by the PHY rather than the MAC. Software must also configure these
1915  * bits when link is forced on a fiber connection.
1916  */
1917 s32 e1000_force_mac_fc(struct e1000_hw *hw)
1918 {
1919         u32 ctrl;
1920
1921         DEBUGFUNC("e1000_force_mac_fc");
1922
1923         /* Get the current configuration of the Device Control Register */
1924         ctrl = er32(CTRL);
1925
1926         /* Because we didn't get link via the internal auto-negotiation
1927          * mechanism (we either forced link or we got link via PHY
1928          * auto-neg), we have to manually enable/disable transmit an
1929          * receive flow control.
1930          *
1931          * The "Case" statement below enables/disable flow control
1932          * according to the "hw->fc" parameter.
1933          *
1934          * The possible values of the "fc" parameter are:
1935          *      0:  Flow control is completely disabled
1936          *      1:  Rx flow control is enabled (we can receive pause
1937          *          frames but not send pause frames).
1938          *      2:  Tx flow control is enabled (we can send pause frames
1939          *          frames but we do not receive pause frames).
1940          *      3:  Both Rx and TX flow control (symmetric) is enabled.
1941          *  other:  No other values should be possible at this point.
1942          */
1943
1944         switch (hw->fc) {
1945         case E1000_FC_NONE:
1946                 ctrl &= (~(E1000_CTRL_TFCE | E1000_CTRL_RFCE));
1947                 break;
1948         case E1000_FC_RX_PAUSE:
1949                 ctrl &= (~E1000_CTRL_TFCE);
1950                 ctrl |= E1000_CTRL_RFCE;
1951                 break;
1952         case E1000_FC_TX_PAUSE:
1953                 ctrl &= (~E1000_CTRL_RFCE);
1954                 ctrl |= E1000_CTRL_TFCE;
1955                 break;
1956         case E1000_FC_FULL:
1957                 ctrl |= (E1000_CTRL_TFCE | E1000_CTRL_RFCE);
1958                 break;
1959         default:
1960                 DEBUGOUT("Flow control param set incorrectly\n");
1961                 return -E1000_ERR_CONFIG;
1962         }
1963
1964         /* Disable TX Flow Control for 82542 (rev 2.0) */
1965         if (hw->mac_type == e1000_82542_rev2_0)
1966                 ctrl &= (~E1000_CTRL_TFCE);
1967
1968         ew32(CTRL, ctrl);
1969         return E1000_SUCCESS;
1970 }
1971
1972 /**
1973  * e1000_config_fc_after_link_up - configure flow control after autoneg
1974  * @hw: Struct containing variables accessed by shared code
1975  *
1976  * Configures flow control settings after link is established
1977  * Should be called immediately after a valid link has been established.
1978  * Forces MAC flow control settings if link was forced. When in MII/GMII mode
1979  * and autonegotiation is enabled, the MAC flow control settings will be set
1980  * based on the flow control negotiated by the PHY. In TBI mode, the TFCE
1981  * and RFCE bits will be automatically set to the negotiated flow control mode.
1982  */
1983 static s32 e1000_config_fc_after_link_up(struct e1000_hw *hw)
1984 {
1985         s32 ret_val;
1986         u16 mii_status_reg;
1987         u16 mii_nway_adv_reg;
1988         u16 mii_nway_lp_ability_reg;
1989         u16 speed;
1990         u16 duplex;
1991
1992         DEBUGFUNC("e1000_config_fc_after_link_up");
1993
1994         /* Check for the case where we have fiber media and auto-neg failed
1995          * so we had to force link.  In this case, we need to force the
1996          * configuration of the MAC to match the "fc" parameter.
1997          */
1998         if (((hw->media_type == e1000_media_type_fiber) && (hw->autoneg_failed))
1999             || ((hw->media_type == e1000_media_type_internal_serdes)
2000                 && (hw->autoneg_failed))
2001             || ((hw->media_type == e1000_media_type_copper)
2002                 && (!hw->autoneg))) {
2003                 ret_val = e1000_force_mac_fc(hw);
2004                 if (ret_val) {
2005                         DEBUGOUT("Error forcing flow control settings\n");
2006                         return ret_val;
2007                 }
2008         }
2009
2010         /* Check for the case where we have copper media and auto-neg is
2011          * enabled.  In this case, we need to check and see if Auto-Neg
2012          * has completed, and if so, how the PHY and link partner has
2013          * flow control configured.
2014          */
2015         if ((hw->media_type == e1000_media_type_copper) && hw->autoneg) {
2016                 /* Read the MII Status Register and check to see if AutoNeg
2017                  * has completed.  We read this twice because this reg has
2018                  * some "sticky" (latched) bits.
2019                  */
2020                 ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
2021                 if (ret_val)
2022                         return ret_val;
2023                 ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
2024                 if (ret_val)
2025                         return ret_val;
2026
2027                 if (mii_status_reg & MII_SR_AUTONEG_COMPLETE) {
2028                         /* The AutoNeg process has completed, so we now need to
2029                          * read both the Auto Negotiation Advertisement Register
2030                          * (Address 4) and the Auto_Negotiation Base Page Ability
2031                          * Register (Address 5) to determine how flow control was
2032                          * negotiated.
2033                          */
2034                         ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_ADV,
2035                                                      &mii_nway_adv_reg);
2036                         if (ret_val)
2037                                 return ret_val;
2038                         ret_val = e1000_read_phy_reg(hw, PHY_LP_ABILITY,
2039                                                      &mii_nway_lp_ability_reg);
2040                         if (ret_val)
2041                                 return ret_val;
2042
2043                         /* Two bits in the Auto Negotiation Advertisement Register
2044                          * (Address 4) and two bits in the Auto Negotiation Base
2045                          * Page Ability Register (Address 5) determine flow control
2046                          * for both the PHY and the link partner.  The following
2047                          * table, taken out of the IEEE 802.3ab/D6.0 dated March 25,
2048                          * 1999, describes these PAUSE resolution bits and how flow
2049                          * control is determined based upon these settings.
2050                          * NOTE:  DC = Don't Care
2051                          *
2052                          *   LOCAL DEVICE  |   LINK PARTNER
2053                          * PAUSE | ASM_DIR | PAUSE | ASM_DIR | NIC Resolution
2054                          *-------|---------|-------|---------|--------------------
2055                          *   0   |    0    |  DC   |   DC    | E1000_FC_NONE
2056                          *   0   |    1    |   0   |   DC    | E1000_FC_NONE
2057                          *   0   |    1    |   1   |    0    | E1000_FC_NONE
2058                          *   0   |    1    |   1   |    1    | E1000_FC_TX_PAUSE
2059                          *   1   |    0    |   0   |   DC    | E1000_FC_NONE
2060                          *   1   |   DC    |   1   |   DC    | E1000_FC_FULL
2061                          *   1   |    1    |   0   |    0    | E1000_FC_NONE
2062                          *   1   |    1    |   0   |    1    | E1000_FC_RX_PAUSE
2063                          *
2064                          */
2065                         /* Are both PAUSE bits set to 1?  If so, this implies
2066                          * Symmetric Flow Control is enabled at both ends.  The
2067                          * ASM_DIR bits are irrelevant per the spec.
2068                          *
2069                          * For Symmetric Flow Control:
2070                          *
2071                          *   LOCAL DEVICE  |   LINK PARTNER
2072                          * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
2073                          *-------|---------|-------|---------|--------------------
2074                          *   1   |   DC    |   1   |   DC    | E1000_FC_FULL
2075                          *
2076                          */
2077                         if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
2078                             (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE)) {
2079                                 /* Now we need to check if the user selected RX ONLY
2080                                  * of pause frames.  In this case, we had to advertise
2081                                  * FULL flow control because we could not advertise RX
2082                                  * ONLY. Hence, we must now check to see if we need to
2083                                  * turn OFF  the TRANSMISSION of PAUSE frames.
2084                                  */
2085                                 if (hw->original_fc == E1000_FC_FULL) {
2086                                         hw->fc = E1000_FC_FULL;
2087                                         DEBUGOUT("Flow Control = FULL.\n");
2088                                 } else {
2089                                         hw->fc = E1000_FC_RX_PAUSE;
2090                                         DEBUGOUT
2091                                             ("Flow Control = RX PAUSE frames only.\n");
2092                                 }
2093                         }
2094                         /* For receiving PAUSE frames ONLY.
2095                          *
2096                          *   LOCAL DEVICE  |   LINK PARTNER
2097                          * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
2098                          *-------|---------|-------|---------|--------------------
2099                          *   0   |    1    |   1   |    1    | E1000_FC_TX_PAUSE
2100                          *
2101                          */
2102                         else if (!(mii_nway_adv_reg & NWAY_AR_PAUSE) &&
2103                                  (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
2104                                  (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
2105                                  (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR))
2106                         {
2107                                 hw->fc = E1000_FC_TX_PAUSE;
2108                                 DEBUGOUT
2109                                     ("Flow Control = TX PAUSE frames only.\n");
2110                         }
2111                         /* For transmitting PAUSE frames ONLY.
2112                          *
2113                          *   LOCAL DEVICE  |   LINK PARTNER
2114                          * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
2115                          *-------|---------|-------|---------|--------------------
2116                          *   1   |    1    |   0   |    1    | E1000_FC_RX_PAUSE
2117                          *
2118                          */
2119                         else if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
2120                                  (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
2121                                  !(mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
2122                                  (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR))
2123                         {
2124                                 hw->fc = E1000_FC_RX_PAUSE;
2125                                 DEBUGOUT
2126                                     ("Flow Control = RX PAUSE frames only.\n");
2127                         }
2128                         /* Per the IEEE spec, at this point flow control should be
2129                          * disabled.  However, we want to consider that we could
2130                          * be connected to a legacy switch that doesn't advertise
2131                          * desired flow control, but can be forced on the link
2132                          * partner.  So if we advertised no flow control, that is
2133                          * what we will resolve to.  If we advertised some kind of
2134                          * receive capability (Rx Pause Only or Full Flow Control)
2135                          * and the link partner advertised none, we will configure
2136                          * ourselves to enable Rx Flow Control only.  We can do
2137                          * this safely for two reasons:  If the link partner really
2138                          * didn't want flow control enabled, and we enable Rx, no
2139                          * harm done since we won't be receiving any PAUSE frames
2140                          * anyway.  If the intent on the link partner was to have
2141                          * flow control enabled, then by us enabling RX only, we
2142                          * can at least receive pause frames and process them.
2143                          * This is a good idea because in most cases, since we are
2144                          * predominantly a server NIC, more times than not we will
2145                          * be asked to delay transmission of packets than asking
2146                          * our link partner to pause transmission of frames.
2147                          */
2148                         else if ((hw->original_fc == E1000_FC_NONE ||
2149                                   hw->original_fc == E1000_FC_TX_PAUSE) ||
2150                                  hw->fc_strict_ieee) {
2151                                 hw->fc = E1000_FC_NONE;
2152                                 DEBUGOUT("Flow Control = NONE.\n");
2153                         } else {
2154                                 hw->fc = E1000_FC_RX_PAUSE;
2155                                 DEBUGOUT
2156                                     ("Flow Control = RX PAUSE frames only.\n");
2157                         }
2158
2159                         /* Now we need to do one last check...  If we auto-
2160                          * negotiated to HALF DUPLEX, flow control should not be
2161                          * enabled per IEEE 802.3 spec.
2162                          */
2163                         ret_val =
2164                             e1000_get_speed_and_duplex(hw, &speed, &duplex);
2165                         if (ret_val) {
2166                                 DEBUGOUT
2167                                     ("Error getting link speed and duplex\n");
2168                                 return ret_val;
2169                         }
2170
2171                         if (duplex == HALF_DUPLEX)
2172                                 hw->fc = E1000_FC_NONE;
2173
2174                         /* Now we call a subroutine to actually force the MAC
2175                          * controller to use the correct flow control settings.
2176                          */
2177                         ret_val = e1000_force_mac_fc(hw);
2178                         if (ret_val) {
2179                                 DEBUGOUT
2180                                     ("Error forcing flow control settings\n");
2181                                 return ret_val;
2182                         }
2183                 } else {
2184                         DEBUGOUT
2185                             ("Copper PHY and Auto Neg has not completed.\n");
2186                 }
2187         }
2188         return E1000_SUCCESS;
2189 }
2190
2191 /**
2192  * e1000_check_for_serdes_link_generic - Check for link (Serdes)
2193  * @hw: pointer to the HW structure
2194  *
2195  * Checks for link up on the hardware.  If link is not up and we have
2196  * a signal, then we need to force link up.
2197  */
2198 s32 e1000_check_for_serdes_link_generic(struct e1000_hw *hw)
2199 {
2200         u32 rxcw;
2201         u32 ctrl;
2202         u32 status;
2203         s32 ret_val = E1000_SUCCESS;
2204
2205         DEBUGFUNC("e1000_check_for_serdes_link_generic");
2206
2207         ctrl = er32(CTRL);
2208         status = er32(STATUS);
2209         rxcw = er32(RXCW);
2210
2211         /*
2212          * If we don't have link (auto-negotiation failed or link partner
2213          * cannot auto-negotiate), and our link partner is not trying to
2214          * auto-negotiate with us (we are receiving idles or data),
2215          * we need to force link up. We also need to give auto-negotiation
2216          * time to complete.
2217          */
2218         /* (ctrl & E1000_CTRL_SWDPIN1) == 1 == have signal */
2219         if ((!(status & E1000_STATUS_LU)) && (!(rxcw & E1000_RXCW_C))) {
2220                 if (hw->autoneg_failed == 0) {
2221                         hw->autoneg_failed = 1;
2222                         goto out;
2223                 }
2224                 DEBUGOUT("NOT RXing /C/, disable AutoNeg and force link.\n");
2225
2226                 /* Disable auto-negotiation in the TXCW register */
2227                 ew32(TXCW, (hw->txcw & ~E1000_TXCW_ANE));
2228
2229                 /* Force link-up and also force full-duplex. */
2230                 ctrl = er32(CTRL);
2231                 ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FD);
2232                 ew32(CTRL, ctrl);
2233
2234                 /* Configure Flow Control after forcing link up. */
2235                 ret_val = e1000_config_fc_after_link_up(hw);
2236                 if (ret_val) {
2237                         DEBUGOUT("Error configuring flow control\n");
2238                         goto out;
2239                 }
2240         } else if ((ctrl & E1000_CTRL_SLU) && (rxcw & E1000_RXCW_C)) {
2241                 /*
2242                  * If we are forcing link and we are receiving /C/ ordered
2243                  * sets, re-enable auto-negotiation in the TXCW register
2244                  * and disable forced link in the Device Control register
2245                  * in an attempt to auto-negotiate with our link partner.
2246                  */
2247                 DEBUGOUT("RXing /C/, enable AutoNeg and stop forcing link.\n");
2248                 ew32(TXCW, hw->txcw);
2249                 ew32(CTRL, (ctrl & ~E1000_CTRL_SLU));
2250
2251                 hw->serdes_has_link = true;
2252         } else if (!(E1000_TXCW_ANE & er32(TXCW))) {
2253                 /*
2254                  * If we force link for non-auto-negotiation switch, check
2255                  * link status based on MAC synchronization for internal
2256                  * serdes media type.
2257                  */
2258                 /* SYNCH bit and IV bit are sticky. */
2259                 udelay(10);
2260                 rxcw = er32(RXCW);
2261                 if (rxcw & E1000_RXCW_SYNCH) {
2262                         if (!(rxcw & E1000_RXCW_IV)) {
2263                                 hw->serdes_has_link = true;
2264                                 DEBUGOUT("SERDES: Link up - forced.\n");
2265                         }
2266                 } else {
2267                         hw->serdes_has_link = false;
2268                         DEBUGOUT("SERDES: Link down - force failed.\n");
2269                 }
2270         }
2271
2272         if (E1000_TXCW_ANE & er32(TXCW)) {
2273                 status = er32(STATUS);
2274                 if (status & E1000_STATUS_LU) {
2275                         /* SYNCH bit and IV bit are sticky, so reread rxcw. */
2276                         udelay(10);
2277                         rxcw = er32(RXCW);
2278                         if (rxcw & E1000_RXCW_SYNCH) {
2279                                 if (!(rxcw & E1000_RXCW_IV)) {
2280                                         hw->serdes_has_link = true;
2281                                         DEBUGOUT("SERDES: Link up - autoneg "
2282                                                  "completed successfully.\n");
2283                                 } else {
2284                                         hw->serdes_has_link = false;
2285                                         DEBUGOUT("SERDES: Link down - invalid"
2286                                                  "codewords detected in autoneg.\n");
2287                                 }
2288                         } else {
2289                                 hw->serdes_has_link = false;
2290                                 DEBUGOUT("SERDES: Link down - no sync.\n");
2291                         }
2292                 } else {
2293                         hw->serdes_has_link = false;
2294                         DEBUGOUT("SERDES: Link down - autoneg failed\n");
2295                 }
2296         }
2297
2298       out:
2299         return ret_val;
2300 }
2301
2302 /**
2303  * e1000_check_for_link
2304  * @hw: Struct containing variables accessed by shared code
2305  *
2306  * Checks to see if the link status of the hardware has changed.
2307  * Called by any function that needs to check the link status of the adapter.
2308  */
2309 s32 e1000_check_for_link(struct e1000_hw *hw)
2310 {
2311         u32 rxcw = 0;
2312         u32 ctrl;
2313         u32 status;
2314         u32 rctl;
2315         u32 icr;
2316         u32 signal = 0;
2317         s32 ret_val;
2318         u16 phy_data;
2319
2320         DEBUGFUNC("e1000_check_for_link");
2321
2322         ctrl = er32(CTRL);
2323         status = er32(STATUS);
2324
2325         /* On adapters with a MAC newer than 82544, SW Definable pin 1 will be
2326          * set when the optics detect a signal. On older adapters, it will be
2327          * cleared when there is a signal.  This applies to fiber media only.
2328          */
2329         if ((hw->media_type == e1000_media_type_fiber) ||
2330             (hw->media_type == e1000_media_type_internal_serdes)) {
2331                 rxcw = er32(RXCW);
2332
2333                 if (hw->media_type == e1000_media_type_fiber) {
2334                         signal =
2335                             (hw->mac_type >
2336                              e1000_82544) ? E1000_CTRL_SWDPIN1 : 0;
2337                         if (status & E1000_STATUS_LU)
2338                                 hw->get_link_status = false;
2339                 }
2340         }
2341
2342         /* If we have a copper PHY then we only want to go out to the PHY
2343          * registers to see if Auto-Neg has completed and/or if our link
2344          * status has changed.  The get_link_status flag will be set if we
2345          * receive a Link Status Change interrupt or we have Rx Sequence
2346          * Errors.
2347          */
2348         if ((hw->media_type == e1000_media_type_copper) && hw->get_link_status) {
2349                 /* First we want to see if the MII Status Register reports
2350                  * link.  If so, then we want to get the current speed/duplex
2351                  * of the PHY.
2352                  * Read the register twice since the link bit is sticky.
2353                  */
2354                 ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
2355                 if (ret_val)
2356                         return ret_val;
2357                 ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
2358                 if (ret_val)
2359                         return ret_val;
2360
2361                 if (phy_data & MII_SR_LINK_STATUS) {
2362                         hw->get_link_status = false;
2363                         /* Check if there was DownShift, must be checked immediately after
2364                          * link-up */
2365                         e1000_check_downshift(hw);
2366
2367                         /* If we are on 82544 or 82543 silicon and speed/duplex
2368                          * are forced to 10H or 10F, then we will implement the polarity
2369                          * reversal workaround.  We disable interrupts first, and upon
2370                          * returning, place the devices interrupt state to its previous
2371                          * value except for the link status change interrupt which will
2372                          * happen due to the execution of this workaround.
2373                          */
2374
2375                         if ((hw->mac_type == e1000_82544
2376                              || hw->mac_type == e1000_82543) && (!hw->autoneg)
2377                             && (hw->forced_speed_duplex == e1000_10_full
2378                                 || hw->forced_speed_duplex == e1000_10_half)) {
2379                                 ew32(IMC, 0xffffffff);
2380                                 ret_val =
2381                                     e1000_polarity_reversal_workaround(hw);
2382                                 icr = er32(ICR);
2383                                 ew32(ICS, (icr & ~E1000_ICS_LSC));
2384                                 ew32(IMS, IMS_ENABLE_MASK);
2385                         }
2386
2387                 } else {
2388                         /* No link detected */
2389                         e1000_config_dsp_after_link_change(hw, false);
2390                         return 0;
2391                 }
2392
2393                 /* If we are forcing speed/duplex, then we simply return since
2394                  * we have already determined whether we have link or not.
2395                  */
2396                 if (!hw->autoneg)
2397                         return -E1000_ERR_CONFIG;
2398
2399                 /* optimize the dsp settings for the igp phy */
2400                 e1000_config_dsp_after_link_change(hw, true);
2401
2402                 /* We have a M88E1000 PHY and Auto-Neg is enabled.  If we
2403                  * have Si on board that is 82544 or newer, Auto
2404                  * Speed Detection takes care of MAC speed/duplex
2405                  * configuration.  So we only need to configure Collision
2406                  * Distance in the MAC.  Otherwise, we need to force
2407                  * speed/duplex on the MAC to the current PHY speed/duplex
2408                  * settings.
2409                  */
2410                 if (hw->mac_type >= e1000_82544)
2411                         e1000_config_collision_dist(hw);
2412                 else {
2413                         ret_val = e1000_config_mac_to_phy(hw);
2414                         if (ret_val) {
2415                                 DEBUGOUT
2416                                     ("Error configuring MAC to PHY settings\n");
2417                                 return ret_val;
2418                         }
2419                 }
2420
2421                 /* Configure Flow Control now that Auto-Neg has completed. First, we
2422                  * need to restore the desired flow control settings because we may
2423                  * have had to re-autoneg with a different link partner.
2424                  */
2425                 ret_val = e1000_config_fc_after_link_up(hw);
2426                 if (ret_val) {
2427                         DEBUGOUT("Error configuring flow control\n");
2428                         return ret_val;
2429                 }
2430
2431                 /* At this point we know that we are on copper and we have
2432                  * auto-negotiated link.  These are conditions for checking the link
2433                  * partner capability register.  We use the link speed to determine if
2434                  * TBI compatibility needs to be turned on or off.  If the link is not
2435                  * at gigabit speed, then TBI compatibility is not needed.  If we are
2436                  * at gigabit speed, we turn on TBI compatibility.
2437                  */
2438                 if (hw->tbi_compatibility_en) {
2439                         u16 speed, duplex;
2440                         ret_val =
2441                             e1000_get_speed_and_duplex(hw, &speed, &duplex);
2442                         if (ret_val) {
2443                                 DEBUGOUT
2444                                     ("Error getting link speed and duplex\n");
2445                                 return ret_val;
2446                         }
2447                         if (speed != SPEED_1000) {
2448                                 /* If link speed is not set to gigabit speed, we do not need
2449                                  * to enable TBI compatibility.
2450                                  */
2451                                 if (hw->tbi_compatibility_on) {
2452                                         /* If we previously were in the mode, turn it off. */
2453                                         rctl = er32(RCTL);
2454                                         rctl &= ~E1000_RCTL_SBP;
2455                                         ew32(RCTL, rctl);
2456                                         hw->tbi_compatibility_on = false;
2457                                 }
2458                         } else {
2459                                 /* If TBI compatibility is was previously off, turn it on. For
2460                                  * compatibility with a TBI link partner, we will store bad
2461                                  * packets. Some frames have an additional byte on the end and
2462                                  * will look like CRC errors to to the hardware.
2463                                  */
2464                                 if (!hw->tbi_compatibility_on) {
2465                                         hw->tbi_compatibility_on = true;
2466                                         rctl = er32(RCTL);
2467                                         rctl |= E1000_RCTL_SBP;
2468                                         ew32(RCTL, rctl);
2469                                 }
2470                         }
2471                 }
2472         }
2473
2474         if ((hw->media_type == e1000_media_type_fiber) ||
2475             (hw->media_type == e1000_media_type_internal_serdes))
2476                 e1000_check_for_serdes_link_generic(hw);
2477
2478         return E1000_SUCCESS;
2479 }
2480
2481 /**
2482  * e1000_get_speed_and_duplex
2483  * @hw: Struct containing variables accessed by shared code
2484  * @speed: Speed of the connection
2485  * @duplex: Duplex setting of the connection
2486
2487  * Detects the current speed and duplex settings of the hardware.
2488  */
2489 s32 e1000_get_speed_and_duplex(struct e1000_hw *hw, u16 *speed, u16 *duplex)
2490 {
2491         u32 status;
2492         s32 ret_val;
2493         u16 phy_data;
2494
2495         DEBUGFUNC("e1000_get_speed_and_duplex");
2496
2497         if (hw->mac_type >= e1000_82543) {
2498                 status = er32(STATUS);
2499                 if (status & E1000_STATUS_SPEED_1000) {
2500                         *speed = SPEED_1000;
2501                         DEBUGOUT("1000 Mbs, ");
2502                 } else if (status & E1000_STATUS_SPEED_100) {
2503                         *speed = SPEED_100;
2504                         DEBUGOUT("100 Mbs, ");
2505                 } else {
2506                         *speed = SPEED_10;
2507                         DEBUGOUT("10 Mbs, ");
2508                 }
2509
2510                 if (status & E1000_STATUS_FD) {
2511                         *duplex = FULL_DUPLEX;
2512                         DEBUGOUT("Full Duplex\n");
2513                 } else {
2514                         *duplex = HALF_DUPLEX;
2515                         DEBUGOUT(" Half Duplex\n");
2516                 }
2517         } else {
2518                 DEBUGOUT("1000 Mbs, Full Duplex\n");
2519                 *speed = SPEED_1000;
2520                 *duplex = FULL_DUPLEX;
2521         }
2522
2523         /* IGP01 PHY may advertise full duplex operation after speed downgrade even
2524          * if it is operating at half duplex.  Here we set the duplex settings to
2525          * match the duplex in the link partner's capabilities.
2526          */
2527         if (hw->phy_type == e1000_phy_igp && hw->speed_downgraded) {
2528                 ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_EXP, &phy_data);
2529                 if (ret_val)
2530                         return ret_val;
2531
2532                 if (!(phy_data & NWAY_ER_LP_NWAY_CAPS))
2533                         *duplex = HALF_DUPLEX;
2534                 else {
2535                         ret_val =
2536                             e1000_read_phy_reg(hw, PHY_LP_ABILITY, &phy_data);
2537                         if (ret_val)
2538                                 return ret_val;
2539                         if ((*speed == SPEED_100
2540                              && !(phy_data & NWAY_LPAR_100TX_FD_CAPS))
2541                             || (*speed == SPEED_10
2542                                 && !(phy_data & NWAY_LPAR_10T_FD_CAPS)))
2543                                 *duplex = HALF_DUPLEX;
2544                 }
2545         }
2546
2547         return E1000_SUCCESS;
2548 }
2549
2550 /**
2551  * e1000_wait_autoneg
2552  * @hw: Struct containing variables accessed by shared code
2553  *
2554  * Blocks until autoneg completes or times out (~4.5 seconds)
2555  */
2556 static s32 e1000_wait_autoneg(struct e1000_hw *hw)
2557 {
2558         s32 ret_val;
2559         u16 i;
2560         u16 phy_data;
2561
2562         DEBUGFUNC("e1000_wait_autoneg");
2563         DEBUGOUT("Waiting for Auto-Neg to complete.\n");
2564
2565         /* We will wait for autoneg to complete or 4.5 seconds to expire. */
2566         for (i = PHY_AUTO_NEG_TIME; i > 0; i--) {
2567                 /* Read the MII Status Register and wait for Auto-Neg
2568                  * Complete bit to be set.
2569                  */
2570                 ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
2571                 if (ret_val)
2572                         return ret_val;
2573                 ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
2574                 if (ret_val)
2575                         return ret_val;
2576                 if (phy_data & MII_SR_AUTONEG_COMPLETE) {
2577                         return E1000_SUCCESS;
2578                 }
2579                 msleep(100);
2580         }
2581         return E1000_SUCCESS;
2582 }
2583
2584 /**
2585  * e1000_raise_mdi_clk - Raises the Management Data Clock
2586  * @hw: Struct containing variables accessed by shared code
2587  * @ctrl: Device control register's current value
2588  */
2589 static void e1000_raise_mdi_clk(struct e1000_hw *hw, u32 *ctrl)
2590 {
2591         /* Raise the clock input to the Management Data Clock (by setting the MDC
2592          * bit), and then delay 10 microseconds.
2593          */
2594         ew32(CTRL, (*ctrl | E1000_CTRL_MDC));
2595         E1000_WRITE_FLUSH();
2596         udelay(10);
2597 }
2598
2599 /**
2600  * e1000_lower_mdi_clk - Lowers the Management Data Clock
2601  * @hw: Struct containing variables accessed by shared code
2602  * @ctrl: Device control register's current value
2603  */
2604 static void e1000_lower_mdi_clk(struct e1000_hw *hw, u32 *ctrl)
2605 {
2606         /* Lower the clock input to the Management Data Clock (by clearing the MDC
2607          * bit), and then delay 10 microseconds.
2608          */
2609         ew32(CTRL, (*ctrl & ~E1000_CTRL_MDC));
2610         E1000_WRITE_FLUSH();
2611         udelay(10);
2612 }
2613
2614 /**
2615  * e1000_shift_out_mdi_bits - Shifts data bits out to the PHY
2616  * @hw: Struct containing variables accessed by shared code
2617  * @data: Data to send out to the PHY
2618  * @count: Number of bits to shift out
2619  *
2620  * Bits are shifted out in MSB to LSB order.
2621  */
2622 static void e1000_shift_out_mdi_bits(struct e1000_hw *hw, u32 data, u16 count)
2623 {
2624         u32 ctrl;
2625         u32 mask;
2626
2627         /* We need to shift "count" number of bits out to the PHY. So, the value
2628          * in the "data" parameter will be shifted out to the PHY one bit at a
2629          * time. In order to do this, "data" must be broken down into bits.
2630          */
2631         mask = 0x01;
2632         mask <<= (count - 1);
2633
2634         ctrl = er32(CTRL);
2635
2636         /* Set MDIO_DIR and MDC_DIR direction bits to be used as output pins. */
2637         ctrl |= (E1000_CTRL_MDIO_DIR | E1000_CTRL_MDC_DIR);
2638
2639         while (mask) {
2640                 /* A "1" is shifted out to the PHY by setting the MDIO bit to "1" and
2641                  * then raising and lowering the Management Data Clock. A "0" is
2642                  * shifted out to the PHY by setting the MDIO bit to "0" and then
2643                  * raising and lowering the clock.
2644                  */
2645                 if (data & mask)
2646                         ctrl |= E1000_CTRL_MDIO;
2647                 else
2648                         ctrl &= ~E1000_CTRL_MDIO;
2649
2650                 ew32(CTRL, ctrl);
2651                 E1000_WRITE_FLUSH();
2652
2653                 udelay(10);
2654
2655                 e1000_raise_mdi_clk(hw, &ctrl);
2656                 e1000_lower_mdi_clk(hw, &ctrl);
2657
2658                 mask = mask >> 1;
2659         }
2660 }
2661
2662 /**
2663  * e1000_shift_in_mdi_bits - Shifts data bits in from the PHY
2664  * @hw: Struct containing variables accessed by shared code
2665  *
2666  * Bits are shifted in in MSB to LSB order.
2667  */
2668 static u16 e1000_shift_in_mdi_bits(struct e1000_hw *hw)
2669 {
2670         u32 ctrl;
2671         u16 data = 0;
2672         u8 i;
2673
2674         /* In order to read a register from the PHY, we need to shift in a total
2675          * of 18 bits from the PHY. The first two bit (turnaround) times are used
2676          * to avoid contention on the MDIO pin when a read operation is performed.
2677          * These two bits are ignored by us and thrown away. Bits are "shifted in"
2678          * by raising the input to the Management Data Clock (setting the MDC bit),
2679          * and then reading the value of the MDIO bit.
2680          */
2681         ctrl = er32(CTRL);
2682
2683         /* Clear MDIO_DIR (SWDPIO1) to indicate this bit is to be used as input. */
2684         ctrl &= ~E1000_CTRL_MDIO_DIR;
2685         ctrl &= ~E1000_CTRL_MDIO;
2686
2687         ew32(CTRL, ctrl);
2688         E1000_WRITE_FLUSH();
2689
2690         /* Raise and Lower the clock before reading in the data. This accounts for
2691          * the turnaround bits. The first clock occurred when we clocked out the
2692          * last bit of the Register Address.
2693          */
2694         e1000_raise_mdi_clk(hw, &ctrl);
2695         e1000_lower_mdi_clk(hw, &ctrl);
2696
2697         for (data = 0, i = 0; i < 16; i++) {
2698                 data = data << 1;
2699                 e1000_raise_mdi_clk(hw, &ctrl);
2700                 ctrl = er32(CTRL);
2701                 /* Check to see if we shifted in a "1". */
2702                 if (ctrl & E1000_CTRL_MDIO)
2703                         data |= 1;
2704                 e1000_lower_mdi_clk(hw, &ctrl);
2705         }
2706
2707         e1000_raise_mdi_clk(hw, &ctrl);
2708         e1000_lower_mdi_clk(hw, &ctrl);
2709
2710         return data;
2711 }
2712
2713
2714 /**
2715  * e1000_read_phy_reg - read a phy register
2716  * @hw: Struct containing variables accessed by shared code
2717  * @reg_addr: address of the PHY register to read
2718  *
2719  * Reads the value from a PHY register, if the value is on a specific non zero
2720  * page, sets the page first.
2721  */
2722 s32 e1000_read_phy_reg(struct e1000_hw *hw, u32 reg_addr, u16 *phy_data)
2723 {
2724         u32 ret_val;
2725
2726         DEBUGFUNC("e1000_read_phy_reg");
2727
2728         if ((hw->phy_type == e1000_phy_igp) &&
2729             (reg_addr > MAX_PHY_MULTI_PAGE_REG)) {
2730                 ret_val = e1000_write_phy_reg_ex(hw, IGP01E1000_PHY_PAGE_SELECT,
2731                                                  (u16) reg_addr);
2732                 if (ret_val)
2733                         return ret_val;
2734         }
2735
2736         ret_val = e1000_read_phy_reg_ex(hw, MAX_PHY_REG_ADDRESS & reg_addr,
2737                                         phy_data);
2738
2739         return ret_val;
2740 }
2741
2742 static s32 e1000_read_phy_reg_ex(struct e1000_hw *hw, u32 reg_addr,
2743                                  u16 *phy_data)
2744 {
2745         u32 i;
2746         u32 mdic = 0;
2747         const u32 phy_addr = 1;
2748
2749         DEBUGFUNC("e1000_read_phy_reg_ex");
2750
2751         if (reg_addr > MAX_PHY_REG_ADDRESS) {
2752                 DEBUGOUT1("PHY Address %d is out of range\n", reg_addr);
2753                 return -E1000_ERR_PARAM;
2754         }
2755
2756         if (hw->mac_type > e1000_82543) {
2757                 /* Set up Op-code, Phy Address, and register address in the MDI
2758                  * Control register.  The MAC will take care of interfacing with the
2759                  * PHY to retrieve the desired data.
2760                  */
2761                 mdic = ((reg_addr << E1000_MDIC_REG_SHIFT) |
2762                         (phy_addr << E1000_MDIC_PHY_SHIFT) |
2763                         (E1000_MDIC_OP_READ));
2764
2765                 ew32(MDIC, mdic);
2766
2767                 /* Poll the ready bit to see if the MDI read completed */
2768                 for (i = 0; i < 64; i++) {
2769                         udelay(50);
2770                         mdic = er32(MDIC);
2771                         if (mdic & E1000_MDIC_READY)
2772                                 break;
2773                 }
2774                 if (!(mdic & E1000_MDIC_READY)) {
2775                         DEBUGOUT("MDI Read did not complete\n");
2776                         return -E1000_ERR_PHY;
2777                 }
2778                 if (mdic & E1000_MDIC_ERROR) {
2779                         DEBUGOUT("MDI Error\n");
2780                         return -E1000_ERR_PHY;
2781                 }
2782                 *phy_data = (u16) mdic;
2783         } else {
2784                 /* We must first send a preamble through the MDIO pin to signal the
2785                  * beginning of an MII instruction.  This is done by sending 32
2786                  * consecutive "1" bits.
2787                  */
2788                 e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
2789
2790                 /* Now combine the next few fields that are required for a read
2791                  * operation.  We use this method instead of calling the
2792                  * e1000_shift_out_mdi_bits routine five different times. The format of
2793                  * a MII read instruction consists of a shift out of 14 bits and is
2794                  * defined as follows:
2795                  *    <Preamble><SOF><Op Code><Phy Addr><Reg Addr>
2796                  * followed by a shift in of 18 bits.  This first two bits shifted in
2797                  * are TurnAround bits used to avoid contention on the MDIO pin when a
2798                  * READ operation is performed.  These two bits are thrown away
2799                  * followed by a shift in of 16 bits which contains the desired data.
2800                  */
2801                 mdic = ((reg_addr) | (phy_addr << 5) |
2802                         (PHY_OP_READ << 10) | (PHY_SOF << 12));
2803
2804                 e1000_shift_out_mdi_bits(hw, mdic, 14);
2805
2806                 /* Now that we've shifted out the read command to the MII, we need to
2807                  * "shift in" the 16-bit value (18 total bits) of the requested PHY
2808                  * register address.
2809                  */
2810                 *phy_data = e1000_shift_in_mdi_bits(hw);
2811         }
2812         return E1000_SUCCESS;
2813 }
2814
2815 /**
2816  * e1000_write_phy_reg - write a phy register
2817  *
2818  * @hw: Struct containing variables accessed by shared code
2819  * @reg_addr: address of the PHY register to write
2820  * @data: data to write to the PHY
2821
2822  * Writes a value to a PHY register
2823  */
2824 s32 e1000_write_phy_reg(struct e1000_hw *hw, u32 reg_addr, u16 phy_data)
2825 {
2826         u32 ret_val;
2827
2828         DEBUGFUNC("e1000_write_phy_reg");
2829
2830         if ((hw->phy_type == e1000_phy_igp) &&
2831             (reg_addr > MAX_PHY_MULTI_PAGE_REG)) {
2832                 ret_val = e1000_write_phy_reg_ex(hw, IGP01E1000_PHY_PAGE_SELECT,
2833                                                  (u16) reg_addr);
2834                 if (ret_val)
2835                         return ret_val;
2836         }
2837
2838         ret_val = e1000_write_phy_reg_ex(hw, MAX_PHY_REG_ADDRESS & reg_addr,
2839                                          phy_data);
2840
2841         return ret_val;
2842 }
2843
2844 static s32 e1000_write_phy_reg_ex(struct e1000_hw *hw, u32 reg_addr,
2845                                   u16 phy_data)
2846 {
2847         u32 i;
2848         u32 mdic = 0;
2849         const u32 phy_addr = 1;
2850
2851         DEBUGFUNC("e1000_write_phy_reg_ex");
2852
2853         if (reg_addr > MAX_PHY_REG_ADDRESS) {
2854                 DEBUGOUT1("PHY Address %d is out of range\n", reg_addr);
2855                 return -E1000_ERR_PARAM;
2856         }
2857
2858         if (hw->mac_type > e1000_82543) {
2859                 /* Set up Op-code, Phy Address, register address, and data intended
2860                  * for the PHY register in the MDI Control register.  The MAC will take
2861                  * care of interfacing with the PHY to send the desired data.
2862                  */
2863                 mdic = (((u32) phy_data) |
2864                         (reg_addr << E1000_MDIC_REG_SHIFT) |
2865                         (phy_addr << E1000_MDIC_PHY_SHIFT) |
2866                         (E1000_MDIC_OP_WRITE));
2867
2868                 ew32(MDIC, mdic);
2869
2870                 /* Poll the ready bit to see if the MDI read completed */
2871                 for (i = 0; i < 641; i++) {
2872                         udelay(5);
2873                         mdic = er32(MDIC);
2874                         if (mdic & E1000_MDIC_READY)
2875                                 break;
2876                 }
2877                 if (!(mdic & E1000_MDIC_READY)) {
2878                         DEBUGOUT("MDI Write did not complete\n");
2879                         return -E1000_ERR_PHY;
2880                 }
2881         } else {
2882                 /* We'll need to use the SW defined pins to shift the write command
2883                  * out to the PHY. We first send a preamble to the PHY to signal the
2884                  * beginning of the MII instruction.  This is done by sending 32
2885                  * consecutive "1" bits.
2886                  */
2887                 e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
2888
2889                 /* Now combine the remaining required fields that will indicate a
2890                  * write operation. We use this method instead of calling the
2891                  * e1000_shift_out_mdi_bits routine for each field in the command. The
2892                  * format of a MII write instruction is as follows:
2893                  * <Preamble><SOF><Op Code><Phy Addr><Reg Addr><Turnaround><Data>.
2894                  */
2895                 mdic = ((PHY_TURNAROUND) | (reg_addr << 2) | (phy_addr << 7) |
2896                         (PHY_OP_WRITE << 12) | (PHY_SOF << 14));
2897                 mdic <<= 16;
2898                 mdic |= (u32) phy_data;
2899
2900                 e1000_shift_out_mdi_bits(hw, mdic, 32);
2901         }
2902
2903         return E1000_SUCCESS;
2904 }
2905
2906 /**
2907  * e1000_phy_hw_reset - reset the phy, hardware style
2908  * @hw: Struct containing variables accessed by shared code
2909  *
2910  * Returns the PHY to the power-on reset state
2911  */
2912 s32 e1000_phy_hw_reset(struct e1000_hw *hw)
2913 {
2914         u32 ctrl, ctrl_ext;
2915         u32 led_ctrl;
2916         s32 ret_val;
2917
2918         DEBUGFUNC("e1000_phy_hw_reset");
2919
2920         DEBUGOUT("Resetting Phy...\n");
2921
2922         if (hw->mac_type > e1000_82543) {
2923                 /* Read the device control register and assert the E1000_CTRL_PHY_RST
2924                  * bit. Then, take it out of reset.
2925                  * For e1000 hardware, we delay for 10ms between the assert
2926                  * and deassert.
2927                  */
2928                 ctrl = er32(CTRL);
2929                 ew32(CTRL, ctrl | E1000_CTRL_PHY_RST);
2930                 E1000_WRITE_FLUSH();
2931
2932                 msleep(10);
2933
2934                 ew32(CTRL, ctrl);
2935                 E1000_WRITE_FLUSH();
2936
2937         } else {
2938                 /* Read the Extended Device Control Register, assert the PHY_RESET_DIR
2939                  * bit to put the PHY into reset. Then, take it out of reset.
2940                  */
2941                 ctrl_ext = er32(CTRL_EXT);
2942                 ctrl_ext |= E1000_CTRL_EXT_SDP4_DIR;
2943                 ctrl_ext &= ~E1000_CTRL_EXT_SDP4_DATA;
2944                 ew32(CTRL_EXT, ctrl_ext);
2945                 E1000_WRITE_FLUSH();
2946                 msleep(10);
2947                 ctrl_ext |= E1000_CTRL_EXT_SDP4_DATA;
2948                 ew32(CTRL_EXT, ctrl_ext);
2949                 E1000_WRITE_FLUSH();
2950         }
2951         udelay(150);
2952
2953         if ((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
2954                 /* Configure activity LED after PHY reset */
2955                 led_ctrl = er32(LEDCTL);
2956                 led_ctrl &= IGP_ACTIVITY_LED_MASK;
2957                 led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
2958                 ew32(LEDCTL, led_ctrl);
2959         }
2960
2961         /* Wait for FW to finish PHY configuration. */
2962         ret_val = e1000_get_phy_cfg_done(hw);
2963         if (ret_val != E1000_SUCCESS)
2964                 return ret_val;
2965
2966         return ret_val;
2967 }
2968
2969 /**
2970  * e1000_phy_reset - reset the phy to commit settings
2971  * @hw: Struct containing variables accessed by shared code
2972  *
2973  * Resets the PHY
2974  * Sets bit 15 of the MII Control register
2975  */
2976 s32 e1000_phy_reset(struct e1000_hw *hw)
2977 {
2978         s32 ret_val;
2979         u16 phy_data;
2980
2981         DEBUGFUNC("e1000_phy_reset");
2982
2983         switch (hw->phy_type) {
2984         case e1000_phy_igp:
2985                 ret_val = e1000_phy_hw_reset(hw);
2986                 if (ret_val)
2987                         return ret_val;
2988                 break;
2989         default:
2990                 ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
2991                 if (ret_val)
2992                         return ret_val;
2993
2994                 phy_data |= MII_CR_RESET;
2995                 ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
2996                 if (ret_val)
2997                         return ret_val;
2998
2999                 udelay(1);
3000                 break;
3001         }
3002
3003         if (hw->phy_type == e1000_phy_igp)
3004                 e1000_phy_init_script(hw);
3005
3006         return E1000_SUCCESS;
3007 }
3008
3009 /**
3010  * e1000_detect_gig_phy - check the phy type
3011  * @hw: Struct containing variables accessed by shared code
3012  *
3013  * Probes the expected PHY address for known PHY IDs
3014  */
3015 static s32 e1000_detect_gig_phy(struct e1000_hw *hw)
3016 {
3017         s32 phy_init_status, ret_val;
3018         u16 phy_id_high, phy_id_low;
3019         bool match = false;
3020
3021         DEBUGFUNC("e1000_detect_gig_phy");
3022
3023         if (hw->phy_id != 0)
3024                 return E1000_SUCCESS;
3025
3026         /* Read the PHY ID Registers to identify which PHY is onboard. */
3027         ret_val = e1000_read_phy_reg(hw, PHY_ID1, &phy_id_high);
3028         if (ret_val)
3029                 return ret_val;
3030
3031         hw->phy_id = (u32) (phy_id_high << 16);
3032         udelay(20);
3033         ret_val = e1000_read_phy_reg(hw, PHY_ID2, &phy_id_low);
3034         if (ret_val)
3035                 return ret_val;
3036
3037         hw->phy_id |= (u32) (phy_id_low & PHY_REVISION_MASK);
3038         hw->phy_revision = (u32) phy_id_low & ~PHY_REVISION_MASK;
3039
3040         switch (hw->mac_type) {
3041         case e1000_82543:
3042                 if (hw->phy_id == M88E1000_E_PHY_ID)
3043                         match = true;
3044                 break;
3045         case e1000_82544:
3046                 if (hw->phy_id == M88E1000_I_PHY_ID)
3047                         match = true;
3048                 break;
3049         case e1000_82540:
3050         case e1000_82545:
3051         case e1000_82545_rev_3:
3052         case e1000_82546:
3053         case e1000_82546_rev_3:
3054                 if (hw->phy_id == M88E1011_I_PHY_ID)
3055                         match = true;
3056                 break;
3057         case e1000_82541:
3058         case e1000_82541_rev_2:
3059         case e1000_82547:
3060         case e1000_82547_rev_2:
3061                 if (hw->phy_id == IGP01E1000_I_PHY_ID)
3062                         match = true;
3063                 break;
3064         default:
3065                 DEBUGOUT1("Invalid MAC type %d\n", hw->mac_type);
3066                 return -E1000_ERR_CONFIG;
3067         }
3068         phy_init_status = e1000_set_phy_type(hw);
3069
3070         if ((match) && (phy_init_status == E1000_SUCCESS)) {
3071                 DEBUGOUT1("PHY ID 0x%X detected\n", hw->phy_id);
3072                 return E1000_SUCCESS;
3073         }
3074         DEBUGOUT1("Invalid PHY ID 0x%X\n", hw->phy_id);
3075         return -E1000_ERR_PHY;
3076 }
3077
3078 /**
3079  * e1000_phy_reset_dsp - reset DSP
3080  * @hw: Struct containing variables accessed by shared code
3081  *
3082  * Resets the PHY's DSP
3083  */
3084 static s32 e1000_phy_reset_dsp(struct e1000_hw *hw)
3085 {
3086         s32 ret_val;
3087         DEBUGFUNC("e1000_phy_reset_dsp");
3088
3089         do {
3090                 ret_val = e1000_write_phy_reg(hw, 29, 0x001d);
3091                 if (ret_val)
3092                         break;
3093                 ret_val = e1000_write_phy_reg(hw, 30, 0x00c1);
3094                 if (ret_val)
3095                         break;
3096                 ret_val = e1000_write_phy_reg(hw, 30, 0x0000);
3097                 if (ret_val)
3098                         break;
3099                 ret_val = E1000_SUCCESS;
3100         } while (0);
3101
3102         return ret_val;
3103 }
3104
3105 /**
3106  * e1000_phy_igp_get_info - get igp specific registers
3107  * @hw: Struct containing variables accessed by shared code
3108  * @phy_info: PHY information structure
3109  *
3110  * Get PHY information from various PHY registers for igp PHY only.
3111  */
3112 static s32 e1000_phy_igp_get_info(struct e1000_hw *hw,
3113                                   struct e1000_phy_info *phy_info)
3114 {
3115         s32 ret_val;
3116         u16 phy_data, min_length, max_length, average;
3117         e1000_rev_polarity polarity;
3118
3119         DEBUGFUNC("e1000_phy_igp_get_info");
3120
3121         /* The downshift status is checked only once, after link is established,
3122          * and it stored in the hw->speed_downgraded parameter. */
3123         phy_info->downshift = (e1000_downshift) hw->speed_downgraded;
3124
3125         /* IGP01E1000 does not need to support it. */
3126         phy_info->extended_10bt_distance = e1000_10bt_ext_dist_enable_normal;
3127
3128         /* IGP01E1000 always correct polarity reversal */
3129         phy_info->polarity_correction = e1000_polarity_reversal_enabled;
3130
3131         /* Check polarity status */
3132         ret_val = e1000_check_polarity(hw, &polarity);
3133         if (ret_val)
3134                 return ret_val;
3135
3136         phy_info->cable_polarity = polarity;
3137
3138         ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_STATUS, &phy_data);
3139         if (ret_val)
3140                 return ret_val;
3141
3142         phy_info->mdix_mode =
3143             (e1000_auto_x_mode) ((phy_data & IGP01E1000_PSSR_MDIX) >>
3144                                  IGP01E1000_PSSR_MDIX_SHIFT);
3145
3146         if ((phy_data & IGP01E1000_PSSR_SPEED_MASK) ==
3147             IGP01E1000_PSSR_SPEED_1000MBPS) {
3148                 /* Local/Remote Receiver Information are only valid at 1000 Mbps */
3149                 ret_val = e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_data);
3150                 if (ret_val)
3151                         return ret_val;
3152
3153                 phy_info->local_rx = ((phy_data & SR_1000T_LOCAL_RX_STATUS) >>
3154                                       SR_1000T_LOCAL_RX_STATUS_SHIFT) ?
3155                     e1000_1000t_rx_status_ok : e1000_1000t_rx_status_not_ok;
3156                 phy_info->remote_rx = ((phy_data & SR_1000T_REMOTE_RX_STATUS) >>
3157                                        SR_1000T_REMOTE_RX_STATUS_SHIFT) ?
3158                     e1000_1000t_rx_status_ok : e1000_1000t_rx_status_not_ok;
3159
3160                 /* Get cable length */
3161                 ret_val = e1000_get_cable_length(hw, &min_length, &max_length);
3162                 if (ret_val)
3163                         return ret_val;
3164
3165                 /* Translate to old method */
3166                 average = (max_length + min_length) / 2;
3167
3168                 if (average <= e1000_igp_cable_length_50)
3169                         phy_info->cable_length = e1000_cable_length_50;
3170                 else if (average <= e1000_igp_cable_length_80)
3171                         phy_info->cable_length = e1000_cable_length_50_80;
3172                 else if (average <= e1000_igp_cable_length_110)
3173                         phy_info->cable_length = e1000_cable_length_80_110;
3174                 else if (average <= e1000_igp_cable_length_140)
3175                         phy_info->cable_length = e1000_cable_length_110_140;
3176                 else
3177                         phy_info->cable_length = e1000_cable_length_140;
3178         }
3179
3180         return E1000_SUCCESS;
3181 }
3182
3183 /**
3184  * e1000_phy_m88_get_info - get m88 specific registers
3185  * @hw: Struct containing variables accessed by shared code
3186  * @phy_info: PHY information structure
3187  *
3188  * Get PHY information from various PHY registers for m88 PHY only.
3189  */
3190 static s32 e1000_phy_m88_get_info(struct e1000_hw *hw,
3191                                   struct e1000_phy_info *phy_info)
3192 {
3193         s32 ret_val;
3194         u16 phy_data;
3195         e1000_rev_polarity polarity;
3196
3197         DEBUGFUNC("e1000_phy_m88_get_info");
3198
3199         /* The downshift status is checked only once, after link is established,
3200          * and it stored in the hw->speed_downgraded parameter. */
3201         phy_info->downshift = (e1000_downshift) hw->speed_downgraded;
3202
3203         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
3204         if (ret_val)
3205                 return ret_val;
3206
3207         phy_info->extended_10bt_distance =
3208             ((phy_data & M88E1000_PSCR_10BT_EXT_DIST_ENABLE) >>
3209              M88E1000_PSCR_10BT_EXT_DIST_ENABLE_SHIFT) ?
3210             e1000_10bt_ext_dist_enable_lower :
3211             e1000_10bt_ext_dist_enable_normal;
3212
3213         phy_info->polarity_correction =
3214             ((phy_data & M88E1000_PSCR_POLARITY_REVERSAL) >>
3215              M88E1000_PSCR_POLARITY_REVERSAL_SHIFT) ?
3216             e1000_polarity_reversal_disabled : e1000_polarity_reversal_enabled;
3217
3218         /* Check polarity status */
3219         ret_val = e1000_check_polarity(hw, &polarity);
3220         if (ret_val)
3221                 return ret_val;
3222         phy_info->cable_polarity = polarity;
3223
3224         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data);
3225         if (ret_val)
3226                 return ret_val;
3227
3228         phy_info->mdix_mode =
3229             (e1000_auto_x_mode) ((phy_data & M88E1000_PSSR_MDIX) >>
3230                                  M88E1000_PSSR_MDIX_SHIFT);
3231
3232         if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_1000MBS) {
3233                 /* Cable Length Estimation and Local/Remote Receiver Information
3234                  * are only valid at 1000 Mbps.
3235                  */
3236                 phy_info->cable_length =
3237                     (e1000_cable_length) ((phy_data &
3238                                            M88E1000_PSSR_CABLE_LENGTH) >>
3239                                           M88E1000_PSSR_CABLE_LENGTH_SHIFT);
3240
3241                 ret_val = e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_data);
3242                 if (ret_val)
3243                         return ret_val;
3244
3245                 phy_info->local_rx = ((phy_data & SR_1000T_LOCAL_RX_STATUS) >>
3246                                       SR_1000T_LOCAL_RX_STATUS_SHIFT) ?
3247                     e1000_1000t_rx_status_ok : e1000_1000t_rx_status_not_ok;
3248                 phy_info->remote_rx = ((phy_data & SR_1000T_REMOTE_RX_STATUS) >>
3249                                        SR_1000T_REMOTE_RX_STATUS_SHIFT) ?
3250                     e1000_1000t_rx_status_ok : e1000_1000t_rx_status_not_ok;
3251
3252         }
3253
3254         return E1000_SUCCESS;
3255 }
3256
3257 /**
3258  * e1000_phy_get_info - request phy info
3259  * @hw: Struct containing variables accessed by shared code
3260  * @phy_info: PHY information structure
3261  *
3262  * Get PHY information from various PHY registers
3263  */
3264 s32 e1000_phy_get_info(struct e1000_hw *hw, struct e1000_phy_info *phy_info)
3265 {
3266         s32 ret_val;
3267         u16 phy_data;
3268
3269         DEBUGFUNC("e1000_phy_get_info");
3270
3271         phy_info->cable_length = e1000_cable_length_undefined;
3272         phy_info->extended_10bt_distance = e1000_10bt_ext_dist_enable_undefined;
3273         phy_info->cable_polarity = e1000_rev_polarity_undefined;
3274         phy_info->downshift = e1000_downshift_undefined;
3275         phy_info->polarity_correction = e1000_polarity_reversal_undefined;
3276         phy_info->mdix_mode = e1000_auto_x_mode_undefined;
3277         phy_info->local_rx = e1000_1000t_rx_status_undefined;
3278         phy_info->remote_rx = e1000_1000t_rx_status_undefined;
3279
3280         if (hw->media_type != e1000_media_type_copper) {
3281                 DEBUGOUT("PHY info is only valid for copper media\n");
3282                 return -E1000_ERR_CONFIG;
3283         }
3284
3285         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
3286         if (ret_val)
3287                 return ret_val;
3288
3289         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
3290         if (ret_val)
3291                 return ret_val;
3292
3293         if ((phy_data & MII_SR_LINK_STATUS) != MII_SR_LINK_STATUS) {
3294                 DEBUGOUT("PHY info is only valid if link is up\n");
3295                 return -E1000_ERR_CONFIG;
3296         }
3297
3298         if (hw->phy_type == e1000_phy_igp)
3299                 return e1000_phy_igp_get_info(hw, phy_info);
3300         else
3301                 return e1000_phy_m88_get_info(hw, phy_info);
3302 }
3303
3304 s32 e1000_validate_mdi_setting(struct e1000_hw *hw)
3305 {
3306         DEBUGFUNC("e1000_validate_mdi_settings");
3307
3308         if (!hw->autoneg && (hw->mdix == 0 || hw->mdix == 3)) {
3309                 DEBUGOUT("Invalid MDI setting detected\n");
3310                 hw->mdix = 1;
3311                 return -E1000_ERR_CONFIG;
3312         }
3313         return E1000_SUCCESS;
3314 }
3315
3316 /**
3317  * e1000_init_eeprom_params - initialize sw eeprom vars
3318  * @hw: Struct containing variables accessed by shared code
3319  *
3320  * Sets up eeprom variables in the hw struct.  Must be called after mac_type
3321  * is configured.
3322  */
3323 s32 e1000_init_eeprom_params(struct e1000_hw *hw)
3324 {
3325         struct e1000_eeprom_info *eeprom = &hw->eeprom;
3326         u32 eecd = er32(EECD);
3327         s32 ret_val = E1000_SUCCESS;
3328         u16 eeprom_size;
3329
3330         DEBUGFUNC("e1000_init_eeprom_params");
3331
3332         switch (hw->mac_type) {
3333         case e1000_82542_rev2_0:
3334         case e1000_82542_rev2_1:
3335         case e1000_82543:
3336         case e1000_82544:
3337                 eeprom->type = e1000_eeprom_microwire;
3338                 eeprom->word_size = 64;
3339                 eeprom->opcode_bits = 3;
3340                 eeprom->address_bits = 6;
3341                 eeprom->delay_usec = 50;
3342                 eeprom->use_eerd = false;
3343                 eeprom->use_eewr = false;
3344                 break;
3345         case e1000_82540:
3346         case e1000_82545:
3347         case e1000_82545_rev_3:
3348         case e1000_82546:
3349         case e1000_82546_rev_3:
3350                 eeprom->type = e1000_eeprom_microwire;
3351                 eeprom->opcode_bits = 3;
3352                 eeprom->delay_usec = 50;
3353                 if (eecd & E1000_EECD_SIZE) {
3354                         eeprom->word_size = 256;
3355                         eeprom->address_bits = 8;
3356                 } else {
3357                         eeprom->word_size = 64;
3358                         eeprom->address_bits = 6;
3359                 }
3360                 eeprom->use_eerd = false;
3361                 eeprom->use_eewr = false;
3362                 break;
3363         case e1000_82541:
3364         case e1000_82541_rev_2:
3365         case e1000_82547:
3366         case e1000_82547_rev_2:
3367                 if (eecd & E1000_EECD_TYPE) {
3368                         eeprom->type = e1000_eeprom_spi;
3369                         eeprom->opcode_bits = 8;
3370                         eeprom->delay_usec = 1;
3371                         if (eecd & E1000_EECD_ADDR_BITS) {
3372                                 eeprom->page_size = 32;
3373                                 eeprom->address_bits = 16;
3374                         } else {
3375                                 eeprom->page_size = 8;
3376                                 eeprom->address_bits = 8;
3377                         }
3378                 } else {
3379                         eeprom->type = e1000_eeprom_microwire;
3380                         eeprom->opcode_bits = 3;
3381                         eeprom->delay_usec = 50;
3382                         if (eecd & E1000_EECD_ADDR_BITS) {
3383                                 eeprom->word_size = 256;
3384                                 eeprom->address_bits = 8;
3385                         } else {
3386                                 eeprom->word_size = 64;
3387                                 eeprom->address_bits = 6;
3388                         }
3389                 }
3390                 eeprom->use_eerd = false;
3391                 eeprom->use_eewr = false;
3392                 break;
3393         default:
3394                 break;
3395         }
3396
3397         if (eeprom->type == e1000_eeprom_spi) {
3398                 /* eeprom_size will be an enum [0..8] that maps to eeprom sizes 128B to
3399                  * 32KB (incremented by powers of 2).
3400                  */
3401                 /* Set to default value for initial eeprom read. */
3402                 eeprom->word_size = 64;
3403                 ret_val = e1000_read_eeprom(hw, EEPROM_CFG, 1, &eeprom_size);
3404                 if (ret_val)
3405                         return ret_val;
3406                 eeprom_size =
3407                     (eeprom_size & EEPROM_SIZE_MASK) >> EEPROM_SIZE_SHIFT;
3408                 /* 256B eeprom size was not supported in earlier hardware, so we
3409                  * bump eeprom_size up one to ensure that "1" (which maps to 256B)
3410                  * is never the result used in the shifting logic below. */
3411                 if (eeprom_size)
3412                         eeprom_size++;
3413
3414                 eeprom->word_size = 1 << (eeprom_size + EEPROM_WORD_SIZE_SHIFT);
3415         }
3416         return ret_val;
3417 }
3418
3419 /**
3420  * e1000_raise_ee_clk - Raises the EEPROM's clock input.
3421  * @hw: Struct containing variables accessed by shared code
3422  * @eecd: EECD's current value
3423  */
3424 static void e1000_raise_ee_clk(struct e1000_hw *hw, u32 *eecd)
3425 {
3426         /* Raise the clock input to the EEPROM (by setting the SK bit), and then
3427          * wait <delay> microseconds.
3428          */
3429         *eecd = *eecd | E1000_EECD_SK;
3430         ew32(EECD, *eecd);
3431         E1000_WRITE_FLUSH();
3432         udelay(hw->eeprom.delay_usec);
3433 }
3434
3435 /**
3436  * e1000_lower_ee_clk - Lowers the EEPROM's clock input.
3437  * @hw: Struct containing variables accessed by shared code
3438  * @eecd: EECD's current value
3439  */
3440 static void e1000_lower_ee_clk(struct e1000_hw *hw, u32 *eecd)
3441 {
3442         /* Lower the clock input to the EEPROM (by clearing the SK bit), and then
3443          * wait 50 microseconds.
3444          */
3445         *eecd = *eecd & ~E1000_EECD_SK;
3446         ew32(EECD, *eecd);
3447         E1000_WRITE_FLUSH();
3448         udelay(hw->eeprom.delay_usec);
3449 }
3450
3451 /**
3452  * e1000_shift_out_ee_bits - Shift data bits out to the EEPROM.
3453  * @hw: Struct containing variables accessed by shared code
3454  * @data: data to send to the EEPROM
3455  * @count: number of bits to shift out
3456  */
3457 static void e1000_shift_out_ee_bits(struct e1000_hw *hw, u16 data, u16 count)
3458 {
3459         struct e1000_eeprom_info *eeprom = &hw->eeprom;
3460         u32 eecd;
3461         u32 mask;
3462
3463         /* We need to shift "count" bits out to the EEPROM. So, value in the
3464          * "data" parameter will be shifted out to the EEPROM one bit at a time.
3465          * In order to do this, "data" must be broken down into bits.
3466          */
3467         mask = 0x01 << (count - 1);
3468         eecd = er32(EECD);
3469         if (eeprom->type == e1000_eeprom_microwire) {
3470                 eecd &= ~E1000_EECD_DO;
3471         } else if (eeprom->type == e1000_eeprom_spi) {
3472                 eecd |= E1000_EECD_DO;
3473         }
3474         do {
3475                 /* A "1" is shifted out to the EEPROM by setting bit "DI" to a "1",
3476                  * and then raising and then lowering the clock (the SK bit controls
3477                  * the clock input to the EEPROM).  A "0" is shifted out to the EEPROM
3478                  * by setting "DI" to "0" and then raising and then lowering the clock.
3479                  */
3480                 eecd &= ~E1000_EECD_DI;
3481
3482                 if (data & mask)
3483                         eecd |= E1000_EECD_DI;
3484
3485                 ew32(EECD, eecd);
3486                 E1000_WRITE_FLUSH();
3487
3488                 udelay(eeprom->delay_usec);
3489
3490                 e1000_raise_ee_clk(hw, &eecd);
3491                 e1000_lower_ee_clk(hw, &eecd);
3492
3493                 mask = mask >> 1;
3494
3495         } while (mask);
3496
3497         /* We leave the "DI" bit set to "0" when we leave this routine. */
3498         eecd &= ~E1000_EECD_DI;
3499         ew32(EECD, eecd);
3500 }
3501
3502 /**
3503  * e1000_shift_in_ee_bits - Shift data bits in from the EEPROM
3504  * @hw: Struct containing variables accessed by shared code
3505  * @count: number of bits to shift in
3506  */
3507 static u16 e1000_shift_in_ee_bits(struct e1000_hw *hw, u16 count)
3508 {
3509         u32 eecd;
3510         u32 i;
3511         u16 data;
3512
3513         /* In order to read a register from the EEPROM, we need to shift 'count'
3514          * bits in from the EEPROM. Bits are "shifted in" by raising the clock
3515          * input to the EEPROM (setting the SK bit), and then reading the value of
3516          * the "DO" bit.  During this "shifting in" process the "DI" bit should
3517          * always be clear.
3518          */
3519
3520         eecd = er32(EECD);
3521
3522         eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
3523         data = 0;
3524
3525         for (i = 0; i < count; i++) {
3526                 data = data << 1;
3527                 e1000_raise_ee_clk(hw, &eecd);
3528
3529                 eecd = er32(EECD);
3530
3531                 eecd &= ~(E1000_EECD_DI);
3532                 if (eecd & E1000_EECD_DO)
3533                         data |= 1;
3534
3535                 e1000_lower_ee_clk(hw, &eecd);
3536         }
3537
3538         return data;
3539 }
3540
3541 /**
3542  * e1000_acquire_eeprom - Prepares EEPROM for access
3543  * @hw: Struct containing variables accessed by shared code
3544  *
3545  * Lowers EEPROM clock. Clears input pin. Sets the chip select pin. This
3546  * function should be called before issuing a command to the EEPROM.
3547  */
3548 static s32 e1000_acquire_eeprom(struct e1000_hw *hw)
3549 {
3550         struct e1000_eeprom_info *eeprom = &hw->eeprom;
3551         u32 eecd, i = 0;
3552
3553         DEBUGFUNC("e1000_acquire_eeprom");
3554
3555         eecd = er32(EECD);
3556
3557         /* Request EEPROM Access */
3558         if (hw->mac_type > e1000_82544) {
3559                 eecd |= E1000_EECD_REQ;
3560                 ew32(EECD, eecd);
3561                 eecd = er32(EECD);
3562                 while ((!(eecd & E1000_EECD_GNT)) &&
3563                        (i < E1000_EEPROM_GRANT_ATTEMPTS)) {
3564                         i++;
3565                         udelay(5);
3566                         eecd = er32(EECD);
3567                 }
3568                 if (!(eecd & E1000_EECD_GNT)) {
3569                         eecd &= ~E1000_EECD_REQ;
3570                         ew32(EECD, eecd);
3571                         DEBUGOUT("Could not acquire EEPROM grant\n");
3572                         return -E1000_ERR_EEPROM;
3573                 }
3574         }
3575
3576         /* Setup EEPROM for Read/Write */
3577
3578         if (eeprom->type == e1000_eeprom_microwire) {
3579                 /* Clear SK and DI */
3580                 eecd &= ~(E1000_EECD_DI | E1000_EECD_SK);
3581                 ew32(EECD, eecd);
3582
3583                 /* Set CS */
3584                 eecd |= E1000_EECD_CS;
3585                 ew32(EECD, eecd);
3586         } else if (eeprom->type == e1000_eeprom_spi) {
3587                 /* Clear SK and CS */
3588                 eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
3589                 ew32(EECD, eecd);
3590                 udelay(1);
3591         }
3592
3593         return E1000_SUCCESS;
3594 }
3595
3596 /**
3597  * e1000_standby_eeprom - Returns EEPROM to a "standby" state
3598  * @hw: Struct containing variables accessed by shared code
3599  */
3600 static void e1000_standby_eeprom(struct e1000_hw *hw)
3601 {
3602         struct e1000_eeprom_info *eeprom = &hw->eeprom;
3603         u32 eecd;
3604
3605         eecd = er32(EECD);
3606
3607         if (eeprom->type == e1000_eeprom_microwire) {
3608                 eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
3609                 ew32(EECD, eecd);
3610                 E1000_WRITE_FLUSH();
3611                 udelay(eeprom->delay_usec);
3612
3613                 /* Clock high */
3614                 eecd |= E1000_EECD_SK;
3615                 ew32(EECD, eecd);
3616                 E1000_WRITE_FLUSH();
3617                 udelay(eeprom->delay_usec);
3618
3619                 /* Select EEPROM */
3620                 eecd |= E1000_EECD_CS;
3621                 ew32(EECD, eecd);
3622                 E1000_WRITE_FLUSH();
3623                 udelay(eeprom->delay_usec);
3624
3625                 /* Clock low */
3626                 eecd &= ~E1000_EECD_SK;
3627                 ew32(EECD, eecd);
3628                 E1000_WRITE_FLUSH();
3629                 udelay(eeprom->delay_usec);
3630         } else if (eeprom->type == e1000_eeprom_spi) {
3631                 /* Toggle CS to flush commands */
3632                 eecd |= E1000_EECD_CS;
3633                 ew32(EECD, eecd);
3634                 E1000_WRITE_FLUSH();
3635                 udelay(eeprom->delay_usec);
3636                 eecd &= ~E1000_EECD_CS;
3637                 ew32(EECD, eecd);
3638                 E1000_WRITE_FLUSH();
3639                 udelay(eeprom->delay_usec);
3640         }
3641 }
3642
3643 /**
3644  * e1000_release_eeprom - drop chip select
3645  * @hw: Struct containing variables accessed by shared code
3646  *
3647  * Terminates a command by inverting the EEPROM's chip select pin
3648  */
3649 static void e1000_release_eeprom(struct e1000_hw *hw)
3650 {
3651         u32 eecd;
3652
3653         DEBUGFUNC("e1000_release_eeprom");
3654
3655         eecd = er32(EECD);
3656
3657         if (hw->eeprom.type == e1000_eeprom_spi) {
3658                 eecd |= E1000_EECD_CS;  /* Pull CS high */
3659                 eecd &= ~E1000_EECD_SK; /* Lower SCK */
3660
3661                 ew32(EECD, eecd);
3662
3663                 udelay(hw->eeprom.delay_usec);
3664         } else if (hw->eeprom.type == e1000_eeprom_microwire) {
3665                 /* cleanup eeprom */
3666
3667                 /* CS on Microwire is active-high */
3668                 eecd &= ~(E1000_EECD_CS | E1000_EECD_DI);
3669
3670                 ew32(EECD, eecd);
3671
3672                 /* Rising edge of clock */
3673                 eecd |= E1000_EECD_SK;
3674                 ew32(EECD, eecd);
3675                 E1000_WRITE_FLUSH();
3676                 udelay(hw->eeprom.delay_usec);
3677
3678                 /* Falling edge of clock */
3679                 eecd &= ~E1000_EECD_SK;
3680                 ew32(EECD, eecd);
3681                 E1000_WRITE_FLUSH();
3682                 udelay(hw->eeprom.delay_usec);
3683         }
3684
3685         /* Stop requesting EEPROM access */
3686         if (hw->mac_type > e1000_82544) {
3687                 eecd &= ~E1000_EECD_REQ;
3688                 ew32(EECD, eecd);
3689         }
3690 }
3691
3692 /**
3693  * e1000_spi_eeprom_ready - Reads a 16 bit word from the EEPROM.
3694  * @hw: Struct containing variables accessed by shared code
3695  */
3696 static s32 e1000_spi_eeprom_ready(struct e1000_hw *hw)
3697 {
3698         u16 retry_count = 0;
3699         u8 spi_stat_reg;
3700
3701         DEBUGFUNC("e1000_spi_eeprom_ready");
3702
3703         /* Read "Status Register" repeatedly until the LSB is cleared.  The
3704          * EEPROM will signal that the command has been completed by clearing
3705          * bit 0 of the internal status register.  If it's not cleared within
3706          * 5 milliseconds, then error out.
3707          */
3708         retry_count = 0;
3709         do {
3710                 e1000_shift_out_ee_bits(hw, EEPROM_RDSR_OPCODE_SPI,
3711                                         hw->eeprom.opcode_bits);
3712                 spi_stat_reg = (u8) e1000_shift_in_ee_bits(hw, 8);
3713                 if (!(spi_stat_reg & EEPROM_STATUS_RDY_SPI))
3714                         break;
3715
3716                 udelay(5);
3717                 retry_count += 5;
3718
3719                 e1000_standby_eeprom(hw);
3720         } while (retry_count < EEPROM_MAX_RETRY_SPI);
3721
3722         /* ATMEL SPI write time could vary from 0-20mSec on 3.3V devices (and
3723          * only 0-5mSec on 5V devices)
3724          */
3725         if (retry_count >= EEPROM_MAX_RETRY_SPI) {
3726                 DEBUGOUT("SPI EEPROM Status error\n");
3727                 return -E1000_ERR_EEPROM;
3728         }
3729
3730         return E1000_SUCCESS;
3731 }
3732
3733 /**
3734  * e1000_read_eeprom - Reads a 16 bit word from the EEPROM.
3735  * @hw: Struct containing variables accessed by shared code
3736  * @offset: offset of  word in the EEPROM to read
3737  * @data: word read from the EEPROM
3738  * @words: number of words to read
3739  */
3740 s32 e1000_read_eeprom(struct e1000_hw *hw, u16 offset, u16 words, u16 *data)
3741 {
3742         s32 ret;
3743         spin_lock(&e1000_eeprom_lock);
3744         ret = e1000_do_read_eeprom(hw, offset, words, data);
3745         spin_unlock(&e1000_eeprom_lock);
3746         return ret;
3747 }
3748
3749 static s32 e1000_do_read_eeprom(struct e1000_hw *hw, u16 offset, u16 words,
3750                                 u16 *data)
3751 {
3752         struct e1000_eeprom_info *eeprom = &hw->eeprom;
3753         u32 i = 0;
3754
3755         DEBUGFUNC("e1000_read_eeprom");
3756
3757         /* If eeprom is not yet detected, do so now */
3758         if (eeprom->word_size == 0)
3759                 e1000_init_eeprom_params(hw);
3760
3761         /* A check for invalid values:  offset too large, too many words, and not
3762          * enough words.
3763          */
3764         if ((offset >= eeprom->word_size)
3765             || (words > eeprom->word_size - offset) || (words == 0)) {
3766                 DEBUGOUT2
3767                     ("\"words\" parameter out of bounds. Words = %d, size = %d\n",
3768                      offset, eeprom->word_size);
3769                 return -E1000_ERR_EEPROM;
3770         }
3771
3772         /* EEPROM's that don't use EERD to read require us to bit-bang the SPI
3773          * directly. In this case, we need to acquire the EEPROM so that
3774          * FW or other port software does not interrupt.
3775          */
3776         if (!hw->eeprom.use_eerd) {
3777                 /* Prepare the EEPROM for bit-bang reading */
3778                 if (e1000_acquire_eeprom(hw) != E1000_SUCCESS)
3779                         return -E1000_ERR_EEPROM;
3780         }
3781
3782         /* Eerd register EEPROM access requires no eeprom aquire/release */
3783         if (eeprom->use_eerd)
3784                 return e1000_read_eeprom_eerd(hw, offset, words, data);
3785
3786         /* Set up the SPI or Microwire EEPROM for bit-bang reading.  We have
3787          * acquired the EEPROM at this point, so any returns should release it */
3788         if (eeprom->type == e1000_eeprom_spi) {
3789                 u16 word_in;
3790                 u8 read_opcode = EEPROM_READ_OPCODE_SPI;
3791
3792                 if (e1000_spi_eeprom_ready(hw)) {
3793                         e1000_release_eeprom(hw);
3794                         return -E1000_ERR_EEPROM;
3795                 }
3796
3797                 e1000_standby_eeprom(hw);
3798
3799                 /* Some SPI eeproms use the 8th address bit embedded in the opcode */
3800                 if ((eeprom->address_bits == 8) && (offset >= 128))
3801                         read_opcode |= EEPROM_A8_OPCODE_SPI;
3802
3803                 /* Send the READ command (opcode + addr)  */
3804                 e1000_shift_out_ee_bits(hw, read_opcode, eeprom->opcode_bits);
3805                 e1000_shift_out_ee_bits(hw, (u16) (offset * 2),
3806                                         eeprom->address_bits);
3807
3808                 /* Read the data.  The address of the eeprom internally increments with
3809                  * each byte (spi) being read, saving on the overhead of eeprom setup
3810                  * and tear-down.  The address counter will roll over if reading beyond
3811                  * the size of the eeprom, thus allowing the entire memory to be read
3812                  * starting from any offset. */
3813                 for (i = 0; i < words; i++) {
3814                         word_in = e1000_shift_in_ee_bits(hw, 16);
3815                         data[i] = (word_in >> 8) | (word_in << 8);
3816                 }
3817         } else if (eeprom->type == e1000_eeprom_microwire) {
3818                 for (i = 0; i < words; i++) {
3819                         /* Send the READ command (opcode + addr)  */
3820                         e1000_shift_out_ee_bits(hw,
3821                                                 EEPROM_READ_OPCODE_MICROWIRE,
3822                                                 eeprom->opcode_bits);
3823                         e1000_shift_out_ee_bits(hw, (u16) (offset + i),
3824                                                 eeprom->address_bits);
3825
3826                         /* Read the data.  For microwire, each word requires the overhead
3827                          * of eeprom setup and tear-down. */
3828                         data[i] = e1000_shift_in_ee_bits(hw, 16);
3829                         e1000_standby_eeprom(hw);
3830                 }
3831         }
3832
3833         /* End this read operation */
3834         e1000_release_eeprom(hw);
3835
3836         return E1000_SUCCESS;
3837 }
3838
3839 /**
3840  * Reads a 16 bit word from the EEPROM using the EERD register.
3841  *
3842  * @hw: Struct containing variables accessed by shared code
3843  * offset - offset of  word in the EEPROM to read
3844  * data - word read from the EEPROM
3845  * words - number of words to read
3846  */
3847 static s32 e1000_read_eeprom_eerd(struct e1000_hw *hw, u16 offset, u16 words,
3848                                   u16 *data)
3849 {
3850         u32 i, eerd = 0;
3851         s32 error = 0;
3852
3853         for (i = 0; i < words; i++) {
3854                 eerd = ((offset + i) << E1000_EEPROM_RW_ADDR_SHIFT) +
3855                     E1000_EEPROM_RW_REG_START;
3856
3857                 ew32(EERD, eerd);
3858                 error = e1000_poll_eerd_eewr_done(hw, E1000_EEPROM_POLL_READ);
3859
3860                 if (error) {
3861                         break;
3862                 }
3863                 data[i] = (er32(EERD) >> E1000_EEPROM_RW_REG_DATA);
3864
3865         }
3866
3867         return error;
3868 }
3869
3870 /**
3871  * Writes a 16 bit word from the EEPROM using the EEWR register.
3872  *
3873  * @hw: Struct containing variables accessed by shared code
3874  * offset - offset of  word in the EEPROM to read
3875  * data - word read from the EEPROM
3876  * words - number of words to read
3877  */
3878 static s32 e1000_write_eeprom_eewr(struct e1000_hw *hw, u16 offset, u16 words,
3879                                    u16 *data)
3880 {
3881         u32 register_value = 0;
3882         u32 i = 0;
3883         s32 error = 0;
3884
3885         for (i = 0; i < words; i++) {
3886                 register_value = (data[i] << E1000_EEPROM_RW_REG_DATA) |
3887                     ((offset + i) << E1000_EEPROM_RW_ADDR_SHIFT) |
3888                     E1000_EEPROM_RW_REG_START;
3889
3890                 error = e1000_poll_eerd_eewr_done(hw, E1000_EEPROM_POLL_WRITE);
3891                 if (error) {
3892                         break;
3893                 }
3894
3895                 ew32(EEWR, register_value);
3896
3897                 error = e1000_poll_eerd_eewr_done(hw, E1000_EEPROM_POLL_WRITE);
3898
3899                 if (error) {
3900                         break;
3901                 }
3902         }
3903
3904         return error;
3905 }
3906
3907 /**
3908  * Polls the status bit (bit 1) of the EERD to determine when the read is done.
3909  *
3910  * @hw: Struct containing variables accessed by shared code
3911  */
3912 static s32 e1000_poll_eerd_eewr_done(struct e1000_hw *hw, int eerd)
3913 {
3914         u32 attempts = 100000;
3915         u32 i, reg = 0;
3916         s32 done = E1000_ERR_EEPROM;
3917
3918         for (i = 0; i < attempts; i++) {
3919                 if (eerd == E1000_EEPROM_POLL_READ)
3920                         reg = er32(EERD);
3921                 else
3922                         reg = er32(EEWR);
3923
3924                 if (reg & E1000_EEPROM_RW_REG_DONE) {
3925                         done = E1000_SUCCESS;
3926                         break;
3927                 }
3928                 udelay(5);
3929         }
3930
3931         return done;
3932 }
3933
3934 /**
3935  * e1000_validate_eeprom_checksum - Verifies that the EEPROM has a valid checksum
3936  * @hw: Struct containing variables accessed by shared code
3937  *
3938  * Reads the first 64 16 bit words of the EEPROM and sums the values read.
3939  * If the the sum of the 64 16 bit words is 0xBABA, the EEPROM's checksum is
3940  * valid.
3941  */
3942 s32 e1000_validate_eeprom_checksum(struct e1000_hw *hw)
3943 {
3944         u16 checksum = 0;
3945         u16 i, eeprom_data;
3946
3947         DEBUGFUNC("e1000_validate_eeprom_checksum");
3948
3949         for (i = 0; i < (EEPROM_CHECKSUM_REG + 1); i++) {
3950                 if (e1000_read_eeprom(hw, i, 1, &eeprom_data) < 0) {
3951                         DEBUGOUT("EEPROM Read Error\n");
3952                         return -E1000_ERR_EEPROM;
3953                 }
3954                 checksum += eeprom_data;
3955         }
3956
3957         if (checksum == (u16) EEPROM_SUM)
3958                 return E1000_SUCCESS;
3959         else {
3960                 DEBUGOUT("EEPROM Checksum Invalid\n");
3961                 return -E1000_ERR_EEPROM;
3962         }
3963 }
3964
3965 /**
3966  * e1000_update_eeprom_checksum - Calculates/writes the EEPROM checksum
3967  * @hw: Struct containing variables accessed by shared code
3968  *
3969  * Sums the first 63 16 bit words of the EEPROM. Subtracts the sum from 0xBABA.
3970  * Writes the difference to word offset 63 of the EEPROM.
3971  */
3972 s32 e1000_update_eeprom_checksum(struct e1000_hw *hw)
3973 {
3974         u16 checksum = 0;
3975         u16 i, eeprom_data;
3976
3977         DEBUGFUNC("e1000_update_eeprom_checksum");
3978
3979         for (i = 0; i < EEPROM_CHECKSUM_REG; i++) {
3980                 if (e1000_read_eeprom(hw, i, 1, &eeprom_data) < 0) {
3981                         DEBUGOUT("EEPROM Read Error\n");
3982                         return -E1000_ERR_EEPROM;
3983                 }
3984                 checksum += eeprom_data;
3985         }
3986         checksum = (u16) EEPROM_SUM - checksum;
3987         if (e1000_write_eeprom(hw, EEPROM_CHECKSUM_REG, 1, &checksum) < 0) {
3988                 DEBUGOUT("EEPROM Write Error\n");
3989                 return -E1000_ERR_EEPROM;
3990         }
3991         return E1000_SUCCESS;
3992 }
3993
3994 /**
3995  * e1000_write_eeprom - write words to the different EEPROM types.
3996  * @hw: Struct containing variables accessed by shared code
3997  * @offset: offset within the EEPROM to be written to
3998  * @words: number of words to write
3999  * @data: 16 bit word to be written to the EEPROM
4000  *
4001  * If e1000_update_eeprom_checksum is not called after this function, the
4002  * EEPROM will most likely contain an invalid checksum.
4003  */
4004 s32 e1000_write_eeprom(struct e1000_hw *hw, u16 offset, u16 words, u16 *data)
4005 {
4006         s32 ret;
4007         spin_lock(&e1000_eeprom_lock);
4008         ret = e1000_do_write_eeprom(hw, offset, words, data);
4009         spin_unlock(&e1000_eeprom_lock);
4010         return ret;
4011 }
4012
4013 static s32 e1000_do_write_eeprom(struct e1000_hw *hw, u16 offset, u16 words,
4014                                  u16 *data)
4015 {
4016         struct e1000_eeprom_info *eeprom = &hw->eeprom;
4017         s32 status = 0;
4018
4019         DEBUGFUNC("e1000_write_eeprom");
4020
4021         /* If eeprom is not yet detected, do so now */
4022         if (eeprom->word_size == 0)
4023                 e1000_init_eeprom_params(hw);
4024
4025         /* A check for invalid values:  offset too large, too many words, and not
4026          * enough words.
4027          */
4028         if ((offset >= eeprom->word_size)
4029             || (words > eeprom->word_size - offset) || (words == 0)) {
4030                 DEBUGOUT("\"words\" parameter out of bounds\n");
4031                 return -E1000_ERR_EEPROM;
4032         }
4033
4034         if (eeprom->use_eewr)
4035                 return e1000_write_eeprom_eewr(hw, offset, words, data);
4036
4037         /* Prepare the EEPROM for writing  */
4038         if (e1000_acquire_eeprom(hw) != E1000_SUCCESS)
4039                 return -E1000_ERR_EEPROM;
4040
4041         if (eeprom->type == e1000_eeprom_microwire) {
4042                 status = e1000_write_eeprom_microwire(hw, offset, words, data);
4043         } else {
4044                 status = e1000_write_eeprom_spi(hw, offset, words, data);
4045                 msleep(10);
4046         }
4047
4048         /* Done with writing */
4049         e1000_release_eeprom(hw);
4050
4051         return status;
4052 }
4053
4054 /**
4055  * e1000_write_eeprom_spi - Writes a 16 bit word to a given offset in an SPI EEPROM.
4056  * @hw: Struct containing variables accessed by shared code
4057  * @offset: offset within the EEPROM to be written to
4058  * @words: number of words to write
4059  * @data: pointer to array of 8 bit words to be written to the EEPROM
4060  */
4061 static s32 e1000_write_eeprom_spi(struct e1000_hw *hw, u16 offset, u16 words,
4062                                   u16 *data)
4063 {
4064         struct e1000_eeprom_info *eeprom = &hw->eeprom;
4065         u16 widx = 0;
4066
4067         DEBUGFUNC("e1000_write_eeprom_spi");
4068
4069         while (widx < words) {
4070                 u8 write_opcode = EEPROM_WRITE_OPCODE_SPI;
4071
4072                 if (e1000_spi_eeprom_ready(hw))
4073                         return -E1000_ERR_EEPROM;
4074
4075                 e1000_standby_eeprom(hw);
4076
4077                 /*  Send the WRITE ENABLE command (8 bit opcode )  */
4078                 e1000_shift_out_ee_bits(hw, EEPROM_WREN_OPCODE_SPI,
4079                                         eeprom->opcode_bits);
4080
4081                 e1000_standby_eeprom(hw);
4082
4083                 /* Some SPI eeproms use the 8th address bit embedded in the opcode */
4084                 if ((eeprom->address_bits == 8) && (offset >= 128))
4085                         write_opcode |= EEPROM_A8_OPCODE_SPI;
4086
4087                 /* Send the Write command (8-bit opcode + addr) */
4088                 e1000_shift_out_ee_bits(hw, write_opcode, eeprom->opcode_bits);
4089
4090                 e1000_shift_out_ee_bits(hw, (u16) ((offset + widx) * 2),
4091                                         eeprom->address_bits);
4092
4093                 /* Send the data */
4094
4095                 /* Loop to allow for up to whole page write (32 bytes) of eeprom */
4096                 while (widx < words) {
4097                         u16 word_out = data[widx];
4098                         word_out = (word_out >> 8) | (word_out << 8);
4099                         e1000_shift_out_ee_bits(hw, word_out, 16);
4100                         widx++;
4101
4102                         /* Some larger eeprom sizes are capable of a 32-byte PAGE WRITE
4103                          * operation, while the smaller eeproms are capable of an 8-byte
4104                          * PAGE WRITE operation.  Break the inner loop to pass new address
4105                          */
4106                         if ((((offset + widx) * 2) % eeprom->page_size) == 0) {
4107                                 e1000_standby_eeprom(hw);
4108                                 break;
4109                         }
4110                 }
4111         }
4112
4113         return E1000_SUCCESS;
4114 }
4115
4116 /**
4117  * e1000_write_eeprom_microwire - Writes a 16 bit word to a given offset in a Microwire EEPROM.
4118  * @hw: Struct containing variables accessed by shared code
4119  * @offset: offset within the EEPROM to be written to
4120  * @words: number of words to write
4121  * @data: pointer to array of 8 bit words to be written to the EEPROM
4122  */
4123 static s32 e1000_write_eeprom_microwire(struct e1000_hw *hw, u16 offset,
4124                                         u16 words, u16 *data)
4125 {
4126         struct e1000_eeprom_info *eeprom = &hw->eeprom;
4127         u32 eecd;
4128         u16 words_written = 0;
4129         u16 i = 0;
4130
4131         DEBUGFUNC("e1000_write_eeprom_microwire");
4132
4133         /* Send the write enable command to the EEPROM (3-bit opcode plus
4134          * 6/8-bit dummy address beginning with 11).  It's less work to include
4135          * the 11 of the dummy address as part of the opcode than it is to shift
4136          * it over the correct number of bits for the address.  This puts the
4137          * EEPROM into write/erase mode.
4138          */
4139         e1000_shift_out_ee_bits(hw, EEPROM_EWEN_OPCODE_MICROWIRE,
4140                                 (u16) (eeprom->opcode_bits + 2));
4141
4142         e1000_shift_out_ee_bits(hw, 0, (u16) (eeprom->address_bits - 2));
4143
4144         /* Prepare the EEPROM */
4145         e1000_standby_eeprom(hw);
4146
4147         while (words_written < words) {
4148                 /* Send the Write command (3-bit opcode + addr) */
4149                 e1000_shift_out_ee_bits(hw, EEPROM_WRITE_OPCODE_MICROWIRE,
4150                                         eeprom->opcode_bits);
4151
4152                 e1000_shift_out_ee_bits(hw, (u16) (offset + words_written),
4153                                         eeprom->address_bits);
4154
4155                 /* Send the data */
4156                 e1000_shift_out_ee_bits(hw, data[words_written], 16);
4157
4158                 /* Toggle the CS line.  This in effect tells the EEPROM to execute
4159                  * the previous command.
4160                  */
4161                 e1000_standby_eeprom(hw);
4162
4163                 /* Read DO repeatedly until it is high (equal to '1').  The EEPROM will
4164                  * signal that the command has been completed by raising the DO signal.
4165                  * If DO does not go high in 10 milliseconds, then error out.
4166                  */
4167                 for (i = 0; i < 200; i++) {
4168                         eecd = er32(EECD);
4169                         if (eecd & E1000_EECD_DO)
4170                                 break;
4171                         udelay(50);
4172                 }
4173                 if (i == 200) {
4174                         DEBUGOUT("EEPROM Write did not complete\n");
4175                         return -E1000_ERR_EEPROM;
4176                 }
4177
4178                 /* Recover from write */
4179                 e1000_standby_eeprom(hw);
4180
4181                 words_written++;
4182         }
4183
4184         /* Send the write disable command to the EEPROM (3-bit opcode plus
4185          * 6/8-bit dummy address beginning with 10).  It's less work to include
4186          * the 10 of the dummy address as part of the opcode than it is to shift
4187          * it over the correct number of bits for the address.  This takes the
4188          * EEPROM out of write/erase mode.
4189          */
4190         e1000_shift_out_ee_bits(hw, EEPROM_EWDS_OPCODE_MICROWIRE,
4191                                 (u16) (eeprom->opcode_bits + 2));
4192
4193         e1000_shift_out_ee_bits(hw, 0, (u16) (eeprom->address_bits - 2));
4194
4195         return E1000_SUCCESS;
4196 }
4197
4198 /**
4199  * e1000_read_mac_addr - read the adapters MAC from eeprom
4200  * @hw: Struct containing variables accessed by shared code
4201  *
4202  * Reads the adapter's MAC address from the EEPROM and inverts the LSB for the
4203  * second function of dual function devices
4204  */
4205 s32 e1000_read_mac_addr(struct e1000_hw *hw)
4206 {
4207         u16 offset;
4208         u16 eeprom_data, i;
4209
4210         DEBUGFUNC("e1000_read_mac_addr");
4211
4212         for (i = 0; i < NODE_ADDRESS_SIZE; i += 2) {
4213                 offset = i >> 1;
4214                 if (e1000_read_eeprom(hw, offset, 1, &eeprom_data) < 0) {
4215                         DEBUGOUT("EEPROM Read Error\n");
4216                         return -E1000_ERR_EEPROM;
4217                 }
4218                 hw->perm_mac_addr[i] = (u8) (eeprom_data & 0x00FF);
4219                 hw->perm_mac_addr[i + 1] = (u8) (eeprom_data >> 8);
4220         }
4221
4222         switch (hw->mac_type) {
4223         default:
4224                 break;
4225         case e1000_82546:
4226         case e1000_82546_rev_3:
4227                 if (er32(STATUS) & E1000_STATUS_FUNC_1)
4228                         hw->perm_mac_addr[5] ^= 0x01;
4229                 break;
4230         }
4231
4232         for (i = 0; i < NODE_ADDRESS_SIZE; i++)
4233                 hw->mac_addr[i] = hw->perm_mac_addr[i];
4234         return E1000_SUCCESS;
4235 }
4236
4237 /**
4238  * e1000_init_rx_addrs - Initializes receive address filters.
4239  * @hw: Struct containing variables accessed by shared code
4240  *
4241  * Places the MAC address in receive address register 0 and clears the rest
4242  * of the receive address registers. Clears the multicast table. Assumes
4243  * the receiver is in reset when the routine is called.
4244  */
4245 static void e1000_init_rx_addrs(struct e1000_hw *hw)
4246 {
4247         u32 i;
4248         u32 rar_num;
4249
4250         DEBUGFUNC("e1000_init_rx_addrs");
4251
4252         /* Setup the receive address. */
4253         DEBUGOUT("Programming MAC Address into RAR[0]\n");
4254
4255         e1000_rar_set(hw, hw->mac_addr, 0);
4256
4257         rar_num = E1000_RAR_ENTRIES;
4258
4259         /* Zero out the other 15 receive addresses. */
4260         DEBUGOUT("Clearing RAR[1-15]\n");
4261         for (i = 1; i < rar_num; i++) {
4262                 E1000_WRITE_REG_ARRAY(hw, RA, (i << 1), 0);
4263                 E1000_WRITE_FLUSH();
4264                 E1000_WRITE_REG_ARRAY(hw, RA, ((i << 1) + 1), 0);
4265                 E1000_WRITE_FLUSH();
4266         }
4267 }
4268
4269 /**
4270  * e1000_hash_mc_addr - Hashes an address to determine its location in the multicast table
4271  * @hw: Struct containing variables accessed by shared code
4272  * @mc_addr: the multicast address to hash
4273  */
4274 u32 e1000_hash_mc_addr(struct e1000_hw *hw, u8 *mc_addr)
4275 {
4276         u32 hash_value = 0;
4277
4278         /* The portion of the address that is used for the hash table is
4279          * determined by the mc_filter_type setting.
4280          */
4281         switch (hw->mc_filter_type) {
4282                 /* [0] [1] [2] [3] [4] [5]
4283                  * 01  AA  00  12  34  56
4284                  * LSB                 MSB
4285                  */
4286         case 0:
4287                 /* [47:36] i.e. 0x563 for above example address */
4288                 hash_value = ((mc_addr[4] >> 4) | (((u16) mc_addr[5]) << 4));
4289                 break;
4290         case 1:
4291                 /* [46:35] i.e. 0xAC6 for above example address */
4292                 hash_value = ((mc_addr[4] >> 3) | (((u16) mc_addr[5]) << 5));
4293                 break;
4294         case 2:
4295                 /* [45:34] i.e. 0x5D8 for above example address */
4296                 hash_value = ((mc_addr[4] >> 2) | (((u16) mc_addr[5]) << 6));
4297                 break;
4298         case 3:
4299                 /* [43:32] i.e. 0x634 for above example address */
4300                 hash_value = ((mc_addr[4]) | (((u16) mc_addr[5]) << 8));
4301                 break;
4302         }
4303
4304         hash_value &= 0xFFF;
4305         return hash_value;
4306 }
4307
4308 /**
4309  * e1000_rar_set - Puts an ethernet address into a receive address register.
4310  * @hw: Struct containing variables accessed by shared code
4311  * @addr: Address to put into receive address register
4312  * @index: Receive address register to write
4313  */
4314 void e1000_rar_set(struct e1000_hw *hw, u8 *addr, u32 index)
4315 {
4316         u32 rar_low, rar_high;
4317
4318         /* HW expects these in little endian so we reverse the byte order
4319          * from network order (big endian) to little endian
4320          */
4321         rar_low = ((u32) addr[0] | ((u32) addr[1] << 8) |
4322                    ((u32) addr[2] << 16) | ((u32) addr[3] << 24));
4323         rar_high = ((u32) addr[4] | ((u32) addr[5] << 8));
4324
4325         /* Disable Rx and flush all Rx frames before enabling RSS to avoid Rx
4326          * unit hang.
4327          *
4328          * Description:
4329          * If there are any Rx frames queued up or otherwise present in the HW
4330          * before RSS is enabled, and then we enable RSS, the HW Rx unit will
4331          * hang.  To work around this issue, we have to disable receives and
4332          * flush out all Rx frames before we enable RSS. To do so, we modify we
4333          * redirect all Rx traffic to manageability and then reset the HW.
4334          * This flushes away Rx frames, and (since the redirections to
4335          * manageability persists across resets) keeps new ones from coming in
4336          * while we work.  Then, we clear the Address Valid AV bit for all MAC
4337          * addresses and undo the re-direction to manageability.
4338          * Now, frames are coming in again, but the MAC won't accept them, so
4339          * far so good.  We now proceed to initialize RSS (if necessary) and
4340          * configure the Rx unit.  Last, we re-enable the AV bits and continue
4341          * on our merry way.
4342          */
4343         switch (hw->mac_type) {
4344         default:
4345                 /* Indicate to hardware the Address is Valid. */
4346                 rar_high |= E1000_RAH_AV;
4347                 break;
4348         }
4349
4350         E1000_WRITE_REG_ARRAY(hw, RA, (index << 1), rar_low);
4351         E1000_WRITE_FLUSH();
4352         E1000_WRITE_REG_ARRAY(hw, RA, ((index << 1) + 1), rar_high);
4353         E1000_WRITE_FLUSH();
4354 }
4355
4356 /**
4357  * e1000_write_vfta - Writes a value to the specified offset in the VLAN filter table.
4358  * @hw: Struct containing variables accessed by shared code
4359  * @offset: Offset in VLAN filer table to write
4360  * @value: Value to write into VLAN filter table
4361  */
4362 void e1000_write_vfta(struct e1000_hw *hw, u32 offset, u32 value)
4363 {
4364         u32 temp;
4365
4366         if ((hw->mac_type == e1000_82544) && ((offset & 0x1) == 1)) {
4367                 temp = E1000_READ_REG_ARRAY(hw, VFTA, (offset - 1));
4368                 E1000_WRITE_REG_ARRAY(hw, VFTA, offset, value);
4369                 E1000_WRITE_FLUSH();
4370                 E1000_WRITE_REG_ARRAY(hw, VFTA, (offset - 1), temp);
4371                 E1000_WRITE_FLUSH();
4372         } else {
4373                 E1000_WRITE_REG_ARRAY(hw, VFTA, offset, value);
4374                 E1000_WRITE_FLUSH();
4375         }
4376 }
4377
4378 /**
4379  * e1000_clear_vfta - Clears the VLAN filer table
4380  * @hw: Struct containing variables accessed by shared code
4381  */
4382 static void e1000_clear_vfta(struct e1000_hw *hw)
4383 {
4384         u32 offset;
4385         u32 vfta_value = 0;
4386         u32 vfta_offset = 0;
4387         u32 vfta_bit_in_reg = 0;
4388
4389         for (offset = 0; offset < E1000_VLAN_FILTER_TBL_SIZE; offset++) {
4390                 /* If the offset we want to clear is the same offset of the
4391                  * manageability VLAN ID, then clear all bits except that of the
4392                  * manageability unit */
4393                 vfta_value = (offset == vfta_offset) ? vfta_bit_in_reg : 0;
4394                 E1000_WRITE_REG_ARRAY(hw, VFTA, offset, vfta_value);
4395                 E1000_WRITE_FLUSH();
4396         }
4397 }
4398
4399 static s32 e1000_id_led_init(struct e1000_hw *hw)
4400 {
4401         u32 ledctl;
4402         const u32 ledctl_mask = 0x000000FF;
4403         const u32 ledctl_on = E1000_LEDCTL_MODE_LED_ON;
4404         const u32 ledctl_off = E1000_LEDCTL_MODE_LED_OFF;
4405         u16 eeprom_data, i, temp;
4406         const u16 led_mask = 0x0F;
4407
4408         DEBUGFUNC("e1000_id_led_init");
4409
4410         if (hw->mac_type < e1000_82540) {
4411                 /* Nothing to do */
4412                 return E1000_SUCCESS;
4413         }
4414
4415         ledctl = er32(LEDCTL);
4416         hw->ledctl_default = ledctl;
4417         hw->ledctl_mode1 = hw->ledctl_default;
4418         hw->ledctl_mode2 = hw->ledctl_default;
4419
4420         if (e1000_read_eeprom(hw, EEPROM_ID_LED_SETTINGS, 1, &eeprom_data) < 0) {
4421                 DEBUGOUT("EEPROM Read Error\n");
4422                 return -E1000_ERR_EEPROM;
4423         }
4424
4425         if ((eeprom_data == ID_LED_RESERVED_0000) ||
4426             (eeprom_data == ID_LED_RESERVED_FFFF)) {
4427                 eeprom_data = ID_LED_DEFAULT;
4428         }
4429
4430         for (i = 0; i < 4; i++) {
4431                 temp = (eeprom_data >> (i << 2)) & led_mask;
4432                 switch (temp) {
4433                 case ID_LED_ON1_DEF2:
4434                 case ID_LED_ON1_ON2:
4435                 case ID_LED_ON1_OFF2:
4436                         hw->ledctl_mode1 &= ~(ledctl_mask << (i << 3));
4437                         hw->ledctl_mode1 |= ledctl_on << (i << 3);
4438                         break;
4439                 case ID_LED_OFF1_DEF2:
4440                 case ID_LED_OFF1_ON2:
4441                 case ID_LED_OFF1_OFF2:
4442                         hw->ledctl_mode1 &= ~(ledctl_mask << (i << 3));
4443                         hw->ledctl_mode1 |= ledctl_off << (i << 3);
4444                         break;
4445                 default:
4446                         /* Do nothing */
4447                         break;
4448                 }
4449                 switch (temp) {
4450                 case ID_LED_DEF1_ON2:
4451                 case ID_LED_ON1_ON2:
4452                 case ID_LED_OFF1_ON2:
4453                         hw->ledctl_mode2 &= ~(ledctl_mask << (i << 3));
4454                         hw->ledctl_mode2 |= ledctl_on << (i << 3);
4455                         break;
4456                 case ID_LED_DEF1_OFF2:
4457                 case ID_LED_ON1_OFF2:
4458                 case ID_LED_OFF1_OFF2:
4459                         hw->ledctl_mode2 &= ~(ledctl_mask << (i << 3));
4460                         hw->ledctl_mode2 |= ledctl_off << (i << 3);
4461                         break;
4462                 default:
4463                         /* Do nothing */
4464                         break;
4465                 }
4466         }
4467         return E1000_SUCCESS;
4468 }
4469
4470 /**
4471  * e1000_setup_led
4472  * @hw: Struct containing variables accessed by shared code
4473  *
4474  * Prepares SW controlable LED for use and saves the current state of the LED.
4475  */
4476 s32 e1000_setup_led(struct e1000_hw *hw)
4477 {
4478         u32 ledctl;
4479         s32 ret_val = E1000_SUCCESS;
4480
4481         DEBUGFUNC("e1000_setup_led");
4482
4483         switch (hw->mac_type) {
4484         case e1000_82542_rev2_0:
4485         case e1000_82542_rev2_1:
4486         case e1000_82543:
4487         case e1000_82544:
4488                 /* No setup necessary */
4489                 break;
4490         case e1000_82541:
4491         case e1000_82547:
4492         case e1000_82541_rev_2:
4493         case e1000_82547_rev_2:
4494                 /* Turn off PHY Smart Power Down (if enabled) */
4495                 ret_val = e1000_read_phy_reg(hw, IGP01E1000_GMII_FIFO,
4496                                              &hw->phy_spd_default);
4497                 if (ret_val)
4498                         return ret_val;
4499                 ret_val = e1000_write_phy_reg(hw, IGP01E1000_GMII_FIFO,
4500                                               (u16) (hw->phy_spd_default &
4501                                                      ~IGP01E1000_GMII_SPD));
4502                 if (ret_val)
4503                         return ret_val;
4504                 /* Fall Through */
4505         default:
4506                 if (hw->media_type == e1000_media_type_fiber) {
4507                         ledctl = er32(LEDCTL);
4508                         /* Save current LEDCTL settings */
4509                         hw->ledctl_default = ledctl;
4510                         /* Turn off LED0 */
4511                         ledctl &= ~(E1000_LEDCTL_LED0_IVRT |
4512                                     E1000_LEDCTL_LED0_BLINK |
4513                                     E1000_LEDCTL_LED0_MODE_MASK);
4514                         ledctl |= (E1000_LEDCTL_MODE_LED_OFF <<
4515                                    E1000_LEDCTL_LED0_MODE_SHIFT);
4516                         ew32(LEDCTL, ledctl);
4517                 } else if (hw->media_type == e1000_media_type_copper)
4518                         ew32(LEDCTL, hw->ledctl_mode1);
4519                 break;
4520         }
4521
4522         return E1000_SUCCESS;
4523 }
4524
4525 /**
4526  * e1000_cleanup_led - Restores the saved state of the SW controlable LED.
4527  * @hw: Struct containing variables accessed by shared code
4528  */
4529 s32 e1000_cleanup_led(struct e1000_hw *hw)
4530 {
4531         s32 ret_val = E1000_SUCCESS;
4532
4533         DEBUGFUNC("e1000_cleanup_led");
4534
4535         switch (hw->mac_type) {
4536         case e1000_82542_rev2_0:
4537         case e1000_82542_rev2_1:
4538         case e1000_82543:
4539         case e1000_82544:
4540                 /* No cleanup necessary */
4541                 break;
4542         case e1000_82541:
4543         case e1000_82547:
4544         case e1000_82541_rev_2:
4545         case e1000_82547_rev_2:
4546                 /* Turn on PHY Smart Power Down (if previously enabled) */
4547                 ret_val = e1000_write_phy_reg(hw, IGP01E1000_GMII_FIFO,
4548                                               hw->phy_spd_default);
4549                 if (ret_val)
4550                         return ret_val;
4551                 /* Fall Through */
4552         default:
4553                 /* Restore LEDCTL settings */
4554                 ew32(LEDCTL, hw->ledctl_default);
4555                 break;
4556         }
4557
4558         return E1000_SUCCESS;
4559 }
4560
4561 /**
4562  * e1000_led_on - Turns on the software controllable LED
4563  * @hw: Struct containing variables accessed by shared code
4564  */
4565 s32 e1000_led_on(struct e1000_hw *hw)
4566 {
4567         u32 ctrl = er32(CTRL);
4568
4569         DEBUGFUNC("e1000_led_on");
4570
4571         switch (hw->mac_type) {
4572         case e1000_82542_rev2_0:
4573         case e1000_82542_rev2_1:
4574         case e1000_82543:
4575                 /* Set SW Defineable Pin 0 to turn on the LED */
4576                 ctrl |= E1000_CTRL_SWDPIN0;
4577                 ctrl |= E1000_CTRL_SWDPIO0;
4578                 break;
4579         case e1000_82544:
4580                 if (hw->media_type == e1000_media_type_fiber) {
4581                         /* Set SW Defineable Pin 0 to turn on the LED */
4582                         ctrl |= E1000_CTRL_SWDPIN0;
4583                         ctrl |= E1000_CTRL_SWDPIO0;
4584                 } else {
4585                         /* Clear SW Defineable Pin 0 to turn on the LED */
4586                         ctrl &= ~E1000_CTRL_SWDPIN0;
4587                         ctrl |= E1000_CTRL_SWDPIO0;
4588                 }
4589                 break;
4590         default:
4591                 if (hw->media_type == e1000_media_type_fiber) {
4592                         /* Clear SW Defineable Pin 0 to turn on the LED */
4593                         ctrl &= ~E1000_CTRL_SWDPIN0;
4594                         ctrl |= E1000_CTRL_SWDPIO0;
4595                 } else if (hw->media_type == e1000_media_type_copper) {
4596                         ew32(LEDCTL, hw->ledctl_mode2);
4597                         return E1000_SUCCESS;
4598                 }
4599                 break;
4600         }
4601
4602         ew32(CTRL, ctrl);
4603
4604         return E1000_SUCCESS;
4605 }
4606
4607 /**
4608  * e1000_led_off - Turns off the software controllable LED
4609  * @hw: Struct containing variables accessed by shared code
4610  */
4611 s32 e1000_led_off(struct e1000_hw *hw)
4612 {
4613         u32 ctrl = er32(CTRL);
4614
4615         DEBUGFUNC("e1000_led_off");
4616
4617         switch (hw->mac_type) {
4618         case e1000_82542_rev2_0:
4619         case e1000_82542_rev2_1:
4620         case e1000_82543:
4621                 /* Clear SW Defineable Pin 0 to turn off the LED */
4622                 ctrl &= ~E1000_CTRL_SWDPIN0;
4623                 ctrl |= E1000_CTRL_SWDPIO0;
4624                 break;
4625         case e1000_82544:
4626                 if (hw->media_type == e1000_media_type_fiber) {
4627                         /* Clear SW Defineable Pin 0 to turn off the LED */
4628                         ctrl &= ~E1000_CTRL_SWDPIN0;
4629                         ctrl |= E1000_CTRL_SWDPIO0;
4630                 } else {
4631                         /* Set SW Defineable Pin 0 to turn off the LED */
4632                         ctrl |= E1000_CTRL_SWDPIN0;
4633                         ctrl |= E1000_CTRL_SWDPIO0;
4634                 }
4635                 break;
4636         default:
4637                 if (hw->media_type == e1000_media_type_fiber) {
4638                         /* Set SW Defineable Pin 0 to turn off the LED */
4639                         ctrl |= E1000_CTRL_SWDPIN0;
4640                         ctrl |= E1000_CTRL_SWDPIO0;
4641                 } else if (hw->media_type == e1000_media_type_copper) {
4642                         ew32(LEDCTL, hw->ledctl_mode1);
4643                         return E1000_SUCCESS;
4644                 }
4645                 break;
4646         }
4647
4648         ew32(CTRL, ctrl);
4649
4650         return E1000_SUCCESS;
4651 }
4652
4653 /**
4654  * e1000_clear_hw_cntrs - Clears all hardware statistics counters.
4655  * @hw: Struct containing variables accessed by shared code
4656  */
4657 static void e1000_clear_hw_cntrs(struct e1000_hw *hw)
4658 {
4659         volatile u32 temp;
4660
4661         temp = er32(CRCERRS);
4662         temp = er32(SYMERRS);
4663         temp = er32(MPC);
4664         temp = er32(SCC);
4665         temp = er32(ECOL);
4666         temp = er32(MCC);
4667         temp = er32(LATECOL);
4668         temp = er32(COLC);
4669         temp = er32(DC);
4670         temp = er32(SEC);
4671         temp = er32(RLEC);
4672         temp = er32(XONRXC);
4673         temp = er32(XONTXC);
4674         temp = er32(XOFFRXC);
4675         temp = er32(XOFFTXC);
4676         temp = er32(FCRUC);
4677
4678         temp = er32(PRC64);
4679         temp = er32(PRC127);
4680         temp = er32(PRC255);
4681         temp = er32(PRC511);
4682         temp = er32(PRC1023);
4683         temp = er32(PRC1522);
4684
4685         temp = er32(GPRC);
4686         temp = er32(BPRC);
4687         temp = er32(MPRC);
4688         temp = er32(GPTC);
4689         temp = er32(GORCL);
4690         temp = er32(GORCH);
4691         temp = er32(GOTCL);
4692         temp = er32(GOTCH);
4693         temp = er32(RNBC);
4694         temp = er32(RUC);
4695         temp = er32(RFC);
4696         temp = er32(ROC);
4697         temp = er32(RJC);
4698         temp = er32(TORL);
4699         temp = er32(TORH);
4700         temp = er32(TOTL);
4701         temp = er32(TOTH);
4702         temp = er32(TPR);
4703         temp = er32(TPT);
4704
4705         temp = er32(PTC64);
4706         temp = er32(PTC127);
4707         temp = er32(PTC255);
4708         temp = er32(PTC511);
4709         temp = er32(PTC1023);
4710         temp = er32(PTC1522);
4711
4712         temp = er32(MPTC);
4713         temp = er32(BPTC);
4714
4715         if (hw->mac_type < e1000_82543)
4716                 return;
4717
4718         temp = er32(ALGNERRC);
4719         temp = er32(RXERRC);
4720         temp = er32(TNCRS);
4721         temp = er32(CEXTERR);
4722         temp = er32(TSCTC);
4723         temp = er32(TSCTFC);
4724
4725         if (hw->mac_type <= e1000_82544)
4726                 return;
4727
4728         temp = er32(MGTPRC);
4729         temp = er32(MGTPDC);
4730         temp = er32(MGTPTC);
4731 }
4732
4733 /**
4734  * e1000_reset_adaptive - Resets Adaptive IFS to its default state.
4735  * @hw: Struct containing variables accessed by shared code
4736  *
4737  * Call this after e1000_init_hw. You may override the IFS defaults by setting
4738  * hw->ifs_params_forced to true. However, you must initialize hw->
4739  * current_ifs_val, ifs_min_val, ifs_max_val, ifs_step_size, and ifs_ratio
4740  * before calling this function.
4741  */
4742 void e1000_reset_adaptive(struct e1000_hw *hw)
4743 {
4744         DEBUGFUNC("e1000_reset_adaptive");
4745
4746         if (hw->adaptive_ifs) {
4747                 if (!hw->ifs_params_forced) {
4748                         hw->current_ifs_val = 0;
4749                         hw->ifs_min_val = IFS_MIN;
4750                         hw->ifs_max_val = IFS_MAX;
4751                         hw->ifs_step_size = IFS_STEP;
4752                         hw->ifs_ratio = IFS_RATIO;
4753                 }
4754                 hw->in_ifs_mode = false;
4755                 ew32(AIT, 0);
4756         } else {
4757                 DEBUGOUT("Not in Adaptive IFS mode!\n");
4758         }
4759 }
4760
4761 /**
4762  * e1000_update_adaptive - update adaptive IFS
4763  * @hw: Struct containing variables accessed by shared code
4764  * @tx_packets: Number of transmits since last callback
4765  * @total_collisions: Number of collisions since last callback
4766  *
4767  * Called during the callback/watchdog routine to update IFS value based on
4768  * the ratio of transmits to collisions.
4769  */
4770 void e1000_update_adaptive(struct e1000_hw *hw)
4771 {
4772         DEBUGFUNC("e1000_update_adaptive");
4773
4774         if (hw->adaptive_ifs) {
4775                 if ((hw->collision_delta *hw->ifs_ratio) > hw->tx_packet_delta) {
4776                         if (hw->tx_packet_delta > MIN_NUM_XMITS) {
4777                                 hw->in_ifs_mode = true;
4778                                 if (hw->current_ifs_val < hw->ifs_max_val) {
4779                                         if (hw->current_ifs_val == 0)
4780                                                 hw->current_ifs_val =
4781                                                     hw->ifs_min_val;
4782                                         else
4783                                                 hw->current_ifs_val +=
4784                                                     hw->ifs_step_size;
4785                                         ew32(AIT, hw->current_ifs_val);
4786                                 }
4787                         }
4788                 } else {
4789                         if (hw->in_ifs_mode
4790                             && (hw->tx_packet_delta <= MIN_NUM_XMITS)) {
4791                                 hw->current_ifs_val = 0;
4792                                 hw->in_ifs_mode = false;
4793                                 ew32(AIT, 0);
4794                         }
4795                 }
4796         } else {
4797                 DEBUGOUT("Not in Adaptive IFS mode!\n");
4798         }
4799 }
4800
4801 /**
4802  * e1000_tbi_adjust_stats
4803  * @hw: Struct containing variables accessed by shared code
4804  * @frame_len: The length of the frame in question
4805  * @mac_addr: The Ethernet destination address of the frame in question
4806  *
4807  * Adjusts the statistic counters when a frame is accepted by TBI_ACCEPT
4808  */
4809 void e1000_tbi_adjust_stats(struct e1000_hw *hw, struct e1000_hw_stats *stats,
4810                             u32 frame_len, u8 *mac_addr)
4811 {
4812         u64 carry_bit;
4813
4814         /* First adjust the frame length. */
4815         frame_len--;
4816         /* We need to adjust the statistics counters, since the hardware
4817          * counters overcount this packet as a CRC error and undercount
4818          * the packet as a good packet
4819          */
4820         /* This packet should not be counted as a CRC error.    */
4821         stats->crcerrs--;
4822         /* This packet does count as a Good Packet Received.    */
4823         stats->gprc++;
4824
4825         /* Adjust the Good Octets received counters             */
4826         carry_bit = 0x80000000 & stats->gorcl;
4827         stats->gorcl += frame_len;
4828         /* If the high bit of Gorcl (the low 32 bits of the Good Octets
4829          * Received Count) was one before the addition,
4830          * AND it is zero after, then we lost the carry out,
4831          * need to add one to Gorch (Good Octets Received Count High).
4832          * This could be simplified if all environments supported
4833          * 64-bit integers.
4834          */
4835         if (carry_bit && ((stats->gorcl & 0x80000000) == 0))
4836                 stats->gorch++;
4837         /* Is this a broadcast or multicast?  Check broadcast first,
4838          * since the test for a multicast frame will test positive on
4839          * a broadcast frame.
4840          */
4841         if ((mac_addr[0] == (u8) 0xff) && (mac_addr[1] == (u8) 0xff))
4842                 /* Broadcast packet */
4843                 stats->bprc++;
4844         else if (*mac_addr & 0x01)
4845                 /* Multicast packet */
4846                 stats->mprc++;
4847
4848         if (frame_len == hw->max_frame_size) {
4849                 /* In this case, the hardware has overcounted the number of
4850                  * oversize frames.
4851                  */
4852                 if (stats->roc > 0)
4853                         stats->roc--;
4854         }
4855
4856         /* Adjust the bin counters when the extra byte put the frame in the
4857          * wrong bin. Remember that the frame_len was adjusted above.
4858          */
4859         if (frame_len == 64) {
4860                 stats->prc64++;
4861                 stats->prc127--;
4862         } else if (frame_len == 127) {
4863                 stats->prc127++;
4864                 stats->prc255--;
4865         } else if (frame_len == 255) {
4866                 stats->prc255++;
4867                 stats->prc511--;
4868         } else if (frame_len == 511) {
4869                 stats->prc511++;
4870                 stats->prc1023--;
4871         } else if (frame_len == 1023) {
4872                 stats->prc1023++;
4873                 stats->prc1522--;
4874         } else if (frame_len == 1522) {
4875                 stats->prc1522++;
4876         }
4877 }
4878
4879 /**
4880  * e1000_get_bus_info
4881  * @hw: Struct containing variables accessed by shared code
4882  *
4883  * Gets the current PCI bus type, speed, and width of the hardware
4884  */
4885 void e1000_get_bus_info(struct e1000_hw *hw)
4886 {
4887         u32 status;
4888
4889         switch (hw->mac_type) {
4890         case e1000_82542_rev2_0:
4891         case e1000_82542_rev2_1:
4892                 hw->bus_type = e1000_bus_type_pci;
4893                 hw->bus_speed = e1000_bus_speed_unknown;
4894                 hw->bus_width = e1000_bus_width_unknown;
4895                 break;
4896         default:
4897                 status = er32(STATUS);
4898                 hw->bus_type = (status & E1000_STATUS_PCIX_MODE) ?
4899                     e1000_bus_type_pcix : e1000_bus_type_pci;
4900
4901                 if (hw->device_id == E1000_DEV_ID_82546EB_QUAD_COPPER) {
4902                         hw->bus_speed = (hw->bus_type == e1000_bus_type_pci) ?
4903                             e1000_bus_speed_66 : e1000_bus_speed_120;
4904                 } else if (hw->bus_type == e1000_bus_type_pci) {
4905                         hw->bus_speed = (status & E1000_STATUS_PCI66) ?
4906                             e1000_bus_speed_66 : e1000_bus_speed_33;
4907                 } else {
4908                         switch (status & E1000_STATUS_PCIX_SPEED) {
4909                         case E1000_STATUS_PCIX_SPEED_66:
4910                                 hw->bus_speed = e1000_bus_speed_66;
4911                                 break;
4912                         case E1000_STATUS_PCIX_SPEED_100:
4913                                 hw->bus_speed = e1000_bus_speed_100;
4914                                 break;
4915                         case E1000_STATUS_PCIX_SPEED_133:
4916                                 hw->bus_speed = e1000_bus_speed_133;
4917                                 break;
4918                         default:
4919                                 hw->bus_speed = e1000_bus_speed_reserved;
4920                                 break;
4921                         }
4922                 }
4923                 hw->bus_width = (status & E1000_STATUS_BUS64) ?
4924                     e1000_bus_width_64 : e1000_bus_width_32;
4925                 break;
4926         }
4927 }
4928
4929 /**
4930  * e1000_write_reg_io
4931  * @hw: Struct containing variables accessed by shared code
4932  * @offset: offset to write to
4933  * @value: value to write
4934  *
4935  * Writes a value to one of the devices registers using port I/O (as opposed to
4936  * memory mapped I/O). Only 82544 and newer devices support port I/O.
4937  */
4938 static void e1000_write_reg_io(struct e1000_hw *hw, u32 offset, u32 value)
4939 {
4940         unsigned long io_addr = hw->io_base;
4941         unsigned long io_data = hw->io_base + 4;
4942
4943         e1000_io_write(hw, io_addr, offset);
4944         e1000_io_write(hw, io_data, value);
4945 }
4946
4947 /**
4948  * e1000_get_cable_length - Estimates the cable length.
4949  * @hw: Struct containing variables accessed by shared code
4950  * @min_length: The estimated minimum length
4951  * @max_length: The estimated maximum length
4952  *
4953  * returns: - E1000_ERR_XXX
4954  *            E1000_SUCCESS
4955  *
4956  * This function always returns a ranged length (minimum & maximum).
4957  * So for M88 phy's, this function interprets the one value returned from the
4958  * register to the minimum and maximum range.
4959  * For IGP phy's, the function calculates the range by the AGC registers.
4960  */
4961 static s32 e1000_get_cable_length(struct e1000_hw *hw, u16 *min_length,
4962                                   u16 *max_length)
4963 {
4964         s32 ret_val;
4965         u16 agc_value = 0;
4966         u16 i, phy_data;
4967         u16 cable_length;
4968
4969         DEBUGFUNC("e1000_get_cable_length");
4970
4971         *min_length = *max_length = 0;
4972
4973         /* Use old method for Phy older than IGP */
4974         if (hw->phy_type == e1000_phy_m88) {
4975
4976                 ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS,
4977                                              &phy_data);
4978                 if (ret_val)
4979                         return ret_val;
4980                 cable_length = (phy_data & M88E1000_PSSR_CABLE_LENGTH) >>
4981                     M88E1000_PSSR_CABLE_LENGTH_SHIFT;
4982
4983                 /* Convert the enum value to ranged values */
4984                 switch (cable_length) {
4985                 case e1000_cable_length_50:
4986                         *min_length = 0;
4987                         *max_length = e1000_igp_cable_length_50;
4988                         break;
4989                 case e1000_cable_length_50_80:
4990                         *min_length = e1000_igp_cable_length_50;
4991                         *max_length = e1000_igp_cable_length_80;
4992                         break;
4993                 case e1000_cable_length_80_110:
4994                         *min_length = e1000_igp_cable_length_80;
4995                         *max_length = e1000_igp_cable_length_110;
4996                         break;
4997                 case e1000_cable_length_110_140:
4998                         *min_length = e1000_igp_cable_length_110;
4999                         *max_length = e1000_igp_cable_length_140;
5000                         break;
5001                 case e1000_cable_length_140:
5002                         *min_length = e1000_igp_cable_length_140;
5003                         *max_length = e1000_igp_cable_length_170;
5004                         break;
5005                 default:
5006                         return -E1000_ERR_PHY;
5007                         break;
5008                 }
5009         } else if (hw->phy_type == e1000_phy_igp) {     /* For IGP PHY */
5010                 u16 cur_agc_value;
5011                 u16 min_agc_value = IGP01E1000_AGC_LENGTH_TABLE_SIZE;
5012                 u16 agc_reg_array[IGP01E1000_PHY_CHANNEL_NUM] =
5013                     { IGP01E1000_PHY_AGC_A,
5014                         IGP01E1000_PHY_AGC_B,
5015                         IGP01E1000_PHY_AGC_C,
5016                         IGP01E1000_PHY_AGC_D
5017                 };
5018                 /* Read the AGC registers for all channels */
5019                 for (i = 0; i < IGP01E1000_PHY_CHANNEL_NUM; i++) {
5020
5021                         ret_val =
5022                             e1000_read_phy_reg(hw, agc_reg_array[i], &phy_data);
5023                         if (ret_val)
5024                                 return ret_val;
5025
5026                         cur_agc_value = phy_data >> IGP01E1000_AGC_LENGTH_SHIFT;
5027
5028                         /* Value bound check. */
5029                         if ((cur_agc_value >=
5030                              IGP01E1000_AGC_LENGTH_TABLE_SIZE - 1)
5031                             || (cur_agc_value == 0))
5032                                 return -E1000_ERR_PHY;
5033
5034                         agc_value += cur_agc_value;
5035
5036                         /* Update minimal AGC value. */
5037                         if (min_agc_value > cur_agc_value)
5038                                 min_agc_value = cur_agc_value;
5039                 }
5040
5041                 /* Remove the minimal AGC result for length < 50m */
5042                 if (agc_value <
5043                     IGP01E1000_PHY_CHANNEL_NUM * e1000_igp_cable_length_50) {
5044                         agc_value -= min_agc_value;
5045
5046                         /* Get the average length of the remaining 3 channels */
5047                         agc_value /= (IGP01E1000_PHY_CHANNEL_NUM - 1);
5048                 } else {
5049                         /* Get the average length of all the 4 channels. */
5050                         agc_value /= IGP01E1000_PHY_CHANNEL_NUM;
5051                 }
5052
5053                 /* Set the range of the calculated length. */
5054                 *min_length = ((e1000_igp_cable_length_table[agc_value] -
5055                                 IGP01E1000_AGC_RANGE) > 0) ?
5056                     (e1000_igp_cable_length_table[agc_value] -
5057                      IGP01E1000_AGC_RANGE) : 0;
5058                 *max_length = e1000_igp_cable_length_table[agc_value] +
5059                     IGP01E1000_AGC_RANGE;
5060         }
5061
5062         return E1000_SUCCESS;
5063 }
5064
5065 /**
5066  * e1000_check_polarity - Check the cable polarity
5067  * @hw: Struct containing variables accessed by shared code
5068  * @polarity: output parameter : 0 - Polarity is not reversed
5069  *                               1 - Polarity is reversed.
5070  *
5071  * returns: - E1000_ERR_XXX
5072  *            E1000_SUCCESS
5073  *
5074  * For phy's older than IGP, this function simply reads the polarity bit in the
5075  * Phy Status register.  For IGP phy's, this bit is valid only if link speed is
5076  * 10 Mbps.  If the link speed is 100 Mbps there is no polarity so this bit will
5077  * return 0.  If the link speed is 1000 Mbps the polarity status is in the
5078  * IGP01E1000_PHY_PCS_INIT_REG.
5079  */
5080 static s32 e1000_check_polarity(struct e1000_hw *hw,
5081                                 e1000_rev_polarity *polarity)
5082 {
5083         s32 ret_val;
5084         u16 phy_data;
5085
5086         DEBUGFUNC("e1000_check_polarity");
5087
5088         if (hw->phy_type == e1000_phy_m88) {
5089                 /* return the Polarity bit in the Status register. */
5090                 ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS,
5091                                              &phy_data);
5092                 if (ret_val)
5093                         return ret_val;
5094                 *polarity = ((phy_data & M88E1000_PSSR_REV_POLARITY) >>
5095                              M88E1000_PSSR_REV_POLARITY_SHIFT) ?
5096                     e1000_rev_polarity_reversed : e1000_rev_polarity_normal;
5097
5098         } else if (hw->phy_type == e1000_phy_igp) {
5099                 /* Read the Status register to check the speed */
5100                 ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_STATUS,
5101                                              &phy_data);
5102                 if (ret_val)
5103                         return ret_val;
5104
5105                 /* If speed is 1000 Mbps, must read the IGP01E1000_PHY_PCS_INIT_REG to
5106                  * find the polarity status */
5107                 if ((phy_data & IGP01E1000_PSSR_SPEED_MASK) ==
5108                     IGP01E1000_PSSR_SPEED_1000MBPS) {
5109
5110                         /* Read the GIG initialization PCS register (0x00B4) */
5111                         ret_val =
5112                             e1000_read_phy_reg(hw, IGP01E1000_PHY_PCS_INIT_REG,
5113                                                &phy_data);
5114                         if (ret_val)
5115                                 return ret_val;
5116
5117                         /* Check the polarity bits */
5118                         *polarity = (phy_data & IGP01E1000_PHY_POLARITY_MASK) ?
5119                             e1000_rev_polarity_reversed :
5120                             e1000_rev_polarity_normal;
5121                 } else {
5122                         /* For 10 Mbps, read the polarity bit in the status register. (for
5123                          * 100 Mbps this bit is always 0) */
5124                         *polarity =
5125                             (phy_data & IGP01E1000_PSSR_POLARITY_REVERSED) ?
5126                             e1000_rev_polarity_reversed :
5127                             e1000_rev_polarity_normal;
5128                 }
5129         }
5130         return E1000_SUCCESS;
5131 }
5132
5133 /**
5134  * e1000_check_downshift - Check if Downshift occurred
5135  * @hw: Struct containing variables accessed by shared code
5136  * @downshift: output parameter : 0 - No Downshift occurred.
5137  *                                1 - Downshift occurred.
5138  *
5139  * returns: - E1000_ERR_XXX
5140  *            E1000_SUCCESS
5141  *
5142  * For phy's older than IGP, this function reads the Downshift bit in the Phy
5143  * Specific Status register.  For IGP phy's, it reads the Downgrade bit in the
5144  * Link Health register.  In IGP this bit is latched high, so the driver must
5145  * read it immediately after link is established.
5146  */
5147 static s32 e1000_check_downshift(struct e1000_hw *hw)
5148 {
5149         s32 ret_val;
5150         u16 phy_data;
5151
5152         DEBUGFUNC("e1000_check_downshift");
5153
5154         if (hw->phy_type == e1000_phy_igp) {
5155                 ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_LINK_HEALTH,
5156                                              &phy_data);
5157                 if (ret_val)
5158                         return ret_val;
5159
5160                 hw->speed_downgraded =
5161                     (phy_data & IGP01E1000_PLHR_SS_DOWNGRADE) ? 1 : 0;
5162         } else if (hw->phy_type == e1000_phy_m88) {
5163                 ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS,
5164                                              &phy_data);
5165                 if (ret_val)
5166                         return ret_val;
5167
5168                 hw->speed_downgraded = (phy_data & M88E1000_PSSR_DOWNSHIFT) >>
5169                     M88E1000_PSSR_DOWNSHIFT_SHIFT;
5170         }
5171
5172         return E1000_SUCCESS;
5173 }
5174
5175 /**
5176  * e1000_config_dsp_after_link_change
5177  * @hw: Struct containing variables accessed by shared code
5178  * @link_up: was link up at the time this was called
5179  *
5180  * returns: - E1000_ERR_PHY if fail to read/write the PHY
5181  *            E1000_SUCCESS at any other case.
5182  *
5183  * 82541_rev_2 & 82547_rev_2 have the capability to configure the DSP when a
5184  * gigabit link is achieved to improve link quality.
5185  */
5186
5187 static s32 e1000_config_dsp_after_link_change(struct e1000_hw *hw, bool link_up)
5188 {
5189         s32 ret_val;
5190         u16 phy_data, phy_saved_data, speed, duplex, i;
5191         u16 dsp_reg_array[IGP01E1000_PHY_CHANNEL_NUM] =
5192             { IGP01E1000_PHY_AGC_PARAM_A,
5193                 IGP01E1000_PHY_AGC_PARAM_B,
5194                 IGP01E1000_PHY_AGC_PARAM_C,
5195                 IGP01E1000_PHY_AGC_PARAM_D
5196         };
5197         u16 min_length, max_length;
5198
5199         DEBUGFUNC("e1000_config_dsp_after_link_change");
5200
5201         if (hw->phy_type != e1000_phy_igp)
5202                 return E1000_SUCCESS;
5203
5204         if (link_up) {
5205                 ret_val = e1000_get_speed_and_duplex(hw, &speed, &duplex);
5206                 if (ret_val) {
5207                         DEBUGOUT("Error getting link speed and duplex\n");
5208                         return ret_val;
5209                 }
5210
5211                 if (speed == SPEED_1000) {
5212
5213                         ret_val =
5214                             e1000_get_cable_length(hw, &min_length,
5215                                                    &max_length);
5216                         if (ret_val)
5217                                 return ret_val;
5218
5219                         if ((hw->dsp_config_state == e1000_dsp_config_enabled)
5220                             && min_length >= e1000_igp_cable_length_50) {
5221
5222                                 for (i = 0; i < IGP01E1000_PHY_CHANNEL_NUM; i++) {
5223                                         ret_val =
5224                                             e1000_read_phy_reg(hw,
5225                                                                dsp_reg_array[i],
5226                                                                &phy_data);
5227                                         if (ret_val)
5228                                                 return ret_val;
5229
5230                                         phy_data &=
5231                                             ~IGP01E1000_PHY_EDAC_MU_INDEX;
5232
5233                                         ret_val =
5234                                             e1000_write_phy_reg(hw,
5235                                                                 dsp_reg_array
5236                                                                 [i], phy_data);
5237                                         if (ret_val)
5238                                                 return ret_val;
5239                                 }
5240                                 hw->dsp_config_state =
5241                                     e1000_dsp_config_activated;
5242                         }
5243
5244                         if ((hw->ffe_config_state == e1000_ffe_config_enabled)
5245                             && (min_length < e1000_igp_cable_length_50)) {
5246
5247                                 u16 ffe_idle_err_timeout =
5248                                     FFE_IDLE_ERR_COUNT_TIMEOUT_20;
5249                                 u32 idle_errs = 0;
5250
5251                                 /* clear previous idle error counts */
5252                                 ret_val =
5253                                     e1000_read_phy_reg(hw, PHY_1000T_STATUS,
5254                                                        &phy_data);
5255                                 if (ret_val)
5256                                         return ret_val;
5257
5258                                 for (i = 0; i < ffe_idle_err_timeout; i++) {
5259                                         udelay(1000);
5260                                         ret_val =
5261                                             e1000_read_phy_reg(hw,
5262                                                                PHY_1000T_STATUS,
5263                                                                &phy_data);
5264                                         if (ret_val)
5265                                                 return ret_val;
5266
5267                                         idle_errs +=
5268                                             (phy_data &
5269                                              SR_1000T_IDLE_ERROR_CNT);
5270                                         if (idle_errs >
5271                                             SR_1000T_PHY_EXCESSIVE_IDLE_ERR_COUNT)
5272                                         {
5273                                                 hw->ffe_config_state =
5274                                                     e1000_ffe_config_active;
5275
5276                                                 ret_val =
5277                                                     e1000_write_phy_reg(hw,
5278                                                                         IGP01E1000_PHY_DSP_FFE,
5279                                                                         IGP01E1000_PHY_DSP_FFE_CM_CP);
5280                                                 if (ret_val)
5281                                                         return ret_val;
5282                                                 break;
5283                                         }
5284
5285                                         if (idle_errs)
5286                                                 ffe_idle_err_timeout =
5287                                                     FFE_IDLE_ERR_COUNT_TIMEOUT_100;
5288                                 }
5289                         }
5290                 }
5291         } else {
5292                 if (hw->dsp_config_state == e1000_dsp_config_activated) {
5293                         /* Save off the current value of register 0x2F5B to be restored at
5294                          * the end of the routines. */
5295                         ret_val =
5296                             e1000_read_phy_reg(hw, 0x2F5B, &phy_saved_data);
5297
5298                         if (ret_val)
5299                                 return ret_val;
5300
5301                         /* Disable the PHY transmitter */
5302                         ret_val = e1000_write_phy_reg(hw, 0x2F5B, 0x0003);
5303
5304                         if (ret_val)
5305                                 return ret_val;
5306
5307                         mdelay(20);
5308
5309                         ret_val = e1000_write_phy_reg(hw, 0x0000,
5310                                                       IGP01E1000_IEEE_FORCE_GIGA);
5311                         if (ret_val)
5312                                 return ret_val;
5313                         for (i = 0; i < IGP01E1000_PHY_CHANNEL_NUM; i++) {
5314                                 ret_val =
5315                                     e1000_read_phy_reg(hw, dsp_reg_array[i],
5316                                                        &phy_data);
5317                                 if (ret_val)
5318                                         return ret_val;
5319
5320                                 phy_data &= ~IGP01E1000_PHY_EDAC_MU_INDEX;
5321                                 phy_data |= IGP01E1000_PHY_EDAC_SIGN_EXT_9_BITS;
5322
5323                                 ret_val =
5324                                     e1000_write_phy_reg(hw, dsp_reg_array[i],
5325                                                         phy_data);
5326                                 if (ret_val)
5327                                         return ret_val;
5328                         }
5329
5330                         ret_val = e1000_write_phy_reg(hw, 0x0000,
5331                                                       IGP01E1000_IEEE_RESTART_AUTONEG);
5332                         if (ret_val)
5333                                 return ret_val;
5334
5335                         mdelay(20);
5336
5337                         /* Now enable the transmitter */
5338                         ret_val =
5339                             e1000_write_phy_reg(hw, 0x2F5B, phy_saved_data);
5340
5341                         if (ret_val)
5342                                 return ret_val;
5343
5344                         hw->dsp_config_state = e1000_dsp_config_enabled;
5345                 }
5346
5347                 if (hw->ffe_config_state == e1000_ffe_config_active) {
5348                         /* Save off the current value of register 0x2F5B to be restored at
5349                          * the end of the routines. */
5350                         ret_val =
5351                             e1000_read_phy_reg(hw, 0x2F5B, &phy_saved_data);
5352
5353                         if (ret_val)
5354                                 return ret_val;
5355
5356                         /* Disable the PHY transmitter */
5357                         ret_val = e1000_write_phy_reg(hw, 0x2F5B, 0x0003);
5358
5359                         if (ret_val)
5360                                 return ret_val;
5361
5362                         mdelay(20);
5363
5364                         ret_val = e1000_write_phy_reg(hw, 0x0000,
5365                                                       IGP01E1000_IEEE_FORCE_GIGA);
5366                         if (ret_val)
5367                                 return ret_val;
5368                         ret_val =
5369                             e1000_write_phy_reg(hw, IGP01E1000_PHY_DSP_FFE,
5370                                                 IGP01E1000_PHY_DSP_FFE_DEFAULT);
5371                         if (ret_val)
5372                                 return ret_val;
5373
5374                         ret_val = e1000_write_phy_reg(hw, 0x0000,
5375                                                       IGP01E1000_IEEE_RESTART_AUTONEG);
5376                         if (ret_val)
5377                                 return ret_val;
5378
5379                         mdelay(20);
5380
5381                         /* Now enable the transmitter */
5382                         ret_val =
5383                             e1000_write_phy_reg(hw, 0x2F5B, phy_saved_data);
5384
5385                         if (ret_val)
5386                                 return ret_val;
5387
5388                         hw->ffe_config_state = e1000_ffe_config_enabled;
5389                 }
5390         }
5391         return E1000_SUCCESS;
5392 }
5393
5394 /**
5395  * e1000_set_phy_mode - Set PHY to class A mode
5396  * @hw: Struct containing variables accessed by shared code
5397  *
5398  * Assumes the following operations will follow to enable the new class mode.
5399  *  1. Do a PHY soft reset
5400  *  2. Restart auto-negotiation or force link.
5401  */
5402 static s32 e1000_set_phy_mode(struct e1000_hw *hw)
5403 {
5404         s32 ret_val;
5405         u16 eeprom_data;
5406
5407         DEBUGFUNC("e1000_set_phy_mode");
5408
5409         if ((hw->mac_type == e1000_82545_rev_3) &&
5410             (hw->media_type == e1000_media_type_copper)) {
5411                 ret_val =
5412                     e1000_read_eeprom(hw, EEPROM_PHY_CLASS_WORD, 1,
5413                                       &eeprom_data);
5414                 if (ret_val) {
5415                         return ret_val;
5416                 }
5417
5418                 if ((eeprom_data != EEPROM_RESERVED_WORD) &&
5419                     (eeprom_data & EEPROM_PHY_CLASS_A)) {
5420                         ret_val =
5421                             e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT,
5422                                                 0x000B);
5423                         if (ret_val)
5424                                 return ret_val;
5425                         ret_val =
5426                             e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL,
5427                                                 0x8104);
5428                         if (ret_val)
5429                                 return ret_val;
5430
5431                         hw->phy_reset_disable = false;
5432                 }
5433         }
5434
5435         return E1000_SUCCESS;
5436 }
5437
5438 /**
5439  * e1000_set_d3_lplu_state - set d3 link power state
5440  * @hw: Struct containing variables accessed by shared code
5441  * @active: true to enable lplu false to disable lplu.
5442  *
5443  * This function sets the lplu state according to the active flag.  When
5444  * activating lplu this function also disables smart speed and vise versa.
5445  * lplu will not be activated unless the device autonegotiation advertisement
5446  * meets standards of either 10 or 10/100 or 10/100/1000 at all duplexes.
5447  *
5448  * returns: - E1000_ERR_PHY if fail to read/write the PHY
5449  *            E1000_SUCCESS at any other case.
5450  */
5451 static s32 e1000_set_d3_lplu_state(struct e1000_hw *hw, bool active)
5452 {
5453         s32 ret_val;
5454         u16 phy_data;
5455         DEBUGFUNC("e1000_set_d3_lplu_state");
5456
5457         if (hw->phy_type != e1000_phy_igp)
5458                 return E1000_SUCCESS;
5459
5460         /* During driver activity LPLU should not be used or it will attain link
5461          * from the lowest speeds starting from 10Mbps. The capability is used for
5462          * Dx transitions and states */
5463         if (hw->mac_type == e1000_82541_rev_2
5464             || hw->mac_type == e1000_82547_rev_2) {
5465                 ret_val =
5466                     e1000_read_phy_reg(hw, IGP01E1000_GMII_FIFO, &phy_data);
5467                 if (ret_val)
5468                         return ret_val;
5469         }
5470
5471         if (!active) {
5472                 if (hw->mac_type == e1000_82541_rev_2 ||
5473                     hw->mac_type == e1000_82547_rev_2) {
5474                         phy_data &= ~IGP01E1000_GMII_FLEX_SPD;
5475                         ret_val =
5476                             e1000_write_phy_reg(hw, IGP01E1000_GMII_FIFO,
5477                                                 phy_data);
5478                         if (ret_val)
5479                                 return ret_val;
5480                 }
5481
5482                 /* LPLU and SmartSpeed are mutually exclusive.  LPLU is used during
5483                  * Dx states where the power conservation is most important.  During
5484                  * driver activity we should enable SmartSpeed, so performance is
5485                  * maintained. */
5486                 if (hw->smart_speed == e1000_smart_speed_on) {
5487                         ret_val =
5488                             e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
5489                                                &phy_data);
5490                         if (ret_val)
5491                                 return ret_val;
5492
5493                         phy_data |= IGP01E1000_PSCFR_SMART_SPEED;
5494                         ret_val =
5495                             e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
5496                                                 phy_data);
5497                         if (ret_val)
5498                                 return ret_val;
5499                 } else if (hw->smart_speed == e1000_smart_speed_off) {
5500                         ret_val =
5501                             e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
5502                                                &phy_data);
5503                         if (ret_val)
5504                                 return ret_val;
5505
5506                         phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
5507                         ret_val =
5508                             e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
5509                                                 phy_data);
5510                         if (ret_val)
5511                                 return ret_val;
5512                 }
5513         } else if ((hw->autoneg_advertised == AUTONEG_ADVERTISE_SPEED_DEFAULT)
5514                    || (hw->autoneg_advertised == AUTONEG_ADVERTISE_10_ALL)
5515                    || (hw->autoneg_advertised ==
5516                        AUTONEG_ADVERTISE_10_100_ALL)) {
5517
5518                 if (hw->mac_type == e1000_82541_rev_2 ||
5519                     hw->mac_type == e1000_82547_rev_2) {
5520                         phy_data |= IGP01E1000_GMII_FLEX_SPD;
5521                         ret_val =
5522                             e1000_write_phy_reg(hw, IGP01E1000_GMII_FIFO,
5523                                                 phy_data);
5524                         if (ret_val)
5525                                 return ret_val;
5526                 }
5527
5528                 /* When LPLU is enabled we should disable SmartSpeed */
5529                 ret_val =
5530                     e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
5531                                        &phy_data);
5532                 if (ret_val)
5533                         return ret_val;
5534
5535                 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
5536                 ret_val =
5537                     e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
5538                                         phy_data);
5539                 if (ret_val)
5540                         return ret_val;
5541
5542         }
5543         return E1000_SUCCESS;
5544 }
5545
5546 /**
5547  * e1000_set_vco_speed
5548  * @hw: Struct containing variables accessed by shared code
5549  *
5550  * Change VCO speed register to improve Bit Error Rate performance of SERDES.
5551  */
5552 static s32 e1000_set_vco_speed(struct e1000_hw *hw)
5553 {
5554         s32 ret_val;
5555         u16 default_page = 0;
5556         u16 phy_data;
5557
5558         DEBUGFUNC("e1000_set_vco_speed");
5559
5560         switch (hw->mac_type) {
5561         case e1000_82545_rev_3:
5562         case e1000_82546_rev_3:
5563                 break;
5564         default:
5565                 return E1000_SUCCESS;
5566         }
5567
5568         /* Set PHY register 30, page 5, bit 8 to 0 */
5569
5570         ret_val =
5571             e1000_read_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, &default_page);
5572         if (ret_val)
5573                 return ret_val;
5574
5575         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x0005);
5576         if (ret_val)
5577                 return ret_val;
5578
5579         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, &phy_data);
5580         if (ret_val)
5581                 return ret_val;
5582
5583         phy_data &= ~M88E1000_PHY_VCO_REG_BIT8;
5584         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, phy_data);
5585         if (ret_val)
5586                 return ret_val;
5587
5588         /* Set PHY register 30, page 4, bit 11 to 1 */
5589
5590         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x0004);
5591         if (ret_val)
5592                 return ret_val;
5593
5594         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, &phy_data);
5595         if (ret_val)
5596                 return ret_val;
5597
5598         phy_data |= M88E1000_PHY_VCO_REG_BIT11;
5599         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, phy_data);
5600         if (ret_val)
5601                 return ret_val;
5602
5603         ret_val =
5604             e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, default_page);
5605         if (ret_val)
5606                 return ret_val;
5607
5608         return E1000_SUCCESS;
5609 }
5610
5611
5612 /**
5613  * e1000_enable_mng_pass_thru - check for bmc pass through
5614  * @hw: Struct containing variables accessed by shared code
5615  *
5616  * Verifies the hardware needs to allow ARPs to be processed by the host
5617  * returns: - true/false
5618  */
5619 u32 e1000_enable_mng_pass_thru(struct e1000_hw *hw)
5620 {
5621         u32 manc;
5622
5623         if (hw->asf_firmware_present) {
5624                 manc = er32(MANC);
5625
5626                 if (!(manc & E1000_MANC_RCV_TCO_EN) ||
5627                     !(manc & E1000_MANC_EN_MAC_ADDR_FILTER))
5628                         return false;
5629                 if ((manc & E1000_MANC_SMBUS_EN) && !(manc & E1000_MANC_ASF_EN))
5630                         return true;
5631         }
5632         return false;
5633 }
5634
5635 static s32 e1000_polarity_reversal_workaround(struct e1000_hw *hw)
5636 {
5637         s32 ret_val;
5638         u16 mii_status_reg;
5639         u16 i;
5640
5641         /* Polarity reversal workaround for forced 10F/10H links. */
5642
5643         /* Disable the transmitter on the PHY */
5644
5645         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x0019);
5646         if (ret_val)
5647                 return ret_val;
5648         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, 0xFFFF);
5649         if (ret_val)
5650                 return ret_val;
5651
5652         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x0000);
5653         if (ret_val)
5654                 return ret_val;
5655
5656         /* This loop will early-out if the NO link condition has been met. */
5657         for (i = PHY_FORCE_TIME; i > 0; i--) {
5658                 /* Read the MII Status Register and wait for Link Status bit
5659                  * to be clear.
5660                  */
5661
5662                 ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
5663                 if (ret_val)
5664                         return ret_val;
5665
5666                 ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
5667                 if (ret_val)
5668                         return ret_val;
5669
5670                 if ((mii_status_reg & ~MII_SR_LINK_STATUS) == 0)
5671                         break;
5672                 mdelay(100);
5673         }
5674
5675         /* Recommended delay time after link has been lost */
5676         mdelay(1000);
5677
5678         /* Now we will re-enable th transmitter on the PHY */
5679
5680         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x0019);
5681         if (ret_val)
5682                 return ret_val;
5683         mdelay(50);
5684         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, 0xFFF0);
5685         if (ret_val)
5686                 return ret_val;
5687         mdelay(50);
5688         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, 0xFF00);
5689         if (ret_val)
5690                 return ret_val;
5691         mdelay(50);
5692         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, 0x0000);
5693         if (ret_val)
5694                 return ret_val;
5695
5696         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x0000);
5697         if (ret_val)
5698                 return ret_val;
5699
5700         /* This loop will early-out if the link condition has been met. */
5701         for (i = PHY_FORCE_TIME; i > 0; i--) {
5702                 /* Read the MII Status Register and wait for Link Status bit
5703                  * to be set.
5704                  */
5705
5706                 ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
5707                 if (ret_val)
5708                         return ret_val;
5709
5710                 ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
5711                 if (ret_val)
5712                         return ret_val;
5713
5714                 if (mii_status_reg & MII_SR_LINK_STATUS)
5715                         break;
5716                 mdelay(100);
5717         }
5718         return E1000_SUCCESS;
5719 }
5720
5721 /**
5722  * e1000_get_auto_rd_done
5723  * @hw: Struct containing variables accessed by shared code
5724  *
5725  * Check for EEPROM Auto Read bit done.
5726  * returns: - E1000_ERR_RESET if fail to reset MAC
5727  *            E1000_SUCCESS at any other case.
5728  */
5729 static s32 e1000_get_auto_rd_done(struct e1000_hw *hw)
5730 {
5731         DEBUGFUNC("e1000_get_auto_rd_done");
5732         msleep(5);
5733         return E1000_SUCCESS;
5734 }
5735
5736 /**
5737  * e1000_get_phy_cfg_done
5738  * @hw: Struct containing variables accessed by shared code
5739  *
5740  * Checks if the PHY configuration is done
5741  * returns: - E1000_ERR_RESET if fail to reset MAC
5742  *            E1000_SUCCESS at any other case.
5743  */
5744 static s32 e1000_get_phy_cfg_done(struct e1000_hw *hw)
5745 {
5746         DEBUGFUNC("e1000_get_phy_cfg_done");
5747         mdelay(10);
5748         return E1000_SUCCESS;
5749 }