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