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