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