]> nv-tegra.nvidia Code Review - linux-3.10.git/blob - drivers/net/e1000/e1000_hw.c
[PATCH] e1000:82573 specific code & packet split code
[linux-3.10.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_set_vco_speed(struct e1000_hw *hw);
67 static int32_t e1000_polarity_reversal_workaround(struct e1000_hw *hw);
68 static int32_t e1000_set_phy_mode(struct e1000_hw *hw);
69 static int32_t e1000_host_if_read_cookie(struct e1000_hw *hw, uint8_t *buffer);
70 static uint8_t e1000_calculate_mng_checksum(char *buffer, uint32_t length);
71
72 /* IGP cable length table */
73 static const
74 uint16_t e1000_igp_cable_length_table[IGP01E1000_AGC_LENGTH_TABLE_SIZE] =
75     { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
76       5, 10, 10, 10, 10, 10, 10, 10, 20, 20, 20, 20, 20, 25, 25, 25,
77       25, 25, 25, 25, 30, 30, 30, 30, 40, 40, 40, 40, 40, 40, 40, 40,
78       40, 50, 50, 50, 50, 50, 50, 50, 60, 60, 60, 60, 60, 60, 60, 60,
79       60, 70, 70, 70, 70, 70, 70, 80, 80, 80, 80, 80, 80, 90, 90, 90,
80       90, 90, 90, 90, 90, 90, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
81       100, 100, 100, 100, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110,
82       110, 110, 110, 110, 110, 110, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120};
83
84 static const
85 uint16_t e1000_igp_2_cable_length_table[IGP02E1000_AGC_LENGTH_TABLE_SIZE] =
86     { 8, 13, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43,
87       22, 24, 27, 30, 32, 35, 37, 40, 42, 44, 47, 49, 51, 54, 56, 58,
88       32, 35, 38, 41, 44, 47, 50, 53, 55, 58, 61, 63, 66, 69, 71, 74,
89       43, 47, 51, 54, 58, 61, 64, 67, 71, 74, 77, 80, 82, 85, 88, 90,
90       57, 62, 66, 70, 74, 77, 81, 85, 88, 91, 94, 97, 100, 103, 106, 108,
91       73, 78, 82, 87, 91, 95, 98, 102, 105, 109, 112, 114, 117, 119, 122, 124,
92       91, 96, 101, 105, 109, 113, 116, 119, 122, 125, 127, 128, 128, 128, 128, 128,
93       108, 113, 117, 121, 124, 127, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128};
94
95
96 /******************************************************************************
97  * Set the phy type member in the hw struct.
98  *
99  * hw - Struct containing variables accessed by shared code
100  *****************************************************************************/
101 int32_t
102 e1000_set_phy_type(struct e1000_hw *hw)
103 {
104     DEBUGFUNC("e1000_set_phy_type");
105
106     if(hw->mac_type == e1000_undefined)
107         return -E1000_ERR_PHY_TYPE;
108
109     switch(hw->phy_id) {
110     case M88E1000_E_PHY_ID:
111     case M88E1000_I_PHY_ID:
112     case M88E1011_I_PHY_ID:
113     case M88E1111_I_PHY_ID:
114         hw->phy_type = e1000_phy_m88;
115         break;
116     case IGP01E1000_I_PHY_ID:
117         if(hw->mac_type == e1000_82541 ||
118            hw->mac_type == e1000_82541_rev_2 ||
119            hw->mac_type == e1000_82547 ||
120            hw->mac_type == e1000_82547_rev_2) {
121             hw->phy_type = e1000_phy_igp;
122             break;
123         }
124         /* Fall Through */
125     default:
126         /* Should never have loaded on this device */
127         hw->phy_type = e1000_phy_undefined;
128         return -E1000_ERR_PHY_TYPE;
129     }
130
131     return E1000_SUCCESS;
132 }
133
134 /******************************************************************************
135  * IGP phy init script - initializes the GbE PHY
136  *
137  * hw - Struct containing variables accessed by shared code
138  *****************************************************************************/
139 static void
140 e1000_phy_init_script(struct e1000_hw *hw)
141 {
142     uint32_t ret_val;
143     uint16_t phy_saved_data;
144
145     DEBUGFUNC("e1000_phy_init_script");
146
147
148     if(hw->phy_init_script) {
149         msec_delay(20);
150
151         /* Save off the current value of register 0x2F5B to be restored at
152          * the end of this routine. */
153         ret_val = e1000_read_phy_reg(hw, 0x2F5B, &phy_saved_data);
154
155         /* Disabled the PHY transmitter */
156         e1000_write_phy_reg(hw, 0x2F5B, 0x0003);
157
158         msec_delay(20);
159
160         e1000_write_phy_reg(hw,0x0000,0x0140);
161
162         msec_delay(5);
163
164         switch(hw->mac_type) {
165         case e1000_82541:
166         case e1000_82547:
167             e1000_write_phy_reg(hw, 0x1F95, 0x0001);
168
169             e1000_write_phy_reg(hw, 0x1F71, 0xBD21);
170
171             e1000_write_phy_reg(hw, 0x1F79, 0x0018);
172
173             e1000_write_phy_reg(hw, 0x1F30, 0x1600);
174
175             e1000_write_phy_reg(hw, 0x1F31, 0x0014);
176
177             e1000_write_phy_reg(hw, 0x1F32, 0x161C);
178
179             e1000_write_phy_reg(hw, 0x1F94, 0x0003);
180
181             e1000_write_phy_reg(hw, 0x1F96, 0x003F);
182
183             e1000_write_phy_reg(hw, 0x2010, 0x0008);
184             break;
185
186         case e1000_82541_rev_2:
187         case e1000_82547_rev_2:
188             e1000_write_phy_reg(hw, 0x1F73, 0x0099);
189             break;
190         default:
191             break;
192         }
193
194         e1000_write_phy_reg(hw, 0x0000, 0x3300);
195
196         msec_delay(20);
197
198         /* Now enable the transmitter */
199         e1000_write_phy_reg(hw, 0x2F5B, phy_saved_data);
200
201         if(hw->mac_type == e1000_82547) {
202             uint16_t fused, fine, coarse;
203
204             /* Move to analog registers page */
205             e1000_read_phy_reg(hw, IGP01E1000_ANALOG_SPARE_FUSE_STATUS, &fused);
206
207             if(!(fused & IGP01E1000_ANALOG_SPARE_FUSE_ENABLED)) {
208                 e1000_read_phy_reg(hw, IGP01E1000_ANALOG_FUSE_STATUS, &fused);
209
210                 fine = fused & IGP01E1000_ANALOG_FUSE_FINE_MASK;
211                 coarse = fused & IGP01E1000_ANALOG_FUSE_COARSE_MASK;
212
213                 if(coarse > IGP01E1000_ANALOG_FUSE_COARSE_THRESH) {
214                     coarse -= IGP01E1000_ANALOG_FUSE_COARSE_10;
215                     fine -= IGP01E1000_ANALOG_FUSE_FINE_1;
216                 } else if(coarse == IGP01E1000_ANALOG_FUSE_COARSE_THRESH)
217                     fine -= IGP01E1000_ANALOG_FUSE_FINE_10;
218
219                 fused = (fused & IGP01E1000_ANALOG_FUSE_POLY_MASK) |
220                         (fine & IGP01E1000_ANALOG_FUSE_FINE_MASK) |
221                         (coarse & IGP01E1000_ANALOG_FUSE_COARSE_MASK);
222
223                 e1000_write_phy_reg(hw, IGP01E1000_ANALOG_FUSE_CONTROL, fused);
224                 e1000_write_phy_reg(hw, IGP01E1000_ANALOG_FUSE_BYPASS,
225                                     IGP01E1000_ANALOG_FUSE_ENABLE_SW_CONTROL);
226             }
227         }
228     }
229 }
230
231 /******************************************************************************
232  * Set the mac type member in the hw struct.
233  *
234  * hw - Struct containing variables accessed by shared code
235  *****************************************************************************/
236 int32_t
237 e1000_set_mac_type(struct e1000_hw *hw)
238 {
239     DEBUGFUNC("e1000_set_mac_type");
240
241     switch (hw->device_id) {
242     case E1000_DEV_ID_82542:
243         switch (hw->revision_id) {
244         case E1000_82542_2_0_REV_ID:
245             hw->mac_type = e1000_82542_rev2_0;
246             break;
247         case E1000_82542_2_1_REV_ID:
248             hw->mac_type = e1000_82542_rev2_1;
249             break;
250         default:
251             /* Invalid 82542 revision ID */
252             return -E1000_ERR_MAC_TYPE;
253         }
254         break;
255     case E1000_DEV_ID_82543GC_FIBER:
256     case E1000_DEV_ID_82543GC_COPPER:
257         hw->mac_type = e1000_82543;
258         break;
259     case E1000_DEV_ID_82544EI_COPPER:
260     case E1000_DEV_ID_82544EI_FIBER:
261     case E1000_DEV_ID_82544GC_COPPER:
262     case E1000_DEV_ID_82544GC_LOM:
263         hw->mac_type = e1000_82544;
264         break;
265     case E1000_DEV_ID_82540EM:
266     case E1000_DEV_ID_82540EM_LOM:
267     case E1000_DEV_ID_82540EP:
268     case E1000_DEV_ID_82540EP_LOM:
269     case E1000_DEV_ID_82540EP_LP:
270         hw->mac_type = e1000_82540;
271         break;
272     case E1000_DEV_ID_82545EM_COPPER:
273     case E1000_DEV_ID_82545EM_FIBER:
274         hw->mac_type = e1000_82545;
275         break;
276     case E1000_DEV_ID_82545GM_COPPER:
277     case E1000_DEV_ID_82545GM_FIBER:
278     case E1000_DEV_ID_82545GM_SERDES:
279         hw->mac_type = e1000_82545_rev_3;
280         break;
281     case E1000_DEV_ID_82546EB_COPPER:
282     case E1000_DEV_ID_82546EB_FIBER:
283     case E1000_DEV_ID_82546EB_QUAD_COPPER:
284         hw->mac_type = e1000_82546;
285         break;
286     case E1000_DEV_ID_82546GB_COPPER:
287     case E1000_DEV_ID_82546GB_FIBER:
288     case E1000_DEV_ID_82546GB_SERDES:
289     case E1000_DEV_ID_82546GB_PCIE:
290     case E1000_DEV_ID_82546GB_QUAD_COPPER:
291         hw->mac_type = e1000_82546_rev_3;
292         break;
293     case E1000_DEV_ID_82541EI:
294     case E1000_DEV_ID_82541EI_MOBILE:
295         hw->mac_type = e1000_82541;
296         break;
297     case E1000_DEV_ID_82541ER:
298     case E1000_DEV_ID_82541GI:
299     case E1000_DEV_ID_82541GI_LF:
300     case E1000_DEV_ID_82541GI_MOBILE:
301         hw->mac_type = e1000_82541_rev_2;
302         break;
303     case E1000_DEV_ID_82547EI:
304         hw->mac_type = e1000_82547;
305         break;
306     case E1000_DEV_ID_82547GI:
307         hw->mac_type = e1000_82547_rev_2;
308         break;
309     case E1000_DEV_ID_82573E:
310     case E1000_DEV_ID_82573E_IAMT:
311         hw->mac_type = e1000_82573;
312         break;
313     default:
314         /* Should never have loaded on this device */
315         return -E1000_ERR_MAC_TYPE;
316     }
317
318     switch(hw->mac_type) {
319     case e1000_82573:
320         hw->eeprom_semaphore_present = TRUE;
321         /* fall through */
322     case e1000_82541:
323     case e1000_82547:
324     case e1000_82541_rev_2:
325     case e1000_82547_rev_2:
326         hw->asf_firmware_present = TRUE;
327         break;
328     default:
329         break;
330     }
331
332     return E1000_SUCCESS;
333 }
334
335 /*****************************************************************************
336  * Set media type and TBI compatibility.
337  *
338  * hw - Struct containing variables accessed by shared code
339  * **************************************************************************/
340 void
341 e1000_set_media_type(struct e1000_hw *hw)
342 {
343     uint32_t status;
344
345     DEBUGFUNC("e1000_set_media_type");
346
347     if(hw->mac_type != e1000_82543) {
348         /* tbi_compatibility is only valid on 82543 */
349         hw->tbi_compatibility_en = FALSE;
350     }
351
352     switch (hw->device_id) {
353     case E1000_DEV_ID_82545GM_SERDES:
354     case E1000_DEV_ID_82546GB_SERDES:
355         hw->media_type = e1000_media_type_internal_serdes;
356         break;
357     default:
358         if(hw->mac_type >= e1000_82543) {
359             status = E1000_READ_REG(hw, STATUS);
360             if(status & E1000_STATUS_TBIMODE) {
361                 hw->media_type = e1000_media_type_fiber;
362                 /* tbi_compatibility not valid on fiber */
363                 hw->tbi_compatibility_en = FALSE;
364             } else {
365                 hw->media_type = e1000_media_type_copper;
366             }
367         } else {
368             /* This is an 82542 (fiber only) */
369             hw->media_type = e1000_media_type_fiber;
370         }
371     }
372 }
373
374 /******************************************************************************
375  * Reset the transmit and receive units; mask and clear all interrupts.
376  *
377  * hw - Struct containing variables accessed by shared code
378  *****************************************************************************/
379 int32_t
380 e1000_reset_hw(struct e1000_hw *hw)
381 {
382     uint32_t ctrl;
383     uint32_t ctrl_ext;
384     uint32_t icr;
385     uint32_t manc;
386     uint32_t led_ctrl;
387     uint32_t timeout;
388     uint32_t extcnf_ctrl;
389     int32_t ret_val;
390
391     DEBUGFUNC("e1000_reset_hw");
392
393     /* For 82542 (rev 2.0), disable MWI before issuing a device reset */
394     if(hw->mac_type == e1000_82542_rev2_0) {
395         DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
396         e1000_pci_clear_mwi(hw);
397     }
398
399     if(hw->bus_type == e1000_bus_type_pci_express) {
400         /* Prevent the PCI-E bus from sticking if there is no TLP connection
401          * on the last TLP read/write transaction when MAC is reset.
402          */
403         if(e1000_disable_pciex_master(hw) != E1000_SUCCESS) {
404             DEBUGOUT("PCI-E Master disable polling has failed.\n");
405         }
406     }
407
408     /* Clear interrupt mask to stop board from generating interrupts */
409     DEBUGOUT("Masking off all interrupts\n");
410     E1000_WRITE_REG(hw, IMC, 0xffffffff);
411
412     /* Disable the Transmit and Receive units.  Then delay to allow
413      * any pending transactions to complete before we hit the MAC with
414      * the global reset.
415      */
416     E1000_WRITE_REG(hw, RCTL, 0);
417     E1000_WRITE_REG(hw, TCTL, E1000_TCTL_PSP);
418     E1000_WRITE_FLUSH(hw);
419
420     /* The tbi_compatibility_on Flag must be cleared when Rctl is cleared. */
421     hw->tbi_compatibility_on = FALSE;
422
423     /* Delay to allow any outstanding PCI transactions to complete before
424      * resetting the device
425      */
426     msec_delay(10);
427
428     ctrl = E1000_READ_REG(hw, CTRL);
429
430     /* Must reset the PHY before resetting the MAC */
431     if((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
432         E1000_WRITE_REG(hw, CTRL, (ctrl | E1000_CTRL_PHY_RST));
433         msec_delay(5);
434     }
435
436     /* Must acquire the MDIO ownership before MAC reset.
437      * Ownership defaults to firmware after a reset. */
438     if(hw->mac_type == e1000_82573) {
439         timeout = 10;
440
441         extcnf_ctrl = E1000_READ_REG(hw, EXTCNF_CTRL);
442         extcnf_ctrl |= E1000_EXTCNF_CTRL_MDIO_SW_OWNERSHIP;
443
444         do {
445             E1000_WRITE_REG(hw, EXTCNF_CTRL, extcnf_ctrl);
446             extcnf_ctrl = E1000_READ_REG(hw, EXTCNF_CTRL);
447
448             if(extcnf_ctrl & E1000_EXTCNF_CTRL_MDIO_SW_OWNERSHIP)
449                 break;
450             else
451                 extcnf_ctrl |= E1000_EXTCNF_CTRL_MDIO_SW_OWNERSHIP;
452
453             msec_delay(2);
454             timeout--;
455         } while(timeout);
456     }
457
458     /* Issue a global reset to the MAC.  This will reset the chip's
459      * transmit, receive, DMA, and link units.  It will not effect
460      * the current PCI configuration.  The global reset bit is self-
461      * clearing, and should clear within a microsecond.
462      */
463     DEBUGOUT("Issuing a global reset to MAC\n");
464
465     switch(hw->mac_type) {
466         case e1000_82544:
467         case e1000_82540:
468         case e1000_82545:
469         case e1000_82546:
470         case e1000_82541:
471         case e1000_82541_rev_2:
472             /* These controllers can't ack the 64-bit write when issuing the
473              * reset, so use IO-mapping as a workaround to issue the reset */
474             E1000_WRITE_REG_IO(hw, CTRL, (ctrl | E1000_CTRL_RST));
475             break;
476         case e1000_82545_rev_3:
477         case e1000_82546_rev_3:
478             /* Reset is performed on a shadow of the control register */
479             E1000_WRITE_REG(hw, CTRL_DUP, (ctrl | E1000_CTRL_RST));
480             break;
481         default:
482             E1000_WRITE_REG(hw, CTRL, (ctrl | E1000_CTRL_RST));
483             break;
484     }
485
486     /* After MAC reset, force reload of EEPROM to restore power-on settings to
487      * device.  Later controllers reload the EEPROM automatically, so just wait
488      * for reload to complete.
489      */
490     switch(hw->mac_type) {
491         case e1000_82542_rev2_0:
492         case e1000_82542_rev2_1:
493         case e1000_82543:
494         case e1000_82544:
495             /* Wait for reset to complete */
496             udelay(10);
497             ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
498             ctrl_ext |= E1000_CTRL_EXT_EE_RST;
499             E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
500             E1000_WRITE_FLUSH(hw);
501             /* Wait for EEPROM reload */
502             msec_delay(2);
503             break;
504         case e1000_82541:
505         case e1000_82541_rev_2:
506         case e1000_82547:
507         case e1000_82547_rev_2:
508             /* Wait for EEPROM reload */
509             msec_delay(20);
510             break;
511         case e1000_82573:
512             udelay(10);
513             ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
514             ctrl_ext |= E1000_CTRL_EXT_EE_RST;
515             E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
516             E1000_WRITE_FLUSH(hw);
517             /* fall through */
518             ret_val = e1000_get_auto_rd_done(hw);
519             if(ret_val)
520                 /* We don't want to continue accessing MAC registers. */
521                 return ret_val;
522             break;
523         default:
524             /* Wait for EEPROM reload (it happens automatically) */
525             msec_delay(5);
526             break;
527     }
528
529     /* Disable HW ARPs on ASF enabled adapters */
530     if(hw->mac_type >= e1000_82540 && hw->mac_type <= e1000_82547_rev_2) {
531         manc = E1000_READ_REG(hw, MANC);
532         manc &= ~(E1000_MANC_ARP_EN);
533         E1000_WRITE_REG(hw, MANC, manc);
534     }
535
536     if((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
537         e1000_phy_init_script(hw);
538
539         /* Configure activity LED after PHY reset */
540         led_ctrl = E1000_READ_REG(hw, LEDCTL);
541         led_ctrl &= IGP_ACTIVITY_LED_MASK;
542         led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
543         E1000_WRITE_REG(hw, LEDCTL, led_ctrl);
544     }
545
546     /* Clear interrupt mask to stop board from generating interrupts */
547     DEBUGOUT("Masking off all interrupts\n");
548     E1000_WRITE_REG(hw, IMC, 0xffffffff);
549
550     /* Clear any pending interrupt events. */
551     icr = E1000_READ_REG(hw, ICR);
552
553     /* If MWI was previously enabled, reenable it. */
554     if(hw->mac_type == e1000_82542_rev2_0) {
555         if(hw->pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
556             e1000_pci_set_mwi(hw);
557     }
558
559     return E1000_SUCCESS;
560 }
561
562 /******************************************************************************
563  * Performs basic configuration of the adapter.
564  *
565  * hw - Struct containing variables accessed by shared code
566  *
567  * Assumes that the controller has previously been reset and is in a
568  * post-reset uninitialized state. Initializes the receive address registers,
569  * multicast table, and VLAN filter table. Calls routines to setup link
570  * configuration and flow control settings. Clears all on-chip counters. Leaves
571  * the transmit and receive units disabled and uninitialized.
572  *****************************************************************************/
573 int32_t
574 e1000_init_hw(struct e1000_hw *hw)
575 {
576     uint32_t ctrl;
577     uint32_t i;
578     int32_t ret_val;
579     uint16_t pcix_cmd_word;
580     uint16_t pcix_stat_hi_word;
581     uint16_t cmd_mmrbc;
582     uint16_t stat_mmrbc;
583     uint32_t mta_size;
584
585     DEBUGFUNC("e1000_init_hw");
586
587     /* Initialize Identification LED */
588     ret_val = e1000_id_led_init(hw);
589     if(ret_val) {
590         DEBUGOUT("Error Initializing Identification LED\n");
591         return ret_val;
592     }
593
594     /* Set the media type and TBI compatibility */
595     e1000_set_media_type(hw);
596
597     /* Disabling VLAN filtering. */
598     DEBUGOUT("Initializing the IEEE VLAN\n");
599     if (hw->mac_type < e1000_82545_rev_3)
600         E1000_WRITE_REG(hw, VET, 0);
601     e1000_clear_vfta(hw);
602
603     /* For 82542 (rev 2.0), disable MWI and put the receiver into reset */
604     if(hw->mac_type == e1000_82542_rev2_0) {
605         DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
606         e1000_pci_clear_mwi(hw);
607         E1000_WRITE_REG(hw, RCTL, E1000_RCTL_RST);
608         E1000_WRITE_FLUSH(hw);
609         msec_delay(5);
610     }
611
612     /* Setup the receive address. This involves initializing all of the Receive
613      * Address Registers (RARs 0 - 15).
614      */
615     e1000_init_rx_addrs(hw);
616
617     /* For 82542 (rev 2.0), take the receiver out of reset and enable MWI */
618     if(hw->mac_type == e1000_82542_rev2_0) {
619         E1000_WRITE_REG(hw, RCTL, 0);
620         E1000_WRITE_FLUSH(hw);
621         msec_delay(1);
622         if(hw->pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
623             e1000_pci_set_mwi(hw);
624     }
625
626     /* Zero out the Multicast HASH table */
627     DEBUGOUT("Zeroing the MTA\n");
628     mta_size = E1000_MC_TBL_SIZE;
629     for(i = 0; i < mta_size; i++)
630         E1000_WRITE_REG_ARRAY(hw, MTA, i, 0);
631
632     /* Set the PCI priority bit correctly in the CTRL register.  This
633      * determines if the adapter gives priority to receives, or if it
634      * gives equal priority to transmits and receives.  Valid only on
635      * 82542 and 82543 silicon.
636      */
637     if(hw->dma_fairness && hw->mac_type <= e1000_82543) {
638         ctrl = E1000_READ_REG(hw, CTRL);
639         E1000_WRITE_REG(hw, CTRL, ctrl | E1000_CTRL_PRIOR);
640     }
641
642     switch(hw->mac_type) {
643     case e1000_82545_rev_3:
644     case e1000_82546_rev_3:
645         break;
646     default:
647         /* Workaround for PCI-X problem when BIOS sets MMRBC incorrectly. */
648         if(hw->bus_type == e1000_bus_type_pcix) {
649             e1000_read_pci_cfg(hw, PCIX_COMMAND_REGISTER, &pcix_cmd_word);
650             e1000_read_pci_cfg(hw, PCIX_STATUS_REGISTER_HI,
651                 &pcix_stat_hi_word);
652             cmd_mmrbc = (pcix_cmd_word & PCIX_COMMAND_MMRBC_MASK) >>
653                 PCIX_COMMAND_MMRBC_SHIFT;
654             stat_mmrbc = (pcix_stat_hi_word & PCIX_STATUS_HI_MMRBC_MASK) >>
655                 PCIX_STATUS_HI_MMRBC_SHIFT;
656             if(stat_mmrbc == PCIX_STATUS_HI_MMRBC_4K)
657                 stat_mmrbc = PCIX_STATUS_HI_MMRBC_2K;
658             if(cmd_mmrbc > stat_mmrbc) {
659                 pcix_cmd_word &= ~PCIX_COMMAND_MMRBC_MASK;
660                 pcix_cmd_word |= stat_mmrbc << PCIX_COMMAND_MMRBC_SHIFT;
661                 e1000_write_pci_cfg(hw, PCIX_COMMAND_REGISTER,
662                     &pcix_cmd_word);
663             }
664         }
665         break;
666     }
667
668     /* Call a subroutine to configure the link and setup flow control. */
669     ret_val = e1000_setup_link(hw);
670
671     /* Set the transmit descriptor write-back policy */
672     if(hw->mac_type > e1000_82544) {
673         ctrl = E1000_READ_REG(hw, TXDCTL);
674         ctrl = (ctrl & ~E1000_TXDCTL_WTHRESH) | E1000_TXDCTL_FULL_TX_DESC_WB;
675         switch (hw->mac_type) {
676         default:
677             break;
678         case e1000_82573:
679             ctrl |= E1000_TXDCTL_COUNT_DESC;
680             break;
681         }
682         E1000_WRITE_REG(hw, TXDCTL, ctrl);
683     }
684
685     if (hw->mac_type == e1000_82573) {
686         e1000_enable_tx_pkt_filtering(hw); 
687     }
688
689
690     /* Clear all of the statistics registers (clear on read).  It is
691      * important that we do this after we have tried to establish link
692      * because the symbol error count will increment wildly if there
693      * is no link.
694      */
695     e1000_clear_hw_cntrs(hw);
696
697     return ret_val;
698 }
699
700 /******************************************************************************
701  * Adjust SERDES output amplitude based on EEPROM setting.
702  *
703  * hw - Struct containing variables accessed by shared code.
704  *****************************************************************************/
705 static int32_t
706 e1000_adjust_serdes_amplitude(struct e1000_hw *hw)
707 {
708     uint16_t eeprom_data;
709     int32_t  ret_val;
710
711     DEBUGFUNC("e1000_adjust_serdes_amplitude");
712
713     if(hw->media_type != e1000_media_type_internal_serdes)
714         return E1000_SUCCESS;
715
716     switch(hw->mac_type) {
717     case e1000_82545_rev_3:
718     case e1000_82546_rev_3:
719         break;
720     default:
721         return E1000_SUCCESS;
722     }
723
724     ret_val = e1000_read_eeprom(hw, EEPROM_SERDES_AMPLITUDE, 1, &eeprom_data);
725     if (ret_val) {
726         return ret_val;
727     }
728
729     if(eeprom_data != EEPROM_RESERVED_WORD) {
730         /* Adjust SERDES output amplitude only. */
731         eeprom_data &= EEPROM_SERDES_AMPLITUDE_MASK; 
732         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_EXT_CTRL, eeprom_data);
733         if(ret_val)
734             return ret_val;
735     }
736
737     return E1000_SUCCESS;
738 }
739
740 /******************************************************************************
741  * Configures flow control and link settings.
742  *
743  * hw - Struct containing variables accessed by shared code
744  *
745  * Determines which flow control settings to use. Calls the apropriate media-
746  * specific link configuration function. Configures the flow control settings.
747  * Assuming the adapter has a valid link partner, a valid link should be
748  * established. Assumes the hardware has previously been reset and the
749  * transmitter and receiver are not enabled.
750  *****************************************************************************/
751 int32_t
752 e1000_setup_link(struct e1000_hw *hw)
753 {
754     uint32_t ctrl_ext;
755     int32_t ret_val;
756     uint16_t eeprom_data;
757
758     DEBUGFUNC("e1000_setup_link");
759
760     /* Read and store word 0x0F of the EEPROM. This word contains bits
761      * that determine the hardware's default PAUSE (flow control) mode,
762      * a bit that determines whether the HW defaults to enabling or
763      * disabling auto-negotiation, and the direction of the
764      * SW defined pins. If there is no SW over-ride of the flow
765      * control setting, then the variable hw->fc will
766      * be initialized based on a value in the EEPROM.
767      */
768     if(e1000_read_eeprom(hw, EEPROM_INIT_CONTROL2_REG, 1, &eeprom_data)) {
769         DEBUGOUT("EEPROM Read Error\n");
770         return -E1000_ERR_EEPROM;
771     }
772
773     if(hw->fc == e1000_fc_default) {
774         if((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) == 0)
775             hw->fc = e1000_fc_none;
776         else if((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) ==
777                 EEPROM_WORD0F_ASM_DIR)
778             hw->fc = e1000_fc_tx_pause;
779         else
780             hw->fc = e1000_fc_full;
781     }
782
783     /* We want to save off the original Flow Control configuration just
784      * in case we get disconnected and then reconnected into a different
785      * hub or switch with different Flow Control capabilities.
786      */
787     if(hw->mac_type == e1000_82542_rev2_0)
788         hw->fc &= (~e1000_fc_tx_pause);
789
790     if((hw->mac_type < e1000_82543) && (hw->report_tx_early == 1))
791         hw->fc &= (~e1000_fc_rx_pause);
792
793     hw->original_fc = hw->fc;
794
795     DEBUGOUT1("After fix-ups FlowControl is now = %x\n", hw->fc);
796
797     /* Take the 4 bits from EEPROM word 0x0F that determine the initial
798      * polarity value for the SW controlled pins, and setup the
799      * Extended Device Control reg with that info.
800      * This is needed because one of the SW controlled pins is used for
801      * signal detection.  So this should be done before e1000_setup_pcs_link()
802      * or e1000_phy_setup() is called.
803      */
804     if(hw->mac_type == e1000_82543) {
805         ctrl_ext = ((eeprom_data & EEPROM_WORD0F_SWPDIO_EXT) <<
806                     SWDPIO__EXT_SHIFT);
807         E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
808     }
809
810     /* Call the necessary subroutine to configure the link. */
811     ret_val = (hw->media_type == e1000_media_type_copper) ?
812               e1000_setup_copper_link(hw) :
813               e1000_setup_fiber_serdes_link(hw);
814
815     /* Initialize the flow control address, type, and PAUSE timer
816      * registers to their default values.  This is done even if flow
817      * control is disabled, because it does not hurt anything to
818      * initialize these registers.
819      */
820     DEBUGOUT("Initializing the Flow Control address, type and timer regs\n");
821
822     E1000_WRITE_REG(hw, FCAL, FLOW_CONTROL_ADDRESS_LOW);
823     E1000_WRITE_REG(hw, FCAH, FLOW_CONTROL_ADDRESS_HIGH);
824     E1000_WRITE_REG(hw, FCT, FLOW_CONTROL_TYPE);
825
826     E1000_WRITE_REG(hw, FCTTV, hw->fc_pause_time);
827
828     /* Set the flow control receive threshold registers.  Normally,
829      * these registers will be set to a default threshold that may be
830      * adjusted later by the driver's runtime code.  However, if the
831      * ability to transmit pause frames in not enabled, then these
832      * registers will be set to 0.
833      */
834     if(!(hw->fc & e1000_fc_tx_pause)) {
835         E1000_WRITE_REG(hw, FCRTL, 0);
836         E1000_WRITE_REG(hw, FCRTH, 0);
837     } else {
838         /* We need to set up the Receive Threshold high and low water marks
839          * as well as (optionally) enabling the transmission of XON frames.
840          */
841         if(hw->fc_send_xon) {
842             E1000_WRITE_REG(hw, FCRTL, (hw->fc_low_water | E1000_FCRTL_XONE));
843             E1000_WRITE_REG(hw, FCRTH, hw->fc_high_water);
844         } else {
845             E1000_WRITE_REG(hw, FCRTL, hw->fc_low_water);
846             E1000_WRITE_REG(hw, FCRTH, hw->fc_high_water);
847         }
848     }
849     return ret_val;
850 }
851
852 /******************************************************************************
853  * Sets up link for a fiber based or serdes based adapter
854  *
855  * hw - Struct containing variables accessed by shared code
856  *
857  * Manipulates Physical Coding Sublayer functions in order to configure
858  * link. Assumes the hardware has been previously reset and the transmitter
859  * and receiver are not enabled.
860  *****************************************************************************/
861 static int32_t
862 e1000_setup_fiber_serdes_link(struct e1000_hw *hw)
863 {
864     uint32_t ctrl;
865     uint32_t status;
866     uint32_t txcw = 0;
867     uint32_t i;
868     uint32_t signal = 0;
869     int32_t ret_val;
870
871     DEBUGFUNC("e1000_setup_fiber_serdes_link");
872
873     /* On adapters with a MAC newer than 82544, SW Defineable pin 1 will be
874      * set when the optics detect a signal. On older adapters, it will be
875      * cleared when there is a signal.  This applies to fiber media only.
876      * If we're on serdes media, adjust the output amplitude to value set in
877      * the EEPROM.
878      */
879     ctrl = E1000_READ_REG(hw, CTRL);
880     if(hw->media_type == e1000_media_type_fiber)
881         signal = (hw->mac_type > e1000_82544) ? E1000_CTRL_SWDPIN1 : 0;
882
883     ret_val = e1000_adjust_serdes_amplitude(hw);
884     if(ret_val)
885         return ret_val;
886
887     /* Take the link out of reset */
888     ctrl &= ~(E1000_CTRL_LRST);
889
890     /* Adjust VCO speed to improve BER performance */
891     ret_val = e1000_set_vco_speed(hw);
892     if(ret_val)
893         return ret_val;
894
895     e1000_config_collision_dist(hw);
896
897     /* Check for a software override of the flow control settings, and setup
898      * the device accordingly.  If auto-negotiation is enabled, then software
899      * will have to set the "PAUSE" bits to the correct value in the Tranmsit
900      * Config Word Register (TXCW) and re-start auto-negotiation.  However, if
901      * auto-negotiation is disabled, then software will have to manually
902      * configure the two flow control enable bits in the CTRL register.
903      *
904      * The possible values of the "fc" parameter are:
905      *      0:  Flow control is completely disabled
906      *      1:  Rx flow control is enabled (we can receive pause frames, but
907      *          not send pause frames).
908      *      2:  Tx flow control is enabled (we can send pause frames but we do
909      *          not support receiving pause frames).
910      *      3:  Both Rx and TX flow control (symmetric) are enabled.
911      */
912     switch (hw->fc) {
913     case e1000_fc_none:
914         /* Flow control is completely disabled by a software over-ride. */
915         txcw = (E1000_TXCW_ANE | E1000_TXCW_FD);
916         break;
917     case e1000_fc_rx_pause:
918         /* RX Flow control is enabled and TX Flow control is disabled by a
919          * software over-ride. Since there really isn't a way to advertise
920          * that we are capable of RX Pause ONLY, we will advertise that we
921          * support both symmetric and asymmetric RX PAUSE. Later, we will
922          *  disable the adapter's ability to send PAUSE frames.
923          */
924         txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
925         break;
926     case e1000_fc_tx_pause:
927         /* TX Flow control is enabled, and RX Flow control is disabled, by a
928          * software over-ride.
929          */
930         txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_ASM_DIR);
931         break;
932     case e1000_fc_full:
933         /* Flow control (both RX and TX) is enabled by a software over-ride. */
934         txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
935         break;
936     default:
937         DEBUGOUT("Flow control param set incorrectly\n");
938         return -E1000_ERR_CONFIG;
939         break;
940     }
941
942     /* Since auto-negotiation is enabled, take the link out of reset (the link
943      * will be in reset, because we previously reset the chip). This will
944      * restart auto-negotiation.  If auto-neogtiation is successful then the
945      * link-up status bit will be set and the flow control enable bits (RFCE
946      * and TFCE) will be set according to their negotiated value.
947      */
948     DEBUGOUT("Auto-negotiation enabled\n");
949
950     E1000_WRITE_REG(hw, TXCW, txcw);
951     E1000_WRITE_REG(hw, CTRL, ctrl);
952     E1000_WRITE_FLUSH(hw);
953
954     hw->txcw = txcw;
955     msec_delay(1);
956
957     /* If we have a signal (the cable is plugged in) then poll for a "Link-Up"
958      * indication in the Device Status Register.  Time-out if a link isn't
959      * seen in 500 milliseconds seconds (Auto-negotiation should complete in
960      * less than 500 milliseconds even if the other end is doing it in SW).
961      * For internal serdes, we just assume a signal is present, then poll.
962      */
963     if(hw->media_type == e1000_media_type_internal_serdes ||
964        (E1000_READ_REG(hw, CTRL) & E1000_CTRL_SWDPIN1) == signal) {
965         DEBUGOUT("Looking for Link\n");
966         for(i = 0; i < (LINK_UP_TIMEOUT / 10); i++) {
967             msec_delay(10);
968             status = E1000_READ_REG(hw, STATUS);
969             if(status & E1000_STATUS_LU) break;
970         }
971         if(i == (LINK_UP_TIMEOUT / 10)) {
972             DEBUGOUT("Never got a valid link from auto-neg!!!\n");
973             hw->autoneg_failed = 1;
974             /* AutoNeg failed to achieve a link, so we'll call
975              * e1000_check_for_link. This routine will force the link up if
976              * we detect a signal. This will allow us to communicate with
977              * non-autonegotiating link partners.
978              */
979             ret_val = e1000_check_for_link(hw);
980             if(ret_val) {
981                 DEBUGOUT("Error while checking for link\n");
982                 return ret_val;
983             }
984             hw->autoneg_failed = 0;
985         } else {
986             hw->autoneg_failed = 0;
987             DEBUGOUT("Valid Link Found\n");
988         }
989     } else {
990         DEBUGOUT("No Signal Detected\n");
991     }
992     return E1000_SUCCESS;
993 }
994
995 /******************************************************************************
996 * Make sure we have a valid PHY and change PHY mode before link setup.
997 *
998 * hw - Struct containing variables accessed by shared code
999 ******************************************************************************/
1000 static int32_t
1001 e1000_copper_link_preconfig(struct e1000_hw *hw)
1002 {
1003     uint32_t ctrl;
1004     int32_t ret_val;
1005     uint16_t phy_data;
1006
1007     DEBUGFUNC("e1000_copper_link_preconfig");
1008
1009     ctrl = E1000_READ_REG(hw, CTRL);
1010     /* With 82543, we need to force speed and duplex on the MAC equal to what
1011      * the PHY speed and duplex configuration is. In addition, we need to
1012      * perform a hardware reset on the PHY to take it out of reset.
1013      */
1014     if(hw->mac_type > e1000_82543) {
1015         ctrl |= E1000_CTRL_SLU;
1016         ctrl &= ~(E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
1017         E1000_WRITE_REG(hw, CTRL, ctrl);
1018     } else {
1019         ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX | E1000_CTRL_SLU);
1020         E1000_WRITE_REG(hw, CTRL, ctrl);
1021         ret_val = e1000_phy_hw_reset(hw);
1022         if(ret_val)
1023             return ret_val;
1024     }
1025
1026     /* Make sure we have a valid PHY */
1027     ret_val = e1000_detect_gig_phy(hw);
1028     if(ret_val) {
1029         DEBUGOUT("Error, did not detect valid phy.\n");
1030         return ret_val;
1031     }
1032     DEBUGOUT1("Phy ID = %x \n", hw->phy_id);
1033
1034     /* Set PHY to class A mode (if necessary) */
1035     ret_val = e1000_set_phy_mode(hw);
1036     if(ret_val)
1037         return ret_val;
1038
1039     if((hw->mac_type == e1000_82545_rev_3) ||
1040        (hw->mac_type == e1000_82546_rev_3)) {
1041         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
1042         phy_data |= 0x00000008;
1043         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
1044     }
1045
1046     if(hw->mac_type <= e1000_82543 ||
1047        hw->mac_type == e1000_82541 || hw->mac_type == e1000_82547 ||
1048        hw->mac_type == e1000_82541_rev_2 || hw->mac_type == e1000_82547_rev_2)
1049         hw->phy_reset_disable = FALSE;
1050
1051    return E1000_SUCCESS;
1052 }
1053
1054
1055 /********************************************************************
1056 * Copper link setup for e1000_phy_igp series.
1057 *
1058 * hw - Struct containing variables accessed by shared code
1059 *********************************************************************/
1060 static int32_t
1061 e1000_copper_link_igp_setup(struct e1000_hw *hw)
1062 {
1063     uint32_t led_ctrl;
1064     int32_t ret_val;
1065     uint16_t phy_data;
1066
1067     DEBUGFUNC("e1000_copper_link_igp_setup");
1068
1069     if (hw->phy_reset_disable)
1070         return E1000_SUCCESS;
1071     
1072     ret_val = e1000_phy_reset(hw);
1073     if (ret_val) {
1074         DEBUGOUT("Error Resetting the PHY\n");
1075         return ret_val;
1076     }
1077
1078     /* Wait 10ms for MAC to configure PHY from eeprom settings */
1079     msec_delay(15);
1080
1081     /* Configure activity LED after PHY reset */
1082     led_ctrl = E1000_READ_REG(hw, LEDCTL);
1083     led_ctrl &= IGP_ACTIVITY_LED_MASK;
1084     led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
1085     E1000_WRITE_REG(hw, LEDCTL, led_ctrl);
1086
1087     /* disable lplu d3 during driver init */
1088     ret_val = e1000_set_d3_lplu_state(hw, FALSE);
1089     if (ret_val) {
1090         DEBUGOUT("Error Disabling LPLU D3\n");
1091         return ret_val;
1092     }
1093
1094     /* disable lplu d0 during driver init */
1095     ret_val = e1000_set_d0_lplu_state(hw, FALSE);
1096     if (ret_val) {
1097         DEBUGOUT("Error Disabling LPLU D0\n");
1098         return ret_val;
1099     }
1100     /* Configure mdi-mdix settings */
1101     ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, &phy_data);
1102     if (ret_val)
1103         return ret_val;
1104
1105     if ((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
1106         hw->dsp_config_state = e1000_dsp_config_disabled;
1107         /* Force MDI for earlier revs of the IGP PHY */
1108         phy_data &= ~(IGP01E1000_PSCR_AUTO_MDIX | IGP01E1000_PSCR_FORCE_MDI_MDIX);
1109         hw->mdix = 1;
1110
1111     } else {
1112         hw->dsp_config_state = e1000_dsp_config_enabled;
1113         phy_data &= ~IGP01E1000_PSCR_AUTO_MDIX;
1114
1115         switch (hw->mdix) {
1116         case 1:
1117             phy_data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
1118             break;
1119         case 2:
1120             phy_data |= IGP01E1000_PSCR_FORCE_MDI_MDIX;
1121             break;
1122         case 0:
1123         default:
1124             phy_data |= IGP01E1000_PSCR_AUTO_MDIX;
1125             break;
1126         }
1127     }
1128     ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, phy_data);
1129     if(ret_val)
1130         return ret_val;
1131
1132     /* set auto-master slave resolution settings */
1133     if(hw->autoneg) {
1134         e1000_ms_type phy_ms_setting = hw->master_slave;
1135
1136         if(hw->ffe_config_state == e1000_ffe_config_active)
1137             hw->ffe_config_state = e1000_ffe_config_enabled;
1138
1139         if(hw->dsp_config_state == e1000_dsp_config_activated)
1140             hw->dsp_config_state = e1000_dsp_config_enabled;
1141
1142         /* when autonegotiation advertisment is only 1000Mbps then we
1143           * should disable SmartSpeed and enable Auto MasterSlave
1144           * resolution as hardware default. */
1145         if(hw->autoneg_advertised == ADVERTISE_1000_FULL) {
1146             /* Disable SmartSpeed */
1147             ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG, &phy_data);
1148             if(ret_val)
1149                 return ret_val;
1150             phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
1151             ret_val = e1000_write_phy_reg(hw,
1152                                                   IGP01E1000_PHY_PORT_CONFIG,
1153                                                   phy_data);
1154             if(ret_val)
1155                 return ret_val;
1156             /* Set auto Master/Slave resolution process */
1157             ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_data);
1158             if(ret_val)
1159                 return ret_val;
1160             phy_data &= ~CR_1000T_MS_ENABLE;
1161             ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_data);
1162             if(ret_val)
1163                 return ret_val;
1164         }
1165
1166         ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_data);
1167         if(ret_val)
1168             return ret_val;
1169
1170         /* load defaults for future use */
1171         hw->original_master_slave = (phy_data & CR_1000T_MS_ENABLE) ?
1172                                         ((phy_data & CR_1000T_MS_VALUE) ?
1173                                          e1000_ms_force_master :
1174                                          e1000_ms_force_slave) :
1175                                          e1000_ms_auto;
1176
1177         switch (phy_ms_setting) {
1178         case e1000_ms_force_master:
1179             phy_data |= (CR_1000T_MS_ENABLE | CR_1000T_MS_VALUE);
1180             break;
1181         case e1000_ms_force_slave:
1182             phy_data |= CR_1000T_MS_ENABLE;
1183             phy_data &= ~(CR_1000T_MS_VALUE);
1184             break;
1185         case e1000_ms_auto:
1186             phy_data &= ~CR_1000T_MS_ENABLE;
1187             default:
1188             break;
1189         }
1190         ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_data);
1191         if(ret_val)
1192             return ret_val;
1193         }
1194
1195    return E1000_SUCCESS;
1196 }
1197
1198
1199 /********************************************************************
1200 * Copper link setup for e1000_phy_m88 series.
1201 *
1202 * hw - Struct containing variables accessed by shared code
1203 *********************************************************************/
1204 static int32_t
1205 e1000_copper_link_mgp_setup(struct e1000_hw *hw)
1206 {
1207     int32_t ret_val;
1208     uint16_t phy_data;
1209
1210     DEBUGFUNC("e1000_copper_link_mgp_setup");
1211
1212     if(hw->phy_reset_disable)
1213         return E1000_SUCCESS;
1214     
1215     /* Enable CRS on TX. This must be set for half-duplex operation. */
1216     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
1217     if(ret_val)
1218         return ret_val;
1219
1220     phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
1221
1222     /* Options:
1223      *   MDI/MDI-X = 0 (default)
1224      *   0 - Auto for all speeds
1225      *   1 - MDI mode
1226      *   2 - MDI-X mode
1227      *   3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
1228      */
1229     phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
1230
1231     switch (hw->mdix) {
1232     case 1:
1233         phy_data |= M88E1000_PSCR_MDI_MANUAL_MODE;
1234         break;
1235     case 2:
1236         phy_data |= M88E1000_PSCR_MDIX_MANUAL_MODE;
1237         break;
1238     case 3:
1239         phy_data |= M88E1000_PSCR_AUTO_X_1000T;
1240         break;
1241     case 0:
1242     default:
1243         phy_data |= M88E1000_PSCR_AUTO_X_MODE;
1244         break;
1245     }
1246
1247     /* Options:
1248      *   disable_polarity_correction = 0 (default)
1249      *       Automatic Correction for Reversed Cable Polarity
1250      *   0 - Disabled
1251      *   1 - Enabled
1252      */
1253     phy_data &= ~M88E1000_PSCR_POLARITY_REVERSAL;
1254     if(hw->disable_polarity_correction == 1)
1255         phy_data |= M88E1000_PSCR_POLARITY_REVERSAL;
1256         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
1257         if(ret_val)
1258             return ret_val;
1259
1260     /* Force TX_CLK in the Extended PHY Specific Control Register
1261      * to 25MHz clock.
1262      */
1263     ret_val = e1000_read_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, &phy_data);
1264     if(ret_val)
1265         return ret_val;
1266
1267     phy_data |= M88E1000_EPSCR_TX_CLK_25;
1268
1269     if (hw->phy_revision < M88E1011_I_REV_4) {
1270         /* Configure Master and Slave downshift values */
1271         phy_data &= ~(M88E1000_EPSCR_MASTER_DOWNSHIFT_MASK |
1272                               M88E1000_EPSCR_SLAVE_DOWNSHIFT_MASK);
1273         phy_data |= (M88E1000_EPSCR_MASTER_DOWNSHIFT_1X |
1274                              M88E1000_EPSCR_SLAVE_DOWNSHIFT_1X);
1275         ret_val = e1000_write_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
1276         if(ret_val)
1277             return ret_val;
1278     }
1279
1280     /* SW Reset the PHY so all changes take effect */
1281     ret_val = e1000_phy_reset(hw);
1282     if(ret_val) {
1283         DEBUGOUT("Error Resetting the PHY\n");
1284         return ret_val;
1285     }
1286
1287    return E1000_SUCCESS;
1288 }
1289
1290 /********************************************************************
1291 * Setup auto-negotiation and flow control advertisements,
1292 * and then perform auto-negotiation.
1293 *
1294 * hw - Struct containing variables accessed by shared code
1295 *********************************************************************/
1296 static int32_t
1297 e1000_copper_link_autoneg(struct e1000_hw *hw)
1298 {
1299     int32_t ret_val;
1300     uint16_t phy_data;
1301
1302     DEBUGFUNC("e1000_copper_link_autoneg");
1303
1304     /* Perform some bounds checking on the hw->autoneg_advertised
1305      * parameter.  If this variable is zero, then set it to the default.
1306      */
1307     hw->autoneg_advertised &= AUTONEG_ADVERTISE_SPEED_DEFAULT;
1308
1309     /* If autoneg_advertised is zero, we assume it was not defaulted
1310      * by the calling code so we set to advertise full capability.
1311      */
1312     if(hw->autoneg_advertised == 0)
1313         hw->autoneg_advertised = AUTONEG_ADVERTISE_SPEED_DEFAULT;
1314
1315     DEBUGOUT("Reconfiguring auto-neg advertisement params\n");
1316     ret_val = e1000_phy_setup_autoneg(hw);
1317     if(ret_val) {
1318         DEBUGOUT("Error Setting up Auto-Negotiation\n");
1319         return ret_val;
1320     }
1321     DEBUGOUT("Restarting Auto-Neg\n");
1322
1323     /* Restart auto-negotiation by setting the Auto Neg Enable bit and
1324      * the Auto Neg Restart bit in the PHY control register.
1325      */
1326     ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
1327     if(ret_val)
1328         return ret_val;
1329
1330     phy_data |= (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
1331     ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
1332     if(ret_val)
1333         return ret_val;
1334
1335     /* Does the user want to wait for Auto-Neg to complete here, or
1336      * check at a later time (for example, callback routine).
1337      */
1338     if(hw->wait_autoneg_complete) {
1339         ret_val = e1000_wait_autoneg(hw);
1340         if(ret_val) {
1341             DEBUGOUT("Error while waiting for autoneg to complete\n");
1342             return ret_val;
1343         }
1344     }
1345
1346     hw->get_link_status = TRUE;
1347
1348     return E1000_SUCCESS;
1349 }
1350
1351
1352 /******************************************************************************
1353 * Config the MAC and the PHY after link is up.
1354 *   1) Set up the MAC to the current PHY speed/duplex
1355 *      if we are on 82543.  If we
1356 *      are on newer silicon, we only need to configure
1357 *      collision distance in the Transmit Control Register.
1358 *   2) Set up flow control on the MAC to that established with
1359 *      the link partner.
1360 *   3) Config DSP to improve Gigabit link quality for some PHY revisions.    
1361 *
1362 * hw - Struct containing variables accessed by shared code
1363 ******************************************************************************/
1364 static int32_t
1365 e1000_copper_link_postconfig(struct e1000_hw *hw)
1366 {
1367     int32_t ret_val;
1368     DEBUGFUNC("e1000_copper_link_postconfig");
1369     
1370     if(hw->mac_type >= e1000_82544) {
1371         e1000_config_collision_dist(hw);
1372     } else {
1373         ret_val = e1000_config_mac_to_phy(hw);
1374         if(ret_val) {
1375             DEBUGOUT("Error configuring MAC to PHY settings\n");
1376             return ret_val;
1377         }
1378     }
1379     ret_val = e1000_config_fc_after_link_up(hw);
1380     if(ret_val) {
1381         DEBUGOUT("Error Configuring Flow Control\n");
1382         return ret_val;
1383     }
1384
1385     /* Config DSP to improve Giga link quality */
1386     if(hw->phy_type == e1000_phy_igp) {
1387         ret_val = e1000_config_dsp_after_link_change(hw, TRUE);
1388         if(ret_val) {
1389             DEBUGOUT("Error Configuring DSP after link up\n");
1390             return ret_val;
1391         }
1392     }
1393                 
1394     return E1000_SUCCESS;
1395 }
1396
1397 /******************************************************************************
1398 * Detects which PHY is present and setup the speed and duplex
1399 *
1400 * hw - Struct containing variables accessed by shared code
1401 ******************************************************************************/
1402 static int32_t
1403 e1000_setup_copper_link(struct e1000_hw *hw)
1404 {
1405     int32_t ret_val;
1406     uint16_t i;
1407     uint16_t phy_data;
1408
1409     DEBUGFUNC("e1000_setup_copper_link");
1410
1411     /* Check if it is a valid PHY and set PHY mode if necessary. */
1412     ret_val = e1000_copper_link_preconfig(hw);
1413     if(ret_val)
1414         return ret_val;
1415
1416     if (hw->phy_type == e1000_phy_igp ||
1417         hw->phy_type == e1000_phy_igp_2) {
1418         ret_val = e1000_copper_link_igp_setup(hw);
1419         if(ret_val)
1420             return ret_val;
1421     } else if (hw->phy_type == e1000_phy_m88) {
1422         ret_val = e1000_copper_link_mgp_setup(hw);
1423         if(ret_val)
1424             return ret_val;
1425     }
1426
1427     if(hw->autoneg) {
1428         /* Setup autoneg and flow control advertisement 
1429           * and perform autonegotiation */   
1430         ret_val = e1000_copper_link_autoneg(hw);
1431         if(ret_val)
1432             return ret_val;           
1433     } else {
1434         /* PHY will be set to 10H, 10F, 100H,or 100F
1435           * depending on value from forced_speed_duplex. */
1436         DEBUGOUT("Forcing speed and duplex\n");
1437         ret_val = e1000_phy_force_speed_duplex(hw);
1438         if(ret_val) {
1439             DEBUGOUT("Error Forcing Speed and Duplex\n");
1440             return ret_val;
1441         }
1442     }
1443
1444     /* Check link status. Wait up to 100 microseconds for link to become
1445      * valid.
1446      */
1447     for(i = 0; i < 10; i++) {
1448         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
1449         if(ret_val)
1450             return ret_val;
1451         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
1452         if(ret_val)
1453             return ret_val;
1454
1455         if(phy_data & MII_SR_LINK_STATUS) {
1456             /* Config the MAC and PHY after link is up */
1457             ret_val = e1000_copper_link_postconfig(hw);
1458             if(ret_val)
1459                 return ret_val;
1460             
1461             DEBUGOUT("Valid link established!!!\n");
1462             return E1000_SUCCESS;
1463         }
1464         udelay(10);
1465     }
1466
1467     DEBUGOUT("Unable to establish link!!!\n");
1468     return E1000_SUCCESS;
1469 }
1470
1471 /******************************************************************************
1472 * Configures PHY autoneg and flow control advertisement settings
1473 *
1474 * hw - Struct containing variables accessed by shared code
1475 ******************************************************************************/
1476 int32_t
1477 e1000_phy_setup_autoneg(struct e1000_hw *hw)
1478 {
1479     int32_t ret_val;
1480     uint16_t mii_autoneg_adv_reg;
1481     uint16_t mii_1000t_ctrl_reg;
1482
1483     DEBUGFUNC("e1000_phy_setup_autoneg");
1484
1485     /* Read the MII Auto-Neg Advertisement Register (Address 4). */
1486     ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_ADV, &mii_autoneg_adv_reg);
1487     if(ret_val)
1488         return ret_val;
1489
1490         /* Read the MII 1000Base-T Control Register (Address 9). */
1491         ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL, &mii_1000t_ctrl_reg);
1492         if(ret_val)
1493             return ret_val;
1494
1495     /* Need to parse both autoneg_advertised and fc and set up
1496      * the appropriate PHY registers.  First we will parse for
1497      * autoneg_advertised software override.  Since we can advertise
1498      * a plethora of combinations, we need to check each bit
1499      * individually.
1500      */
1501
1502     /* First we clear all the 10/100 mb speed bits in the Auto-Neg
1503      * Advertisement Register (Address 4) and the 1000 mb speed bits in
1504      * the  1000Base-T Control Register (Address 9).
1505      */
1506     mii_autoneg_adv_reg &= ~REG4_SPEED_MASK;
1507     mii_1000t_ctrl_reg &= ~REG9_SPEED_MASK;
1508
1509     DEBUGOUT1("autoneg_advertised %x\n", hw->autoneg_advertised);
1510
1511     /* Do we want to advertise 10 Mb Half Duplex? */
1512     if(hw->autoneg_advertised & ADVERTISE_10_HALF) {
1513         DEBUGOUT("Advertise 10mb Half duplex\n");
1514         mii_autoneg_adv_reg |= NWAY_AR_10T_HD_CAPS;
1515     }
1516
1517     /* Do we want to advertise 10 Mb Full Duplex? */
1518     if(hw->autoneg_advertised & ADVERTISE_10_FULL) {
1519         DEBUGOUT("Advertise 10mb Full duplex\n");
1520         mii_autoneg_adv_reg |= NWAY_AR_10T_FD_CAPS;
1521     }
1522
1523     /* Do we want to advertise 100 Mb Half Duplex? */
1524     if(hw->autoneg_advertised & ADVERTISE_100_HALF) {
1525         DEBUGOUT("Advertise 100mb Half duplex\n");
1526         mii_autoneg_adv_reg |= NWAY_AR_100TX_HD_CAPS;
1527     }
1528
1529     /* Do we want to advertise 100 Mb Full Duplex? */
1530     if(hw->autoneg_advertised & ADVERTISE_100_FULL) {
1531         DEBUGOUT("Advertise 100mb Full duplex\n");
1532         mii_autoneg_adv_reg |= NWAY_AR_100TX_FD_CAPS;
1533     }
1534
1535     /* We do not allow the Phy to advertise 1000 Mb Half Duplex */
1536     if(hw->autoneg_advertised & ADVERTISE_1000_HALF) {
1537         DEBUGOUT("Advertise 1000mb Half duplex requested, request denied!\n");
1538     }
1539
1540     /* Do we want to advertise 1000 Mb Full Duplex? */
1541     if(hw->autoneg_advertised & ADVERTISE_1000_FULL) {
1542         DEBUGOUT("Advertise 1000mb Full duplex\n");
1543         mii_1000t_ctrl_reg |= CR_1000T_FD_CAPS;
1544     }
1545
1546     /* Check for a software override of the flow control settings, and
1547      * setup the PHY advertisement registers accordingly.  If
1548      * auto-negotiation is enabled, then software will have to set the
1549      * "PAUSE" bits to the correct value in the Auto-Negotiation
1550      * Advertisement Register (PHY_AUTONEG_ADV) and re-start auto-negotiation.
1551      *
1552      * The possible values of the "fc" parameter are:
1553      *      0:  Flow control is completely disabled
1554      *      1:  Rx flow control is enabled (we can receive pause frames
1555      *          but not send pause frames).
1556      *      2:  Tx flow control is enabled (we can send pause frames
1557      *          but we do not support receiving pause frames).
1558      *      3:  Both Rx and TX flow control (symmetric) are enabled.
1559      *  other:  No software override.  The flow control configuration
1560      *          in the EEPROM is used.
1561      */
1562     switch (hw->fc) {
1563     case e1000_fc_none: /* 0 */
1564         /* Flow control (RX & TX) is completely disabled by a
1565          * software over-ride.
1566          */
1567         mii_autoneg_adv_reg &= ~(NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
1568         break;
1569     case e1000_fc_rx_pause: /* 1 */
1570         /* RX Flow control is enabled, and TX Flow control is
1571          * disabled, by a software over-ride.
1572          */
1573         /* Since there really isn't a way to advertise that we are
1574          * capable of RX Pause ONLY, we will advertise that we
1575          * support both symmetric and asymmetric RX PAUSE.  Later
1576          * (in e1000_config_fc_after_link_up) we will disable the
1577          *hw's ability to send PAUSE frames.
1578          */
1579         mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
1580         break;
1581     case e1000_fc_tx_pause: /* 2 */
1582         /* TX Flow control is enabled, and RX Flow control is
1583          * disabled, by a software over-ride.
1584          */
1585         mii_autoneg_adv_reg |= NWAY_AR_ASM_DIR;
1586         mii_autoneg_adv_reg &= ~NWAY_AR_PAUSE;
1587         break;
1588     case e1000_fc_full: /* 3 */
1589         /* Flow control (both RX and TX) is enabled by a software
1590          * over-ride.
1591          */
1592         mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
1593         break;
1594     default:
1595         DEBUGOUT("Flow control param set incorrectly\n");
1596         return -E1000_ERR_CONFIG;
1597     }
1598
1599     ret_val = e1000_write_phy_reg(hw, PHY_AUTONEG_ADV, mii_autoneg_adv_reg);
1600     if(ret_val)
1601         return ret_val;
1602
1603     DEBUGOUT1("Auto-Neg Advertising %x\n", mii_autoneg_adv_reg);
1604
1605     ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL, mii_1000t_ctrl_reg);    
1606     if(ret_val)
1607         return ret_val;
1608
1609     return E1000_SUCCESS;
1610 }
1611
1612 /******************************************************************************
1613 * Force PHY speed and duplex settings to hw->forced_speed_duplex
1614 *
1615 * hw - Struct containing variables accessed by shared code
1616 ******************************************************************************/
1617 static int32_t
1618 e1000_phy_force_speed_duplex(struct e1000_hw *hw)
1619 {
1620     uint32_t ctrl;
1621     int32_t ret_val;
1622     uint16_t mii_ctrl_reg;
1623     uint16_t mii_status_reg;
1624     uint16_t phy_data;
1625     uint16_t i;
1626
1627     DEBUGFUNC("e1000_phy_force_speed_duplex");
1628
1629     /* Turn off Flow control if we are forcing speed and duplex. */
1630     hw->fc = e1000_fc_none;
1631
1632     DEBUGOUT1("hw->fc = %d\n", hw->fc);
1633
1634     /* Read the Device Control Register. */
1635     ctrl = E1000_READ_REG(hw, CTRL);
1636
1637     /* Set the bits to Force Speed and Duplex in the Device Ctrl Reg. */
1638     ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
1639     ctrl &= ~(DEVICE_SPEED_MASK);
1640
1641     /* Clear the Auto Speed Detect Enable bit. */
1642     ctrl &= ~E1000_CTRL_ASDE;
1643
1644     /* Read the MII Control Register. */
1645     ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &mii_ctrl_reg);
1646     if(ret_val)
1647         return ret_val;
1648
1649     /* We need to disable autoneg in order to force link and duplex. */
1650
1651     mii_ctrl_reg &= ~MII_CR_AUTO_NEG_EN;
1652
1653     /* Are we forcing Full or Half Duplex? */
1654     if(hw->forced_speed_duplex == e1000_100_full ||
1655        hw->forced_speed_duplex == e1000_10_full) {
1656         /* We want to force full duplex so we SET the full duplex bits in the
1657          * Device and MII Control Registers.
1658          */
1659         ctrl |= E1000_CTRL_FD;
1660         mii_ctrl_reg |= MII_CR_FULL_DUPLEX;
1661         DEBUGOUT("Full Duplex\n");
1662     } else {
1663         /* We want to force half duplex so we CLEAR the full duplex bits in
1664          * the Device and MII Control Registers.
1665          */
1666         ctrl &= ~E1000_CTRL_FD;
1667         mii_ctrl_reg &= ~MII_CR_FULL_DUPLEX;
1668         DEBUGOUT("Half Duplex\n");
1669     }
1670
1671     /* Are we forcing 100Mbps??? */
1672     if(hw->forced_speed_duplex == e1000_100_full ||
1673        hw->forced_speed_duplex == e1000_100_half) {
1674         /* Set the 100Mb bit and turn off the 1000Mb and 10Mb bits. */
1675         ctrl |= E1000_CTRL_SPD_100;
1676         mii_ctrl_reg |= MII_CR_SPEED_100;
1677         mii_ctrl_reg &= ~(MII_CR_SPEED_1000 | MII_CR_SPEED_10);
1678         DEBUGOUT("Forcing 100mb ");
1679     } else {
1680         /* Set the 10Mb bit and turn off the 1000Mb and 100Mb bits. */
1681         ctrl &= ~(E1000_CTRL_SPD_1000 | E1000_CTRL_SPD_100);
1682         mii_ctrl_reg |= MII_CR_SPEED_10;
1683         mii_ctrl_reg &= ~(MII_CR_SPEED_1000 | MII_CR_SPEED_100);
1684         DEBUGOUT("Forcing 10mb ");
1685     }
1686
1687     e1000_config_collision_dist(hw);
1688
1689     /* Write the configured values back to the Device Control Reg. */
1690     E1000_WRITE_REG(hw, CTRL, ctrl);
1691
1692     if (hw->phy_type == e1000_phy_m88) {
1693         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
1694         if(ret_val)
1695             return ret_val;
1696
1697         /* Clear Auto-Crossover to force MDI manually. M88E1000 requires MDI
1698          * forced whenever speed are duplex are forced.
1699          */
1700         phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
1701         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
1702         if(ret_val)
1703             return ret_val;
1704
1705         DEBUGOUT1("M88E1000 PSCR: %x \n", phy_data);
1706
1707         /* Need to reset the PHY or these changes will be ignored */
1708         mii_ctrl_reg |= MII_CR_RESET;
1709     } else {
1710         /* Clear Auto-Crossover to force MDI manually.  IGP requires MDI
1711          * forced whenever speed or duplex are forced.
1712          */
1713         ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, &phy_data);
1714         if(ret_val)
1715             return ret_val;
1716
1717         phy_data &= ~IGP01E1000_PSCR_AUTO_MDIX;
1718         phy_data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
1719
1720         ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, phy_data);
1721         if(ret_val)
1722             return ret_val;
1723     }
1724
1725     /* Write back the modified PHY MII control register. */
1726     ret_val = e1000_write_phy_reg(hw, PHY_CTRL, mii_ctrl_reg);
1727     if(ret_val)
1728         return ret_val;
1729
1730     udelay(1);
1731
1732     /* The wait_autoneg_complete flag may be a little misleading here.
1733      * Since we are forcing speed and duplex, Auto-Neg is not enabled.
1734      * But we do want to delay for a period while forcing only so we
1735      * don't generate false No Link messages.  So we will wait here
1736      * only if the user has set wait_autoneg_complete to 1, which is
1737      * the default.
1738      */
1739     if(hw->wait_autoneg_complete) {
1740         /* We will wait for autoneg to complete. */
1741         DEBUGOUT("Waiting for forced speed/duplex link.\n");
1742         mii_status_reg = 0;
1743
1744         /* We will wait for autoneg to complete or 4.5 seconds to expire. */
1745         for(i = PHY_FORCE_TIME; i > 0; i--) {
1746             /* Read the MII Status Register and wait for Auto-Neg Complete bit
1747              * to be set.
1748              */
1749             ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
1750             if(ret_val)
1751                 return ret_val;
1752
1753             ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
1754             if(ret_val)
1755                 return ret_val;
1756
1757             if(mii_status_reg & MII_SR_LINK_STATUS) break;
1758             msec_delay(100);
1759         }
1760         if((i == 0) &&
1761            (hw->phy_type == e1000_phy_m88)) {
1762             /* We didn't get link.  Reset the DSP and wait again for link. */
1763             ret_val = e1000_phy_reset_dsp(hw);
1764             if(ret_val) {
1765                 DEBUGOUT("Error Resetting PHY DSP\n");
1766                 return ret_val;
1767             }
1768         }
1769         /* This loop will early-out if the link condition has been met.  */
1770         for(i = PHY_FORCE_TIME; i > 0; i--) {
1771             if(mii_status_reg & MII_SR_LINK_STATUS) break;
1772             msec_delay(100);
1773             /* Read the MII Status Register and wait for Auto-Neg Complete bit
1774              * to be set.
1775              */
1776             ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
1777             if(ret_val)
1778                 return ret_val;
1779
1780             ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
1781             if(ret_val)
1782                 return ret_val;
1783         }
1784     }
1785
1786     if (hw->phy_type == e1000_phy_m88) {
1787         /* Because we reset the PHY above, we need to re-force TX_CLK in the
1788          * Extended PHY Specific Control Register to 25MHz clock.  This value
1789          * defaults back to a 2.5MHz clock when the PHY is reset.
1790          */
1791         ret_val = e1000_read_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, &phy_data);
1792         if(ret_val)
1793             return ret_val;
1794
1795         phy_data |= M88E1000_EPSCR_TX_CLK_25;
1796         ret_val = e1000_write_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
1797         if(ret_val)
1798             return ret_val;
1799
1800         /* In addition, because of the s/w reset above, we need to enable CRS on
1801          * TX.  This must be set for both full and half duplex operation.
1802          */
1803         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
1804         if(ret_val)
1805             return ret_val;
1806
1807         phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
1808         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
1809         if(ret_val)
1810             return ret_val;
1811
1812         if((hw->mac_type == e1000_82544 || hw->mac_type == e1000_82543) &&
1813            (!hw->autoneg) &&
1814            (hw->forced_speed_duplex == e1000_10_full ||
1815             hw->forced_speed_duplex == e1000_10_half)) {
1816             ret_val = e1000_polarity_reversal_workaround(hw);
1817             if(ret_val)
1818                 return ret_val;
1819         }
1820     }
1821     return E1000_SUCCESS;
1822 }
1823
1824 /******************************************************************************
1825 * Sets the collision distance in the Transmit Control register
1826 *
1827 * hw - Struct containing variables accessed by shared code
1828 *
1829 * Link should have been established previously. Reads the speed and duplex
1830 * information from the Device Status register.
1831 ******************************************************************************/
1832 void
1833 e1000_config_collision_dist(struct e1000_hw *hw)
1834 {
1835     uint32_t tctl;
1836
1837     DEBUGFUNC("e1000_config_collision_dist");
1838
1839     tctl = E1000_READ_REG(hw, TCTL);
1840
1841     tctl &= ~E1000_TCTL_COLD;
1842     tctl |= E1000_COLLISION_DISTANCE << E1000_COLD_SHIFT;
1843
1844     E1000_WRITE_REG(hw, TCTL, tctl);
1845     E1000_WRITE_FLUSH(hw);
1846 }
1847
1848 /******************************************************************************
1849 * Sets MAC speed and duplex settings to reflect the those in the PHY
1850 *
1851 * hw - Struct containing variables accessed by shared code
1852 * mii_reg - data to write to the MII control register
1853 *
1854 * The contents of the PHY register containing the needed information need to
1855 * be passed in.
1856 ******************************************************************************/
1857 static int32_t
1858 e1000_config_mac_to_phy(struct e1000_hw *hw)
1859 {
1860     uint32_t ctrl;
1861     int32_t ret_val;
1862     uint16_t phy_data;
1863
1864     DEBUGFUNC("e1000_config_mac_to_phy");
1865
1866     /* 82544 or newer MAC, Auto Speed Detection takes care of 
1867     * MAC speed/duplex configuration.*/
1868     if (hw->mac_type >= e1000_82544)
1869         return E1000_SUCCESS;
1870
1871     /* Read the Device Control Register and set the bits to Force Speed
1872      * and Duplex.
1873      */
1874     ctrl = E1000_READ_REG(hw, CTRL);
1875     ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
1876     ctrl &= ~(E1000_CTRL_SPD_SEL | E1000_CTRL_ILOS);
1877
1878     /* Set up duplex in the Device Control and Transmit Control
1879      * registers depending on negotiated values.
1880      */
1881     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data);
1882     if(ret_val)
1883         return ret_val;
1884
1885     if(phy_data & M88E1000_PSSR_DPLX) 
1886         ctrl |= E1000_CTRL_FD;
1887     else 
1888         ctrl &= ~E1000_CTRL_FD;
1889
1890     e1000_config_collision_dist(hw);
1891
1892     /* Set up speed in the Device Control register depending on
1893      * negotiated values.
1894      */
1895     if((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_1000MBS)
1896         ctrl |= E1000_CTRL_SPD_1000;
1897     else if((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_100MBS)
1898         ctrl |= E1000_CTRL_SPD_100;
1899
1900     /* Write the configured values back to the Device Control Reg. */
1901     E1000_WRITE_REG(hw, CTRL, ctrl);
1902     return E1000_SUCCESS;
1903 }
1904
1905 /******************************************************************************
1906  * Forces the MAC's flow control settings.
1907  *
1908  * hw - Struct containing variables accessed by shared code
1909  *
1910  * Sets the TFCE and RFCE bits in the device control register to reflect
1911  * the adapter settings. TFCE and RFCE need to be explicitly set by
1912  * software when a Copper PHY is used because autonegotiation is managed
1913  * by the PHY rather than the MAC. Software must also configure these
1914  * bits when link is forced on a fiber connection.
1915  *****************************************************************************/
1916 int32_t
1917 e1000_force_mac_fc(struct e1000_hw *hw)
1918 {
1919     uint32_t ctrl;
1920
1921     DEBUGFUNC("e1000_force_mac_fc");
1922
1923     /* Get the current configuration of the Device Control Register */
1924     ctrl = E1000_READ_REG(hw, CTRL);
1925
1926     /* Because we didn't get link via the internal auto-negotiation
1927      * mechanism (we either forced link or we got link via PHY
1928      * auto-neg), we have to manually enable/disable transmit an
1929      * receive flow control.
1930      *
1931      * The "Case" statement below enables/disable flow control
1932      * according to the "hw->fc" parameter.
1933      *
1934      * The possible values of the "fc" parameter are:
1935      *      0:  Flow control is completely disabled
1936      *      1:  Rx flow control is enabled (we can receive pause
1937      *          frames but not send pause frames).
1938      *      2:  Tx flow control is enabled (we can send pause frames
1939      *          frames but we do not receive pause frames).
1940      *      3:  Both Rx and TX flow control (symmetric) is enabled.
1941      *  other:  No other values should be possible at this point.
1942      */
1943
1944     switch (hw->fc) {
1945     case e1000_fc_none:
1946         ctrl &= (~(E1000_CTRL_TFCE | E1000_CTRL_RFCE));
1947         break;
1948     case e1000_fc_rx_pause:
1949         ctrl &= (~E1000_CTRL_TFCE);
1950         ctrl |= E1000_CTRL_RFCE;
1951         break;
1952     case e1000_fc_tx_pause:
1953         ctrl &= (~E1000_CTRL_RFCE);
1954         ctrl |= E1000_CTRL_TFCE;
1955         break;
1956     case e1000_fc_full:
1957         ctrl |= (E1000_CTRL_TFCE | E1000_CTRL_RFCE);
1958         break;
1959     default:
1960         DEBUGOUT("Flow control param set incorrectly\n");
1961         return -E1000_ERR_CONFIG;
1962     }
1963
1964     /* Disable TX Flow Control for 82542 (rev 2.0) */
1965     if(hw->mac_type == e1000_82542_rev2_0)
1966         ctrl &= (~E1000_CTRL_TFCE);
1967
1968     E1000_WRITE_REG(hw, CTRL, ctrl);
1969     return E1000_SUCCESS;
1970 }
1971
1972 /******************************************************************************
1973  * Configures flow control settings after link is established
1974  *
1975  * hw - Struct containing variables accessed by shared code
1976  *
1977  * Should be called immediately after a valid link has been established.
1978  * Forces MAC flow control settings if link was forced. When in MII/GMII mode
1979  * and autonegotiation is enabled, the MAC flow control settings will be set
1980  * based on the flow control negotiated by the PHY. In TBI mode, the TFCE
1981  * and RFCE bits will be automaticaly set to the negotiated flow control mode.
1982  *****************************************************************************/
1983 int32_t
1984 e1000_config_fc_after_link_up(struct e1000_hw *hw)
1985 {
1986     int32_t ret_val;
1987     uint16_t mii_status_reg;
1988     uint16_t mii_nway_adv_reg;
1989     uint16_t mii_nway_lp_ability_reg;
1990     uint16_t speed;
1991     uint16_t duplex;
1992
1993     DEBUGFUNC("e1000_config_fc_after_link_up");
1994
1995     /* Check for the case where we have fiber media and auto-neg failed
1996      * so we had to force link.  In this case, we need to force the
1997      * configuration of the MAC to match the "fc" parameter.
1998      */
1999     if(((hw->media_type == e1000_media_type_fiber) && (hw->autoneg_failed)) ||
2000        ((hw->media_type == e1000_media_type_internal_serdes) && (hw->autoneg_failed)) ||
2001        ((hw->media_type == e1000_media_type_copper) && (!hw->autoneg))) {
2002         ret_val = e1000_force_mac_fc(hw);
2003         if(ret_val) {
2004             DEBUGOUT("Error forcing flow control settings\n");
2005             return ret_val;
2006         }
2007     }
2008
2009     /* Check for the case where we have copper media and auto-neg is
2010      * enabled.  In this case, we need to check and see if Auto-Neg
2011      * has completed, and if so, how the PHY and link partner has
2012      * flow control configured.
2013      */
2014     if((hw->media_type == e1000_media_type_copper) && hw->autoneg) {
2015         /* Read the MII Status Register and check to see if AutoNeg
2016          * has completed.  We read this twice because this reg has
2017          * some "sticky" (latched) bits.
2018          */
2019         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
2020         if(ret_val)
2021             return ret_val;
2022         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
2023         if(ret_val)
2024             return ret_val;
2025
2026         if(mii_status_reg & MII_SR_AUTONEG_COMPLETE) {
2027             /* The AutoNeg process has completed, so we now need to
2028              * read both the Auto Negotiation Advertisement Register
2029              * (Address 4) and the Auto_Negotiation Base Page Ability
2030              * Register (Address 5) to determine how flow control was
2031              * negotiated.
2032              */
2033             ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_ADV,
2034                                          &mii_nway_adv_reg);
2035             if(ret_val)
2036                 return ret_val;
2037             ret_val = e1000_read_phy_reg(hw, PHY_LP_ABILITY,
2038                                          &mii_nway_lp_ability_reg);
2039             if(ret_val)
2040                 return ret_val;
2041
2042             /* Two bits in the Auto Negotiation Advertisement Register
2043              * (Address 4) and two bits in the Auto Negotiation Base
2044              * Page Ability Register (Address 5) determine flow control
2045              * for both the PHY and the link partner.  The following
2046              * table, taken out of the IEEE 802.3ab/D6.0 dated March 25,
2047              * 1999, describes these PAUSE resolution bits and how flow
2048              * control is determined based upon these settings.
2049              * NOTE:  DC = Don't Care
2050              *
2051              *   LOCAL DEVICE  |   LINK PARTNER
2052              * PAUSE | ASM_DIR | PAUSE | ASM_DIR | NIC Resolution
2053              *-------|---------|-------|---------|--------------------
2054              *   0   |    0    |  DC   |   DC    | e1000_fc_none
2055              *   0   |    1    |   0   |   DC    | e1000_fc_none
2056              *   0   |    1    |   1   |    0    | e1000_fc_none
2057              *   0   |    1    |   1   |    1    | e1000_fc_tx_pause
2058              *   1   |    0    |   0   |   DC    | e1000_fc_none
2059              *   1   |   DC    |   1   |   DC    | e1000_fc_full
2060              *   1   |    1    |   0   |    0    | e1000_fc_none
2061              *   1   |    1    |   0   |    1    | e1000_fc_rx_pause
2062              *
2063              */
2064             /* Are both PAUSE bits set to 1?  If so, this implies
2065              * Symmetric Flow Control is enabled at both ends.  The
2066              * ASM_DIR bits are irrelevant per the spec.
2067              *
2068              * For Symmetric Flow Control:
2069              *
2070              *   LOCAL DEVICE  |   LINK PARTNER
2071              * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
2072              *-------|---------|-------|---------|--------------------
2073              *   1   |   DC    |   1   |   DC    | e1000_fc_full
2074              *
2075              */
2076             if((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
2077                (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE)) {
2078                 /* Now we need to check if the user selected RX ONLY
2079                  * of pause frames.  In this case, we had to advertise
2080                  * FULL flow control because we could not advertise RX
2081                  * ONLY. Hence, we must now check to see if we need to
2082                  * turn OFF  the TRANSMISSION of PAUSE frames.
2083                  */
2084                 if(hw->original_fc == e1000_fc_full) {
2085                     hw->fc = e1000_fc_full;
2086                     DEBUGOUT("Flow Control = FULL.\r\n");
2087                 } else {
2088                     hw->fc = e1000_fc_rx_pause;
2089                     DEBUGOUT("Flow Control = RX PAUSE frames only.\r\n");
2090                 }
2091             }
2092             /* For receiving PAUSE frames ONLY.
2093              *
2094              *   LOCAL DEVICE  |   LINK PARTNER
2095              * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
2096              *-------|---------|-------|---------|--------------------
2097              *   0   |    1    |   1   |    1    | e1000_fc_tx_pause
2098              *
2099              */
2100             else if(!(mii_nway_adv_reg & NWAY_AR_PAUSE) &&
2101                     (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
2102                     (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
2103                     (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
2104                 hw->fc = e1000_fc_tx_pause;
2105                 DEBUGOUT("Flow Control = TX PAUSE frames only.\r\n");
2106             }
2107             /* For transmitting PAUSE frames ONLY.
2108              *
2109              *   LOCAL DEVICE  |   LINK PARTNER
2110              * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
2111              *-------|---------|-------|---------|--------------------
2112              *   1   |    1    |   0   |    1    | e1000_fc_rx_pause
2113              *
2114              */
2115             else if((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
2116                     (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
2117                     !(mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
2118                     (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
2119                 hw->fc = e1000_fc_rx_pause;
2120                 DEBUGOUT("Flow Control = RX PAUSE frames only.\r\n");
2121             }
2122             /* Per the IEEE spec, at this point flow control should be
2123              * disabled.  However, we want to consider that we could
2124              * be connected to a legacy switch that doesn't advertise
2125              * desired flow control, but can be forced on the link
2126              * partner.  So if we advertised no flow control, that is
2127              * what we will resolve to.  If we advertised some kind of
2128              * receive capability (Rx Pause Only or Full Flow Control)
2129              * and the link partner advertised none, we will configure
2130              * ourselves to enable Rx Flow Control only.  We can do
2131              * this safely for two reasons:  If the link partner really
2132              * didn't want flow control enabled, and we enable Rx, no
2133              * harm done since we won't be receiving any PAUSE frames
2134              * anyway.  If the intent on the link partner was to have
2135              * flow control enabled, then by us enabling RX only, we
2136              * can at least receive pause frames and process them.
2137              * This is a good idea because in most cases, since we are
2138              * predominantly a server NIC, more times than not we will
2139              * be asked to delay transmission of packets than asking
2140              * our link partner to pause transmission of frames.
2141              */
2142             else if((hw->original_fc == e1000_fc_none ||
2143                      hw->original_fc == e1000_fc_tx_pause) ||
2144                     hw->fc_strict_ieee) {
2145                 hw->fc = e1000_fc_none;
2146                 DEBUGOUT("Flow Control = NONE.\r\n");
2147             } else {
2148                 hw->fc = e1000_fc_rx_pause;
2149                 DEBUGOUT("Flow Control = RX PAUSE frames only.\r\n");
2150             }
2151
2152             /* Now we need to do one last check...  If we auto-
2153              * negotiated to HALF DUPLEX, flow control should not be
2154              * enabled per IEEE 802.3 spec.
2155              */
2156             ret_val = e1000_get_speed_and_duplex(hw, &speed, &duplex);
2157             if(ret_val) {
2158                 DEBUGOUT("Error getting link speed and duplex\n");
2159                 return ret_val;
2160             }
2161
2162             if(duplex == HALF_DUPLEX)
2163                 hw->fc = e1000_fc_none;
2164
2165             /* Now we call a subroutine to actually force the MAC
2166              * controller to use the correct flow control settings.
2167              */
2168             ret_val = e1000_force_mac_fc(hw);
2169             if(ret_val) {
2170                 DEBUGOUT("Error forcing flow control settings\n");
2171                 return ret_val;
2172             }
2173         } else {
2174             DEBUGOUT("Copper PHY and Auto Neg has not completed.\r\n");
2175         }
2176     }
2177     return E1000_SUCCESS;
2178 }
2179
2180 /******************************************************************************
2181  * Checks to see if the link status of the hardware has changed.
2182  *
2183  * hw - Struct containing variables accessed by shared code
2184  *
2185  * Called by any function that needs to check the link status of the adapter.
2186  *****************************************************************************/
2187 int32_t
2188 e1000_check_for_link(struct e1000_hw *hw)
2189 {
2190     uint32_t rxcw = 0;
2191     uint32_t ctrl;
2192     uint32_t status;
2193     uint32_t rctl;
2194     uint32_t icr;
2195     uint32_t signal = 0;
2196     int32_t ret_val;
2197     uint16_t phy_data;
2198
2199     DEBUGFUNC("e1000_check_for_link");
2200
2201     ctrl = E1000_READ_REG(hw, CTRL);
2202     status = E1000_READ_REG(hw, STATUS);
2203
2204     /* On adapters with a MAC newer than 82544, SW Defineable pin 1 will be
2205      * set when the optics detect a signal. On older adapters, it will be
2206      * cleared when there is a signal.  This applies to fiber media only.
2207      */
2208     if((hw->media_type == e1000_media_type_fiber) ||
2209        (hw->media_type == e1000_media_type_internal_serdes)) {
2210         rxcw = E1000_READ_REG(hw, RXCW);
2211
2212         if(hw->media_type == e1000_media_type_fiber) {
2213             signal = (hw->mac_type > e1000_82544) ? E1000_CTRL_SWDPIN1 : 0;
2214             if(status & E1000_STATUS_LU)
2215                 hw->get_link_status = FALSE;
2216         }
2217     }
2218
2219     /* If we have a copper PHY then we only want to go out to the PHY
2220      * registers to see if Auto-Neg has completed and/or if our link
2221      * status has changed.  The get_link_status flag will be set if we
2222      * receive a Link Status Change interrupt or we have Rx Sequence
2223      * Errors.
2224      */
2225     if((hw->media_type == e1000_media_type_copper) && hw->get_link_status) {
2226         /* First we want to see if the MII Status Register reports
2227          * link.  If so, then we want to get the current speed/duplex
2228          * of the PHY.
2229          * Read the register twice since the link bit is sticky.
2230          */
2231         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
2232         if(ret_val)
2233             return ret_val;
2234         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
2235         if(ret_val)
2236             return ret_val;
2237
2238         if(phy_data & MII_SR_LINK_STATUS) {
2239             hw->get_link_status = FALSE;
2240             /* Check if there was DownShift, must be checked immediately after
2241              * link-up */
2242             e1000_check_downshift(hw);
2243
2244             /* If we are on 82544 or 82543 silicon and speed/duplex
2245              * are forced to 10H or 10F, then we will implement the polarity
2246              * reversal workaround.  We disable interrupts first, and upon
2247              * returning, place the devices interrupt state to its previous
2248              * value except for the link status change interrupt which will
2249              * happen due to the execution of this workaround.
2250              */
2251
2252             if((hw->mac_type == e1000_82544 || hw->mac_type == e1000_82543) &&
2253                (!hw->autoneg) &&
2254                (hw->forced_speed_duplex == e1000_10_full ||
2255                 hw->forced_speed_duplex == e1000_10_half)) {
2256                 E1000_WRITE_REG(hw, IMC, 0xffffffff);
2257                 ret_val = e1000_polarity_reversal_workaround(hw);
2258                 icr = E1000_READ_REG(hw, ICR);
2259                 E1000_WRITE_REG(hw, ICS, (icr & ~E1000_ICS_LSC));
2260                 E1000_WRITE_REG(hw, IMS, IMS_ENABLE_MASK);
2261             }
2262
2263         } else {
2264             /* No link detected */
2265             e1000_config_dsp_after_link_change(hw, FALSE);
2266             return 0;
2267         }
2268
2269         /* If we are forcing speed/duplex, then we simply return since
2270          * we have already determined whether we have link or not.
2271          */
2272         if(!hw->autoneg) return -E1000_ERR_CONFIG;
2273
2274         /* optimize the dsp settings for the igp phy */
2275         e1000_config_dsp_after_link_change(hw, TRUE);
2276
2277         /* We have a M88E1000 PHY and Auto-Neg is enabled.  If we
2278          * have Si on board that is 82544 or newer, Auto
2279          * Speed Detection takes care of MAC speed/duplex
2280          * configuration.  So we only need to configure Collision
2281          * Distance in the MAC.  Otherwise, we need to force
2282          * speed/duplex on the MAC to the current PHY speed/duplex
2283          * settings.
2284          */
2285         if(hw->mac_type >= e1000_82544)
2286             e1000_config_collision_dist(hw);
2287         else {
2288             ret_val = e1000_config_mac_to_phy(hw);
2289             if(ret_val) {
2290                 DEBUGOUT("Error configuring MAC to PHY settings\n");
2291                 return ret_val;
2292             }
2293         }
2294
2295         /* Configure Flow Control now that Auto-Neg has completed. First, we
2296          * need to restore the desired flow control settings because we may
2297          * have had to re-autoneg with a different link partner.
2298          */
2299         ret_val = e1000_config_fc_after_link_up(hw);
2300         if(ret_val) {
2301             DEBUGOUT("Error configuring flow control\n");
2302             return ret_val;
2303         }
2304
2305         /* At this point we know that we are on copper and we have
2306          * auto-negotiated link.  These are conditions for checking the link
2307          * partner capability register.  We use the link speed to determine if
2308          * TBI compatibility needs to be turned on or off.  If the link is not
2309          * at gigabit speed, then TBI compatibility is not needed.  If we are
2310          * at gigabit speed, we turn on TBI compatibility.
2311          */
2312         if(hw->tbi_compatibility_en) {
2313             uint16_t speed, duplex;
2314             e1000_get_speed_and_duplex(hw, &speed, &duplex);
2315             if(speed != SPEED_1000) {
2316                 /* If link speed is not set to gigabit speed, we do not need
2317                  * to enable TBI compatibility.
2318                  */
2319                 if(hw->tbi_compatibility_on) {
2320                     /* If we previously were in the mode, turn it off. */
2321                     rctl = E1000_READ_REG(hw, RCTL);
2322                     rctl &= ~E1000_RCTL_SBP;
2323                     E1000_WRITE_REG(hw, RCTL, rctl);
2324                     hw->tbi_compatibility_on = FALSE;
2325                 }
2326             } else {
2327                 /* If TBI compatibility is was previously off, turn it on. For
2328                  * compatibility with a TBI link partner, we will store bad
2329                  * packets. Some frames have an additional byte on the end and
2330                  * will look like CRC errors to to the hardware.
2331                  */
2332                 if(!hw->tbi_compatibility_on) {
2333                     hw->tbi_compatibility_on = TRUE;
2334                     rctl = E1000_READ_REG(hw, RCTL);
2335                     rctl |= E1000_RCTL_SBP;
2336                     E1000_WRITE_REG(hw, RCTL, rctl);
2337                 }
2338             }
2339         }
2340     }
2341     /* If we don't have link (auto-negotiation failed or link partner cannot
2342      * auto-negotiate), the cable is plugged in (we have signal), and our
2343      * link partner is not trying to auto-negotiate with us (we are receiving
2344      * idles or data), we need to force link up. We also need to give
2345      * auto-negotiation time to complete, in case the cable was just plugged
2346      * in. The autoneg_failed flag does this.
2347      */
2348     else if((((hw->media_type == e1000_media_type_fiber) &&
2349               ((ctrl & E1000_CTRL_SWDPIN1) == signal)) ||
2350              (hw->media_type == e1000_media_type_internal_serdes)) &&
2351             (!(status & E1000_STATUS_LU)) &&
2352             (!(rxcw & E1000_RXCW_C))) {
2353         if(hw->autoneg_failed == 0) {
2354             hw->autoneg_failed = 1;
2355             return 0;
2356         }
2357         DEBUGOUT("NOT RXing /C/, disable AutoNeg and force link.\r\n");
2358
2359         /* Disable auto-negotiation in the TXCW register */
2360         E1000_WRITE_REG(hw, TXCW, (hw->txcw & ~E1000_TXCW_ANE));
2361
2362         /* Force link-up and also force full-duplex. */
2363         ctrl = E1000_READ_REG(hw, CTRL);
2364         ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FD);
2365         E1000_WRITE_REG(hw, CTRL, ctrl);
2366
2367         /* Configure Flow Control after forcing link up. */
2368         ret_val = e1000_config_fc_after_link_up(hw);
2369         if(ret_val) {
2370             DEBUGOUT("Error configuring flow control\n");
2371             return ret_val;
2372         }
2373     }
2374     /* If we are forcing link and we are receiving /C/ ordered sets, re-enable
2375      * auto-negotiation in the TXCW register and disable forced link in the
2376      * Device Control register in an attempt to auto-negotiate with our link
2377      * partner.
2378      */
2379     else if(((hw->media_type == e1000_media_type_fiber) ||
2380              (hw->media_type == e1000_media_type_internal_serdes)) &&
2381             (ctrl & E1000_CTRL_SLU) && (rxcw & E1000_RXCW_C)) {
2382         DEBUGOUT("RXing /C/, enable AutoNeg and stop forcing link.\r\n");
2383         E1000_WRITE_REG(hw, TXCW, hw->txcw);
2384         E1000_WRITE_REG(hw, CTRL, (ctrl & ~E1000_CTRL_SLU));
2385
2386         hw->serdes_link_down = FALSE;
2387     }
2388     /* If we force link for non-auto-negotiation switch, check link status
2389      * based on MAC synchronization for internal serdes media type.
2390      */
2391     else if((hw->media_type == e1000_media_type_internal_serdes) &&
2392             !(E1000_TXCW_ANE & E1000_READ_REG(hw, TXCW))) {
2393         /* SYNCH bit and IV bit are sticky. */
2394         udelay(10);
2395         if(E1000_RXCW_SYNCH & E1000_READ_REG(hw, RXCW)) {
2396             if(!(rxcw & E1000_RXCW_IV)) {
2397                 hw->serdes_link_down = FALSE;
2398                 DEBUGOUT("SERDES: Link is up.\n");
2399             }
2400         } else {
2401             hw->serdes_link_down = TRUE;
2402             DEBUGOUT("SERDES: Link is down.\n");
2403         }
2404     }
2405     if((hw->media_type == e1000_media_type_internal_serdes) &&
2406        (E1000_TXCW_ANE & E1000_READ_REG(hw, TXCW))) {
2407         hw->serdes_link_down = !(E1000_STATUS_LU & E1000_READ_REG(hw, STATUS));
2408     }
2409     return E1000_SUCCESS;
2410 }
2411
2412 /******************************************************************************
2413  * Detects the current speed and duplex settings of the hardware.
2414  *
2415  * hw - Struct containing variables accessed by shared code
2416  * speed - Speed of the connection
2417  * duplex - Duplex setting of the connection
2418  *****************************************************************************/
2419 int32_t
2420 e1000_get_speed_and_duplex(struct e1000_hw *hw,
2421                            uint16_t *speed,
2422                            uint16_t *duplex)
2423 {
2424     uint32_t status;
2425     int32_t ret_val;
2426     uint16_t phy_data;
2427
2428     DEBUGFUNC("e1000_get_speed_and_duplex");
2429
2430     if(hw->mac_type >= e1000_82543) {
2431         status = E1000_READ_REG(hw, STATUS);
2432         if(status & E1000_STATUS_SPEED_1000) {
2433             *speed = SPEED_1000;
2434             DEBUGOUT("1000 Mbs, ");
2435         } else if(status & E1000_STATUS_SPEED_100) {
2436             *speed = SPEED_100;
2437             DEBUGOUT("100 Mbs, ");
2438         } else {
2439             *speed = SPEED_10;
2440             DEBUGOUT("10 Mbs, ");
2441         }
2442
2443         if(status & E1000_STATUS_FD) {
2444             *duplex = FULL_DUPLEX;
2445             DEBUGOUT("Full Duplex\r\n");
2446         } else {
2447             *duplex = HALF_DUPLEX;
2448             DEBUGOUT(" Half Duplex\r\n");
2449         }
2450     } else {
2451         DEBUGOUT("1000 Mbs, Full Duplex\r\n");
2452         *speed = SPEED_1000;
2453         *duplex = FULL_DUPLEX;
2454     }
2455
2456     /* IGP01 PHY may advertise full duplex operation after speed downgrade even
2457      * if it is operating at half duplex.  Here we set the duplex settings to
2458      * match the duplex in the link partner's capabilities.
2459      */
2460     if(hw->phy_type == e1000_phy_igp && hw->speed_downgraded) {
2461         ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_EXP, &phy_data);
2462         if(ret_val)
2463             return ret_val;
2464
2465         if(!(phy_data & NWAY_ER_LP_NWAY_CAPS))
2466             *duplex = HALF_DUPLEX;
2467         else {
2468             ret_val = e1000_read_phy_reg(hw, PHY_LP_ABILITY, &phy_data);
2469             if(ret_val)
2470                 return ret_val;
2471             if((*speed == SPEED_100 && !(phy_data & NWAY_LPAR_100TX_FD_CAPS)) ||
2472                (*speed == SPEED_10 && !(phy_data & NWAY_LPAR_10T_FD_CAPS)))
2473                 *duplex = HALF_DUPLEX;
2474         }
2475     }
2476
2477     return E1000_SUCCESS;
2478 }
2479
2480 /******************************************************************************
2481 * Blocks until autoneg completes or times out (~4.5 seconds)
2482 *
2483 * hw - Struct containing variables accessed by shared code
2484 ******************************************************************************/
2485 int32_t
2486 e1000_wait_autoneg(struct e1000_hw *hw)
2487 {
2488     int32_t ret_val;
2489     uint16_t i;
2490     uint16_t phy_data;
2491
2492     DEBUGFUNC("e1000_wait_autoneg");
2493     DEBUGOUT("Waiting for Auto-Neg to complete.\n");
2494
2495     /* We will wait for autoneg to complete or 4.5 seconds to expire. */
2496     for(i = PHY_AUTO_NEG_TIME; i > 0; i--) {
2497         /* Read the MII Status Register and wait for Auto-Neg
2498          * Complete bit to be set.
2499          */
2500         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
2501         if(ret_val)
2502             return ret_val;
2503         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
2504         if(ret_val)
2505             return ret_val;
2506         if(phy_data & MII_SR_AUTONEG_COMPLETE) {
2507             return E1000_SUCCESS;
2508         }
2509         msec_delay(100);
2510     }
2511     return E1000_SUCCESS;
2512 }
2513
2514 /******************************************************************************
2515 * Raises the Management Data Clock
2516 *
2517 * hw - Struct containing variables accessed by shared code
2518 * ctrl - Device control register's current value
2519 ******************************************************************************/
2520 static void
2521 e1000_raise_mdi_clk(struct e1000_hw *hw,
2522                     uint32_t *ctrl)
2523 {
2524     /* Raise the clock input to the Management Data Clock (by setting the MDC
2525      * bit), and then delay 10 microseconds.
2526      */
2527     E1000_WRITE_REG(hw, CTRL, (*ctrl | E1000_CTRL_MDC));
2528     E1000_WRITE_FLUSH(hw);
2529     udelay(10);
2530 }
2531
2532 /******************************************************************************
2533 * Lowers the Management Data Clock
2534 *
2535 * hw - Struct containing variables accessed by shared code
2536 * ctrl - Device control register's current value
2537 ******************************************************************************/
2538 static void
2539 e1000_lower_mdi_clk(struct e1000_hw *hw,
2540                     uint32_t *ctrl)
2541 {
2542     /* Lower the clock input to the Management Data Clock (by clearing the MDC
2543      * bit), and then delay 10 microseconds.
2544      */
2545     E1000_WRITE_REG(hw, CTRL, (*ctrl & ~E1000_CTRL_MDC));
2546     E1000_WRITE_FLUSH(hw);
2547     udelay(10);
2548 }
2549
2550 /******************************************************************************
2551 * Shifts data bits out to the PHY
2552 *
2553 * hw - Struct containing variables accessed by shared code
2554 * data - Data to send out to the PHY
2555 * count - Number of bits to shift out
2556 *
2557 * Bits are shifted out in MSB to LSB order.
2558 ******************************************************************************/
2559 static void
2560 e1000_shift_out_mdi_bits(struct e1000_hw *hw,
2561                          uint32_t data,
2562                          uint16_t count)
2563 {
2564     uint32_t ctrl;
2565     uint32_t mask;
2566
2567     /* We need to shift "count" number of bits out to the PHY. So, the value
2568      * in the "data" parameter will be shifted out to the PHY one bit at a
2569      * time. In order to do this, "data" must be broken down into bits.
2570      */
2571     mask = 0x01;
2572     mask <<= (count - 1);
2573
2574     ctrl = E1000_READ_REG(hw, CTRL);
2575
2576     /* Set MDIO_DIR and MDC_DIR direction bits to be used as output pins. */
2577     ctrl |= (E1000_CTRL_MDIO_DIR | E1000_CTRL_MDC_DIR);
2578
2579     while(mask) {
2580         /* A "1" is shifted out to the PHY by setting the MDIO bit to "1" and
2581          * then raising and lowering the Management Data Clock. A "0" is
2582          * shifted out to the PHY by setting the MDIO bit to "0" and then
2583          * raising and lowering the clock.
2584          */
2585         if(data & mask) ctrl |= E1000_CTRL_MDIO;
2586         else ctrl &= ~E1000_CTRL_MDIO;
2587
2588         E1000_WRITE_REG(hw, CTRL, ctrl);
2589         E1000_WRITE_FLUSH(hw);
2590
2591         udelay(10);
2592
2593         e1000_raise_mdi_clk(hw, &ctrl);
2594         e1000_lower_mdi_clk(hw, &ctrl);
2595
2596         mask = mask >> 1;
2597     }
2598 }
2599
2600 /******************************************************************************
2601 * Shifts data bits in from the PHY
2602 *
2603 * hw - Struct containing variables accessed by shared code
2604 *
2605 * Bits are shifted in in MSB to LSB order.
2606 ******************************************************************************/
2607 static uint16_t
2608 e1000_shift_in_mdi_bits(struct e1000_hw *hw)
2609 {
2610     uint32_t ctrl;
2611     uint16_t data = 0;
2612     uint8_t i;
2613
2614     /* In order to read a register from the PHY, we need to shift in a total
2615      * of 18 bits from the PHY. The first two bit (turnaround) times are used
2616      * to avoid contention on the MDIO pin when a read operation is performed.
2617      * These two bits are ignored by us and thrown away. Bits are "shifted in"
2618      * by raising the input to the Management Data Clock (setting the MDC bit),
2619      * and then reading the value of the MDIO bit.
2620      */
2621     ctrl = E1000_READ_REG(hw, CTRL);
2622
2623     /* Clear MDIO_DIR (SWDPIO1) to indicate this bit is to be used as input. */
2624     ctrl &= ~E1000_CTRL_MDIO_DIR;
2625     ctrl &= ~E1000_CTRL_MDIO;
2626
2627     E1000_WRITE_REG(hw, CTRL, ctrl);
2628     E1000_WRITE_FLUSH(hw);
2629
2630     /* Raise and Lower the clock before reading in the data. This accounts for
2631      * the turnaround bits. The first clock occurred when we clocked out the
2632      * last bit of the Register Address.
2633      */
2634     e1000_raise_mdi_clk(hw, &ctrl);
2635     e1000_lower_mdi_clk(hw, &ctrl);
2636
2637     for(data = 0, i = 0; i < 16; i++) {
2638         data = data << 1;
2639         e1000_raise_mdi_clk(hw, &ctrl);
2640         ctrl = E1000_READ_REG(hw, CTRL);
2641         /* Check to see if we shifted in a "1". */
2642         if(ctrl & E1000_CTRL_MDIO) data |= 1;
2643         e1000_lower_mdi_clk(hw, &ctrl);
2644     }
2645
2646     e1000_raise_mdi_clk(hw, &ctrl);
2647     e1000_lower_mdi_clk(hw, &ctrl);
2648
2649     return data;
2650 }
2651
2652 /*****************************************************************************
2653 * Reads the value from a PHY register, if the value is on a specific non zero
2654 * page, sets the page first.
2655 * hw - Struct containing variables accessed by shared code
2656 * reg_addr - address of the PHY register to read
2657 ******************************************************************************/
2658 int32_t
2659 e1000_read_phy_reg(struct e1000_hw *hw,
2660                    uint32_t reg_addr,
2661                    uint16_t *phy_data)
2662 {
2663     uint32_t ret_val;
2664
2665     DEBUGFUNC("e1000_read_phy_reg");
2666
2667     if((hw->phy_type == e1000_phy_igp || 
2668         hw->phy_type == e1000_phy_igp_2) &&
2669        (reg_addr > MAX_PHY_MULTI_PAGE_REG)) {
2670         ret_val = e1000_write_phy_reg_ex(hw, IGP01E1000_PHY_PAGE_SELECT,
2671                                          (uint16_t)reg_addr);
2672         if(ret_val) {
2673             return ret_val;
2674         }
2675     }
2676
2677     ret_val = e1000_read_phy_reg_ex(hw, MAX_PHY_REG_ADDRESS & reg_addr,
2678                                     phy_data);
2679
2680     return ret_val;
2681 }
2682
2683 int32_t
2684 e1000_read_phy_reg_ex(struct e1000_hw *hw,
2685                       uint32_t reg_addr,
2686                       uint16_t *phy_data)
2687 {
2688     uint32_t i;
2689     uint32_t mdic = 0;
2690     const uint32_t phy_addr = 1;
2691
2692     DEBUGFUNC("e1000_read_phy_reg_ex");
2693
2694     if(reg_addr > MAX_PHY_REG_ADDRESS) {
2695         DEBUGOUT1("PHY Address %d is out of range\n", reg_addr);
2696         return -E1000_ERR_PARAM;
2697     }
2698
2699     if(hw->mac_type > e1000_82543) {
2700         /* Set up Op-code, Phy Address, and register address in the MDI
2701          * Control register.  The MAC will take care of interfacing with the
2702          * PHY to retrieve the desired data.
2703          */
2704         mdic = ((reg_addr << E1000_MDIC_REG_SHIFT) |
2705                 (phy_addr << E1000_MDIC_PHY_SHIFT) |
2706                 (E1000_MDIC_OP_READ));
2707
2708         E1000_WRITE_REG(hw, MDIC, mdic);
2709
2710         /* Poll the ready bit to see if the MDI read completed */
2711         for(i = 0; i < 64; i++) {
2712             udelay(50);
2713             mdic = E1000_READ_REG(hw, MDIC);
2714             if(mdic & E1000_MDIC_READY) break;
2715         }
2716         if(!(mdic & E1000_MDIC_READY)) {
2717             DEBUGOUT("MDI Read did not complete\n");
2718             return -E1000_ERR_PHY;
2719         }
2720         if(mdic & E1000_MDIC_ERROR) {
2721             DEBUGOUT("MDI Error\n");
2722             return -E1000_ERR_PHY;
2723         }
2724         *phy_data = (uint16_t) mdic;
2725     } else {
2726         /* We must first send a preamble through the MDIO pin to signal the
2727          * beginning of an MII instruction.  This is done by sending 32
2728          * consecutive "1" bits.
2729          */
2730         e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
2731
2732         /* Now combine the next few fields that are required for a read
2733          * operation.  We use this method instead of calling the
2734          * e1000_shift_out_mdi_bits routine five different times. The format of
2735          * a MII read instruction consists of a shift out of 14 bits and is
2736          * defined as follows:
2737          *    <Preamble><SOF><Op Code><Phy Addr><Reg Addr>
2738          * followed by a shift in of 18 bits.  This first two bits shifted in
2739          * are TurnAround bits used to avoid contention on the MDIO pin when a
2740          * READ operation is performed.  These two bits are thrown away
2741          * followed by a shift in of 16 bits which contains the desired data.
2742          */
2743         mdic = ((reg_addr) | (phy_addr << 5) |
2744                 (PHY_OP_READ << 10) | (PHY_SOF << 12));
2745
2746         e1000_shift_out_mdi_bits(hw, mdic, 14);
2747
2748         /* Now that we've shifted out the read command to the MII, we need to
2749          * "shift in" the 16-bit value (18 total bits) of the requested PHY
2750          * register address.
2751          */
2752         *phy_data = e1000_shift_in_mdi_bits(hw);
2753     }
2754     return E1000_SUCCESS;
2755 }
2756
2757 /******************************************************************************
2758 * Writes a value to a PHY register
2759 *
2760 * hw - Struct containing variables accessed by shared code
2761 * reg_addr - address of the PHY register to write
2762 * data - data to write to the PHY
2763 ******************************************************************************/
2764 int32_t
2765 e1000_write_phy_reg(struct e1000_hw *hw,
2766                     uint32_t reg_addr,
2767                     uint16_t phy_data)
2768 {
2769     uint32_t ret_val;
2770
2771     DEBUGFUNC("e1000_write_phy_reg");
2772
2773     if((hw->phy_type == e1000_phy_igp || 
2774         hw->phy_type == e1000_phy_igp_2) &&
2775        (reg_addr > MAX_PHY_MULTI_PAGE_REG)) {
2776         ret_val = e1000_write_phy_reg_ex(hw, IGP01E1000_PHY_PAGE_SELECT,
2777                                          (uint16_t)reg_addr);
2778         if(ret_val) {
2779             return ret_val;
2780         }
2781     }
2782
2783     ret_val = e1000_write_phy_reg_ex(hw, MAX_PHY_REG_ADDRESS & reg_addr,
2784                                      phy_data);
2785
2786     return ret_val;
2787 }
2788
2789 int32_t
2790 e1000_write_phy_reg_ex(struct e1000_hw *hw,
2791                     uint32_t reg_addr,
2792                     uint16_t phy_data)
2793 {
2794     uint32_t i;
2795     uint32_t mdic = 0;
2796     const uint32_t phy_addr = 1;
2797
2798     DEBUGFUNC("e1000_write_phy_reg_ex");
2799
2800     if(reg_addr > MAX_PHY_REG_ADDRESS) {
2801         DEBUGOUT1("PHY Address %d is out of range\n", reg_addr);
2802         return -E1000_ERR_PARAM;
2803     }
2804
2805     if(hw->mac_type > e1000_82543) {
2806         /* Set up Op-code, Phy Address, register address, and data intended
2807          * for the PHY register in the MDI Control register.  The MAC will take
2808          * care of interfacing with the PHY to send the desired data.
2809          */
2810         mdic = (((uint32_t) phy_data) |
2811                 (reg_addr << E1000_MDIC_REG_SHIFT) |
2812                 (phy_addr << E1000_MDIC_PHY_SHIFT) |
2813                 (E1000_MDIC_OP_WRITE));
2814
2815         E1000_WRITE_REG(hw, MDIC, mdic);
2816
2817         /* Poll the ready bit to see if the MDI read completed */
2818         for(i = 0; i < 640; i++) {
2819             udelay(5);
2820             mdic = E1000_READ_REG(hw, MDIC);
2821             if(mdic & E1000_MDIC_READY) break;
2822         }
2823         if(!(mdic & E1000_MDIC_READY)) {
2824             DEBUGOUT("MDI Write did not complete\n");
2825             return -E1000_ERR_PHY;
2826         }
2827     } else {
2828         /* We'll need to use the SW defined pins to shift the write command
2829          * out to the PHY. We first send a preamble to the PHY to signal the
2830          * beginning of the MII instruction.  This is done by sending 32
2831          * consecutive "1" bits.
2832          */
2833         e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
2834
2835         /* Now combine the remaining required fields that will indicate a
2836          * write operation. We use this method instead of calling the
2837          * e1000_shift_out_mdi_bits routine for each field in the command. The
2838          * format of a MII write instruction is as follows:
2839          * <Preamble><SOF><Op Code><Phy Addr><Reg Addr><Turnaround><Data>.
2840          */
2841         mdic = ((PHY_TURNAROUND) | (reg_addr << 2) | (phy_addr << 7) |
2842                 (PHY_OP_WRITE << 12) | (PHY_SOF << 14));
2843         mdic <<= 16;
2844         mdic |= (uint32_t) phy_data;
2845
2846         e1000_shift_out_mdi_bits(hw, mdic, 32);
2847     }
2848
2849     return E1000_SUCCESS;
2850 }
2851
2852
2853 /******************************************************************************
2854 * Returns the PHY to the power-on reset state
2855 *
2856 * hw - Struct containing variables accessed by shared code
2857 ******************************************************************************/
2858 int32_t
2859 e1000_phy_hw_reset(struct e1000_hw *hw)
2860 {
2861     uint32_t ctrl, ctrl_ext;
2862     uint32_t led_ctrl;
2863     int32_t ret_val;
2864
2865     DEBUGFUNC("e1000_phy_hw_reset");
2866
2867     /* In the case of the phy reset being blocked, it's not an error, we
2868      * simply return success without performing the reset. */
2869     ret_val = e1000_check_phy_reset_block(hw);
2870     if (ret_val)
2871         return E1000_SUCCESS;
2872
2873     DEBUGOUT("Resetting Phy...\n");
2874
2875     if(hw->mac_type > e1000_82543) {
2876         /* Read the device control register and assert the E1000_CTRL_PHY_RST
2877          * bit. Then, take it out of reset.
2878          */
2879         ctrl = E1000_READ_REG(hw, CTRL);
2880         E1000_WRITE_REG(hw, CTRL, ctrl | E1000_CTRL_PHY_RST);
2881         E1000_WRITE_FLUSH(hw);
2882         msec_delay(10);
2883         E1000_WRITE_REG(hw, CTRL, ctrl);
2884         E1000_WRITE_FLUSH(hw);
2885     } else {
2886         /* Read the Extended Device Control Register, assert the PHY_RESET_DIR
2887          * bit to put the PHY into reset. Then, take it out of reset.
2888          */
2889         ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
2890         ctrl_ext |= E1000_CTRL_EXT_SDP4_DIR;
2891         ctrl_ext &= ~E1000_CTRL_EXT_SDP4_DATA;
2892         E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
2893         E1000_WRITE_FLUSH(hw);
2894         msec_delay(10);
2895         ctrl_ext |= E1000_CTRL_EXT_SDP4_DATA;
2896         E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
2897         E1000_WRITE_FLUSH(hw);
2898     }
2899     udelay(150);
2900
2901     if((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
2902         /* Configure activity LED after PHY reset */
2903         led_ctrl = E1000_READ_REG(hw, LEDCTL);
2904         led_ctrl &= IGP_ACTIVITY_LED_MASK;
2905         led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
2906         E1000_WRITE_REG(hw, LEDCTL, led_ctrl);
2907     }
2908
2909     /* Wait for FW to finish PHY configuration. */
2910     ret_val = e1000_get_phy_cfg_done(hw);
2911
2912     return ret_val;
2913 }
2914
2915 /******************************************************************************
2916 * Resets the PHY
2917 *
2918 * hw - Struct containing variables accessed by shared code
2919 *
2920 * Sets bit 15 of the MII Control regiser
2921 ******************************************************************************/
2922 int32_t
2923 e1000_phy_reset(struct e1000_hw *hw)
2924 {
2925     int32_t ret_val;
2926     uint16_t phy_data;
2927
2928     DEBUGFUNC("e1000_phy_reset");
2929
2930     /* In the case of the phy reset being blocked, it's not an error, we
2931      * simply return success without performing the reset. */
2932     ret_val = e1000_check_phy_reset_block(hw);
2933     if (ret_val)
2934         return E1000_SUCCESS;
2935
2936     switch (hw->mac_type) {
2937     case e1000_82541_rev_2:
2938         ret_val = e1000_phy_hw_reset(hw);
2939         if(ret_val)
2940             return ret_val;
2941         break;
2942     default:
2943         ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
2944         if(ret_val)
2945             return ret_val;
2946
2947         phy_data |= MII_CR_RESET;
2948         ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
2949         if(ret_val)
2950             return ret_val;
2951
2952         udelay(1);
2953         break;
2954     }
2955
2956     if(hw->phy_type == e1000_phy_igp || hw->phy_type == e1000_phy_igp_2)
2957         e1000_phy_init_script(hw);
2958
2959     return E1000_SUCCESS;
2960 }
2961
2962 /******************************************************************************
2963 * Probes the expected PHY address for known PHY IDs
2964 *
2965 * hw - Struct containing variables accessed by shared code
2966 ******************************************************************************/
2967 int32_t
2968 e1000_detect_gig_phy(struct e1000_hw *hw)
2969 {
2970     int32_t phy_init_status, ret_val;
2971     uint16_t phy_id_high, phy_id_low;
2972     boolean_t match = FALSE;
2973
2974     DEBUGFUNC("e1000_detect_gig_phy");
2975
2976     /* Read the PHY ID Registers to identify which PHY is onboard. */
2977     ret_val = e1000_read_phy_reg(hw, PHY_ID1, &phy_id_high);
2978     if(ret_val)
2979         return ret_val;
2980
2981     hw->phy_id = (uint32_t) (phy_id_high << 16);
2982     udelay(20);
2983     ret_val = e1000_read_phy_reg(hw, PHY_ID2, &phy_id_low);
2984     if(ret_val)
2985         return ret_val;
2986
2987     hw->phy_id |= (uint32_t) (phy_id_low & PHY_REVISION_MASK);
2988     hw->phy_revision = (uint32_t) phy_id_low & ~PHY_REVISION_MASK;
2989
2990     switch(hw->mac_type) {
2991     case e1000_82543:
2992         if(hw->phy_id == M88E1000_E_PHY_ID) match = TRUE;
2993         break;
2994     case e1000_82544:
2995         if(hw->phy_id == M88E1000_I_PHY_ID) match = TRUE;
2996         break;
2997     case e1000_82540:
2998     case e1000_82545:
2999     case e1000_82545_rev_3:
3000     case e1000_82546:
3001     case e1000_82546_rev_3:
3002         if(hw->phy_id == M88E1011_I_PHY_ID) match = TRUE;
3003         break;
3004     case e1000_82541:
3005     case e1000_82541_rev_2:
3006     case e1000_82547:
3007     case e1000_82547_rev_2:
3008         if(hw->phy_id == IGP01E1000_I_PHY_ID) match = TRUE;
3009         break;
3010     case e1000_82573:
3011         if(hw->phy_id == M88E1111_I_PHY_ID) match = TRUE;
3012         break;
3013     default:
3014         DEBUGOUT1("Invalid MAC type %d\n", hw->mac_type);
3015         return -E1000_ERR_CONFIG;
3016     }
3017     phy_init_status = e1000_set_phy_type(hw);
3018
3019     if ((match) && (phy_init_status == E1000_SUCCESS)) {
3020         DEBUGOUT1("PHY ID 0x%X detected\n", hw->phy_id);
3021         return E1000_SUCCESS;
3022     }
3023     DEBUGOUT1("Invalid PHY ID 0x%X\n", hw->phy_id);
3024     return -E1000_ERR_PHY;
3025 }
3026
3027 /******************************************************************************
3028 * Resets the PHY's DSP
3029 *
3030 * hw - Struct containing variables accessed by shared code
3031 ******************************************************************************/
3032 static int32_t
3033 e1000_phy_reset_dsp(struct e1000_hw *hw)
3034 {
3035     int32_t ret_val;
3036     DEBUGFUNC("e1000_phy_reset_dsp");
3037
3038     do {
3039         ret_val = e1000_write_phy_reg(hw, 29, 0x001d);
3040         if(ret_val) break;
3041         ret_val = e1000_write_phy_reg(hw, 30, 0x00c1);
3042         if(ret_val) break;
3043         ret_val = e1000_write_phy_reg(hw, 30, 0x0000);
3044         if(ret_val) break;
3045         ret_val = E1000_SUCCESS;
3046     } while(0);
3047
3048     return ret_val;
3049 }
3050
3051 /******************************************************************************
3052 * Get PHY information from various PHY registers for igp PHY only.
3053 *
3054 * hw - Struct containing variables accessed by shared code
3055 * phy_info - PHY information structure
3056 ******************************************************************************/
3057 int32_t
3058 e1000_phy_igp_get_info(struct e1000_hw *hw,
3059                        struct e1000_phy_info *phy_info)
3060 {
3061     int32_t ret_val;
3062     uint16_t phy_data, polarity, min_length, max_length, average;
3063
3064     DEBUGFUNC("e1000_phy_igp_get_info");
3065
3066     /* The downshift status is checked only once, after link is established,
3067      * and it stored in the hw->speed_downgraded parameter. */
3068     phy_info->downshift = (e1000_downshift)hw->speed_downgraded;
3069
3070     /* IGP01E1000 does not need to support it. */
3071     phy_info->extended_10bt_distance = e1000_10bt_ext_dist_enable_normal;
3072
3073     /* IGP01E1000 always correct polarity reversal */
3074     phy_info->polarity_correction = e1000_polarity_reversal_enabled;
3075
3076     /* Check polarity status */
3077     ret_val = e1000_check_polarity(hw, &polarity);
3078     if(ret_val)
3079         return ret_val;
3080
3081     phy_info->cable_polarity = polarity;
3082
3083     ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_STATUS, &phy_data);
3084     if(ret_val)
3085         return ret_val;
3086
3087     phy_info->mdix_mode = (phy_data & IGP01E1000_PSSR_MDIX) >>
3088                           IGP01E1000_PSSR_MDIX_SHIFT;
3089
3090     if((phy_data & IGP01E1000_PSSR_SPEED_MASK) ==
3091        IGP01E1000_PSSR_SPEED_1000MBPS) {
3092         /* Local/Remote Receiver Information are only valid at 1000 Mbps */
3093         ret_val = e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_data);
3094         if(ret_val)
3095             return ret_val;
3096
3097         phy_info->local_rx = (phy_data & SR_1000T_LOCAL_RX_STATUS) >>
3098                              SR_1000T_LOCAL_RX_STATUS_SHIFT;
3099         phy_info->remote_rx = (phy_data & SR_1000T_REMOTE_RX_STATUS) >>
3100                               SR_1000T_REMOTE_RX_STATUS_SHIFT;
3101
3102         /* Get cable length */
3103         ret_val = e1000_get_cable_length(hw, &min_length, &max_length);
3104         if(ret_val)
3105             return ret_val;
3106
3107         /* Translate to old method */
3108         average = (max_length + min_length) / 2;
3109
3110         if(average <= e1000_igp_cable_length_50)
3111             phy_info->cable_length = e1000_cable_length_50;
3112         else if(average <= e1000_igp_cable_length_80)
3113             phy_info->cable_length = e1000_cable_length_50_80;
3114         else if(average <= e1000_igp_cable_length_110)
3115             phy_info->cable_length = e1000_cable_length_80_110;
3116         else if(average <= e1000_igp_cable_length_140)
3117             phy_info->cable_length = e1000_cable_length_110_140;
3118         else
3119             phy_info->cable_length = e1000_cable_length_140;
3120     }
3121
3122     return E1000_SUCCESS;
3123 }
3124
3125 /******************************************************************************
3126 * Get PHY information from various PHY registers fot m88 PHY only.
3127 *
3128 * hw - Struct containing variables accessed by shared code
3129 * phy_info - PHY information structure
3130 ******************************************************************************/
3131 int32_t
3132 e1000_phy_m88_get_info(struct e1000_hw *hw,
3133                        struct e1000_phy_info *phy_info)
3134 {
3135     int32_t ret_val;
3136     uint16_t phy_data, polarity;
3137
3138     DEBUGFUNC("e1000_phy_m88_get_info");
3139
3140     /* The downshift status is checked only once, after link is established,
3141      * and it stored in the hw->speed_downgraded parameter. */
3142     phy_info->downshift = (e1000_downshift)hw->speed_downgraded;
3143
3144     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
3145     if(ret_val)
3146         return ret_val;
3147
3148     phy_info->extended_10bt_distance =
3149         (phy_data & M88E1000_PSCR_10BT_EXT_DIST_ENABLE) >>
3150         M88E1000_PSCR_10BT_EXT_DIST_ENABLE_SHIFT;
3151     phy_info->polarity_correction =
3152         (phy_data & M88E1000_PSCR_POLARITY_REVERSAL) >>
3153         M88E1000_PSCR_POLARITY_REVERSAL_SHIFT;
3154
3155     /* Check polarity status */
3156     ret_val = e1000_check_polarity(hw, &polarity);
3157     if(ret_val)
3158         return ret_val; 
3159     phy_info->cable_polarity = polarity;
3160
3161     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data);
3162     if(ret_val)
3163         return ret_val;
3164
3165     phy_info->mdix_mode = (phy_data & M88E1000_PSSR_MDIX) >>
3166                           M88E1000_PSSR_MDIX_SHIFT;
3167
3168     if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_1000MBS) {
3169         /* Cable Length Estimation and Local/Remote Receiver Information
3170          * are only valid at 1000 Mbps.
3171          */
3172         phy_info->cable_length = ((phy_data & M88E1000_PSSR_CABLE_LENGTH) >>
3173                                   M88E1000_PSSR_CABLE_LENGTH_SHIFT);
3174
3175         ret_val = e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_data);
3176         if(ret_val)
3177             return ret_val;
3178
3179         phy_info->local_rx = (phy_data & SR_1000T_LOCAL_RX_STATUS) >>
3180                              SR_1000T_LOCAL_RX_STATUS_SHIFT;
3181
3182         phy_info->remote_rx = (phy_data & SR_1000T_REMOTE_RX_STATUS) >>
3183                               SR_1000T_REMOTE_RX_STATUS_SHIFT;
3184     }
3185
3186     return E1000_SUCCESS;
3187 }
3188
3189 /******************************************************************************
3190 * Get PHY information from various PHY registers
3191 *
3192 * hw - Struct containing variables accessed by shared code
3193 * phy_info - PHY information structure
3194 ******************************************************************************/
3195 int32_t
3196 e1000_phy_get_info(struct e1000_hw *hw,
3197                    struct e1000_phy_info *phy_info)
3198 {
3199     int32_t ret_val;
3200     uint16_t phy_data;
3201
3202     DEBUGFUNC("e1000_phy_get_info");
3203
3204     phy_info->cable_length = e1000_cable_length_undefined;
3205     phy_info->extended_10bt_distance = e1000_10bt_ext_dist_enable_undefined;
3206     phy_info->cable_polarity = e1000_rev_polarity_undefined;
3207     phy_info->downshift = e1000_downshift_undefined;
3208     phy_info->polarity_correction = e1000_polarity_reversal_undefined;
3209     phy_info->mdix_mode = e1000_auto_x_mode_undefined;
3210     phy_info->local_rx = e1000_1000t_rx_status_undefined;
3211     phy_info->remote_rx = e1000_1000t_rx_status_undefined;
3212
3213     if(hw->media_type != e1000_media_type_copper) {
3214         DEBUGOUT("PHY info is only valid for copper media\n");
3215         return -E1000_ERR_CONFIG;
3216     }
3217
3218     ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
3219     if(ret_val)
3220         return ret_val;
3221
3222     ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
3223     if(ret_val)
3224         return ret_val;
3225
3226     if((phy_data & MII_SR_LINK_STATUS) != MII_SR_LINK_STATUS) {
3227         DEBUGOUT("PHY info is only valid if link is up\n");
3228         return -E1000_ERR_CONFIG;
3229     }
3230
3231     if(hw->phy_type == e1000_phy_igp ||
3232         hw->phy_type == e1000_phy_igp_2)
3233         return e1000_phy_igp_get_info(hw, phy_info);
3234     else
3235         return e1000_phy_m88_get_info(hw, phy_info);
3236 }
3237
3238 int32_t
3239 e1000_validate_mdi_setting(struct e1000_hw *hw)
3240 {
3241     DEBUGFUNC("e1000_validate_mdi_settings");
3242
3243     if(!hw->autoneg && (hw->mdix == 0 || hw->mdix == 3)) {
3244         DEBUGOUT("Invalid MDI setting detected\n");
3245         hw->mdix = 1;
3246         return -E1000_ERR_CONFIG;
3247     }
3248     return E1000_SUCCESS;
3249 }
3250
3251
3252 /******************************************************************************
3253  * Sets up eeprom variables in the hw struct.  Must be called after mac_type
3254  * is configured.
3255  *
3256  * hw - Struct containing variables accessed by shared code
3257  *****************************************************************************/
3258 int32_t
3259 e1000_init_eeprom_params(struct e1000_hw *hw)
3260 {
3261     struct e1000_eeprom_info *eeprom = &hw->eeprom;
3262     uint32_t eecd = E1000_READ_REG(hw, EECD);
3263     int32_t ret_val = E1000_SUCCESS;
3264     uint16_t eeprom_size;
3265
3266     DEBUGFUNC("e1000_init_eeprom_params");
3267
3268     switch (hw->mac_type) {
3269     case e1000_82542_rev2_0:
3270     case e1000_82542_rev2_1:
3271     case e1000_82543:
3272     case e1000_82544:
3273         eeprom->type = e1000_eeprom_microwire;
3274         eeprom->word_size = 64;
3275         eeprom->opcode_bits = 3;
3276         eeprom->address_bits = 6;
3277         eeprom->delay_usec = 50;
3278         eeprom->use_eerd = FALSE;
3279         eeprom->use_eewr = FALSE;
3280         break;
3281     case e1000_82540:
3282     case e1000_82545:
3283     case e1000_82545_rev_3:
3284     case e1000_82546:
3285     case e1000_82546_rev_3:
3286         eeprom->type = e1000_eeprom_microwire;
3287         eeprom->opcode_bits = 3;
3288         eeprom->delay_usec = 50;
3289         if(eecd & E1000_EECD_SIZE) {
3290             eeprom->word_size = 256;
3291             eeprom->address_bits = 8;
3292         } else {
3293             eeprom->word_size = 64;
3294             eeprom->address_bits = 6;
3295         }
3296         eeprom->use_eerd = FALSE;
3297         eeprom->use_eewr = FALSE;
3298         break;
3299     case e1000_82541:
3300     case e1000_82541_rev_2:
3301     case e1000_82547:
3302     case e1000_82547_rev_2:
3303         if (eecd & E1000_EECD_TYPE) {
3304             eeprom->type = e1000_eeprom_spi;
3305             eeprom->opcode_bits = 8;
3306             eeprom->delay_usec = 1;
3307             if (eecd & E1000_EECD_ADDR_BITS) {
3308                 eeprom->page_size = 32;
3309                 eeprom->address_bits = 16;
3310             } else {
3311                 eeprom->page_size = 8;
3312                 eeprom->address_bits = 8;
3313             }
3314         } else {
3315             eeprom->type = e1000_eeprom_microwire;
3316             eeprom->opcode_bits = 3;
3317             eeprom->delay_usec = 50;
3318             if (eecd & E1000_EECD_ADDR_BITS) {
3319                 eeprom->word_size = 256;
3320                 eeprom->address_bits = 8;
3321             } else {
3322                 eeprom->word_size = 64;
3323                 eeprom->address_bits = 6;
3324             }
3325         }
3326         eeprom->use_eerd = FALSE;
3327         eeprom->use_eewr = FALSE;
3328         break;
3329     case e1000_82573:
3330         eeprom->type = e1000_eeprom_spi;
3331         eeprom->opcode_bits = 8;
3332         eeprom->delay_usec = 1;
3333         if (eecd & E1000_EECD_ADDR_BITS) {
3334             eeprom->page_size = 32;
3335             eeprom->address_bits = 16;
3336         } else {
3337             eeprom->page_size = 8;
3338             eeprom->address_bits = 8;
3339         }
3340         eeprom->use_eerd = TRUE;
3341         eeprom->use_eewr = TRUE;
3342         if(e1000_is_onboard_nvm_eeprom(hw) == FALSE) {
3343             eeprom->type = e1000_eeprom_flash;
3344             eeprom->word_size = 2048;
3345
3346             /* Ensure that the Autonomous FLASH update bit is cleared due to
3347              * Flash update issue on parts which use a FLASH for NVM. */
3348             eecd &= ~E1000_EECD_AUPDEN;
3349             E1000_WRITE_REG(hw, EECD, eecd);
3350         }
3351         break;
3352     default:
3353         break;
3354     }
3355
3356     if (eeprom->type == e1000_eeprom_spi) {
3357         /* eeprom_size will be an enum [0..8] that maps to eeprom sizes 128B to
3358          * 32KB (incremented by powers of 2).
3359          */
3360         if(hw->mac_type <= e1000_82547_rev_2) {
3361             /* Set to default value for initial eeprom read. */
3362             eeprom->word_size = 64;
3363             ret_val = e1000_read_eeprom(hw, EEPROM_CFG, 1, &eeprom_size);
3364             if(ret_val)
3365                 return ret_val;
3366             eeprom_size = (eeprom_size & EEPROM_SIZE_MASK) >> EEPROM_SIZE_SHIFT;
3367             /* 256B eeprom size was not supported in earlier hardware, so we
3368              * bump eeprom_size up one to ensure that "1" (which maps to 256B)
3369              * is never the result used in the shifting logic below. */
3370             if(eeprom_size)
3371                 eeprom_size++;
3372         } else {
3373             eeprom_size = (uint16_t)((eecd & E1000_EECD_SIZE_EX_MASK) >>
3374                           E1000_EECD_SIZE_EX_SHIFT);
3375         }
3376
3377         eeprom->word_size = 1 << (eeprom_size + EEPROM_WORD_SIZE_SHIFT);
3378     }
3379     return ret_val;
3380 }
3381
3382 /******************************************************************************
3383  * Raises the EEPROM's clock input.
3384  *
3385  * hw - Struct containing variables accessed by shared code
3386  * eecd - EECD's current value
3387  *****************************************************************************/
3388 static void
3389 e1000_raise_ee_clk(struct e1000_hw *hw,
3390                    uint32_t *eecd)
3391 {