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