e1000: add ich8lan core functions
[safe/jmp/linux-2.6] / drivers / net / e1000 / e1000_hw.c
1 /*******************************************************************************
2
3   
4   Copyright(c) 1999 - 2006 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   e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
26   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27
28 *******************************************************************************/
29
30 /* e1000_hw.c
31  * Shared functions for accessing and configuring the MAC
32  */
33
34 #include "e1000_hw.h"
35
36 static int32_t e1000_set_phy_type(struct e1000_hw *hw);
37 static void e1000_phy_init_script(struct e1000_hw *hw);
38 static int32_t e1000_setup_copper_link(struct e1000_hw *hw);
39 static int32_t e1000_setup_fiber_serdes_link(struct e1000_hw *hw);
40 static int32_t e1000_adjust_serdes_amplitude(struct e1000_hw *hw);
41 static int32_t e1000_phy_force_speed_duplex(struct e1000_hw *hw);
42 static int32_t e1000_config_mac_to_phy(struct e1000_hw *hw);
43 static void e1000_raise_mdi_clk(struct e1000_hw *hw, uint32_t *ctrl);
44 static void e1000_lower_mdi_clk(struct e1000_hw *hw, uint32_t *ctrl);
45 static void e1000_shift_out_mdi_bits(struct e1000_hw *hw, uint32_t data,
46                                      uint16_t count);
47 static uint16_t e1000_shift_in_mdi_bits(struct e1000_hw *hw);
48 static int32_t e1000_phy_reset_dsp(struct e1000_hw *hw);
49 static int32_t e1000_write_eeprom_spi(struct e1000_hw *hw, uint16_t offset,
50                                       uint16_t words, uint16_t *data);
51 static int32_t e1000_write_eeprom_microwire(struct e1000_hw *hw,
52                                             uint16_t offset, uint16_t words,
53                                             uint16_t *data);
54 static int32_t e1000_spi_eeprom_ready(struct e1000_hw *hw);
55 static void e1000_raise_ee_clk(struct e1000_hw *hw, uint32_t *eecd);
56 static void e1000_lower_ee_clk(struct e1000_hw *hw, uint32_t *eecd);
57 static void e1000_shift_out_ee_bits(struct e1000_hw *hw, uint16_t data,
58                                     uint16_t count);
59 static int32_t e1000_write_phy_reg_ex(struct e1000_hw *hw, uint32_t reg_addr,
60                                       uint16_t phy_data);
61 static int32_t e1000_read_phy_reg_ex(struct e1000_hw *hw,uint32_t reg_addr,
62                                      uint16_t *phy_data);
63 static uint16_t e1000_shift_in_ee_bits(struct e1000_hw *hw, uint16_t count);
64 static int32_t e1000_acquire_eeprom(struct e1000_hw *hw);
65 static void e1000_release_eeprom(struct e1000_hw *hw);
66 static void e1000_standby_eeprom(struct e1000_hw *hw);
67 static int32_t e1000_set_vco_speed(struct e1000_hw *hw);
68 static int32_t e1000_polarity_reversal_workaround(struct e1000_hw *hw);
69 static int32_t e1000_set_phy_mode(struct e1000_hw *hw);
70 static int32_t e1000_host_if_read_cookie(struct e1000_hw *hw, uint8_t *buffer);
71 static uint8_t e1000_calculate_mng_checksum(char *buffer, uint32_t length);
72 static uint8_t e1000_arc_subsystem_valid(struct e1000_hw *hw);
73 static int32_t e1000_check_downshift(struct e1000_hw *hw);
74 static int32_t e1000_check_polarity(struct e1000_hw *hw, uint16_t *polarity);
75 static void e1000_clear_hw_cntrs(struct e1000_hw *hw);
76 static void e1000_clear_vfta(struct e1000_hw *hw);
77 static int32_t e1000_commit_shadow_ram(struct e1000_hw *hw);
78 static int32_t e1000_config_dsp_after_link_change(struct e1000_hw *hw,
79                                                   boolean_t link_up);
80 static int32_t e1000_config_fc_after_link_up(struct e1000_hw *hw);
81 static int32_t e1000_detect_gig_phy(struct e1000_hw *hw);
82 static int32_t e1000_get_auto_rd_done(struct e1000_hw *hw);
83 static int32_t e1000_get_cable_length(struct e1000_hw *hw,
84                                       uint16_t *min_length,
85                                       uint16_t *max_length);
86 static int32_t e1000_get_hw_eeprom_semaphore(struct e1000_hw *hw);
87 static int32_t e1000_get_phy_cfg_done(struct e1000_hw *hw);
88 static int32_t e1000_id_led_init(struct e1000_hw * hw);
89 static void e1000_init_rx_addrs(struct e1000_hw *hw);
90 static boolean_t e1000_is_onboard_nvm_eeprom(struct e1000_hw *hw);
91 static int32_t e1000_poll_eerd_eewr_done(struct e1000_hw *hw, int eerd);
92 static void e1000_put_hw_eeprom_semaphore(struct e1000_hw *hw);
93 static int32_t e1000_read_eeprom_eerd(struct e1000_hw *hw, uint16_t offset,
94                                       uint16_t words, uint16_t *data);
95 static int32_t e1000_set_d0_lplu_state(struct e1000_hw *hw, boolean_t active);
96 static int32_t e1000_set_d3_lplu_state(struct e1000_hw *hw, boolean_t active);
97 static int32_t e1000_wait_autoneg(struct e1000_hw *hw);
98
99 static void e1000_write_reg_io(struct e1000_hw *hw, uint32_t offset,
100                                uint32_t value);
101
102 #define E1000_WRITE_REG_IO(a, reg, val) \
103             e1000_write_reg_io((a), E1000_##reg, val)
104 static int32_t e1000_configure_kmrn_for_10_100(struct e1000_hw *hw);
105 static int32_t e1000_configure_kmrn_for_1000(struct e1000_hw *hw);
106
107 /* IGP cable length table */
108 static const
109 uint16_t e1000_igp_cable_length_table[IGP01E1000_AGC_LENGTH_TABLE_SIZE] =
110     { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
111       5, 10, 10, 10, 10, 10, 10, 10, 20, 20, 20, 20, 20, 25, 25, 25,
112       25, 25, 25, 25, 30, 30, 30, 30, 40, 40, 40, 40, 40, 40, 40, 40,
113       40, 50, 50, 50, 50, 50, 50, 50, 60, 60, 60, 60, 60, 60, 60, 60,
114       60, 70, 70, 70, 70, 70, 70, 80, 80, 80, 80, 80, 80, 90, 90, 90,
115       90, 90, 90, 90, 90, 90, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
116       100, 100, 100, 100, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110,
117       110, 110, 110, 110, 110, 110, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120};
118
119 static const
120 uint16_t e1000_igp_2_cable_length_table[IGP02E1000_AGC_LENGTH_TABLE_SIZE] =
121     { 0, 0, 0, 0, 0, 0, 0, 0, 3, 5, 8, 11, 13, 16, 18, 21,
122       0, 0, 0, 3, 6, 10, 13, 16, 19, 23, 26, 29, 32, 35, 38, 41,
123       6, 10, 14, 18, 22, 26, 30, 33, 37, 41, 44, 48, 51, 54, 58, 61,
124       21, 26, 31, 35, 40, 44, 49, 53, 57, 61, 65, 68, 72, 75, 79, 82,
125       40, 45, 51, 56, 61, 66, 70, 75, 79, 83, 87, 91, 94, 98, 101, 104,
126       60, 66, 72, 77, 82, 87, 92, 96, 100, 104, 108, 111, 114, 117, 119, 121,
127       83, 89, 95, 100, 105, 109, 113, 116, 119, 122, 124,
128       104, 109, 114, 118, 121, 124};
129
130
131 /******************************************************************************
132  * Set the phy type member in the hw struct.
133  *
134  * hw - Struct containing variables accessed by shared code
135  *****************************************************************************/
136 int32_t
137 e1000_set_phy_type(struct e1000_hw *hw)
138 {
139     DEBUGFUNC("e1000_set_phy_type");
140
141     if(hw->mac_type == e1000_undefined)
142         return -E1000_ERR_PHY_TYPE;
143
144     switch(hw->phy_id) {
145     case M88E1000_E_PHY_ID:
146     case M88E1000_I_PHY_ID:
147     case M88E1011_I_PHY_ID:
148     case M88E1111_I_PHY_ID:
149         hw->phy_type = e1000_phy_m88;
150         break;
151     case IGP01E1000_I_PHY_ID:
152         if(hw->mac_type == e1000_82541 ||
153            hw->mac_type == e1000_82541_rev_2 ||
154            hw->mac_type == e1000_82547 ||
155            hw->mac_type == e1000_82547_rev_2) {
156             hw->phy_type = e1000_phy_igp;
157             break;
158         }
159     case GG82563_E_PHY_ID:
160         if (hw->mac_type == e1000_80003es2lan) {
161             hw->phy_type = e1000_phy_gg82563;
162             break;
163         }
164         /* Fall Through */
165     default:
166         /* Should never have loaded on this device */
167         hw->phy_type = e1000_phy_undefined;
168         return -E1000_ERR_PHY_TYPE;
169     }
170
171     return E1000_SUCCESS;
172 }
173
174 /******************************************************************************
175  * IGP phy init script - initializes the GbE PHY
176  *
177  * hw - Struct containing variables accessed by shared code
178  *****************************************************************************/
179 static void
180 e1000_phy_init_script(struct e1000_hw *hw)
181 {
182     uint32_t ret_val;
183     uint16_t phy_saved_data;
184
185     DEBUGFUNC("e1000_phy_init_script");
186
187     if(hw->phy_init_script) {
188         msec_delay(20);
189
190         /* Save off the current value of register 0x2F5B to be restored at
191          * the end of this routine. */
192         ret_val = e1000_read_phy_reg(hw, 0x2F5B, &phy_saved_data);
193
194         /* Disabled the PHY transmitter */
195         e1000_write_phy_reg(hw, 0x2F5B, 0x0003);
196
197         msec_delay(20);
198
199         e1000_write_phy_reg(hw,0x0000,0x0140);
200
201         msec_delay(5);
202
203         switch(hw->mac_type) {
204         case e1000_82541:
205         case e1000_82547:
206             e1000_write_phy_reg(hw, 0x1F95, 0x0001);
207
208             e1000_write_phy_reg(hw, 0x1F71, 0xBD21);
209
210             e1000_write_phy_reg(hw, 0x1F79, 0x0018);
211
212             e1000_write_phy_reg(hw, 0x1F30, 0x1600);
213
214             e1000_write_phy_reg(hw, 0x1F31, 0x0014);
215
216             e1000_write_phy_reg(hw, 0x1F32, 0x161C);
217
218             e1000_write_phy_reg(hw, 0x1F94, 0x0003);
219
220             e1000_write_phy_reg(hw, 0x1F96, 0x003F);
221
222             e1000_write_phy_reg(hw, 0x2010, 0x0008);
223             break;
224
225         case e1000_82541_rev_2:
226         case e1000_82547_rev_2:
227             e1000_write_phy_reg(hw, 0x1F73, 0x0099);
228             break;
229         default:
230             break;
231         }
232
233         e1000_write_phy_reg(hw, 0x0000, 0x3300);
234
235         msec_delay(20);
236
237         /* Now enable the transmitter */
238         e1000_write_phy_reg(hw, 0x2F5B, phy_saved_data);
239
240         if(hw->mac_type == e1000_82547) {
241             uint16_t fused, fine, coarse;
242
243             /* Move to analog registers page */
244             e1000_read_phy_reg(hw, IGP01E1000_ANALOG_SPARE_FUSE_STATUS, &fused);
245
246             if(!(fused & IGP01E1000_ANALOG_SPARE_FUSE_ENABLED)) {
247                 e1000_read_phy_reg(hw, IGP01E1000_ANALOG_FUSE_STATUS, &fused);
248
249                 fine = fused & IGP01E1000_ANALOG_FUSE_FINE_MASK;
250                 coarse = fused & IGP01E1000_ANALOG_FUSE_COARSE_MASK;
251
252                 if(coarse > IGP01E1000_ANALOG_FUSE_COARSE_THRESH) {
253                     coarse -= IGP01E1000_ANALOG_FUSE_COARSE_10;
254                     fine -= IGP01E1000_ANALOG_FUSE_FINE_1;
255                 } else if(coarse == IGP01E1000_ANALOG_FUSE_COARSE_THRESH)
256                     fine -= IGP01E1000_ANALOG_FUSE_FINE_10;
257
258                 fused = (fused & IGP01E1000_ANALOG_FUSE_POLY_MASK) |
259                         (fine & IGP01E1000_ANALOG_FUSE_FINE_MASK) |
260                         (coarse & IGP01E1000_ANALOG_FUSE_COARSE_MASK);
261
262                 e1000_write_phy_reg(hw, IGP01E1000_ANALOG_FUSE_CONTROL, fused);
263                 e1000_write_phy_reg(hw, IGP01E1000_ANALOG_FUSE_BYPASS,
264                                     IGP01E1000_ANALOG_FUSE_ENABLE_SW_CONTROL);
265             }
266         }
267     }
268 }
269
270 /******************************************************************************
271  * Set the mac type member in the hw struct.
272  *
273  * hw - Struct containing variables accessed by shared code
274  *****************************************************************************/
275 int32_t
276 e1000_set_mac_type(struct e1000_hw *hw)
277 {
278     DEBUGFUNC("e1000_set_mac_type");
279
280     switch (hw->device_id) {
281     case E1000_DEV_ID_82542:
282         switch (hw->revision_id) {
283         case E1000_82542_2_0_REV_ID:
284             hw->mac_type = e1000_82542_rev2_0;
285             break;
286         case E1000_82542_2_1_REV_ID:
287             hw->mac_type = e1000_82542_rev2_1;
288             break;
289         default:
290             /* Invalid 82542 revision ID */
291             return -E1000_ERR_MAC_TYPE;
292         }
293         break;
294     case E1000_DEV_ID_82543GC_FIBER:
295     case E1000_DEV_ID_82543GC_COPPER:
296         hw->mac_type = e1000_82543;
297         break;
298     case E1000_DEV_ID_82544EI_COPPER:
299     case E1000_DEV_ID_82544EI_FIBER:
300     case E1000_DEV_ID_82544GC_COPPER:
301     case E1000_DEV_ID_82544GC_LOM:
302         hw->mac_type = e1000_82544;
303         break;
304     case E1000_DEV_ID_82540EM:
305     case E1000_DEV_ID_82540EM_LOM:
306     case E1000_DEV_ID_82540EP:
307     case E1000_DEV_ID_82540EP_LOM:
308     case E1000_DEV_ID_82540EP_LP:
309         hw->mac_type = e1000_82540;
310         break;
311     case E1000_DEV_ID_82545EM_COPPER:
312     case E1000_DEV_ID_82545EM_FIBER:
313         hw->mac_type = e1000_82545;
314         break;
315     case E1000_DEV_ID_82545GM_COPPER:
316     case E1000_DEV_ID_82545GM_FIBER:
317     case E1000_DEV_ID_82545GM_SERDES:
318         hw->mac_type = e1000_82545_rev_3;
319         break;
320     case E1000_DEV_ID_82546EB_COPPER:
321     case E1000_DEV_ID_82546EB_FIBER:
322     case E1000_DEV_ID_82546EB_QUAD_COPPER:
323         hw->mac_type = e1000_82546;
324         break;
325     case E1000_DEV_ID_82546GB_COPPER:
326     case E1000_DEV_ID_82546GB_FIBER:
327     case E1000_DEV_ID_82546GB_SERDES:
328     case E1000_DEV_ID_82546GB_PCIE:
329     case E1000_DEV_ID_82546GB_QUAD_COPPER:
330     case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
331         hw->mac_type = e1000_82546_rev_3;
332         break;
333     case E1000_DEV_ID_82541EI:
334     case E1000_DEV_ID_82541EI_MOBILE:
335         hw->mac_type = e1000_82541;
336         break;
337     case E1000_DEV_ID_82541ER:
338     case E1000_DEV_ID_82541GI:
339     case E1000_DEV_ID_82541GI_LF:
340     case E1000_DEV_ID_82541GI_MOBILE:
341         hw->mac_type = e1000_82541_rev_2;
342         break;
343     case E1000_DEV_ID_82547EI:
344         hw->mac_type = e1000_82547;
345         break;
346     case E1000_DEV_ID_82547GI:
347         hw->mac_type = e1000_82547_rev_2;
348         break;
349     case E1000_DEV_ID_82571EB_COPPER:
350     case E1000_DEV_ID_82571EB_FIBER:
351     case E1000_DEV_ID_82571EB_SERDES:
352             hw->mac_type = e1000_82571;
353         break;
354     case E1000_DEV_ID_82572EI_COPPER:
355     case E1000_DEV_ID_82572EI_FIBER:
356     case E1000_DEV_ID_82572EI_SERDES:
357         hw->mac_type = e1000_82572;
358         break;
359     case E1000_DEV_ID_82573E:
360     case E1000_DEV_ID_82573E_IAMT:
361     case E1000_DEV_ID_82573L:
362         hw->mac_type = e1000_82573;
363         break;
364     case E1000_DEV_ID_80003ES2LAN_COPPER_DPT:
365     case E1000_DEV_ID_80003ES2LAN_SERDES_DPT:
366         hw->mac_type = e1000_80003es2lan;
367         break;
368     default:
369         /* Should never have loaded on this device */
370         return -E1000_ERR_MAC_TYPE;
371     }
372
373     switch(hw->mac_type) {
374     case e1000_80003es2lan:
375         hw->swfw_sync_present = TRUE;
376         /* fall through */
377     case e1000_82571:
378     case e1000_82572:
379     case e1000_82573:
380         hw->eeprom_semaphore_present = TRUE;
381         /* fall through */
382     case e1000_82541:
383     case e1000_82547:
384     case e1000_82541_rev_2:
385     case e1000_82547_rev_2:
386         hw->asf_firmware_present = TRUE;
387         break;
388     default:
389         break;
390     }
391
392     return E1000_SUCCESS;
393 }
394
395 /*****************************************************************************
396  * Set media type and TBI compatibility.
397  *
398  * hw - Struct containing variables accessed by shared code
399  * **************************************************************************/
400 void
401 e1000_set_media_type(struct e1000_hw *hw)
402 {
403     uint32_t status;
404
405     DEBUGFUNC("e1000_set_media_type");
406
407     if(hw->mac_type != e1000_82543) {
408         /* tbi_compatibility is only valid on 82543 */
409         hw->tbi_compatibility_en = FALSE;
410     }
411
412     switch (hw->device_id) {
413     case E1000_DEV_ID_82545GM_SERDES:
414     case E1000_DEV_ID_82546GB_SERDES:
415     case E1000_DEV_ID_82571EB_SERDES:
416     case E1000_DEV_ID_82572EI_SERDES:
417     case E1000_DEV_ID_80003ES2LAN_SERDES_DPT:
418         hw->media_type = e1000_media_type_internal_serdes;
419         break;
420     default:
421         switch (hw->mac_type) {
422         case e1000_82542_rev2_0:
423         case e1000_82542_rev2_1:
424             hw->media_type = e1000_media_type_fiber;
425             break;
426         case e1000_82573:
427             /* The STATUS_TBIMODE bit is reserved or reused for the this
428              * device.
429              */
430             hw->media_type = e1000_media_type_copper;
431             break;
432         default:
433             status = E1000_READ_REG(hw, STATUS);
434             if (status & E1000_STATUS_TBIMODE) {
435                 hw->media_type = e1000_media_type_fiber;
436                 /* tbi_compatibility not valid on fiber */
437                 hw->tbi_compatibility_en = FALSE;
438             } else {
439                 hw->media_type = e1000_media_type_copper;
440             }
441             break;
442         }
443     }
444 }
445
446 /******************************************************************************
447  * Reset the transmit and receive units; mask and clear all interrupts.
448  *
449  * hw - Struct containing variables accessed by shared code
450  *****************************************************************************/
451 int32_t
452 e1000_reset_hw(struct e1000_hw *hw)
453 {
454     uint32_t ctrl;
455     uint32_t ctrl_ext;
456     uint32_t icr;
457     uint32_t manc;
458     uint32_t led_ctrl;
459     uint32_t timeout;
460     uint32_t extcnf_ctrl;
461     int32_t ret_val;
462
463     DEBUGFUNC("e1000_reset_hw");
464
465     /* For 82542 (rev 2.0), disable MWI before issuing a device reset */
466     if(hw->mac_type == e1000_82542_rev2_0) {
467         DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
468         e1000_pci_clear_mwi(hw);
469     }
470
471     if(hw->bus_type == e1000_bus_type_pci_express) {
472         /* Prevent the PCI-E bus from sticking if there is no TLP connection
473          * on the last TLP read/write transaction when MAC is reset.
474          */
475         if(e1000_disable_pciex_master(hw) != E1000_SUCCESS) {
476             DEBUGOUT("PCI-E Master disable polling has failed.\n");
477         }
478     }
479
480     /* Clear interrupt mask to stop board from generating interrupts */
481     DEBUGOUT("Masking off all interrupts\n");
482     E1000_WRITE_REG(hw, IMC, 0xffffffff);
483
484     /* Disable the Transmit and Receive units.  Then delay to allow
485      * any pending transactions to complete before we hit the MAC with
486      * the global reset.
487      */
488     E1000_WRITE_REG(hw, RCTL, 0);
489     E1000_WRITE_REG(hw, TCTL, E1000_TCTL_PSP);
490     E1000_WRITE_FLUSH(hw);
491
492     /* The tbi_compatibility_on Flag must be cleared when Rctl is cleared. */
493     hw->tbi_compatibility_on = FALSE;
494
495     /* Delay to allow any outstanding PCI transactions to complete before
496      * resetting the device
497      */
498     msec_delay(10);
499
500     ctrl = E1000_READ_REG(hw, CTRL);
501
502     /* Must reset the PHY before resetting the MAC */
503     if((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
504         E1000_WRITE_REG(hw, CTRL, (ctrl | E1000_CTRL_PHY_RST));
505         msec_delay(5);
506     }
507
508     /* Must acquire the MDIO ownership before MAC reset.
509      * Ownership defaults to firmware after a reset. */
510     if(hw->mac_type == e1000_82573) {
511         timeout = 10;
512
513         extcnf_ctrl = E1000_READ_REG(hw, EXTCNF_CTRL);
514         extcnf_ctrl |= E1000_EXTCNF_CTRL_MDIO_SW_OWNERSHIP;
515
516         do {
517             E1000_WRITE_REG(hw, EXTCNF_CTRL, extcnf_ctrl);
518             extcnf_ctrl = E1000_READ_REG(hw, EXTCNF_CTRL);
519
520             if(extcnf_ctrl & E1000_EXTCNF_CTRL_MDIO_SW_OWNERSHIP)
521                 break;
522             else
523                 extcnf_ctrl |= E1000_EXTCNF_CTRL_MDIO_SW_OWNERSHIP;
524
525             msec_delay(2);
526             timeout--;
527         } while(timeout);
528     }
529
530     /* Issue a global reset to the MAC.  This will reset the chip's
531      * transmit, receive, DMA, and link units.  It will not effect
532      * the current PCI configuration.  The global reset bit is self-
533      * clearing, and should clear within a microsecond.
534      */
535     DEBUGOUT("Issuing a global reset to MAC\n");
536
537     switch(hw->mac_type) {
538         case e1000_82544:
539         case e1000_82540:
540         case e1000_82545:
541         case e1000_82546:
542         case e1000_82541:
543         case e1000_82541_rev_2:
544             /* These controllers can't ack the 64-bit write when issuing the
545              * reset, so use IO-mapping as a workaround to issue the reset */
546             E1000_WRITE_REG_IO(hw, CTRL, (ctrl | E1000_CTRL_RST));
547             break;
548         case e1000_82545_rev_3:
549         case e1000_82546_rev_3:
550             /* Reset is performed on a shadow of the control register */
551             E1000_WRITE_REG(hw, CTRL_DUP, (ctrl | E1000_CTRL_RST));
552             break;
553         default:
554             E1000_WRITE_REG(hw, CTRL, (ctrl | E1000_CTRL_RST));
555             break;
556     }
557
558     /* After MAC reset, force reload of EEPROM to restore power-on settings to
559      * device.  Later controllers reload the EEPROM automatically, so just wait
560      * for reload to complete.
561      */
562     switch(hw->mac_type) {
563         case e1000_82542_rev2_0:
564         case e1000_82542_rev2_1:
565         case e1000_82543:
566         case e1000_82544:
567             /* Wait for reset to complete */
568             udelay(10);
569             ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
570             ctrl_ext |= E1000_CTRL_EXT_EE_RST;
571             E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
572             E1000_WRITE_FLUSH(hw);
573             /* Wait for EEPROM reload */
574             msec_delay(2);
575             break;
576         case e1000_82541:
577         case e1000_82541_rev_2:
578         case e1000_82547:
579         case e1000_82547_rev_2:
580             /* Wait for EEPROM reload */
581             msec_delay(20);
582             break;
583         case e1000_82573:
584             if (e1000_is_onboard_nvm_eeprom(hw) == FALSE) {
585                 udelay(10);
586                 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
587                 ctrl_ext |= E1000_CTRL_EXT_EE_RST;
588                 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
589                 E1000_WRITE_FLUSH(hw);
590             }
591             /* fall through */
592         case e1000_82571:
593         case e1000_82572:
594         case e1000_80003es2lan:
595             ret_val = e1000_get_auto_rd_done(hw);
596             if(ret_val)
597                 /* We don't want to continue accessing MAC registers. */
598                 return ret_val;
599             break;
600         default:
601             /* Wait for EEPROM reload (it happens automatically) */
602             msec_delay(5);
603             break;
604     }
605
606     /* Disable HW ARPs on ASF enabled adapters */
607     if(hw->mac_type >= e1000_82540 && hw->mac_type <= e1000_82547_rev_2) {
608         manc = E1000_READ_REG(hw, MANC);
609         manc &= ~(E1000_MANC_ARP_EN);
610         E1000_WRITE_REG(hw, MANC, manc);
611     }
612
613     if((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
614         e1000_phy_init_script(hw);
615
616         /* Configure activity LED after PHY reset */
617         led_ctrl = E1000_READ_REG(hw, LEDCTL);
618         led_ctrl &= IGP_ACTIVITY_LED_MASK;
619         led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
620         E1000_WRITE_REG(hw, LEDCTL, led_ctrl);
621     }
622
623     /* Clear interrupt mask to stop board from generating interrupts */
624     DEBUGOUT("Masking off all interrupts\n");
625     E1000_WRITE_REG(hw, IMC, 0xffffffff);
626
627     /* Clear any pending interrupt events. */
628     icr = E1000_READ_REG(hw, ICR);
629
630     /* If MWI was previously enabled, reenable it. */
631     if(hw->mac_type == e1000_82542_rev2_0) {
632         if(hw->pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
633             e1000_pci_set_mwi(hw);
634     }
635
636     return E1000_SUCCESS;
637 }
638
639 /******************************************************************************
640  * Performs basic configuration of the adapter.
641  *
642  * hw - Struct containing variables accessed by shared code
643  *
644  * Assumes that the controller has previously been reset and is in a
645  * post-reset uninitialized state. Initializes the receive address registers,
646  * multicast table, and VLAN filter table. Calls routines to setup link
647  * configuration and flow control settings. Clears all on-chip counters. Leaves
648  * the transmit and receive units disabled and uninitialized.
649  *****************************************************************************/
650 int32_t
651 e1000_init_hw(struct e1000_hw *hw)
652 {
653     uint32_t ctrl;
654     uint32_t i;
655     int32_t ret_val;
656     uint16_t pcix_cmd_word;
657     uint16_t pcix_stat_hi_word;
658     uint16_t cmd_mmrbc;
659     uint16_t stat_mmrbc;
660     uint32_t mta_size;
661     uint32_t reg_data;
662     uint32_t ctrl_ext;
663
664     DEBUGFUNC("e1000_init_hw");
665
666     /* Initialize Identification LED */
667     ret_val = e1000_id_led_init(hw);
668     if(ret_val) {
669         DEBUGOUT("Error Initializing Identification LED\n");
670         return ret_val;
671     }
672
673     /* Set the media type and TBI compatibility */
674     e1000_set_media_type(hw);
675
676     /* Disabling VLAN filtering. */
677     DEBUGOUT("Initializing the IEEE VLAN\n");
678     if (hw->mac_type < e1000_82545_rev_3)
679         E1000_WRITE_REG(hw, VET, 0);
680     e1000_clear_vfta(hw);
681
682     /* For 82542 (rev 2.0), disable MWI and put the receiver into reset */
683     if(hw->mac_type == e1000_82542_rev2_0) {
684         DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
685         e1000_pci_clear_mwi(hw);
686         E1000_WRITE_REG(hw, RCTL, E1000_RCTL_RST);
687         E1000_WRITE_FLUSH(hw);
688         msec_delay(5);
689     }
690
691     /* Setup the receive address. This involves initializing all of the Receive
692      * Address Registers (RARs 0 - 15).
693      */
694     e1000_init_rx_addrs(hw);
695
696     /* For 82542 (rev 2.0), take the receiver out of reset and enable MWI */
697     if(hw->mac_type == e1000_82542_rev2_0) {
698         E1000_WRITE_REG(hw, RCTL, 0);
699         E1000_WRITE_FLUSH(hw);
700         msec_delay(1);
701         if(hw->pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
702             e1000_pci_set_mwi(hw);
703     }
704
705     /* Zero out the Multicast HASH table */
706     DEBUGOUT("Zeroing the MTA\n");
707     mta_size = E1000_MC_TBL_SIZE;
708     for(i = 0; i < mta_size; i++) {
709         E1000_WRITE_REG_ARRAY(hw, MTA, i, 0);
710         /* use write flush to prevent Memory Write Block (MWB) from
711          * occuring when accessing our register space */
712         E1000_WRITE_FLUSH(hw);
713     }
714
715     /* Set the PCI priority bit correctly in the CTRL register.  This
716      * determines if the adapter gives priority to receives, or if it
717      * gives equal priority to transmits and receives.  Valid only on
718      * 82542 and 82543 silicon.
719      */
720     if(hw->dma_fairness && hw->mac_type <= e1000_82543) {
721         ctrl = E1000_READ_REG(hw, CTRL);
722         E1000_WRITE_REG(hw, CTRL, ctrl | E1000_CTRL_PRIOR);
723     }
724
725     switch(hw->mac_type) {
726     case e1000_82545_rev_3:
727     case e1000_82546_rev_3:
728         break;
729     default:
730         /* Workaround for PCI-X problem when BIOS sets MMRBC incorrectly. */
731         if(hw->bus_type == e1000_bus_type_pcix) {
732             e1000_read_pci_cfg(hw, PCIX_COMMAND_REGISTER, &pcix_cmd_word);
733             e1000_read_pci_cfg(hw, PCIX_STATUS_REGISTER_HI,
734                 &pcix_stat_hi_word);
735             cmd_mmrbc = (pcix_cmd_word & PCIX_COMMAND_MMRBC_MASK) >>
736                 PCIX_COMMAND_MMRBC_SHIFT;
737             stat_mmrbc = (pcix_stat_hi_word & PCIX_STATUS_HI_MMRBC_MASK) >>
738                 PCIX_STATUS_HI_MMRBC_SHIFT;
739             if(stat_mmrbc == PCIX_STATUS_HI_MMRBC_4K)
740                 stat_mmrbc = PCIX_STATUS_HI_MMRBC_2K;
741             if(cmd_mmrbc > stat_mmrbc) {
742                 pcix_cmd_word &= ~PCIX_COMMAND_MMRBC_MASK;
743                 pcix_cmd_word |= stat_mmrbc << PCIX_COMMAND_MMRBC_SHIFT;
744                 e1000_write_pci_cfg(hw, PCIX_COMMAND_REGISTER,
745                     &pcix_cmd_word);
746             }
747         }
748         break;
749     }
750
751     /* Call a subroutine to configure the link and setup flow control. */
752     ret_val = e1000_setup_link(hw);
753
754     /* Set the transmit descriptor write-back policy */
755     if(hw->mac_type > e1000_82544) {
756         ctrl = E1000_READ_REG(hw, TXDCTL);
757         ctrl = (ctrl & ~E1000_TXDCTL_WTHRESH) | E1000_TXDCTL_FULL_TX_DESC_WB;
758         switch (hw->mac_type) {
759         default:
760             break;
761         case e1000_82571:
762         case e1000_82572:
763         case e1000_82573:
764         case e1000_80003es2lan:
765             ctrl |= E1000_TXDCTL_COUNT_DESC;
766             break;
767         }
768         E1000_WRITE_REG(hw, TXDCTL, ctrl);
769     }
770
771     if (hw->mac_type == e1000_82573) {
772         e1000_enable_tx_pkt_filtering(hw);
773     }
774
775     switch (hw->mac_type) {
776     default:
777         break;
778     case e1000_80003es2lan:
779         /* Enable retransmit on late collisions */
780         reg_data = E1000_READ_REG(hw, TCTL);
781         reg_data |= E1000_TCTL_RTLC;
782         E1000_WRITE_REG(hw, TCTL, reg_data);
783
784         /* Configure Gigabit Carry Extend Padding */
785         reg_data = E1000_READ_REG(hw, TCTL_EXT);
786         reg_data &= ~E1000_TCTL_EXT_GCEX_MASK;
787         reg_data |= DEFAULT_80003ES2LAN_TCTL_EXT_GCEX;
788         E1000_WRITE_REG(hw, TCTL_EXT, reg_data);
789
790         /* Configure Transmit Inter-Packet Gap */
791         reg_data = E1000_READ_REG(hw, TIPG);
792         reg_data &= ~E1000_TIPG_IPGT_MASK;
793         reg_data |= DEFAULT_80003ES2LAN_TIPG_IPGT_1000;
794         E1000_WRITE_REG(hw, TIPG, reg_data);
795
796         reg_data = E1000_READ_REG_ARRAY(hw, FFLT, 0x0001);
797         reg_data &= ~0x00100000;
798         E1000_WRITE_REG_ARRAY(hw, FFLT, 0x0001, reg_data);
799         /* Fall through */
800     case e1000_82571:
801     case e1000_82572:
802         ctrl = E1000_READ_REG(hw, TXDCTL1);
803         ctrl = (ctrl & ~E1000_TXDCTL_WTHRESH) | E1000_TXDCTL_FULL_TX_DESC_WB;
804         if(hw->mac_type >= e1000_82571)
805             ctrl |= E1000_TXDCTL_COUNT_DESC;
806         E1000_WRITE_REG(hw, TXDCTL1, ctrl);
807         break;
808     }
809
810
811
812     if (hw->mac_type == e1000_82573) {
813         uint32_t gcr = E1000_READ_REG(hw, GCR);
814         gcr |= E1000_GCR_L1_ACT_WITHOUT_L0S_RX;
815         E1000_WRITE_REG(hw, GCR, gcr);
816     }
817
818     /* Clear all of the statistics registers (clear on read).  It is
819      * important that we do this after we have tried to establish link
820      * because the symbol error count will increment wildly if there
821      * is no link.
822      */
823     e1000_clear_hw_cntrs(hw);
824
825     if (hw->device_id == E1000_DEV_ID_82546GB_QUAD_COPPER ||
826         hw->device_id == E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3) {
827         ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
828         /* Relaxed ordering must be disabled to avoid a parity
829          * error crash in a PCI slot. */
830         ctrl_ext |= E1000_CTRL_EXT_RO_DIS;
831         E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
832     }
833
834     return ret_val;
835 }
836
837 /******************************************************************************
838  * Adjust SERDES output amplitude based on EEPROM setting.
839  *
840  * hw - Struct containing variables accessed by shared code.
841  *****************************************************************************/
842 static int32_t
843 e1000_adjust_serdes_amplitude(struct e1000_hw *hw)
844 {
845     uint16_t eeprom_data;
846     int32_t  ret_val;
847
848     DEBUGFUNC("e1000_adjust_serdes_amplitude");
849
850     if(hw->media_type != e1000_media_type_internal_serdes)
851         return E1000_SUCCESS;
852
853     switch(hw->mac_type) {
854     case e1000_82545_rev_3:
855     case e1000_82546_rev_3:
856         break;
857     default:
858         return E1000_SUCCESS;
859     }
860
861     ret_val = e1000_read_eeprom(hw, EEPROM_SERDES_AMPLITUDE, 1, &eeprom_data);
862     if (ret_val) {
863         return ret_val;
864     }
865
866     if(eeprom_data != EEPROM_RESERVED_WORD) {
867         /* Adjust SERDES output amplitude only. */
868         eeprom_data &= EEPROM_SERDES_AMPLITUDE_MASK;
869         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_EXT_CTRL, eeprom_data);
870         if(ret_val)
871             return ret_val;
872     }
873
874     return E1000_SUCCESS;
875 }
876
877 /******************************************************************************
878  * Configures flow control and link settings.
879  *
880  * hw - Struct containing variables accessed by shared code
881  *
882  * Determines which flow control settings to use. Calls the apropriate media-
883  * specific link configuration function. Configures the flow control settings.
884  * Assuming the adapter has a valid link partner, a valid link should be
885  * established. Assumes the hardware has previously been reset and the
886  * transmitter and receiver are not enabled.
887  *****************************************************************************/
888 int32_t
889 e1000_setup_link(struct e1000_hw *hw)
890 {
891     uint32_t ctrl_ext;
892     int32_t ret_val;
893     uint16_t eeprom_data;
894
895     DEBUGFUNC("e1000_setup_link");
896
897     /* In the case of the phy reset being blocked, we already have a link.
898      * We do not have to set it up again. */
899     if (e1000_check_phy_reset_block(hw))
900         return E1000_SUCCESS;
901
902     /* Read and store word 0x0F of the EEPROM. This word contains bits
903      * that determine the hardware's default PAUSE (flow control) mode,
904      * a bit that determines whether the HW defaults to enabling or
905      * disabling auto-negotiation, and the direction of the
906      * SW defined pins. If there is no SW over-ride of the flow
907      * control setting, then the variable hw->fc will
908      * be initialized based on a value in the EEPROM.
909      */
910     if (hw->fc == e1000_fc_default) {
911         switch (hw->mac_type) {
912         case e1000_82573:
913             hw->fc = e1000_fc_full;
914             break;
915         default:
916             ret_val = e1000_read_eeprom(hw, EEPROM_INIT_CONTROL2_REG,
917                                         1, &eeprom_data);
918             if (ret_val) {
919                 DEBUGOUT("EEPROM Read Error\n");
920                 return -E1000_ERR_EEPROM;
921             }
922             if ((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) == 0)
923                 hw->fc = e1000_fc_none;
924             else if ((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) ==
925                     EEPROM_WORD0F_ASM_DIR)
926                 hw->fc = e1000_fc_tx_pause;
927             else
928                 hw->fc = e1000_fc_full;
929             break;
930         }
931     }
932
933     /* We want to save off the original Flow Control configuration just
934      * in case we get disconnected and then reconnected into a different
935      * hub or switch with different Flow Control capabilities.
936      */
937     if(hw->mac_type == e1000_82542_rev2_0)
938         hw->fc &= (~e1000_fc_tx_pause);
939
940     if((hw->mac_type < e1000_82543) && (hw->report_tx_early == 1))
941         hw->fc &= (~e1000_fc_rx_pause);
942
943     hw->original_fc = hw->fc;
944
945     DEBUGOUT1("After fix-ups FlowControl is now = %x\n", hw->fc);
946
947     /* Take the 4 bits from EEPROM word 0x0F that determine the initial
948      * polarity value for the SW controlled pins, and setup the
949      * Extended Device Control reg with that info.
950      * This is needed because one of the SW controlled pins is used for
951      * signal detection.  So this should be done before e1000_setup_pcs_link()
952      * or e1000_phy_setup() is called.
953      */
954     if (hw->mac_type == e1000_82543) {
955                 ret_val = e1000_read_eeprom(hw, EEPROM_INIT_CONTROL2_REG,
956                                                                         1, &eeprom_data);
957                 if (ret_val) {
958                         DEBUGOUT("EEPROM Read Error\n");
959                         return -E1000_ERR_EEPROM;
960                 }
961         ctrl_ext = ((eeprom_data & EEPROM_WORD0F_SWPDIO_EXT) <<
962                     SWDPIO__EXT_SHIFT);
963         E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
964     }
965
966     /* Call the necessary subroutine to configure the link. */
967     ret_val = (hw->media_type == e1000_media_type_copper) ?
968               e1000_setup_copper_link(hw) :
969               e1000_setup_fiber_serdes_link(hw);
970
971     /* Initialize the flow control address, type, and PAUSE timer
972      * registers to their default values.  This is done even if flow
973      * control is disabled, because it does not hurt anything to
974      * initialize these registers.
975      */
976     DEBUGOUT("Initializing the Flow Control address, type and timer regs\n");
977
978     E1000_WRITE_REG(hw, FCAL, FLOW_CONTROL_ADDRESS_LOW);
979     E1000_WRITE_REG(hw, FCAH, FLOW_CONTROL_ADDRESS_HIGH);
980     E1000_WRITE_REG(hw, FCT, FLOW_CONTROL_TYPE);
981
982     E1000_WRITE_REG(hw, FCTTV, hw->fc_pause_time);
983
984     /* Set the flow control receive threshold registers.  Normally,
985      * these registers will be set to a default threshold that may be
986      * adjusted later by the driver's runtime code.  However, if the
987      * ability to transmit pause frames in not enabled, then these
988      * registers will be set to 0.
989      */
990     if(!(hw->fc & e1000_fc_tx_pause)) {
991         E1000_WRITE_REG(hw, FCRTL, 0);
992         E1000_WRITE_REG(hw, FCRTH, 0);
993     } else {
994         /* We need to set up the Receive Threshold high and low water marks
995          * as well as (optionally) enabling the transmission of XON frames.
996          */
997         if(hw->fc_send_xon) {
998             E1000_WRITE_REG(hw, FCRTL, (hw->fc_low_water | E1000_FCRTL_XONE));
999             E1000_WRITE_REG(hw, FCRTH, hw->fc_high_water);
1000         } else {
1001             E1000_WRITE_REG(hw, FCRTL, hw->fc_low_water);
1002             E1000_WRITE_REG(hw, FCRTH, hw->fc_high_water);
1003         }
1004     }
1005     return ret_val;
1006 }
1007
1008 /******************************************************************************
1009  * Sets up link for a fiber based or serdes based adapter
1010  *
1011  * hw - Struct containing variables accessed by shared code
1012  *
1013  * Manipulates Physical Coding Sublayer functions in order to configure
1014  * link. Assumes the hardware has been previously reset and the transmitter
1015  * and receiver are not enabled.
1016  *****************************************************************************/
1017 static int32_t
1018 e1000_setup_fiber_serdes_link(struct e1000_hw *hw)
1019 {
1020     uint32_t ctrl;
1021     uint32_t status;
1022     uint32_t txcw = 0;
1023     uint32_t i;
1024     uint32_t signal = 0;
1025     int32_t ret_val;
1026
1027     DEBUGFUNC("e1000_setup_fiber_serdes_link");
1028
1029     /* On 82571 and 82572 Fiber connections, SerDes loopback mode persists
1030      * until explicitly turned off or a power cycle is performed.  A read to
1031      * the register does not indicate its status.  Therefore, we ensure
1032      * loopback mode is disabled during initialization.
1033      */
1034     if (hw->mac_type == e1000_82571 || hw->mac_type == e1000_82572)
1035         E1000_WRITE_REG(hw, SCTL, E1000_DISABLE_SERDES_LOOPBACK);
1036
1037     /* On adapters with a MAC newer than 82544, SW Defineable pin 1 will be
1038      * set when the optics detect a signal. On older adapters, it will be
1039      * cleared when there is a signal.  This applies to fiber media only.
1040      * If we're on serdes media, adjust the output amplitude to value set in
1041      * the EEPROM.
1042      */
1043     ctrl = E1000_READ_REG(hw, CTRL);
1044     if(hw->media_type == e1000_media_type_fiber)
1045         signal = (hw->mac_type > e1000_82544) ? E1000_CTRL_SWDPIN1 : 0;
1046
1047     ret_val = e1000_adjust_serdes_amplitude(hw);
1048     if(ret_val)
1049         return ret_val;
1050
1051     /* Take the link out of reset */
1052     ctrl &= ~(E1000_CTRL_LRST);
1053
1054     /* Adjust VCO speed to improve BER performance */
1055     ret_val = e1000_set_vco_speed(hw);
1056     if(ret_val)
1057         return ret_val;
1058
1059     e1000_config_collision_dist(hw);
1060
1061     /* Check for a software override of the flow control settings, and setup
1062      * the device accordingly.  If auto-negotiation is enabled, then software
1063      * will have to set the "PAUSE" bits to the correct value in the Tranmsit
1064      * Config Word Register (TXCW) and re-start auto-negotiation.  However, if
1065      * auto-negotiation is disabled, then software will have to manually
1066      * configure the two flow control enable bits in the CTRL register.
1067      *
1068      * The possible values of the "fc" parameter are:
1069      *      0:  Flow control is completely disabled
1070      *      1:  Rx flow control is enabled (we can receive pause frames, but
1071      *          not send pause frames).
1072      *      2:  Tx flow control is enabled (we can send pause frames but we do
1073      *          not support receiving pause frames).
1074      *      3:  Both Rx and TX flow control (symmetric) are enabled.
1075      */
1076     switch (hw->fc) {
1077     case e1000_fc_none:
1078         /* Flow control is completely disabled by a software over-ride. */
1079         txcw = (E1000_TXCW_ANE | E1000_TXCW_FD);
1080         break;
1081     case e1000_fc_rx_pause:
1082         /* RX Flow control is enabled and TX Flow control is disabled by a
1083          * software over-ride. Since there really isn't a way to advertise
1084          * that we are capable of RX Pause ONLY, we will advertise that we
1085          * support both symmetric and asymmetric RX PAUSE. Later, we will
1086          *  disable the adapter's ability to send PAUSE frames.
1087          */
1088         txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
1089         break;
1090     case e1000_fc_tx_pause:
1091         /* TX Flow control is enabled, and RX Flow control is disabled, by a
1092          * software over-ride.
1093          */
1094         txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_ASM_DIR);
1095         break;
1096     case e1000_fc_full:
1097         /* Flow control (both RX and TX) is enabled by a software over-ride. */
1098         txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
1099         break;
1100     default:
1101         DEBUGOUT("Flow control param set incorrectly\n");
1102         return -E1000_ERR_CONFIG;
1103         break;
1104     }
1105
1106     /* Since auto-negotiation is enabled, take the link out of reset (the link
1107      * will be in reset, because we previously reset the chip). This will
1108      * restart auto-negotiation.  If auto-neogtiation is successful then the
1109      * link-up status bit will be set and the flow control enable bits (RFCE
1110      * and TFCE) will be set according to their negotiated value.
1111      */
1112     DEBUGOUT("Auto-negotiation enabled\n");
1113
1114     E1000_WRITE_REG(hw, TXCW, txcw);
1115     E1000_WRITE_REG(hw, CTRL, ctrl);
1116     E1000_WRITE_FLUSH(hw);
1117
1118     hw->txcw = txcw;
1119     msec_delay(1);
1120
1121     /* If we have a signal (the cable is plugged in) then poll for a "Link-Up"
1122      * indication in the Device Status Register.  Time-out if a link isn't
1123      * seen in 500 milliseconds seconds (Auto-negotiation should complete in
1124      * less than 500 milliseconds even if the other end is doing it in SW).
1125      * For internal serdes, we just assume a signal is present, then poll.
1126      */
1127     if(hw->media_type == e1000_media_type_internal_serdes ||
1128        (E1000_READ_REG(hw, CTRL) & E1000_CTRL_SWDPIN1) == signal) {
1129         DEBUGOUT("Looking for Link\n");
1130         for(i = 0; i < (LINK_UP_TIMEOUT / 10); i++) {
1131             msec_delay(10);
1132             status = E1000_READ_REG(hw, STATUS);
1133             if(status & E1000_STATUS_LU) break;
1134         }
1135         if(i == (LINK_UP_TIMEOUT / 10)) {
1136             DEBUGOUT("Never got a valid link from auto-neg!!!\n");
1137             hw->autoneg_failed = 1;
1138             /* AutoNeg failed to achieve a link, so we'll call
1139              * e1000_check_for_link. This routine will force the link up if
1140              * we detect a signal. This will allow us to communicate with
1141              * non-autonegotiating link partners.
1142              */
1143             ret_val = e1000_check_for_link(hw);
1144             if(ret_val) {
1145                 DEBUGOUT("Error while checking for link\n");
1146                 return ret_val;
1147             }
1148             hw->autoneg_failed = 0;
1149         } else {
1150             hw->autoneg_failed = 0;
1151             DEBUGOUT("Valid Link Found\n");
1152         }
1153     } else {
1154         DEBUGOUT("No Signal Detected\n");
1155     }
1156     return E1000_SUCCESS;
1157 }
1158
1159 /******************************************************************************
1160 * Make sure we have a valid PHY and change PHY mode before link setup.
1161 *
1162 * hw - Struct containing variables accessed by shared code
1163 ******************************************************************************/
1164 static int32_t
1165 e1000_copper_link_preconfig(struct e1000_hw *hw)
1166 {
1167     uint32_t ctrl;
1168     int32_t ret_val;
1169     uint16_t phy_data;
1170
1171     DEBUGFUNC("e1000_copper_link_preconfig");
1172
1173     ctrl = E1000_READ_REG(hw, CTRL);
1174     /* With 82543, we need to force speed and duplex on the MAC equal to what
1175      * the PHY speed and duplex configuration is. In addition, we need to
1176      * perform a hardware reset on the PHY to take it out of reset.
1177      */
1178     if(hw->mac_type > e1000_82543) {
1179         ctrl |= E1000_CTRL_SLU;
1180         ctrl &= ~(E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
1181         E1000_WRITE_REG(hw, CTRL, ctrl);
1182     } else {
1183         ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX | E1000_CTRL_SLU);
1184         E1000_WRITE_REG(hw, CTRL, ctrl);
1185         ret_val = e1000_phy_hw_reset(hw);
1186         if(ret_val)
1187             return ret_val;
1188     }
1189
1190     /* Make sure we have a valid PHY */
1191     ret_val = e1000_detect_gig_phy(hw);
1192     if(ret_val) {
1193         DEBUGOUT("Error, did not detect valid phy.\n");
1194         return ret_val;
1195     }
1196     DEBUGOUT1("Phy ID = %x \n", hw->phy_id);
1197
1198     /* Set PHY to class A mode (if necessary) */
1199     ret_val = e1000_set_phy_mode(hw);
1200     if(ret_val)
1201         return ret_val;
1202
1203     if((hw->mac_type == e1000_82545_rev_3) ||
1204        (hw->mac_type == e1000_82546_rev_3)) {
1205         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
1206         phy_data |= 0x00000008;
1207         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
1208     }
1209
1210     if(hw->mac_type <= e1000_82543 ||
1211        hw->mac_type == e1000_82541 || hw->mac_type == e1000_82547 ||
1212        hw->mac_type == e1000_82541_rev_2 || hw->mac_type == e1000_82547_rev_2)
1213         hw->phy_reset_disable = FALSE;
1214
1215    return E1000_SUCCESS;
1216 }
1217
1218
1219 /********************************************************************
1220 * Copper link setup for e1000_phy_igp series.
1221 *
1222 * hw - Struct containing variables accessed by shared code
1223 *********************************************************************/
1224 static int32_t
1225 e1000_copper_link_igp_setup(struct e1000_hw *hw)
1226 {
1227     uint32_t led_ctrl;
1228     int32_t ret_val;
1229     uint16_t phy_data;
1230
1231     DEBUGFUNC("e1000_copper_link_igp_setup");
1232
1233     if (hw->phy_reset_disable)
1234         return E1000_SUCCESS;
1235
1236     ret_val = e1000_phy_reset(hw);
1237     if (ret_val) {
1238         DEBUGOUT("Error Resetting the PHY\n");
1239         return ret_val;
1240     }
1241
1242     /* Wait 10ms for MAC to configure PHY from eeprom settings */
1243     msec_delay(15);
1244
1245     /* Configure activity LED after PHY reset */
1246     led_ctrl = E1000_READ_REG(hw, LEDCTL);
1247     led_ctrl &= IGP_ACTIVITY_LED_MASK;
1248     led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
1249     E1000_WRITE_REG(hw, LEDCTL, led_ctrl);
1250
1251     /* disable lplu d3 during driver init */
1252     ret_val = e1000_set_d3_lplu_state(hw, FALSE);
1253     if (ret_val) {
1254         DEBUGOUT("Error Disabling LPLU D3\n");
1255         return ret_val;
1256     }
1257
1258     /* disable lplu d0 during driver init */
1259     ret_val = e1000_set_d0_lplu_state(hw, FALSE);
1260     if (ret_val) {
1261         DEBUGOUT("Error Disabling LPLU D0\n");
1262         return ret_val;
1263     }
1264     /* Configure mdi-mdix settings */
1265     ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, &phy_data);
1266     if (ret_val)
1267         return ret_val;
1268
1269     if ((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
1270         hw->dsp_config_state = e1000_dsp_config_disabled;
1271         /* Force MDI for earlier revs of the IGP PHY */
1272         phy_data &= ~(IGP01E1000_PSCR_AUTO_MDIX | IGP01E1000_PSCR_FORCE_MDI_MDIX);
1273         hw->mdix = 1;
1274
1275     } else {
1276         hw->dsp_config_state = e1000_dsp_config_enabled;
1277         phy_data &= ~IGP01E1000_PSCR_AUTO_MDIX;
1278
1279         switch (hw->mdix) {
1280         case 1:
1281             phy_data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
1282             break;
1283         case 2:
1284             phy_data |= IGP01E1000_PSCR_FORCE_MDI_MDIX;
1285             break;
1286         case 0:
1287         default:
1288             phy_data |= IGP01E1000_PSCR_AUTO_MDIX;
1289             break;
1290         }
1291     }
1292     ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, phy_data);
1293     if(ret_val)
1294         return ret_val;
1295
1296     /* set auto-master slave resolution settings */
1297     if(hw->autoneg) {
1298         e1000_ms_type phy_ms_setting = hw->master_slave;
1299
1300         if(hw->ffe_config_state == e1000_ffe_config_active)
1301             hw->ffe_config_state = e1000_ffe_config_enabled;
1302
1303         if(hw->dsp_config_state == e1000_dsp_config_activated)
1304             hw->dsp_config_state = e1000_dsp_config_enabled;
1305
1306         /* when autonegotiation advertisment is only 1000Mbps then we
1307           * should disable SmartSpeed and enable Auto MasterSlave
1308           * resolution as hardware default. */
1309         if(hw->autoneg_advertised == ADVERTISE_1000_FULL) {
1310             /* Disable SmartSpeed */
1311             ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG, &phy_data);
1312             if(ret_val)
1313                 return ret_val;
1314             phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
1315             ret_val = e1000_write_phy_reg(hw,
1316                                                   IGP01E1000_PHY_PORT_CONFIG,
1317                                                   phy_data);
1318             if(ret_val)
1319                 return ret_val;
1320             /* Set auto Master/Slave resolution process */
1321             ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_data);
1322             if(ret_val)
1323                 return ret_val;
1324             phy_data &= ~CR_1000T_MS_ENABLE;
1325             ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_data);
1326             if(ret_val)
1327                 return ret_val;
1328         }
1329
1330         ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_data);
1331         if(ret_val)
1332             return ret_val;
1333
1334         /* load defaults for future use */
1335         hw->original_master_slave = (phy_data & CR_1000T_MS_ENABLE) ?
1336                                         ((phy_data & CR_1000T_MS_VALUE) ?
1337                                          e1000_ms_force_master :
1338                                          e1000_ms_force_slave) :
1339                                          e1000_ms_auto;
1340
1341         switch (phy_ms_setting) {
1342         case e1000_ms_force_master:
1343             phy_data |= (CR_1000T_MS_ENABLE | CR_1000T_MS_VALUE);
1344             break;
1345         case e1000_ms_force_slave:
1346             phy_data |= CR_1000T_MS_ENABLE;
1347             phy_data &= ~(CR_1000T_MS_VALUE);
1348             break;
1349         case e1000_ms_auto:
1350             phy_data &= ~CR_1000T_MS_ENABLE;
1351             default:
1352             break;
1353         }
1354         ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_data);
1355         if(ret_val)
1356             return ret_val;
1357     }
1358
1359     return E1000_SUCCESS;
1360 }
1361
1362 /********************************************************************
1363 * Copper link setup for e1000_phy_gg82563 series.
1364 *
1365 * hw - Struct containing variables accessed by shared code
1366 *********************************************************************/
1367 static int32_t
1368 e1000_copper_link_ggp_setup(struct e1000_hw *hw)
1369 {
1370     int32_t ret_val;
1371     uint16_t phy_data;
1372     uint32_t reg_data;
1373
1374     DEBUGFUNC("e1000_copper_link_ggp_setup");
1375
1376     if(!hw->phy_reset_disable) {
1377
1378         /* Enable CRS on TX for half-duplex operation. */
1379         ret_val = e1000_read_phy_reg(hw, GG82563_PHY_MAC_SPEC_CTRL,
1380                                      &phy_data);
1381         if(ret_val)
1382             return ret_val;
1383
1384         phy_data |= GG82563_MSCR_ASSERT_CRS_ON_TX;
1385         /* Use 25MHz for both link down and 1000BASE-T for Tx clock */
1386         phy_data |= GG82563_MSCR_TX_CLK_1000MBPS_25MHZ;
1387
1388         ret_val = e1000_write_phy_reg(hw, GG82563_PHY_MAC_SPEC_CTRL,
1389                                       phy_data);
1390         if(ret_val)
1391             return ret_val;
1392
1393         /* Options:
1394          *   MDI/MDI-X = 0 (default)
1395          *   0 - Auto for all speeds
1396          *   1 - MDI mode
1397          *   2 - MDI-X mode
1398          *   3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
1399          */
1400         ret_val = e1000_read_phy_reg(hw, GG82563_PHY_SPEC_CTRL, &phy_data);
1401         if(ret_val)
1402             return ret_val;
1403
1404         phy_data &= ~GG82563_PSCR_CROSSOVER_MODE_MASK;
1405
1406         switch (hw->mdix) {
1407         case 1:
1408             phy_data |= GG82563_PSCR_CROSSOVER_MODE_MDI;
1409             break;
1410         case 2:
1411             phy_data |= GG82563_PSCR_CROSSOVER_MODE_MDIX;
1412             break;
1413         case 0:
1414         default:
1415             phy_data |= GG82563_PSCR_CROSSOVER_MODE_AUTO;
1416             break;
1417         }
1418
1419         /* Options:
1420          *   disable_polarity_correction = 0 (default)
1421          *       Automatic Correction for Reversed Cable Polarity
1422          *   0 - Disabled
1423          *   1 - Enabled
1424          */
1425         phy_data &= ~GG82563_PSCR_POLARITY_REVERSAL_DISABLE;
1426         if(hw->disable_polarity_correction == 1)
1427             phy_data |= GG82563_PSCR_POLARITY_REVERSAL_DISABLE;
1428         ret_val = e1000_write_phy_reg(hw, GG82563_PHY_SPEC_CTRL, phy_data);
1429
1430         if(ret_val)
1431             return ret_val;
1432
1433         /* SW Reset the PHY so all changes take effect */
1434         ret_val = e1000_phy_reset(hw);
1435         if (ret_val) {
1436             DEBUGOUT("Error Resetting the PHY\n");
1437             return ret_val;
1438         }
1439     } /* phy_reset_disable */
1440
1441     if (hw->mac_type == e1000_80003es2lan) {
1442         /* Bypass RX and TX FIFO's */
1443         ret_val = e1000_write_kmrn_reg(hw, E1000_KUMCTRLSTA_OFFSET_FIFO_CTRL,
1444                                        E1000_KUMCTRLSTA_FIFO_CTRL_RX_BYPASS |
1445                                        E1000_KUMCTRLSTA_FIFO_CTRL_TX_BYPASS);
1446         if (ret_val)
1447             return ret_val;
1448
1449         ret_val = e1000_read_phy_reg(hw, GG82563_PHY_SPEC_CTRL_2, &phy_data);
1450         if (ret_val)
1451             return ret_val;
1452
1453         phy_data &= ~GG82563_PSCR2_REVERSE_AUTO_NEG;
1454         ret_val = e1000_write_phy_reg(hw, GG82563_PHY_SPEC_CTRL_2, phy_data);
1455
1456         if (ret_val)
1457             return ret_val;
1458
1459         reg_data = E1000_READ_REG(hw, CTRL_EXT);
1460         reg_data &= ~(E1000_CTRL_EXT_LINK_MODE_MASK);
1461         E1000_WRITE_REG(hw, CTRL_EXT, reg_data);
1462
1463         ret_val = e1000_read_phy_reg(hw, GG82563_PHY_PWR_MGMT_CTRL,
1464                                           &phy_data);
1465         if (ret_val)
1466             return ret_val;
1467
1468         /* Do not init these registers when the HW is in IAMT mode, since the
1469          * firmware will have already initialized them.  We only initialize
1470          * them if the HW is not in IAMT mode.
1471          */
1472         if (e1000_check_mng_mode(hw) == FALSE) {
1473             /* Enable Electrical Idle on the PHY */
1474             phy_data |= GG82563_PMCR_ENABLE_ELECTRICAL_IDLE;
1475             ret_val = e1000_write_phy_reg(hw, GG82563_PHY_PWR_MGMT_CTRL,
1476                                           phy_data);
1477             if (ret_val)
1478                 return ret_val;
1479
1480             ret_val = e1000_read_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL,
1481                                          &phy_data);
1482             if (ret_val)
1483                 return ret_val;
1484
1485             /* Enable Pass False Carrier on the PHY */
1486             phy_data |= GG82563_KMCR_PASS_FALSE_CARRIER;
1487
1488             ret_val = e1000_write_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL,
1489                                           phy_data);
1490             if (ret_val)
1491                 return ret_val;
1492         }
1493
1494         /* Workaround: Disable padding in Kumeran interface in the MAC
1495          * and in the PHY to avoid CRC errors.
1496          */
1497         ret_val = e1000_read_phy_reg(hw, GG82563_PHY_INBAND_CTRL,
1498                                      &phy_data);
1499         if (ret_val)
1500             return ret_val;
1501         phy_data |= GG82563_ICR_DIS_PADDING;
1502         ret_val = e1000_write_phy_reg(hw, GG82563_PHY_INBAND_CTRL,
1503                                       phy_data);
1504         if (ret_val)
1505             return ret_val;
1506     }
1507
1508     return E1000_SUCCESS;
1509 }
1510
1511 /********************************************************************
1512 * Copper link setup for e1000_phy_m88 series.
1513 *
1514 * hw - Struct containing variables accessed by shared code
1515 *********************************************************************/
1516 static int32_t
1517 e1000_copper_link_mgp_setup(struct e1000_hw *hw)
1518 {
1519     int32_t ret_val;
1520     uint16_t phy_data;
1521
1522     DEBUGFUNC("e1000_copper_link_mgp_setup");
1523
1524     if(hw->phy_reset_disable)
1525         return E1000_SUCCESS;
1526
1527     /* Enable CRS on TX. This must be set for half-duplex operation. */
1528     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
1529     if(ret_val)
1530         return ret_val;
1531
1532     phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
1533
1534     /* Options:
1535      *   MDI/MDI-X = 0 (default)
1536      *   0 - Auto for all speeds
1537      *   1 - MDI mode
1538      *   2 - MDI-X mode
1539      *   3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
1540      */
1541     phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
1542
1543     switch (hw->mdix) {
1544     case 1:
1545         phy_data |= M88E1000_PSCR_MDI_MANUAL_MODE;
1546         break;
1547     case 2:
1548         phy_data |= M88E1000_PSCR_MDIX_MANUAL_MODE;
1549         break;
1550     case 3:
1551         phy_data |= M88E1000_PSCR_AUTO_X_1000T;
1552         break;
1553     case 0:
1554     default:
1555         phy_data |= M88E1000_PSCR_AUTO_X_MODE;
1556         break;
1557     }
1558
1559     /* Options:
1560      *   disable_polarity_correction = 0 (default)
1561      *       Automatic Correction for Reversed Cable Polarity
1562      *   0 - Disabled
1563      *   1 - Enabled
1564      */
1565     phy_data &= ~M88E1000_PSCR_POLARITY_REVERSAL;
1566     if(hw->disable_polarity_correction == 1)
1567         phy_data |= M88E1000_PSCR_POLARITY_REVERSAL;
1568     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
1569     if (ret_val)
1570         return ret_val;
1571
1572     if (hw->phy_revision < M88E1011_I_REV_4) {
1573         /* Force TX_CLK in the Extended PHY Specific Control Register
1574          * to 25MHz clock.
1575          */
1576         ret_val = e1000_read_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, &phy_data);
1577         if (ret_val)
1578             return ret_val;
1579
1580         phy_data |= M88E1000_EPSCR_TX_CLK_25;
1581
1582         if ((hw->phy_revision == E1000_REVISION_2) &&
1583             (hw->phy_id == M88E1111_I_PHY_ID)) {
1584             /* Vidalia Phy, set the downshift counter to 5x */
1585             phy_data &= ~(M88EC018_EPSCR_DOWNSHIFT_COUNTER_MASK);
1586             phy_data |= M88EC018_EPSCR_DOWNSHIFT_COUNTER_5X;
1587             ret_val = e1000_write_phy_reg(hw,
1588                                         M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
1589             if (ret_val)
1590                 return ret_val;
1591         } else {
1592             /* Configure Master and Slave downshift values */
1593             phy_data &= ~(M88E1000_EPSCR_MASTER_DOWNSHIFT_MASK |
1594                               M88E1000_EPSCR_SLAVE_DOWNSHIFT_MASK);
1595             phy_data |= (M88E1000_EPSCR_MASTER_DOWNSHIFT_1X |
1596                              M88E1000_EPSCR_SLAVE_DOWNSHIFT_1X);
1597             ret_val = e1000_write_phy_reg(hw,
1598                                         M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
1599             if (ret_val)
1600                return ret_val;
1601         }
1602     }
1603
1604     /* SW Reset the PHY so all changes take effect */
1605     ret_val = e1000_phy_reset(hw);
1606     if(ret_val) {
1607         DEBUGOUT("Error Resetting the PHY\n");
1608         return ret_val;
1609     }
1610
1611    return E1000_SUCCESS;
1612 }
1613
1614 /********************************************************************
1615 * Setup auto-negotiation and flow control advertisements,
1616 * and then perform auto-negotiation.
1617 *
1618 * hw - Struct containing variables accessed by shared code
1619 *********************************************************************/
1620 static int32_t
1621 e1000_copper_link_autoneg(struct e1000_hw *hw)
1622 {
1623     int32_t ret_val;
1624     uint16_t phy_data;
1625
1626     DEBUGFUNC("e1000_copper_link_autoneg");
1627
1628     /* Perform some bounds checking on the hw->autoneg_advertised
1629      * parameter.  If this variable is zero, then set it to the default.
1630      */
1631     hw->autoneg_advertised &= AUTONEG_ADVERTISE_SPEED_DEFAULT;
1632
1633     /* If autoneg_advertised is zero, we assume it was not defaulted
1634      * by the calling code so we set to advertise full capability.
1635      */
1636     if(hw->autoneg_advertised == 0)
1637         hw->autoneg_advertised = AUTONEG_ADVERTISE_SPEED_DEFAULT;
1638
1639     DEBUGOUT("Reconfiguring auto-neg advertisement params\n");
1640     ret_val = e1000_phy_setup_autoneg(hw);
1641     if(ret_val) {
1642         DEBUGOUT("Error Setting up Auto-Negotiation\n");
1643         return ret_val;
1644     }
1645     DEBUGOUT("Restarting Auto-Neg\n");
1646
1647     /* Restart auto-negotiation by setting the Auto Neg Enable bit and
1648      * the Auto Neg Restart bit in the PHY control register.
1649      */
1650     ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
1651     if(ret_val)
1652         return ret_val;
1653
1654     phy_data |= (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
1655     ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
1656     if(ret_val)
1657         return ret_val;
1658
1659     /* Does the user want to wait for Auto-Neg to complete here, or
1660      * check at a later time (for example, callback routine).
1661      */
1662     if(hw->wait_autoneg_complete) {
1663         ret_val = e1000_wait_autoneg(hw);
1664         if(ret_val) {
1665             DEBUGOUT("Error while waiting for autoneg to complete\n");
1666             return ret_val;
1667         }
1668     }
1669
1670     hw->get_link_status = TRUE;
1671
1672     return E1000_SUCCESS;
1673 }
1674
1675
1676 /******************************************************************************
1677 * Config the MAC and the PHY after link is up.
1678 *   1) Set up the MAC to the current PHY speed/duplex
1679 *      if we are on 82543.  If we
1680 *      are on newer silicon, we only need to configure
1681 *      collision distance in the Transmit Control Register.
1682 *   2) Set up flow control on the MAC to that established with
1683 *      the link partner.
1684 *   3) Config DSP to improve Gigabit link quality for some PHY revisions.
1685 *
1686 * hw - Struct containing variables accessed by shared code
1687 ******************************************************************************/
1688 static int32_t
1689 e1000_copper_link_postconfig(struct e1000_hw *hw)
1690 {
1691     int32_t ret_val;
1692     DEBUGFUNC("e1000_copper_link_postconfig");
1693
1694     if(hw->mac_type >= e1000_82544) {
1695         e1000_config_collision_dist(hw);
1696     } else {
1697         ret_val = e1000_config_mac_to_phy(hw);
1698         if(ret_val) {
1699             DEBUGOUT("Error configuring MAC to PHY settings\n");
1700             return ret_val;
1701         }
1702     }
1703     ret_val = e1000_config_fc_after_link_up(hw);
1704     if(ret_val) {
1705         DEBUGOUT("Error Configuring Flow Control\n");
1706         return ret_val;
1707     }
1708
1709     /* Config DSP to improve Giga link quality */
1710     if(hw->phy_type == e1000_phy_igp) {
1711         ret_val = e1000_config_dsp_after_link_change(hw, TRUE);
1712         if(ret_val) {
1713             DEBUGOUT("Error Configuring DSP after link up\n");
1714             return ret_val;
1715         }
1716     }
1717
1718     return E1000_SUCCESS;
1719 }
1720
1721 /******************************************************************************
1722 * Detects which PHY is present and setup the speed and duplex
1723 *
1724 * hw - Struct containing variables accessed by shared code
1725 ******************************************************************************/
1726 static int32_t
1727 e1000_setup_copper_link(struct e1000_hw *hw)
1728 {
1729     int32_t ret_val;
1730     uint16_t i;
1731     uint16_t phy_data;
1732     uint16_t reg_data;
1733
1734     DEBUGFUNC("e1000_setup_copper_link");
1735
1736     /* Check if it is a valid PHY and set PHY mode if necessary. */
1737     ret_val = e1000_copper_link_preconfig(hw);
1738     if(ret_val)
1739         return ret_val;
1740
1741     switch (hw->mac_type) {
1742     case e1000_80003es2lan:
1743         ret_val = e1000_read_kmrn_reg(hw, E1000_KUMCTRLSTA_OFFSET_INB_CTRL,
1744                                       &reg_data);
1745         if (ret_val)
1746             return ret_val;
1747         reg_data |= E1000_KUMCTRLSTA_INB_CTRL_DIS_PADDING;
1748         ret_val = e1000_write_kmrn_reg(hw, E1000_KUMCTRLSTA_OFFSET_INB_CTRL,
1749                                        reg_data);
1750         if (ret_val)
1751             return ret_val;
1752         break;
1753     default:
1754         break;
1755     }
1756
1757     if (hw->phy_type == e1000_phy_igp ||
1758         hw->phy_type == e1000_phy_igp_2) {
1759         ret_val = e1000_copper_link_igp_setup(hw);
1760         if(ret_val)
1761             return ret_val;
1762     } else if (hw->phy_type == e1000_phy_m88) {
1763         ret_val = e1000_copper_link_mgp_setup(hw);
1764         if(ret_val)
1765             return ret_val;
1766     } else if (hw->phy_type == e1000_phy_gg82563) {
1767         ret_val = e1000_copper_link_ggp_setup(hw);
1768         if(ret_val)
1769             return ret_val;
1770     }
1771
1772     if(hw->autoneg) {
1773         /* Setup autoneg and flow control advertisement
1774           * and perform autonegotiation */
1775         ret_val = e1000_copper_link_autoneg(hw);
1776         if(ret_val)
1777             return ret_val;
1778     } else {
1779         /* PHY will be set to 10H, 10F, 100H,or 100F
1780           * depending on value from forced_speed_duplex. */
1781         DEBUGOUT("Forcing speed and duplex\n");
1782         ret_val = e1000_phy_force_speed_duplex(hw);
1783         if(ret_val) {
1784             DEBUGOUT("Error Forcing Speed and Duplex\n");
1785             return ret_val;
1786         }
1787     }
1788
1789     /* Check link status. Wait up to 100 microseconds for link to become
1790      * valid.
1791      */
1792     for(i = 0; i < 10; i++) {
1793         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
1794         if(ret_val)
1795             return ret_val;
1796         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
1797         if(ret_val)
1798             return ret_val;
1799
1800         if(phy_data & MII_SR_LINK_STATUS) {
1801             /* Config the MAC and PHY after link is up */
1802             ret_val = e1000_copper_link_postconfig(hw);
1803             if(ret_val)
1804                 return ret_val;
1805
1806             DEBUGOUT("Valid link established!!!\n");
1807             return E1000_SUCCESS;
1808         }
1809         udelay(10);
1810     }
1811
1812     DEBUGOUT("Unable to establish link!!!\n");
1813     return E1000_SUCCESS;
1814 }
1815
1816 /******************************************************************************
1817 * Configure the MAC-to-PHY interface for 10/100Mbps
1818 *
1819 * hw - Struct containing variables accessed by shared code
1820 ******************************************************************************/
1821 static int32_t
1822 e1000_configure_kmrn_for_10_100(struct e1000_hw *hw)
1823 {
1824     int32_t ret_val = E1000_SUCCESS;
1825     uint32_t tipg;
1826     uint16_t reg_data;
1827
1828     DEBUGFUNC("e1000_configure_kmrn_for_10_100");
1829
1830     reg_data = E1000_KUMCTRLSTA_HD_CTRL_10_100_DEFAULT;
1831     ret_val = e1000_write_kmrn_reg(hw, E1000_KUMCTRLSTA_OFFSET_HD_CTRL,
1832                                    reg_data);
1833     if (ret_val)
1834         return ret_val;
1835
1836     /* Configure Transmit Inter-Packet Gap */
1837     tipg = E1000_READ_REG(hw, TIPG);
1838     tipg &= ~E1000_TIPG_IPGT_MASK;
1839     tipg |= DEFAULT_80003ES2LAN_TIPG_IPGT_10_100;
1840     E1000_WRITE_REG(hw, TIPG, tipg);
1841
1842     return ret_val;
1843 }
1844
1845 static int32_t
1846 e1000_configure_kmrn_for_1000(struct e1000_hw *hw)
1847 {
1848     int32_t ret_val = E1000_SUCCESS;
1849     uint16_t reg_data;
1850     uint32_t tipg;
1851
1852     DEBUGFUNC("e1000_configure_kmrn_for_1000");
1853
1854     reg_data = E1000_KUMCTRLSTA_HD_CTRL_1000_DEFAULT;
1855     ret_val = e1000_write_kmrn_reg(hw, E1000_KUMCTRLSTA_OFFSET_HD_CTRL,
1856                                    reg_data);
1857     if (ret_val)
1858         return ret_val;
1859
1860     /* Configure Transmit Inter-Packet Gap */
1861     tipg = E1000_READ_REG(hw, TIPG);
1862     tipg &= ~E1000_TIPG_IPGT_MASK;
1863     tipg |= DEFAULT_80003ES2LAN_TIPG_IPGT_1000;
1864     E1000_WRITE_REG(hw, TIPG, tipg);
1865
1866     return ret_val;
1867 }
1868
1869 /******************************************************************************
1870 * Configures PHY autoneg and flow control advertisement settings
1871 *
1872 * hw - Struct containing variables accessed by shared code
1873 ******************************************************************************/
1874 int32_t
1875 e1000_phy_setup_autoneg(struct e1000_hw *hw)
1876 {
1877     int32_t ret_val;
1878     uint16_t mii_autoneg_adv_reg;
1879     uint16_t mii_1000t_ctrl_reg;
1880
1881     DEBUGFUNC("e1000_phy_setup_autoneg");
1882
1883     /* Read the MII Auto-Neg Advertisement Register (Address 4). */
1884     ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_ADV, &mii_autoneg_adv_reg);
1885     if(ret_val)
1886         return ret_val;
1887
1888     /* Read the MII 1000Base-T Control Register (Address 9). */
1889     ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL, &mii_1000t_ctrl_reg);
1890     if(ret_val)
1891         return ret_val;
1892
1893     /* Need to parse both autoneg_advertised and fc and set up
1894      * the appropriate PHY registers.  First we will parse for
1895      * autoneg_advertised software override.  Since we can advertise
1896      * a plethora of combinations, we need to check each bit
1897      * individually.
1898      */
1899
1900     /* First we clear all the 10/100 mb speed bits in the Auto-Neg
1901      * Advertisement Register (Address 4) and the 1000 mb speed bits in
1902      * the  1000Base-T Control Register (Address 9).
1903      */
1904     mii_autoneg_adv_reg &= ~REG4_SPEED_MASK;
1905     mii_1000t_ctrl_reg &= ~REG9_SPEED_MASK;
1906
1907     DEBUGOUT1("autoneg_advertised %x\n", hw->autoneg_advertised);
1908
1909     /* Do we want to advertise 10 Mb Half Duplex? */
1910     if(hw->autoneg_advertised & ADVERTISE_10_HALF) {
1911         DEBUGOUT("Advertise 10mb Half duplex\n");
1912         mii_autoneg_adv_reg |= NWAY_AR_10T_HD_CAPS;
1913     }
1914
1915     /* Do we want to advertise 10 Mb Full Duplex? */
1916     if(hw->autoneg_advertised & ADVERTISE_10_FULL) {
1917         DEBUGOUT("Advertise 10mb Full duplex\n");
1918         mii_autoneg_adv_reg |= NWAY_AR_10T_FD_CAPS;
1919     }
1920
1921     /* Do we want to advertise 100 Mb Half Duplex? */
1922     if(hw->autoneg_advertised & ADVERTISE_100_HALF) {
1923         DEBUGOUT("Advertise 100mb Half duplex\n");
1924         mii_autoneg_adv_reg |= NWAY_AR_100TX_HD_CAPS;
1925     }
1926
1927     /* Do we want to advertise 100 Mb Full Duplex? */
1928     if(hw->autoneg_advertised & ADVERTISE_100_FULL) {
1929         DEBUGOUT("Advertise 100mb Full duplex\n");
1930         mii_autoneg_adv_reg |= NWAY_AR_100TX_FD_CAPS;
1931     }
1932
1933     /* We do not allow the Phy to advertise 1000 Mb Half Duplex */
1934     if(hw->autoneg_advertised & ADVERTISE_1000_HALF) {
1935         DEBUGOUT("Advertise 1000mb Half duplex requested, request denied!\n");
1936     }
1937
1938     /* Do we want to advertise 1000 Mb Full Duplex? */
1939     if(hw->autoneg_advertised & ADVERTISE_1000_FULL) {
1940         DEBUGOUT("Advertise 1000mb Full duplex\n");
1941         mii_1000t_ctrl_reg |= CR_1000T_FD_CAPS;
1942     }
1943
1944     /* Check for a software override of the flow control settings, and
1945      * setup the PHY advertisement registers accordingly.  If
1946      * auto-negotiation is enabled, then software will have to set the
1947      * "PAUSE" bits to the correct value in the Auto-Negotiation
1948      * Advertisement Register (PHY_AUTONEG_ADV) and re-start auto-negotiation.
1949      *
1950      * The possible values of the "fc" parameter are:
1951      *      0:  Flow control is completely disabled
1952      *      1:  Rx flow control is enabled (we can receive pause frames
1953      *          but not send pause frames).
1954      *      2:  Tx flow control is enabled (we can send pause frames
1955      *          but we do not support receiving pause frames).
1956      *      3:  Both Rx and TX flow control (symmetric) are enabled.
1957      *  other:  No software override.  The flow control configuration
1958      *          in the EEPROM is used.
1959      */
1960     switch (hw->fc) {
1961     case e1000_fc_none: /* 0 */
1962         /* Flow control (RX & TX) is completely disabled by a
1963          * software over-ride.
1964          */
1965         mii_autoneg_adv_reg &= ~(NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
1966         break;
1967     case e1000_fc_rx_pause: /* 1 */
1968         /* RX Flow control is enabled, and TX Flow control is
1969          * disabled, by a software over-ride.
1970          */
1971         /* Since there really isn't a way to advertise that we are
1972          * capable of RX Pause ONLY, we will advertise that we
1973          * support both symmetric and asymmetric RX PAUSE.  Later
1974          * (in e1000_config_fc_after_link_up) we will disable the
1975          *hw's ability to send PAUSE frames.
1976          */
1977         mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
1978         break;
1979     case e1000_fc_tx_pause: /* 2 */
1980         /* TX Flow control is enabled, and RX Flow control is
1981          * disabled, by a software over-ride.
1982          */
1983         mii_autoneg_adv_reg |= NWAY_AR_ASM_DIR;
1984         mii_autoneg_adv_reg &= ~NWAY_AR_PAUSE;
1985         break;
1986     case e1000_fc_full: /* 3 */
1987         /* Flow control (both RX and TX) is enabled by a software
1988          * over-ride.
1989          */
1990         mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
1991         break;
1992     default:
1993         DEBUGOUT("Flow control param set incorrectly\n");
1994         return -E1000_ERR_CONFIG;
1995     }
1996
1997     ret_val = e1000_write_phy_reg(hw, PHY_AUTONEG_ADV, mii_autoneg_adv_reg);
1998     if(ret_val)
1999         return ret_val;
2000
2001     DEBUGOUT1("Auto-Neg Advertising %x\n", mii_autoneg_adv_reg);
2002
2003     ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL, mii_1000t_ctrl_reg);
2004     if(ret_val)
2005         return ret_val;
2006
2007     return E1000_SUCCESS;
2008 }
2009
2010 /******************************************************************************
2011 * Force PHY speed and duplex settings to hw->forced_speed_duplex
2012 *
2013 * hw - Struct containing variables accessed by shared code
2014 ******************************************************************************/
2015 static int32_t
2016 e1000_phy_force_speed_duplex(struct e1000_hw *hw)
2017 {
2018     uint32_t ctrl;
2019     int32_t ret_val;
2020     uint16_t mii_ctrl_reg;
2021     uint16_t mii_status_reg;
2022     uint16_t phy_data;
2023     uint16_t i;
2024
2025     DEBUGFUNC("e1000_phy_force_speed_duplex");
2026
2027     /* Turn off Flow control if we are forcing speed and duplex. */
2028     hw->fc = e1000_fc_none;
2029
2030     DEBUGOUT1("hw->fc = %d\n", hw->fc);
2031
2032     /* Read the Device Control Register. */
2033     ctrl = E1000_READ_REG(hw, CTRL);
2034
2035     /* Set the bits to Force Speed and Duplex in the Device Ctrl Reg. */
2036     ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
2037     ctrl &= ~(DEVICE_SPEED_MASK);
2038
2039     /* Clear the Auto Speed Detect Enable bit. */
2040     ctrl &= ~E1000_CTRL_ASDE;
2041
2042     /* Read the MII Control Register. */
2043     ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &mii_ctrl_reg);
2044     if(ret_val)
2045         return ret_val;
2046
2047     /* We need to disable autoneg in order to force link and duplex. */
2048
2049     mii_ctrl_reg &= ~MII_CR_AUTO_NEG_EN;
2050
2051     /* Are we forcing Full or Half Duplex? */
2052     if(hw->forced_speed_duplex == e1000_100_full ||
2053        hw->forced_speed_duplex == e1000_10_full) {
2054         /* We want to force full duplex so we SET the full duplex bits in the
2055          * Device and MII Control Registers.
2056          */
2057         ctrl |= E1000_CTRL_FD;
2058         mii_ctrl_reg |= MII_CR_FULL_DUPLEX;
2059         DEBUGOUT("Full Duplex\n");
2060     } else {
2061         /* We want to force half duplex so we CLEAR the full duplex bits in
2062          * the Device and MII Control Registers.
2063          */
2064         ctrl &= ~E1000_CTRL_FD;
2065         mii_ctrl_reg &= ~MII_CR_FULL_DUPLEX;
2066         DEBUGOUT("Half Duplex\n");
2067     }
2068
2069     /* Are we forcing 100Mbps??? */
2070     if(hw->forced_speed_duplex == e1000_100_full ||
2071        hw->forced_speed_duplex == e1000_100_half) {
2072         /* Set the 100Mb bit and turn off the 1000Mb and 10Mb bits. */
2073         ctrl |= E1000_CTRL_SPD_100;
2074         mii_ctrl_reg |= MII_CR_SPEED_100;
2075         mii_ctrl_reg &= ~(MII_CR_SPEED_1000 | MII_CR_SPEED_10);
2076         DEBUGOUT("Forcing 100mb ");
2077     } else {
2078         /* Set the 10Mb bit and turn off the 1000Mb and 100Mb bits. */
2079         ctrl &= ~(E1000_CTRL_SPD_1000 | E1000_CTRL_SPD_100);
2080         mii_ctrl_reg |= MII_CR_SPEED_10;
2081         mii_ctrl_reg &= ~(MII_CR_SPEED_1000 | MII_CR_SPEED_100);
2082         DEBUGOUT("Forcing 10mb ");
2083     }
2084
2085     e1000_config_collision_dist(hw);
2086
2087     /* Write the configured values back to the Device Control Reg. */
2088     E1000_WRITE_REG(hw, CTRL, ctrl);
2089
2090     if ((hw->phy_type == e1000_phy_m88) ||
2091         (hw->phy_type == e1000_phy_gg82563)) {
2092         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
2093         if(ret_val)
2094             return ret_val;
2095
2096         /* Clear Auto-Crossover to force MDI manually. M88E1000 requires MDI
2097          * forced whenever speed are duplex are forced.
2098          */
2099         phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
2100         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
2101         if(ret_val)
2102             return ret_val;
2103
2104         DEBUGOUT1("M88E1000 PSCR: %x \n", phy_data);
2105
2106         /* Need to reset the PHY or these changes will be ignored */
2107         mii_ctrl_reg |= MII_CR_RESET;
2108     } else {
2109         /* Clear Auto-Crossover to force MDI manually.  IGP requires MDI
2110          * forced whenever speed or duplex are forced.
2111          */
2112         ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, &phy_data);
2113         if(ret_val)
2114             return ret_val;
2115
2116         phy_data &= ~IGP01E1000_PSCR_AUTO_MDIX;
2117         phy_data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
2118
2119         ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, phy_data);
2120         if(ret_val)
2121             return ret_val;
2122     }
2123
2124     /* Write back the modified PHY MII control register. */
2125     ret_val = e1000_write_phy_reg(hw, PHY_CTRL, mii_ctrl_reg);
2126     if(ret_val)
2127         return ret_val;
2128
2129     udelay(1);
2130
2131     /* The wait_autoneg_complete flag may be a little misleading here.
2132      * Since we are forcing speed and duplex, Auto-Neg is not enabled.
2133      * But we do want to delay for a period while forcing only so we
2134      * don't generate false No Link messages.  So we will wait here
2135      * only if the user has set wait_autoneg_complete to 1, which is
2136      * the default.
2137      */
2138     if(hw->wait_autoneg_complete) {
2139         /* We will wait for autoneg to complete. */
2140         DEBUGOUT("Waiting for forced speed/duplex link.\n");
2141         mii_status_reg = 0;
2142
2143         /* We will wait for autoneg to complete or 4.5 seconds to expire. */
2144         for(i = PHY_FORCE_TIME; i > 0; i--) {
2145             /* Read the MII Status Register and wait for Auto-Neg Complete bit
2146              * to be set.
2147              */
2148             ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
2149             if(ret_val)
2150                 return ret_val;
2151
2152             ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
2153             if(ret_val)
2154                 return ret_val;
2155
2156             if(mii_status_reg & MII_SR_LINK_STATUS) break;
2157             msec_delay(100);
2158         }
2159         if((i == 0) &&
2160            ((hw->phy_type == e1000_phy_m88) ||
2161             (hw->phy_type == e1000_phy_gg82563))) {
2162             /* We didn't get link.  Reset the DSP and wait again for link. */
2163             ret_val = e1000_phy_reset_dsp(hw);
2164             if(ret_val) {
2165                 DEBUGOUT("Error Resetting PHY DSP\n");
2166                 return ret_val;
2167             }
2168         }
2169         /* This loop will early-out if the link condition has been met.  */
2170         for(i = PHY_FORCE_TIME; i > 0; i--) {
2171             if(mii_status_reg & MII_SR_LINK_STATUS) break;
2172             msec_delay(100);
2173             /* Read the MII Status Register and wait for Auto-Neg Complete bit
2174              * to be set.
2175              */
2176             ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
2177             if(ret_val)
2178                 return ret_val;
2179
2180             ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
2181             if(ret_val)
2182                 return ret_val;
2183         }
2184     }
2185
2186     if (hw->phy_type == e1000_phy_m88) {
2187         /* Because we reset the PHY above, we need to re-force TX_CLK in the
2188          * Extended PHY Specific Control Register to 25MHz clock.  This value
2189          * defaults back to a 2.5MHz clock when the PHY is reset.
2190          */
2191         ret_val = e1000_read_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, &phy_data);
2192         if(ret_val)
2193             return ret_val;
2194
2195         phy_data |= M88E1000_EPSCR_TX_CLK_25;
2196         ret_val = e1000_write_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
2197         if(ret_val)
2198             return ret_val;
2199
2200         /* In addition, because of the s/w reset above, we need to enable CRS on
2201          * TX.  This must be set for both full and half duplex operation.
2202          */
2203         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
2204         if(ret_val)
2205             return ret_val;
2206
2207         phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
2208         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
2209         if(ret_val)
2210             return ret_val;
2211
2212         if((hw->mac_type == e1000_82544 || hw->mac_type == e1000_82543) &&
2213            (!hw->autoneg) &&
2214            (hw->forced_speed_duplex == e1000_10_full ||
2215             hw->forced_speed_duplex == e1000_10_half)) {
2216             ret_val = e1000_polarity_reversal_workaround(hw);
2217             if(ret_val)
2218                 return ret_val;
2219         }
2220     } else if (hw->phy_type == e1000_phy_gg82563) {
2221         /* The TX_CLK of the Extended PHY Specific Control Register defaults
2222          * to 2.5MHz on a reset.  We need to re-force it back to 25MHz, if
2223          * we're not in a forced 10/duplex configuration. */
2224         ret_val = e1000_read_phy_reg(hw, GG82563_PHY_MAC_SPEC_CTRL, &phy_data);
2225         if (ret_val)
2226             return ret_val;
2227
2228         phy_data &= ~GG82563_MSCR_TX_CLK_MASK;
2229         if ((hw->forced_speed_duplex == e1000_10_full) ||
2230             (hw->forced_speed_duplex == e1000_10_half))
2231             phy_data |= GG82563_MSCR_TX_CLK_10MBPS_2_5MHZ;
2232         else
2233             phy_data |= GG82563_MSCR_TX_CLK_100MBPS_25MHZ;
2234
2235         /* Also due to the reset, we need to enable CRS on Tx. */
2236         phy_data |= GG82563_MSCR_ASSERT_CRS_ON_TX;
2237
2238         ret_val = e1000_write_phy_reg(hw, GG82563_PHY_MAC_SPEC_CTRL, phy_data);
2239         if (ret_val)
2240             return ret_val;
2241     }
2242     return E1000_SUCCESS;
2243 }
2244
2245 /******************************************************************************
2246 * Sets the collision distance in the Transmit Control register
2247 *
2248 * hw - Struct containing variables accessed by shared code
2249 *
2250 * Link should have been established previously. Reads the speed and duplex
2251 * information from the Device Status register.
2252 ******************************************************************************/
2253 void
2254 e1000_config_collision_dist(struct e1000_hw *hw)
2255 {
2256     uint32_t tctl, coll_dist;
2257
2258     DEBUGFUNC("e1000_config_collision_dist");
2259
2260     if (hw->mac_type < e1000_82543)
2261         coll_dist = E1000_COLLISION_DISTANCE_82542;
2262     else
2263         coll_dist = E1000_COLLISION_DISTANCE;
2264
2265     tctl = E1000_READ_REG(hw, TCTL);
2266
2267     tctl &= ~E1000_TCTL_COLD;
2268     tctl |= coll_dist << E1000_COLD_SHIFT;
2269
2270     E1000_WRITE_REG(hw, TCTL, tctl);
2271     E1000_WRITE_FLUSH(hw);
2272 }
2273
2274 /******************************************************************************
2275 * Sets MAC speed and duplex settings to reflect the those in the PHY
2276 *
2277 * hw - Struct containing variables accessed by shared code
2278 * mii_reg - data to write to the MII control register
2279 *
2280 * The contents of the PHY register containing the needed information need to
2281 * be passed in.
2282 ******************************************************************************/
2283 static int32_t
2284 e1000_config_mac_to_phy(struct e1000_hw *hw)
2285 {
2286     uint32_t ctrl;
2287     int32_t ret_val;
2288     uint16_t phy_data;
2289
2290     DEBUGFUNC("e1000_config_mac_to_phy");
2291
2292     /* 82544 or newer MAC, Auto Speed Detection takes care of
2293     * MAC speed/duplex configuration.*/
2294     if (hw->mac_type >= e1000_82544)
2295         return E1000_SUCCESS;
2296
2297     /* Read the Device Control Register and set the bits to Force Speed
2298      * and Duplex.
2299      */
2300     ctrl = E1000_READ_REG(hw, CTRL);
2301     ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
2302     ctrl &= ~(E1000_CTRL_SPD_SEL | E1000_CTRL_ILOS);
2303
2304     /* Set up duplex in the Device Control and Transmit Control
2305      * registers depending on negotiated values.
2306      */
2307     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data);
2308     if(ret_val)
2309         return ret_val;
2310
2311     if(phy_data & M88E1000_PSSR_DPLX)
2312         ctrl |= E1000_CTRL_FD;
2313     else
2314         ctrl &= ~E1000_CTRL_FD;
2315
2316     e1000_config_collision_dist(hw);
2317
2318     /* Set up speed in the Device Control register depending on
2319      * negotiated values.
2320      */
2321     if((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_1000MBS)
2322         ctrl |= E1000_CTRL_SPD_1000;
2323     else if((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_100MBS)
2324         ctrl |= E1000_CTRL_SPD_100;
2325
2326     /* Write the configured values back to the Device Control Reg. */
2327     E1000_WRITE_REG(hw, CTRL, ctrl);
2328     return E1000_SUCCESS;
2329 }
2330
2331 /******************************************************************************
2332  * Forces the MAC's flow control settings.
2333  *
2334  * hw - Struct containing variables accessed by shared code
2335  *
2336  * Sets the TFCE and RFCE bits in the device control register to reflect
2337  * the adapter settings. TFCE and RFCE need to be explicitly set by
2338  * software when a Copper PHY is used because autonegotiation is managed
2339  * by the PHY rather than the MAC. Software must also configure these
2340  * bits when link is forced on a fiber connection.
2341  *****************************************************************************/
2342 int32_t
2343 e1000_force_mac_fc(struct e1000_hw *hw)
2344 {
2345     uint32_t ctrl;
2346
2347     DEBUGFUNC("e1000_force_mac_fc");
2348
2349     /* Get the current configuration of the Device Control Register */
2350     ctrl = E1000_READ_REG(hw, CTRL);
2351
2352     /* Because we didn't get link via the internal auto-negotiation
2353      * mechanism (we either forced link or we got link via PHY
2354      * auto-neg), we have to manually enable/disable transmit an
2355      * receive flow control.
2356      *
2357      * The "Case" statement below enables/disable flow control
2358      * according to the "hw->fc" parameter.
2359      *
2360      * The possible values of the "fc" parameter are:
2361      *      0:  Flow control is completely disabled
2362      *      1:  Rx flow control is enabled (we can receive pause
2363      *          frames but not send pause frames).
2364      *      2:  Tx flow control is enabled (we can send pause frames
2365      *          frames but we do not receive pause frames).
2366      *      3:  Both Rx and TX flow control (symmetric) is enabled.
2367      *  other:  No other values should be possible at this point.
2368      */
2369
2370     switch (hw->fc) {
2371     case e1000_fc_none:
2372         ctrl &= (~(E1000_CTRL_TFCE | E1000_CTRL_RFCE));
2373         break;
2374     case e1000_fc_rx_pause:
2375         ctrl &= (~E1000_CTRL_TFCE);
2376         ctrl |= E1000_CTRL_RFCE;
2377         break;
2378     case e1000_fc_tx_pause:
2379         ctrl &= (~E1000_CTRL_RFCE);
2380         ctrl |= E1000_CTRL_TFCE;
2381         break;
2382     case e1000_fc_full:
2383         ctrl |= (E1000_CTRL_TFCE | E1000_CTRL_RFCE);
2384         break;
2385     default:
2386         DEBUGOUT("Flow control param set incorrectly\n");
2387         return -E1000_ERR_CONFIG;
2388     }
2389
2390     /* Disable TX Flow Control for 82542 (rev 2.0) */
2391     if(hw->mac_type == e1000_82542_rev2_0)
2392         ctrl &= (~E1000_CTRL_TFCE);
2393
2394     E1000_WRITE_REG(hw, CTRL, ctrl);
2395     return E1000_SUCCESS;
2396 }
2397
2398 /******************************************************************************
2399  * Configures flow control settings after link is established
2400  *
2401  * hw - Struct containing variables accessed by shared code
2402  *
2403  * Should be called immediately after a valid link has been established.
2404  * Forces MAC flow control settings if link was forced. When in MII/GMII mode
2405  * and autonegotiation is enabled, the MAC flow control settings will be set
2406  * based on the flow control negotiated by the PHY. In TBI mode, the TFCE
2407  * and RFCE bits will be automaticaly set to the negotiated flow control mode.
2408  *****************************************************************************/
2409 static int32_t
2410 e1000_config_fc_after_link_up(struct e1000_hw *hw)
2411 {
2412     int32_t ret_val;
2413     uint16_t mii_status_reg;
2414     uint16_t mii_nway_adv_reg;
2415     uint16_t mii_nway_lp_ability_reg;
2416     uint16_t speed;
2417     uint16_t duplex;
2418
2419     DEBUGFUNC("e1000_config_fc_after_link_up");
2420
2421     /* Check for the case where we have fiber media and auto-neg failed
2422      * so we had to force link.  In this case, we need to force the
2423      * configuration of the MAC to match the "fc" parameter.
2424      */
2425     if(((hw->media_type == e1000_media_type_fiber) && (hw->autoneg_failed)) ||
2426        ((hw->media_type == e1000_media_type_internal_serdes) && (hw->autoneg_failed)) ||
2427        ((hw->media_type == e1000_media_type_copper) && (!hw->autoneg))) {
2428         ret_val = e1000_force_mac_fc(hw);
2429         if(ret_val) {
2430             DEBUGOUT("Error forcing flow control settings\n");
2431             return ret_val;
2432         }
2433     }
2434
2435     /* Check for the case where we have copper media and auto-neg is
2436      * enabled.  In this case, we need to check and see if Auto-Neg
2437      * has completed, and if so, how the PHY and link partner has
2438      * flow control configured.
2439      */
2440     if((hw->media_type == e1000_media_type_copper) && hw->autoneg) {
2441         /* Read the MII Status Register and check to see if AutoNeg
2442          * has completed.  We read this twice because this reg has
2443          * some "sticky" (latched) bits.
2444          */
2445         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
2446         if(ret_val)
2447             return ret_val;
2448         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
2449         if(ret_val)
2450             return ret_val;
2451
2452         if(mii_status_reg & MII_SR_AUTONEG_COMPLETE) {
2453             /* The AutoNeg process has completed, so we now need to
2454              * read both the Auto Negotiation Advertisement Register
2455              * (Address 4) and the Auto_Negotiation Base Page Ability
2456              * Register (Address 5) to determine how flow control was
2457              * negotiated.
2458              */
2459             ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_ADV,
2460                                          &mii_nway_adv_reg);
2461             if(ret_val)
2462                 return ret_val;
2463             ret_val = e1000_read_phy_reg(hw, PHY_LP_ABILITY,
2464                                          &mii_nway_lp_ability_reg);
2465             if(ret_val)
2466                 return ret_val;
2467
2468             /* Two bits in the Auto Negotiation Advertisement Register
2469              * (Address 4) and two bits in the Auto Negotiation Base
2470              * Page Ability Register (Address 5) determine flow control
2471              * for both the PHY and the link partner.  The following
2472              * table, taken out of the IEEE 802.3ab/D6.0 dated March 25,
2473              * 1999, describes these PAUSE resolution bits and how flow
2474              * control is determined based upon these settings.
2475              * NOTE:  DC = Don't Care
2476              *
2477              *   LOCAL DEVICE  |   LINK PARTNER
2478              * PAUSE | ASM_DIR | PAUSE | ASM_DIR | NIC Resolution
2479              *-------|---------|-------|---------|--------------------
2480              *   0   |    0    |  DC   |   DC    | e1000_fc_none
2481              *   0   |    1    |   0   |   DC    | e1000_fc_none
2482              *   0   |    1    |   1   |    0    | e1000_fc_none
2483              *   0   |    1    |   1   |    1    | e1000_fc_tx_pause
2484              *   1   |    0    |   0   |   DC    | e1000_fc_none
2485              *   1   |   DC    |   1   |   DC    | e1000_fc_full
2486              *   1   |    1    |   0   |    0    | e1000_fc_none
2487              *   1   |    1    |   0   |    1    | e1000_fc_rx_pause
2488              *
2489              */
2490             /* Are both PAUSE bits set to 1?  If so, this implies
2491              * Symmetric Flow Control is enabled at both ends.  The
2492              * ASM_DIR bits are irrelevant per the spec.
2493              *
2494              * For Symmetric Flow Control:
2495              *
2496              *   LOCAL DEVICE  |   LINK PARTNER
2497              * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
2498              *-------|---------|-------|---------|--------------------
2499              *   1   |   DC    |   1   |   DC    | e1000_fc_full
2500              *
2501              */
2502             if((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
2503                (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE)) {
2504                 /* Now we need to check if the user selected RX ONLY
2505                  * of pause frames.  In this case, we had to advertise
2506                  * FULL flow control because we could not advertise RX
2507                  * ONLY. Hence, we must now check to see if we need to
2508                  * turn OFF  the TRANSMISSION of PAUSE frames.
2509                  */
2510                 if(hw->original_fc == e1000_fc_full) {
2511                     hw->fc = e1000_fc_full;
2512                     DEBUGOUT("Flow Control = FULL.\n");
2513                 } else {
2514                     hw->fc = e1000_fc_rx_pause;
2515                     DEBUGOUT("Flow Control = RX PAUSE frames only.\n");
2516                 }
2517             }
2518             /* For receiving PAUSE frames ONLY.
2519              *
2520              *   LOCAL DEVICE  |   LINK PARTNER
2521              * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
2522              *-------|---------|-------|---------|--------------------
2523              *   0   |    1    |   1   |    1    | e1000_fc_tx_pause
2524              *
2525              */
2526             else if(!(mii_nway_adv_reg & NWAY_AR_PAUSE) &&
2527                     (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
2528                     (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
2529                     (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
2530                 hw->fc = e1000_fc_tx_pause;
2531                 DEBUGOUT("Flow Control = TX PAUSE frames only.\n");
2532             }
2533             /* For transmitting PAUSE frames ONLY.
2534              *
2535              *   LOCAL DEVICE  |   LINK PARTNER
2536              * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
2537              *-------|---------|-------|---------|--------------------
2538              *   1   |    1    |   0   |    1    | e1000_fc_rx_pause
2539              *
2540              */
2541             else if((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
2542                     (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
2543                     !(mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
2544                     (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
2545                 hw->fc = e1000_fc_rx_pause;
2546                 DEBUGOUT("Flow Control = RX PAUSE frames only.\n");
2547             }
2548             /* Per the IEEE spec, at this point flow control should be
2549              * disabled.  However, we want to consider that we could
2550              * be connected to a legacy switch that doesn't advertise
2551              * desired flow control, but can be forced on the link
2552              * partner.  So if we advertised no flow control, that is
2553              * what we will resolve to.  If we advertised some kind of
2554              * receive capability (Rx Pause Only or Full Flow Control)
2555              * and the link partner advertised none, we will configure
2556              * ourselves to enable Rx Flow Control only.  We can do
2557              * this safely for two reasons:  If the link partner really
2558              * didn't want flow control enabled, and we enable Rx, no
2559              * harm done since we won't be receiving any PAUSE frames
2560              * anyway.  If the intent on the link partner was to have
2561              * flow control enabled, then by us enabling RX only, we
2562              * can at least receive pause frames and process them.
2563              * This is a good idea because in most cases, since we are
2564              * predominantly a server NIC, more times than not we will
2565              * be asked to delay transmission of packets than asking
2566              * our link partner to pause transmission of frames.
2567              */
2568             else if((hw->original_fc == e1000_fc_none ||
2569                      hw->original_fc == e1000_fc_tx_pause) ||
2570                     hw->fc_strict_ieee) {
2571                 hw->fc = e1000_fc_none;
2572                 DEBUGOUT("Flow Control = NONE.\n");
2573             } else {
2574                 hw->fc = e1000_fc_rx_pause;
2575                 DEBUGOUT("Flow Control = RX PAUSE frames only.\n");
2576             }
2577
2578             /* Now we need to do one last check...  If we auto-
2579              * negotiated to HALF DUPLEX, flow control should not be
2580              * enabled per IEEE 802.3 spec.
2581              */
2582             ret_val = e1000_get_speed_and_duplex(hw, &speed, &duplex);
2583             if(ret_val) {
2584                 DEBUGOUT("Error getting link speed and duplex\n");
2585                 return ret_val;
2586             }
2587
2588             if(duplex == HALF_DUPLEX)
2589                 hw->fc = e1000_fc_none;
2590
2591             /* Now we call a subroutine to actually force the MAC
2592              * controller to use the correct flow control settings.
2593              */
2594             ret_val = e1000_force_mac_fc(hw);
2595             if(ret_val) {
2596                 DEBUGOUT("Error forcing flow control settings\n");
2597                 return ret_val;
2598             }
2599         } else {
2600             DEBUGOUT("Copper PHY and Auto Neg has not completed.\n");
2601         }
2602     }
2603     return E1000_SUCCESS;
2604 }
2605
2606 /******************************************************************************
2607  * Checks to see if the link status of the hardware has changed.
2608  *
2609  * hw - Struct containing variables accessed by shared code
2610  *
2611  * Called by any function that needs to check the link status of the adapter.
2612  *****************************************************************************/
2613 int32_t
2614 e1000_check_for_link(struct e1000_hw *hw)
2615 {
2616     uint32_t rxcw = 0;
2617     uint32_t ctrl;
2618     uint32_t status;
2619     uint32_t rctl;
2620     uint32_t icr;
2621     uint32_t signal = 0;
2622     int32_t ret_val;
2623     uint16_t phy_data;
2624
2625     DEBUGFUNC("e1000_check_for_link");
2626
2627     ctrl = E1000_READ_REG(hw, CTRL);
2628     status = E1000_READ_REG(hw, STATUS);
2629
2630     /* On adapters with a MAC newer than 82544, SW Defineable pin 1 will be
2631      * set when the optics detect a signal. On older adapters, it will be
2632      * cleared when there is a signal.  This applies to fiber media only.
2633      */
2634     if((hw->media_type == e1000_media_type_fiber) ||
2635        (hw->media_type == e1000_media_type_internal_serdes)) {
2636         rxcw = E1000_READ_REG(hw, RXCW);
2637
2638         if(hw->media_type == e1000_media_type_fiber) {
2639             signal = (hw->mac_type > e1000_82544) ? E1000_CTRL_SWDPIN1 : 0;
2640             if(status & E1000_STATUS_LU)
2641                 hw->get_link_status = FALSE;
2642         }
2643     }
2644
2645     /* If we have a copper PHY then we only want to go out to the PHY
2646      * registers to see if Auto-Neg has completed and/or if our link
2647      * status has changed.  The get_link_status flag will be set if we
2648      * receive a Link Status Change interrupt or we have Rx Sequence
2649      * Errors.
2650      */
2651     if((hw->media_type == e1000_media_type_copper) && hw->get_link_status) {
2652         /* First we want to see if the MII Status Register reports
2653          * link.  If so, then we want to get the current speed/duplex
2654          * of the PHY.
2655          * Read the register twice since the link bit is sticky.
2656          */
2657         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
2658         if(ret_val)
2659             return ret_val;
2660         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
2661         if(ret_val)
2662             return ret_val;
2663
2664         if(phy_data & MII_SR_LINK_STATUS) {
2665             hw->get_link_status = FALSE;
2666             /* Check if there was DownShift, must be checked immediately after
2667              * link-up */
2668             e1000_check_downshift(hw);
2669
2670             /* If we are on 82544 or 82543 silicon and speed/duplex
2671              * are forced to 10H or 10F, then we will implement the polarity
2672              * reversal workaround.  We disable interrupts first, and upon
2673              * returning, place the devices interrupt state to its previous
2674              * value except for the link status change interrupt which will
2675              * happen due to the execution of this workaround.
2676              */
2677
2678             if((hw->mac_type == e1000_82544 || hw->mac_type == e1000_82543) &&
2679                (!hw->autoneg) &&
2680                (hw->forced_speed_duplex == e1000_10_full ||
2681                 hw->forced_speed_duplex == e1000_10_half)) {
2682                 E1000_WRITE_REG(hw, IMC, 0xffffffff);
2683                 ret_val = e1000_polarity_reversal_workaround(hw);
2684                 icr = E1000_READ_REG(hw, ICR);
2685                 E1000_WRITE_REG(hw, ICS, (icr & ~E1000_ICS_LSC));
2686                 E1000_WRITE_REG(hw, IMS, IMS_ENABLE_MASK);
2687             }
2688
2689         } else {
2690             /* No link detected */
2691             e1000_config_dsp_after_link_change(hw, FALSE);
2692             return 0;
2693         }
2694
2695         /* If we are forcing speed/duplex, then we simply return since
2696          * we have already determined whether we have link or not.
2697          */
2698         if(!hw->autoneg) return -E1000_ERR_CONFIG;
2699
2700         /* optimize the dsp settings for the igp phy */
2701         e1000_config_dsp_after_link_change(hw, TRUE);
2702
2703         /* We have a M88E1000 PHY and Auto-Neg is enabled.  If we
2704          * have Si on board that is 82544 or newer, Auto
2705          * Speed Detection takes care of MAC speed/duplex
2706          * configuration.  So we only need to configure Collision
2707          * Distance in the MAC.  Otherwise, we need to force
2708          * speed/duplex on the MAC to the current PHY speed/duplex
2709          * settings.
2710          */
2711         if(hw->mac_type >= e1000_82544)
2712             e1000_config_collision_dist(hw);
2713         else {
2714             ret_val = e1000_config_mac_to_phy(hw);
2715             if(ret_val) {
2716                 DEBUGOUT("Error configuring MAC to PHY settings\n");
2717                 return ret_val;
2718             }
2719         }
2720
2721         /* Configure Flow Control now that Auto-Neg has completed. First, we
2722          * need to restore the desired flow control settings because we may
2723          * have had to re-autoneg with a different link partner.
2724          */
2725         ret_val = e1000_config_fc_after_link_up(hw);
2726         if(ret_val) {
2727             DEBUGOUT("Error configuring flow control\n");
2728             return ret_val;
2729         }
2730
2731         /* At this point we know that we are on copper and we have
2732          * auto-negotiated link.  These are conditions for checking the link
2733          * partner capability register.  We use the link speed to determine if
2734          * TBI compatibility needs to be turned on or off.  If the link is not
2735          * at gigabit speed, then TBI compatibility is not needed.  If we are
2736          * at gigabit speed, we turn on TBI compatibility.
2737          */
2738         if(hw->tbi_compatibility_en) {
2739             uint16_t speed, duplex;
2740             ret_val = e1000_get_speed_and_duplex(hw, &speed, &duplex);
2741             if (ret_val) {
2742                 DEBUGOUT("Error getting link speed and duplex\n");
2743                 return ret_val;
2744             }
2745             if (speed != SPEED_1000) {
2746                 /* If link speed is not set to gigabit speed, we do not need
2747                  * to enable TBI compatibility.
2748                  */
2749                 if(hw->tbi_compatibility_on) {
2750                     /* If we previously were in the mode, turn it off. */
2751                     rctl = E1000_READ_REG(hw, RCTL);
2752                     rctl &= ~E1000_RCTL_SBP;
2753                     E1000_WRITE_REG(hw, RCTL, rctl);
2754                     hw->tbi_compatibility_on = FALSE;
2755                 }
2756             } else {
2757                 /* If TBI compatibility is was previously off, turn it on. For
2758                  * compatibility with a TBI link partner, we will store bad
2759                  * packets. Some frames have an additional byte on the end and
2760                  * will look like CRC errors to to the hardware.
2761                  */
2762                 if(!hw->tbi_compatibility_on) {
2763                     hw->tbi_compatibility_on = TRUE;
2764                     rctl = E1000_READ_REG(hw, RCTL);
2765                     rctl |= E1000_RCTL_SBP;
2766                     E1000_WRITE_REG(hw, RCTL, rctl);
2767                 }
2768             }
2769         }
2770     }
2771     /* If we don't have link (auto-negotiation failed or link partner cannot
2772      * auto-negotiate), the cable is plugged in (we have signal), and our
2773      * link partner is not trying to auto-negotiate with us (we are receiving
2774      * idles or data), we need to force link up. We also need to give
2775      * auto-negotiation time to complete, in case the cable was just plugged
2776      * in. The autoneg_failed flag does this.
2777      */
2778     else if((((hw->media_type == e1000_media_type_fiber) &&
2779               ((ctrl & E1000_CTRL_SWDPIN1) == signal)) ||
2780              (hw->media_type == e1000_media_type_internal_serdes)) &&
2781             (!(status & E1000_STATUS_LU)) &&
2782             (!(rxcw & E1000_RXCW_C))) {
2783         if(hw->autoneg_failed == 0) {
2784             hw->autoneg_failed = 1;
2785             return 0;
2786         }
2787         DEBUGOUT("NOT RXing /C/, disable AutoNeg and force link.\n");
2788
2789         /* Disable auto-negotiation in the TXCW register */
2790         E1000_WRITE_REG(hw, TXCW, (hw->txcw & ~E1000_TXCW_ANE));
2791
2792         /* Force link-up and also force full-duplex. */
2793         ctrl = E1000_READ_REG(hw, CTRL);
2794         ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FD);
2795         E1000_WRITE_REG(hw, CTRL, ctrl);
2796
2797         /* Configure Flow Control after forcing link up. */
2798         ret_val = e1000_config_fc_after_link_up(hw);
2799         if(ret_val) {
2800             DEBUGOUT("Error configuring flow control\n");
2801             return ret_val;
2802         }
2803     }
2804     /* If we are forcing link and we are receiving /C/ ordered sets, re-enable
2805      * auto-negotiation in the TXCW register and disable forced link in the
2806      * Device Control register in an attempt to auto-negotiate with our link
2807      * partner.
2808      */
2809     else if(((hw->media_type == e1000_media_type_fiber) ||
2810              (hw->media_type == e1000_media_type_internal_serdes)) &&
2811             (ctrl & E1000_CTRL_SLU) && (rxcw & E1000_RXCW_C)) {
2812         DEBUGOUT("RXing /C/, enable AutoNeg and stop forcing link.\n");
2813         E1000_WRITE_REG(hw, TXCW, hw->txcw);
2814         E1000_WRITE_REG(hw, CTRL, (ctrl & ~E1000_CTRL_SLU));
2815
2816         hw->serdes_link_down = FALSE;
2817     }
2818     /* If we force link for non-auto-negotiation switch, check link status
2819      * based on MAC synchronization for internal serdes media type.
2820      */
2821     else if((hw->media_type == e1000_media_type_internal_serdes) &&
2822             !(E1000_TXCW_ANE & E1000_READ_REG(hw, TXCW))) {
2823         /* SYNCH bit and IV bit are sticky. */
2824         udelay(10);
2825         if(E1000_RXCW_SYNCH & E1000_READ_REG(hw, RXCW)) {
2826             if(!(rxcw & E1000_RXCW_IV)) {
2827                 hw->serdes_link_down = FALSE;
2828                 DEBUGOUT("SERDES: Link is up.\n");
2829             }
2830         } else {
2831             hw->serdes_link_down = TRUE;
2832             DEBUGOUT("SERDES: Link is down.\n");
2833         }
2834     }
2835     if((hw->media_type == e1000_media_type_internal_serdes) &&
2836        (E1000_TXCW_ANE & E1000_READ_REG(hw, TXCW))) {
2837         hw->serdes_link_down = !(E1000_STATUS_LU & E1000_READ_REG(hw, STATUS));
2838     }
2839     return E1000_SUCCESS;
2840 }
2841
2842 /******************************************************************************
2843  * Detects the current speed and duplex settings of the hardware.
2844  *
2845  * hw - Struct containing variables accessed by shared code
2846  * speed - Speed of the connection
2847  * duplex - Duplex setting of the connection
2848  *****************************************************************************/
2849 int32_t
2850 e1000_get_speed_and_duplex(struct e1000_hw *hw,
2851                            uint16_t *speed,
2852                            uint16_t *duplex)
2853 {
2854     uint32_t status;
2855     int32_t ret_val;
2856     uint16_t phy_data;
2857
2858     DEBUGFUNC("e1000_get_speed_and_duplex");
2859
2860     if(hw->mac_type >= e1000_82543) {
2861         status = E1000_READ_REG(hw, STATUS);
2862         if(status & E1000_STATUS_SPEED_1000) {
2863             *speed = SPEED_1000;
2864             DEBUGOUT("1000 Mbs, ");
2865         } else if(status & E1000_STATUS_SPEED_100) {
2866             *speed = SPEED_100;
2867             DEBUGOUT("100 Mbs, ");
2868         } else {
2869             *speed = SPEED_10;
2870             DEBUGOUT("10 Mbs, ");
2871         }
2872
2873         if(status & E1000_STATUS_FD) {
2874             *duplex = FULL_DUPLEX;
2875             DEBUGOUT("Full Duplex\n");
2876         } else {
2877             *duplex = HALF_DUPLEX;
2878             DEBUGOUT(" Half Duplex\n");
2879         }
2880     } else {
2881         DEBUGOUT("1000 Mbs, Full Duplex\n");
2882         *speed = SPEED_1000;
2883         *duplex = FULL_DUPLEX;
2884     }
2885
2886     /* IGP01 PHY may advertise full duplex operation after speed downgrade even
2887      * if it is operating at half duplex.  Here we set the duplex settings to
2888      * match the duplex in the link partner's capabilities.
2889      */
2890     if(hw->phy_type == e1000_phy_igp && hw->speed_downgraded) {
2891         ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_EXP, &phy_data);
2892         if(ret_val)
2893             return ret_val;
2894
2895         if(!(phy_data & NWAY_ER_LP_NWAY_CAPS))
2896             *duplex = HALF_DUPLEX;
2897         else {
2898             ret_val = e1000_read_phy_reg(hw, PHY_LP_ABILITY, &phy_data);
2899             if(ret_val)
2900                 return ret_val;
2901             if((*speed == SPEED_100 && !(phy_data & NWAY_LPAR_100TX_FD_CAPS)) ||
2902                (*speed == SPEED_10 && !(phy_data & NWAY_LPAR_10T_FD_CAPS)))
2903                 *duplex = HALF_DUPLEX;
2904         }
2905     }
2906
2907     if ((hw->mac_type == e1000_80003es2lan) &&
2908         (hw->media_type == e1000_media_type_copper)) {
2909         if (*speed == SPEED_1000)
2910             ret_val = e1000_configure_kmrn_for_1000(hw);
2911         else
2912             ret_val = e1000_configure_kmrn_for_10_100(hw);
2913         if (ret_val)
2914             return ret_val;
2915     }
2916
2917     return E1000_SUCCESS;
2918 }
2919
2920 /******************************************************************************
2921 * Blocks until autoneg completes or times out (~4.5 seconds)
2922 *
2923 * hw - Struct containing variables accessed by shared code
2924 ******************************************************************************/
2925 static int32_t
2926 e1000_wait_autoneg(struct e1000_hw *hw)
2927 {
2928     int32_t ret_val;
2929     uint16_t i;
2930     uint16_t phy_data;
2931
2932     DEBUGFUNC("e1000_wait_autoneg");
2933     DEBUGOUT("Waiting for Auto-Neg to complete.\n");
2934
2935     /* We will wait for autoneg to complete or 4.5 seconds to expire. */
2936     for(i = PHY_AUTO_NEG_TIME; i > 0; i--) {
2937         /* Read the MII Status Register and wait for Auto-Neg
2938          * Complete bit to be set.
2939          */
2940         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
2941         if(ret_val)
2942             return ret_val;
2943         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
2944         if(ret_val)
2945             return ret_val;
2946         if(phy_data & MII_SR_AUTONEG_COMPLETE) {
2947             return E1000_SUCCESS;
2948         }
2949         msec_delay(100);
2950     }
2951     return E1000_SUCCESS;
2952 }
2953
2954 /******************************************************************************
2955 * Raises the Management Data Clock
2956 *
2957 * hw - Struct containing variables accessed by shared code
2958 * ctrl - Device control register's current value
2959 ******************************************************************************/
2960 static void
2961 e1000_raise_mdi_clk(struct e1000_hw *hw,
2962                     uint32_t *ctrl)
2963 {
2964     /* Raise the clock input to the Management Data Clock (by setting the MDC
2965      * bit), and then delay 10 microseconds.
2966      */
2967     E1000_WRITE_REG(hw, CTRL, (*ctrl | E1000_CTRL_MDC));
2968     E1000_WRITE_FLUSH(hw);
2969     udelay(10);
2970 }
2971
2972 /******************************************************************************
2973 * Lowers the Management Data Clock
2974 *
2975 * hw - Struct containing variables accessed by shared code
2976 * ctrl - Device control register's current value
2977 ******************************************************************************/
2978 static void
2979 e1000_lower_mdi_clk(struct e1000_hw *hw,
2980                     uint32_t *ctrl)
2981 {
2982     /* Lower the clock input to the Management Data Clock (by clearing the MDC
2983      * bit), and then delay 10 microseconds.
2984      */
2985     E1000_WRITE_REG(hw, CTRL, (*ctrl & ~E1000_CTRL_MDC));
2986     E1000_WRITE_FLUSH(hw);
2987     udelay(10);
2988 }
2989
2990 /******************************************************************************
2991 * Shifts data bits out to the PHY
2992 *
2993 * hw - Struct containing variables accessed by shared code
2994 * data - Data to send out to the PHY
2995 * count - Number of bits to shift out
2996 *
2997 * Bits are shifted out in MSB to LSB order.
2998 ******************************************************************************/
2999 static void
3000 e1000_shift_out_mdi_bits(struct e1000_hw *hw,
3001                          uint32_t data,
3002                          uint16_t count)
3003 {
3004     uint32_t ctrl;
3005     uint32_t mask;
3006
3007     /* We need to shift "count" number of bits out to the PHY. So, the value
3008      * in the "data" parameter will be shifted out to the PHY one bit at a
3009      * time. In order to do this, "data" must be broken down into bits.
3010      */
3011     mask = 0x01;
3012     mask <<= (count - 1);
3013
3014     ctrl = E1000_READ_REG(hw, CTRL);
3015
3016     /* Set MDIO_DIR and MDC_DIR direction bits to be used as output pins. */
3017     ctrl |= (E1000_CTRL_MDIO_DIR | E1000_CTRL_MDC_DIR);
3018
3019     while(mask) {
3020         /* A "1" is shifted out to the PHY by setting the MDIO bit to "1" and
3021          * then raising and lowering the Management Data Clock. A "0" is
3022          * shifted out to the PHY by setting the MDIO bit to "0" and then
3023          * raising and lowering the clock.
3024          */
3025         if(data & mask) ctrl |= E1000_CTRL_MDIO;
3026         else ctrl &= ~E1000_CTRL_MDIO;
3027
3028         E1000_WRITE_REG(hw, CTRL, ctrl);
3029         E1000_WRITE_FLUSH(hw);
3030
3031         udelay(10);
3032
3033         e1000_raise_mdi_clk(hw, &ctrl);
3034         e1000_lower_mdi_clk(hw, &ctrl);
3035
3036         mask = mask >> 1;
3037     }
3038 }
3039
3040 /******************************************************************************
3041 * Shifts data bits in from the PHY
3042 *
3043 * hw - Struct containing variables accessed by shared code
3044 *
3045 * Bits are shifted in in MSB to LSB order.
3046 ******************************************************************************/
3047 static uint16_t
3048 e1000_shift_in_mdi_bits(struct e1000_hw *hw)
3049 {
3050     uint32_t ctrl;
3051     uint16_t data = 0;
3052     uint8_t i;
3053
3054     /* In order to read a register from the PHY, we need to shift in a total
3055      * of 18 bits from the PHY. The first two bit (turnaround) times are used
3056      * to avoid contention on the MDIO pin when a read operation is performed.
3057      * These two bits are ignored by us and thrown away. Bits are "shifted in"
3058      * by raising the input to the Management Data Clock (setting the MDC bit),
3059      * and then reading the value of the MDIO bit.
3060      */
3061     ctrl = E1000_READ_REG(hw, CTRL);
3062
3063     /* Clear MDIO_DIR (SWDPIO1) to indicate this bit is to be used as input. */
3064     ctrl &= ~E1000_CTRL_MDIO_DIR;
3065     ctrl &= ~E1000_CTRL_MDIO;
3066
3067     E1000_WRITE_REG(hw, CTRL, ctrl);
3068     E1000_WRITE_FLUSH(hw);
3069
3070     /* Raise and Lower the clock before reading in the data. This accounts for
3071      * the turnaround bits. The first clock occurred when we clocked out the
3072      * last bit of the Register Address.
3073      */
3074     e1000_raise_mdi_clk(hw, &ctrl);
3075     e1000_lower_mdi_clk(hw, &ctrl);
3076
3077     for(data = 0, i = 0; i < 16; i++) {
3078         data = data << 1;
3079         e1000_raise_mdi_clk(hw, &ctrl);
3080         ctrl = E1000_READ_REG(hw, CTRL);
3081         /* Check to see if we shifted in a "1". */
3082         if(ctrl & E1000_CTRL_MDIO) data |= 1;
3083         e1000_lower_mdi_clk(hw, &ctrl);
3084     }
3085
3086     e1000_raise_mdi_clk(hw, &ctrl);
3087     e1000_lower_mdi_clk(hw, &ctrl);
3088
3089     return data;
3090 }
3091
3092 int32_t
3093 e1000_swfw_sync_acquire(struct e1000_hw *hw, uint16_t mask)
3094 {
3095     uint32_t swfw_sync = 0;
3096     uint32_t swmask = mask;
3097     uint32_t fwmask = mask << 16;
3098     int32_t timeout = 200;
3099
3100     DEBUGFUNC("e1000_swfw_sync_acquire");
3101
3102     if (!hw->swfw_sync_present)
3103         return e1000_get_hw_eeprom_semaphore(hw);
3104
3105     while(timeout) {
3106             if (e1000_get_hw_eeprom_semaphore(hw))
3107                 return -E1000_ERR_SWFW_SYNC;
3108
3109             swfw_sync = E1000_READ_REG(hw, SW_FW_SYNC);
3110             if (!(swfw_sync & (fwmask | swmask))) {
3111                 break;
3112             }
3113
3114             /* firmware currently using resource (fwmask) */
3115             /* or other software thread currently using resource (swmask) */
3116             e1000_put_hw_eeprom_semaphore(hw);
3117             msec_delay_irq(5);
3118             timeout--;
3119     }
3120
3121     if (!timeout) {
3122         DEBUGOUT("Driver can't access resource, SW_FW_SYNC timeout.\n");
3123         return -E1000_ERR_SWFW_SYNC;
3124     }
3125
3126     swfw_sync |= swmask;
3127     E1000_WRITE_REG(hw, SW_FW_SYNC, swfw_sync);
3128
3129     e1000_put_hw_eeprom_semaphore(hw);
3130     return E1000_SUCCESS;
3131 }
3132
3133 void
3134 e1000_swfw_sync_release(struct e1000_hw *hw, uint16_t mask)
3135 {
3136     uint32_t swfw_sync;
3137     uint32_t swmask = mask;
3138
3139     DEBUGFUNC("e1000_swfw_sync_release");
3140
3141     if (!hw->swfw_sync_present) {
3142         e1000_put_hw_eeprom_semaphore(hw);
3143         return;
3144     }
3145
3146     /* if (e1000_get_hw_eeprom_semaphore(hw))
3147      *    return -E1000_ERR_SWFW_SYNC; */
3148     while (e1000_get_hw_eeprom_semaphore(hw) != E1000_SUCCESS);
3149         /* empty */
3150
3151     swfw_sync = E1000_READ_REG(hw, SW_FW_SYNC);
3152     swfw_sync &= ~swmask;
3153     E1000_WRITE_REG(hw, SW_FW_SYNC, swfw_sync);
3154
3155     e1000_put_hw_eeprom_semaphore(hw);
3156 }
3157
3158 /*****************************************************************************
3159 * Reads the value from a PHY register, if the value is on a specific non zero
3160 * page, sets the page first.
3161 * hw - Struct containing variables accessed by shared code
3162 * reg_addr - address of the PHY register to read
3163 ******************************************************************************/
3164 int32_t
3165 e1000_read_phy_reg(struct e1000_hw *hw,
3166                    uint32_t reg_addr,
3167                    uint16_t *phy_data)
3168 {
3169     uint32_t ret_val;
3170     uint16_t swfw;
3171
3172     DEBUGFUNC("e1000_read_phy_reg");
3173
3174     if ((hw->mac_type == e1000_80003es2lan) &&
3175         (E1000_READ_REG(hw, STATUS) & E1000_STATUS_FUNC_1)) {
3176         swfw = E1000_SWFW_PHY1_SM;
3177     } else {
3178         swfw = E1000_SWFW_PHY0_SM;
3179     }
3180     if (e1000_swfw_sync_acquire(hw, swfw))
3181         return -E1000_ERR_SWFW_SYNC;
3182
3183     if((hw->phy_type == e1000_phy_igp ||
3184         hw->phy_type == e1000_phy_igp_2) &&
3185        (reg_addr > MAX_PHY_MULTI_PAGE_REG)) {
3186         ret_val = e1000_write_phy_reg_ex(hw, IGP01E1000_PHY_PAGE_SELECT,
3187                                          (uint16_t)reg_addr);
3188         if(ret_val) {
3189             e1000_swfw_sync_release(hw, swfw);
3190             return ret_val;
3191         }
3192     } else if (hw->phy_type == e1000_phy_gg82563) {
3193         if (((reg_addr & MAX_PHY_REG_ADDRESS) > MAX_PHY_MULTI_PAGE_REG) ||
3194             (hw->mac_type == e1000_80003es2lan)) {
3195             /* Select Configuration Page */
3196             if ((reg_addr & MAX_PHY_REG_ADDRESS) < GG82563_MIN_ALT_REG) {
3197                 ret_val = e1000_write_phy_reg_ex(hw, GG82563_PHY_PAGE_SELECT,
3198                           (uint16_t)((uint16_t)reg_addr >> GG82563_PAGE_SHIFT));
3199             } else {
3200                 /* Use Alternative Page Select register to access
3201                  * registers 30 and 31
3202                  */
3203                 ret_val = e1000_write_phy_reg_ex(hw,
3204                                                  GG82563_PHY_PAGE_SELECT_ALT,
3205                           (uint16_t)((uint16_t)reg_addr >> GG82563_PAGE_SHIFT));
3206             }
3207
3208             if (ret_val) {
3209                 e1000_swfw_sync_release(hw, swfw);
3210                 return ret_val;
3211             }
3212         }
3213     }
3214
3215     ret_val = e1000_read_phy_reg_ex(hw, MAX_PHY_REG_ADDRESS & reg_addr,
3216                                     phy_data);
3217
3218     e1000_swfw_sync_release(hw, swfw);
3219     return ret_val;
3220 }
3221
3222 int32_t
3223 e1000_read_phy_reg_ex(struct e1000_hw *hw,
3224                       uint32_t reg_addr,
3225                       uint16_t *phy_data)
3226 {
3227     uint32_t i;
3228     uint32_t mdic = 0;
3229     const uint32_t phy_addr = 1;
3230
3231     DEBUGFUNC("e1000_read_phy_reg_ex");
3232
3233     if(reg_addr > MAX_PHY_REG_ADDRESS) {
3234         DEBUGOUT1("PHY Address %d is out of range\n", reg_addr);
3235         return -E1000_ERR_PARAM;
3236     }
3237
3238     if(hw->mac_type > e1000_82543) {
3239         /* Set up Op-code, Phy Address, and register address in the MDI
3240          * Control register.  The MAC will take care of interfacing with the
3241          * PHY to retrieve the desired data.
3242          */
3243         mdic = ((reg_addr << E1000_MDIC_REG_SHIFT) |
3244                 (phy_addr << E1000_MDIC_PHY_SHIFT) |
3245                 (E1000_MDIC_OP_READ));
3246
3247         E1000_WRITE_REG(hw, MDIC, mdic);
3248
3249         /* Poll the ready bit to see if the MDI read completed */
3250         for(i = 0; i < 64; i++) {
3251             udelay(50);
3252             mdic = E1000_READ_REG(hw, MDIC);
3253             if(mdic & E1000_MDIC_READY) break;
3254         }
3255         if(!(mdic & E1000_MDIC_READY)) {
3256             DEBUGOUT("MDI Read did not complete\n");
3257             return -E1000_ERR_PHY;
3258         }
3259         if(mdic & E1000_MDIC_ERROR) {
3260             DEBUGOUT("MDI Error\n");
3261             return -E1000_ERR_PHY;
3262         }
3263         *phy_data = (uint16_t) mdic;
3264     } else {
3265         /* We must first send a preamble through the MDIO pin to signal the
3266          * beginning of an MII instruction.  This is done by sending 32
3267          * consecutive "1" bits.
3268          */
3269         e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
3270
3271         /* Now combine the next few fields that are required for a read
3272          * operation.  We use this method instead of calling the
3273          * e1000_shift_out_mdi_bits routine five different times. The format of
3274          * a MII read instruction consists of a shift out of 14 bits and is
3275          * defined as follows:
3276          *    <Preamble><SOF><Op Code><Phy Addr><Reg Addr>
3277          * followed by a shift in of 18 bits.  This first two bits shifted in
3278          * are TurnAround bits used to avoid contention on the MDIO pin when a
3279          * READ operation is performed.  These two bits are thrown away
3280          * followed by a shift in of 16 bits which contains the desired data.
3281          */
3282         mdic = ((reg_addr) | (phy_addr << 5) |
3283                 (PHY_OP_READ << 10) | (PHY_SOF << 12));
3284
3285         e1000_shift_out_mdi_bits(hw, mdic, 14);
3286
3287         /* Now that we've shifted out the read command to the MII, we need to
3288          * "shift in" the 16-bit value (18 total bits) of the requested PHY
3289          * register address.
3290          */
3291         *phy_data = e1000_shift_in_mdi_bits(hw);
3292     }
3293     return E1000_SUCCESS;
3294 }
3295
3296 /******************************************************************************
3297 * Writes a value to a PHY register
3298 *
3299 * hw - Struct containing variables accessed by shared code
3300 * reg_addr - address of the PHY register to write
3301 * data - data to write to the PHY
3302 ******************************************************************************/
3303 int32_t
3304 e1000_write_phy_reg(struct e1000_hw *hw,
3305                     uint32_t reg_addr,
3306                     uint16_t phy_data)
3307 {
3308     uint32_t ret_val;
3309     uint16_t swfw;
3310
3311     DEBUGFUNC("e1000_write_phy_reg");
3312
3313     if ((hw->mac_type == e1000_80003es2lan) &&
3314         (E1000_READ_REG(hw, STATUS) & E1000_STATUS_FUNC_1)) {
3315         swfw = E1000_SWFW_PHY1_SM;
3316     } else {
3317         swfw = E1000_SWFW_PHY0_SM;
3318     }
3319     if (e1000_swfw_sync_acquire(hw, swfw))
3320         return -E1000_ERR_SWFW_SYNC;
3321
3322     if((hw->phy_type == e1000_phy_igp ||
3323         hw->phy_type == e1000_phy_igp_2) &&
3324        (reg_addr > MAX_PHY_MULTI_PAGE_REG)) {
3325         ret_val = e1000_write_phy_reg_ex(hw, IGP01E1000_PHY_PAGE_SELECT,
3326                                          (uint16_t)reg_addr);
3327         if(ret_val) {
3328             e1000_swfw_sync_release(hw, swfw);
3329             return ret_val;
3330         }
3331     } else if (hw->phy_type == e1000_phy_gg82563) {
3332         if (((reg_addr & MAX_PHY_REG_ADDRESS) > MAX_PHY_MULTI_PAGE_REG) ||
3333             (hw->mac_type == e1000_80003es2lan)) {
3334             /* Select Configuration Page */
3335             if ((reg_addr & MAX_PHY_REG_ADDRESS) < GG82563_MIN_ALT_REG) {
3336                 ret_val = e1000_write_phy_reg_ex(hw, GG82563_PHY_PAGE_SELECT,
3337                           (uint16_t)((uint16_t)reg_addr >> GG82563_PAGE_SHIFT));
3338             } else {
3339                 /* Use Alternative Page Select register to access
3340                  * registers 30 and 31
3341                  */
3342                 ret_val = e1000_write_phy_reg_ex(hw,
3343                                                  GG82563_PHY_PAGE_SELECT_ALT,
3344                           (uint16_t)((uint16_t)reg_addr >> GG82563_PAGE_SHIFT));
3345             }
3346
3347             if (ret_val) {
3348                 e1000_swfw_sync_release(hw, swfw);
3349                 return ret_val;
3350             }
3351         }
3352     }
3353
3354     ret_val = e1000_write_phy_reg_ex(hw, MAX_PHY_REG_ADDRESS & reg_addr,
3355                                      phy_data);
3356
3357     e1000_swfw_sync_release(hw, swfw);
3358     return ret_val;
3359 }
3360
3361 int32_t
3362 e1000_write_phy_reg_ex(struct e1000_hw *hw,
3363                     uint32_t reg_addr,
3364                     uint16_t phy_data)
3365 {
3366     uint32_t i;
3367     uint32_t mdic = 0;
3368     const uint32_t phy_addr = 1;
3369
3370     DEBUGFUNC("e1000_write_phy_reg_ex");
3371
3372     if(reg_addr > MAX_PHY_REG_ADDRESS) {
3373         DEBUGOUT1("PHY Address %d is out of range\n", reg_addr);
3374         return -E1000_ERR_PARAM;
3375     }
3376
3377     if(hw->mac_type > e1000_82543) {
3378         /* Set up Op-code, Phy Address, register address, and data intended
3379          * for the PHY register in the MDI Control register.  The MAC will take
3380          * care of interfacing with the PHY to send the desired data.
3381          */
3382         mdic = (((uint32_t) phy_data) |
3383                 (reg_addr << E1000_MDIC_REG_SHIFT) |
3384                 (phy_addr << E1000_MDIC_PHY_SHIFT) |
3385                 (E1000_MDIC_OP_WRITE));
3386
3387         E1000_WRITE_REG(hw, MDIC, mdic);
3388
3389         /* Poll the ready bit to see if the MDI read completed */
3390         for(i = 0; i < 640; i++) {
3391             udelay(5);
3392             mdic = E1000_READ_REG(hw, MDIC);
3393             if(mdic & E1000_MDIC_READY) break;
3394         }
3395         if(!(mdic & E1000_MDIC_READY)) {
3396             DEBUGOUT("MDI Write did not complete\n");
3397             return -E1000_ERR_PHY;
3398         }
3399     } else {
3400         /* We'll need to use the SW defined pins to shift the write command
3401          * out to the PHY. We first send a preamble to the PHY to signal the
3402          * beginning of the MII instruction.  This is done by sending 32
3403          * consecutive "1" bits.
3404          */
3405         e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
3406
3407         /* Now combine the remaining required fields that will indicate a
3408          * write operation. We use this method instead of calling the
3409          * e1000_shift_out_mdi_bits routine for each field in the command. The
3410          * format of a MII write instruction is as follows:
3411          * <Preamble><SOF><Op Code><Phy Addr><Reg Addr><Turnaround><Data>.
3412          */
3413         mdic = ((PHY_TURNAROUND) | (reg_addr << 2) | (phy_addr << 7) |
3414                 (PHY_OP_WRITE << 12) | (PHY_SOF << 14));
3415         mdic <<= 16;
3416         mdic |= (uint32_t) phy_data;
3417
3418         e1000_shift_out_mdi_bits(hw, mdic, 32);
3419     }
3420
3421     return E1000_SUCCESS;
3422 }
3423
3424 int32_t
3425 e1000_read_kmrn_reg(struct e1000_hw *hw,
3426                     uint32_t reg_addr,
3427                     uint16_t *data)
3428 {
3429     uint32_t reg_val;
3430     uint16_t swfw;
3431     DEBUGFUNC("e1000_read_kmrn_reg");
3432
3433     if ((hw->mac_type == e1000_80003es2lan) &&
3434         (E1000_READ_REG(hw, STATUS) & E1000_STATUS_FUNC_1)) {
3435         swfw = E1000_SWFW_PHY1_SM;
3436     } else {
3437         swfw = E1000_SWFW_PHY0_SM;
3438     }
3439     if (e1000_swfw_sync_acquire(hw, swfw))
3440         return -E1000_ERR_SWFW_SYNC;
3441
3442     /* Write register address */
3443     reg_val = ((reg_addr << E1000_KUMCTRLSTA_OFFSET_SHIFT) &
3444               E1000_KUMCTRLSTA_OFFSET) |
3445               E1000_KUMCTRLSTA_REN;
3446     E1000_WRITE_REG(hw, KUMCTRLSTA, reg_val);
3447     udelay(2);
3448
3449     /* Read the data returned */
3450     reg_val = E1000_READ_REG(hw, KUMCTRLSTA);
3451     *data = (uint16_t)reg_val;
3452
3453     e1000_swfw_sync_release(hw, swfw);
3454     return E1000_SUCCESS;
3455 }
3456
3457 int32_t
3458 e1000_write_kmrn_reg(struct e1000_hw *hw,
3459                      uint32_t reg_addr,
3460                      uint16_t data)
3461 {
3462     uint32_t reg_val;
3463     uint16_t swfw;
3464     DEBUGFUNC("e1000_write_kmrn_reg");
3465
3466     if ((hw->mac_type == e1000_80003es2lan) &&
3467         (E1000_READ_REG(hw, STATUS) & E1000_STATUS_FUNC_1)) {
3468         swfw = E1000_SWFW_PHY1_SM;
3469     } else {
3470         swfw = E1000_SWFW_PHY0_SM;
3471     }
3472     if (e1000_swfw_sync_acquire(hw, swfw))
3473         return -E1000_ERR_SWFW_SYNC;
3474
3475     reg_val = ((reg_addr << E1000_KUMCTRLSTA_OFFSET_SHIFT) &
3476               E1000_KUMCTRLSTA_OFFSET) | data;
3477     E1000_WRITE_REG(hw, KUMCTRLSTA, reg_val);
3478     udelay(2);
3479
3480     e1000_swfw_sync_release(hw, swfw);
3481     return E1000_SUCCESS;
3482 }
3483
3484 /******************************************************************************
3485 * Returns the PHY to the power-on reset state
3486 *
3487 * hw - Struct containing variables accessed by shared code
3488 ******************************************************************************/
3489 int32_t
3490 e1000_phy_hw_reset(struct e1000_hw *hw)
3491 {
3492     uint32_t ctrl, ctrl_ext;
3493     uint32_t led_ctrl;
3494     int32_t ret_val;
3495     uint16_t swfw;
3496
3497     DEBUGFUNC("e1000_phy_hw_reset");
3498
3499     /* In the case of the phy reset being blocked, it's not an error, we
3500      * simply return success without performing the reset. */
3501     ret_val = e1000_check_phy_reset_block(hw);
3502     if (ret_val)
3503         return E1000_SUCCESS;
3504
3505     DEBUGOUT("Resetting Phy...\n");
3506
3507     if(hw->mac_type > e1000_82543) {
3508         if ((hw->mac_type == e1000_80003es2lan) &&
3509             (E1000_READ_REG(hw, STATUS) & E1000_STATUS_FUNC_1)) {
3510             swfw = E1000_SWFW_PHY1_SM;
3511         } else {
3512             swfw = E1000_SWFW_PHY0_SM;
3513         }
3514         if (e1000_swfw_sync_acquire(hw, swfw)) {
3515             e1000_release_software_semaphore(hw);
3516             return -E1000_ERR_SWFW_SYNC;
3517         }
3518         /* Read the device control register and assert the E1000_CTRL_PHY_RST
3519          * bit. Then, take it out of reset.
3520          * For pre-e1000_82571 hardware, we delay for 10ms between the assert
3521          * and deassert.  For e1000_82571 hardware and later, we instead delay
3522          * for 50us between and 10ms after the deassertion.
3523          */
3524         ctrl = E1000_READ_REG(hw, CTRL);
3525         E1000_WRITE_REG(hw, CTRL, ctrl | E1000_CTRL_PHY_RST);
3526         E1000_WRITE_FLUSH(hw);
3527
3528         if (hw->mac_type < e1000_82571)
3529             msec_delay(10);
3530         else
3531             udelay(100);
3532
3533         E1000_WRITE_REG(hw, CTRL, ctrl);
3534         E1000_WRITE_FLUSH(hw);
3535
3536         if (hw->mac_type >= e1000_82571)
3537             msec_delay(10);
3538         e1000_swfw_sync_release(hw, swfw);
3539     } else {
3540         /* Read the Extended Device Control Register, assert the PHY_RESET_DIR
3541          * bit to put the PHY into reset. Then, take it out of reset.
3542          */
3543         ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
3544         ctrl_ext |= E1000_CTRL_EXT_SDP4_DIR;
3545         ctrl_ext &= ~E1000_CTRL_EXT_SDP4_DATA;
3546         E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
3547         E1000_WRITE_FLUSH(hw);
3548         msec_delay(10);
3549         ctrl_ext |= E1000_CTRL_EXT_SDP4_DATA;
3550         E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
3551         E1000_WRITE_FLUSH(hw);
3552     }
3553     udelay(150);
3554
3555     if((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
3556         /* Configure activity LED after PHY reset */
3557         led_ctrl = E1000_READ_REG(hw, LEDCTL);
3558         led_ctrl &= IGP_ACTIVITY_LED_MASK;
3559         led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
3560         E1000_WRITE_REG(hw, LEDCTL, led_ctrl);
3561     }
3562
3563     /* Wait for FW to finish PHY configuration. */
3564     ret_val = e1000_get_phy_cfg_done(hw);
3565     e1000_release_software_semaphore(hw);
3566
3567     return ret_val;
3568 }
3569
3570 /******************************************************************************
3571 * Resets the PHY
3572 *
3573 * hw - Struct containing variables accessed by shared code
3574 *
3575 * Sets bit 15 of the MII Control regiser
3576 ******************************************************************************/
3577 int32_t
3578 e1000_phy_reset(struct e1000_hw *hw)
3579 {
3580     int32_t ret_val;
3581     uint16_t phy_data;
3582
3583     DEBUGFUNC("e1000_phy_reset");
3584
3585     /* In the case of the phy reset being blocked, it's not an error, we
3586      * simply return success without performing the reset. */
3587     ret_val = e1000_check_phy_reset_block(hw);
3588     if (ret_val)
3589         return E1000_SUCCESS;
3590
3591     switch (hw->mac_type) {
3592     case e1000_82541_rev_2:
3593     case e1000_82571:
3594     case e1000_82572:
3595         ret_val = e1000_phy_hw_reset(hw);
3596         if(ret_val)
3597             return ret_val;
3598         break;
3599     default:
3600         ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
3601         if(ret_val)
3602             return ret_val;
3603
3604         phy_data |= MII_CR_RESET;
3605         ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
3606         if(ret_val)
3607             return ret_val;
3608
3609         udelay(1);
3610         break;
3611     }
3612
3613     if(hw->phy_type == e1000_phy_igp || hw->phy_type == e1000_phy_igp_2)
3614         e1000_phy_init_script(hw);
3615
3616     return E1000_SUCCESS;
3617 }
3618
3619 /******************************************************************************
3620 * Work-around for 82566 power-down: on D3 entry-
3621 * 1) disable gigabit link
3622 * 2) write VR power-down enable
3623 * 3) read it back
3624 * if successful continue, else issue LCD reset and repeat
3625 *
3626 * hw - struct containing variables accessed by shared code
3627 ******************************************************************************/
3628 void
3629 e1000_phy_powerdown_workaround(struct e1000_hw *hw)
3630 {
3631     int32_t reg;
3632     uint16_t phy_data;
3633     int32_t retry = 0;
3634
3635     DEBUGFUNC("e1000_phy_powerdown_workaround");
3636
3637     if (hw->phy_type != e1000_phy_igp_3)
3638         return;
3639
3640     do {
3641         /* Disable link */
3642         reg = E1000_READ_REG(hw, PHY_CTRL);
3643         E1000_WRITE_REG(hw, PHY_CTRL, reg | E1000_PHY_CTRL_GBE_DISABLE |
3644                         E1000_PHY_CTRL_NOND0A_GBE_DISABLE);
3645
3646         /* Write VR power-down enable */
3647         e1000_read_phy_reg(hw, IGP3_VR_CTRL, &phy_data);
3648         e1000_write_phy_reg(hw, IGP3_VR_CTRL, phy_data |
3649                             IGP3_VR_CTRL_MODE_SHUT);
3650
3651         /* Read it back and test */
3652         e1000_read_phy_reg(hw, IGP3_VR_CTRL, &phy_data);
3653         if ((phy_data & IGP3_VR_CTRL_MODE_SHUT) || retry)
3654             break;
3655
3656         /* Issue PHY reset and repeat at most one more time */
3657         reg = E1000_READ_REG(hw, CTRL);
3658         E1000_WRITE_REG(hw, CTRL, reg | E1000_CTRL_PHY_RST);
3659         retry++;
3660     } while (retry);
3661
3662     return;
3663
3664 }
3665
3666 /******************************************************************************
3667 * Work-around for 82566 Kumeran PCS lock loss:
3668 * On link status change (i.e. PCI reset, speed change) and link is up and
3669 * speed is gigabit-
3670 * 0) if workaround is optionally disabled do nothing
3671 * 1) wait 1ms for Kumeran link to come up
3672 * 2) check Kumeran Diagnostic register PCS lock loss bit
3673 * 3) if not set the link is locked (all is good), otherwise...
3674 * 4) reset the PHY
3675 * 5) repeat up to 10 times
3676 * Note: this is only called for IGP3 copper when speed is 1gb.
3677 *
3678 * hw - struct containing variables accessed by shared code
3679 ******************************************************************************/
3680 int32_t
3681 e1000_kumeran_lock_loss_workaround(struct e1000_hw *hw)
3682 {
3683     int32_t ret_val;
3684     int32_t reg;
3685     int32_t cnt;
3686     uint16_t phy_data;
3687
3688     if (hw->kmrn_lock_loss_workaround_disabled)
3689         return E1000_SUCCESS;
3690
3691     /* Make sure link is up before proceeding. If not just return.
3692      * Attempting this while link is negotiating fouls up link
3693      * stability */
3694     ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
3695     ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
3696
3697     if (phy_data & MII_SR_LINK_STATUS) {
3698         for (cnt = 0; cnt < 10; cnt++) {
3699             /* read once to clear */
3700             ret_val = e1000_read_phy_reg(hw, IGP3_KMRN_DIAG, &phy_data);
3701             if (ret_val)
3702                 return ret_val;
3703             /* and again to get new status */
3704             ret_val = e1000_read_phy_reg(hw, IGP3_KMRN_DIAG, &phy_data);
3705             if (ret_val)
3706                 return ret_val;
3707
3708             /* check for PCS lock */
3709             if (!(phy_data & IGP3_KMRN_DIAG_PCS_LOCK_LOSS))
3710                 return E1000_SUCCESS;
3711
3712             /* Issue PHY reset */
3713             e1000_phy_hw_reset(hw);
3714             msec_delay_irq(5);
3715         }
3716         /* Disable GigE link negotiation */
3717         reg = E1000_READ_REG(hw, PHY_CTRL);
3718         E1000_WRITE_REG(hw, PHY_CTRL, reg | E1000_PHY_CTRL_GBE_DISABLE |
3719                         E1000_PHY_CTRL_NOND0A_GBE_DISABLE);
3720
3721         /* unable to acquire PCS lock */
3722         return E1000_ERR_PHY;
3723     }
3724
3725     return E1000_SUCCESS;
3726 }
3727
3728 /******************************************************************************
3729 * Probes the expected PHY address for known PHY IDs
3730 *
3731 * hw - Struct containing variables accessed by shared code
3732 ******************************************************************************/
3733 int32_t
3734 e1000_detect_gig_phy(struct e1000_hw *hw)
3735 {
3736     int32_t phy_init_status, ret_val;
3737     uint16_t phy_id_high, phy_id_low;
3738     boolean_t match = FALSE;
3739
3740     DEBUGFUNC("e1000_detect_gig_phy");
3741
3742     /* The 82571 firmware may still be configuring the PHY.  In this
3743      * case, we cannot access the PHY until the configuration is done.  So
3744      * we explicitly set the PHY values. */
3745     if(hw->mac_type == e1000_82571 ||
3746        hw->mac_type == e1000_82572) {
3747         hw->phy_id = IGP01E1000_I_PHY_ID;
3748         hw->phy_type = e1000_phy_igp_2;
3749         return E1000_SUCCESS;
3750     }
3751
3752     /* ESB-2 PHY reads require e1000_phy_gg82563 to be set because of a work-
3753      * around that forces PHY page 0 to be set or the reads fail.  The rest of
3754      * the code in this routine uses e1000_read_phy_reg to read the PHY ID.
3755      * So for ESB-2 we need to have this set so our reads won't fail.  If the
3756      * attached PHY is not a e1000_phy_gg82563, the routines below will figure
3757      * this out as well. */
3758     if (hw->mac_type == e1000_80003es2lan)
3759         hw->phy_type = e1000_phy_gg82563;
3760
3761     /* Read the PHY ID Registers to identify which PHY is onboard. */
3762     ret_val = e1000_read_phy_reg(hw, PHY_ID1, &phy_id_high);
3763     if(ret_val)
3764         return ret_val;
3765
3766     hw->phy_id = (uint32_t) (phy_id_high << 16);
3767     udelay(20);
3768     ret_val = e1000_read_phy_reg(hw, PHY_ID2, &phy_id_low);
3769     if(ret_val)
3770         return ret_val;
3771
3772     hw->phy_id |= (uint32_t) (phy_id_low & PHY_REVISION_MASK);
3773     hw->phy_revision = (uint32_t) phy_id_low & ~PHY_REVISION_MASK;
3774
3775     switch(hw->mac_type) {
3776     case e1000_82543:
3777         if(hw->phy_id == M88E1000_E_PHY_ID) match = TRUE;
3778         break;
3779     case e1000_82544:
3780         if(hw->phy_id == M88E1000_I_PHY_ID) match = TRUE;
3781         break;
3782     case e1000_82540:
3783     case e1000_82545:
3784     case e1000_82545_rev_3:
3785     case e1000_82546:
3786     case e1000_82546_rev_3:
3787         if(hw->phy_id == M88E1011_I_PHY_ID) match = TRUE;
3788         break;
3789     case e1000_82541:
3790     case e1000_82541_rev_2:
3791     case e1000_82547:
3792     case e1000_82547_rev_2:
3793         if(hw->phy_id == IGP01E1000_I_PHY_ID) match = TRUE;
3794         break;
3795     case e1000_82573:
3796         if(hw->phy_id == M88E1111_I_PHY_ID) match = TRUE;
3797         break;
3798     case e1000_80003es2lan:
3799         if (hw->phy_id == GG82563_E_PHY_ID) match = TRUE;
3800         break;
3801     default:
3802         DEBUGOUT1("Invalid MAC type %d\n", hw->mac_type);
3803         return -E1000_ERR_CONFIG;
3804     }
3805     phy_init_status = e1000_set_phy_type(hw);
3806
3807     if ((match) && (phy_init_status == E1000_SUCCESS)) {
3808         DEBUGOUT1("PHY ID 0x%X detected\n", hw->phy_id);
3809         return E1000_SUCCESS;
3810     }
3811     DEBUGOUT1("Invalid PHY ID 0x%X\n", hw->phy_id);
3812     return -E1000_ERR_PHY;
3813 }
3814
3815 /******************************************************************************
3816 * Resets the PHY's DSP
3817 *
3818 * hw - Struct containing variables accessed by shared code
3819 ******************************************************************************/
3820 static int32_t
3821 e1000_phy_reset_dsp(struct e1000_hw *hw)
3822 {
3823     int32_t ret_val;
3824     DEBUGFUNC("e1000_phy_reset_dsp");
3825
3826     do {
3827         if (hw->phy_type != e1000_phy_gg82563) {
3828             ret_val = e1000_write_phy_reg(hw, 29, 0x001d);
3829             if(ret_val) break;
3830         }
3831         ret_val = e1000_write_phy_reg(hw, 30, 0x00c1);
3832         if(ret_val) break;
3833         ret_val = e1000_write_phy_reg(hw, 30, 0x0000);
3834         if(ret_val) break;
3835         ret_val = E1000_SUCCESS;
3836     } while(0);
3837
3838     return ret_val;
3839 }
3840
3841 /******************************************************************************
3842 * Get PHY information from various PHY registers for igp PHY only.
3843 *
3844 * hw - Struct containing variables accessed by shared code
3845 * phy_info - PHY information structure
3846 ******************************************************************************/
3847 static int32_t
3848 e1000_phy_igp_get_info(struct e1000_hw *hw,
3849                        struct e1000_phy_info *phy_info)
3850 {
3851     int32_t ret_val;
3852     uint16_t phy_data, polarity, min_length, max_length, average;
3853
3854     DEBUGFUNC("e1000_phy_igp_get_info");
3855
3856     /* The downshift status is checked only once, after link is established,
3857      * and it stored in the hw->speed_downgraded parameter. */
3858     phy_info->downshift = (e1000_downshift)hw->speed_downgraded;
3859
3860     /* IGP01E1000 does not need to support it. */
3861     phy_info->extended_10bt_distance = e1000_10bt_ext_dist_enable_normal;
3862
3863     /* IGP01E1000 always correct polarity reversal */
3864     phy_info->polarity_correction = e1000_polarity_reversal_enabled;
3865
3866     /* Check polarity status */
3867     ret_val = e1000_check_polarity(hw, &polarity);
3868     if(ret_val)
3869         return ret_val;
3870
3871     phy_info->cable_polarity = polarity;
3872
3873     ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_STATUS, &phy_data);
3874     if(ret_val)
3875         return ret_val;
3876
3877     phy_info->mdix_mode = (phy_data & IGP01E1000_PSSR_MDIX) >>
3878                           IGP01E1000_PSSR_MDIX_SHIFT;
3879
3880     if((phy_data & IGP01E1000_PSSR_SPEED_MASK) ==
3881        IGP01E1000_PSSR_SPEED_1000MBPS) {
3882         /* Local/Remote Receiver Information are only valid at 1000 Mbps */
3883         ret_val = e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_data);
3884         if(ret_val)
3885             return ret_val;
3886
3887         phy_info->local_rx = (phy_data & SR_1000T_LOCAL_RX_STATUS) >>
3888                              SR_1000T_LOCAL_RX_STATUS_SHIFT;
3889         phy_info->remote_rx = (phy_data & SR_1000T_REMOTE_RX_STATUS) >>
3890                               SR_1000T_REMOTE_RX_STATUS_SHIFT;
3891
3892         /* Get cable length */
3893         ret_val = e1000_get_cable_length(hw, &min_length, &max_length);
3894         if(ret_val)
3895             return ret_val;
3896
3897         /* Translate to old method */
3898         average = (max_length + min_length) / 2;
3899
3900         if(average <= e1000_igp_cable_length_50)
3901             phy_info->cable_length = e1000_cable_length_50;
3902         else if(average <= e1000_igp_cable_length_80)
3903             phy_info->cable_length = e1000_cable_length_50_80;
3904         else if(average <= e1000_igp_cable_length_110)
3905             phy_info->cable_length = e1000_cable_length_80_110;
3906         else if(average <= e1000_igp_cable_length_140)
3907             phy_info->cable_length = e1000_cable_length_110_140;
3908         else
3909             phy_info->cable_length = e1000_cable_length_140;
3910     }
3911
3912     return E1000_SUCCESS;
3913 }
3914
3915 /******************************************************************************
3916 * Get PHY information from various PHY registers for ife PHY only.
3917 *
3918 * hw - Struct containing variables accessed by shared code
3919 * phy_info - PHY information structure
3920 ******************************************************************************/
3921 int32_t
3922 e1000_phy_ife_get_info(struct e1000_hw *hw,
3923                        struct e1000_phy_info *phy_info)
3924 {
3925     int32_t ret_val;
3926     uint16_t phy_data, polarity;
3927
3928     DEBUGFUNC("e1000_phy_ife_get_info");
3929
3930     phy_info->downshift = (e1000_downshift)hw->speed_downgraded;
3931     phy_info->extended_10bt_distance = e1000_10bt_ext_dist_enable_normal;
3932
3933     ret_val = e1000_read_phy_reg(hw, IFE_PHY_SPECIAL_CONTROL, &phy_data);
3934     if (ret_val)
3935         return ret_val;
3936     phy_info->polarity_correction =
3937                         (phy_data & IFE_PSC_AUTO_POLARITY_DISABLE) >>
3938                         IFE_PSC_AUTO_POLARITY_DISABLE_SHIFT;
3939
3940     if (phy_info->polarity_correction == e1000_polarity_reversal_enabled) {
3941         ret_val = e1000_check_polarity(hw, &polarity);
3942         if (ret_val)
3943             return ret_val;
3944     } else {
3945         /* Polarity is forced. */
3946         polarity = (phy_data & IFE_PSC_FORCE_POLARITY) >>
3947                        IFE_PSC_FORCE_POLARITY_SHIFT;
3948     }
3949     phy_info->cable_polarity = polarity;
3950
3951     ret_val = e1000_read_phy_reg(hw, IFE_PHY_MDIX_CONTROL, &phy_data);
3952     if (ret_val)
3953         return ret_val;
3954
3955     phy_info->mdix_mode =
3956                      (phy_data & (IFE_PMC_AUTO_MDIX | IFE_PMC_FORCE_MDIX)) >>
3957                      IFE_PMC_MDIX_MODE_SHIFT;
3958
3959     return E1000_SUCCESS;
3960 }
3961
3962 /******************************************************************************
3963 * Get PHY information from various PHY registers fot m88 PHY only.
3964 *
3965 * hw - Struct containing variables accessed by shared code
3966 * phy_info - PHY information structure
3967 ******************************************************************************/
3968 static int32_t
3969 e1000_phy_m88_get_info(struct e1000_hw *hw,
3970                        struct e1000_phy_info *phy_info)
3971 {
3972     int32_t ret_val;
3973     uint16_t phy_data, polarity;
3974
3975     DEBUGFUNC("e1000_phy_m88_get_info");
3976
3977     /* The downshift status is checked only once, after link is established,
3978      * and it stored in the hw->speed_downgraded parameter. */
3979     phy_info->downshift = (e1000_downshift)hw->speed_downgraded;
3980
3981     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
3982     if(ret_val)
3983         return ret_val;
3984
3985     phy_info->extended_10bt_distance =
3986         (phy_data & M88E1000_PSCR_10BT_EXT_DIST_ENABLE) >>
3987         M88E1000_PSCR_10BT_EXT_DIST_ENABLE_SHIFT;
3988     phy_info->polarity_correction =
3989         (phy_data & M88E1000_PSCR_POLARITY_REVERSAL) >>
3990         M88E1000_PSCR_POLARITY_REVERSAL_SHIFT;
3991
3992     /* Check polarity status */
3993     ret_val = e1000_check_polarity(hw, &polarity);
3994     if(ret_val)
3995         return ret_val;
3996     phy_info->cable_polarity = polarity;
3997
3998     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data);
3999     if(ret_val)
4000         return ret_val;
4001
4002     phy_info->mdix_mode = (phy_data & M88E1000_PSSR_MDIX) >>
4003                           M88E1000_PSSR_MDIX_SHIFT;
4004
4005     if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_1000MBS) {
4006         /* Cable Length Estimation and Local/Remote Receiver Information
4007          * are only valid at 1000 Mbps.
4008          */
4009         if (hw->phy_type != e1000_phy_gg82563) {
4010             phy_info->cable_length = ((phy_data & M88E1000_PSSR_CABLE_LENGTH) >>
4011                                       M88E1000_PSSR_CABLE_LENGTH_SHIFT);
4012         } else {
4013             ret_val = e1000_read_phy_reg(hw, GG82563_PHY_DSP_DISTANCE,
4014                                          &phy_data);
4015             if (ret_val)
4016                 return ret_val;
4017
4018             phy_info->cable_length = phy_data & GG82563_DSPD_CABLE_LENGTH;
4019         }
4020
4021         ret_val = e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_data);
4022         if(ret_val)
4023             return ret_val;
4024
4025         phy_info->local_rx = (phy_data & SR_1000T_LOCAL_RX_STATUS) >>
4026                              SR_1000T_LOCAL_RX_STATUS_SHIFT;
4027
4028         phy_info->remote_rx = (phy_data & SR_1000T_REMOTE_RX_STATUS) >>
4029                               SR_1000T_REMOTE_RX_STATUS_SHIFT;
4030     }
4031
4032     return E1000_SUCCESS;
4033 }
4034
4035 /******************************************************************************
4036 * Get PHY information from various PHY registers
4037 *
4038 * hw - Struct containing variables accessed by shared code
4039 * phy_info - PHY information structure
4040 ******************************************************************************/
4041 int32_t
4042 e1000_phy_get_info(struct e1000_hw *hw,
4043                    struct e1000_phy_info *phy_info)
4044 {
4045     int32_t ret_val;
4046     uint16_t phy_data;
4047
4048     DEBUGFUNC("e1000_phy_get_info");
4049
4050     phy_info->cable_length = e1000_cable_length_undefined;
4051     phy_info->extended_10bt_distance = e1000_10bt_ext_dist_enable_undefined;
4052     phy_info->cable_polarity = e1000_rev_polarity_undefined;
4053     phy_info->downshift = e1000_downshift_undefined;
4054     phy_info->polarity_correction = e1000_polarity_reversal_undefined;
4055     phy_info->mdix_mode = e1000_auto_x_mode_undefined;
4056     phy_info->local_rx = e1000_1000t_rx_status_undefined;
4057     phy_info->remote_rx = e1000_1000t_rx_status_undefined;
4058
4059     if(hw->media_type != e1000_media_type_copper) {
4060         DEBUGOUT("PHY info is only valid for copper media\n");
4061         return -E1000_ERR_CONFIG;
4062     }
4063
4064     ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
4065     if(ret_val)
4066         return ret_val;
4067
4068     ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
4069     if(ret_val)
4070         return ret_val;
4071
4072     if((phy_data & MII_SR_LINK_STATUS) != MII_SR_LINK_STATUS) {
4073         DEBUGOUT("PHY info is only valid if link is up\n");
4074         return -E1000_ERR_CONFIG;
4075     }
4076
4077     if(hw->phy_type == e1000_phy_igp ||
4078         hw->phy_type == e1000_phy_igp_2)
4079         return e1000_phy_igp_get_info(hw, phy_info);
4080     else
4081         return e1000_phy_m88_get_info(hw, phy_info);
4082 }
4083
4084 int32_t
4085 e1000_validate_mdi_setting(struct e1000_hw *hw)
4086 {
4087     DEBUGFUNC("e1000_validate_mdi_settings");
4088
4089     if(!hw->autoneg && (hw->mdix == 0 || hw->mdix == 3)) {
4090         DEBUGOUT("Invalid MDI setting detected\n");
4091         hw->mdix = 1;
4092         return -E1000_ERR_CONFIG;
4093     }
4094     return E1000_SUCCESS;
4095 }
4096
4097
4098 /******************************************************************************
4099  * Sets up eeprom variables in the hw struct.  Must be called after mac_type
4100  * is configured.  Additionally, if this is ICH8, the flash controller GbE
4101  * registers must be mapped, or this will crash.
4102  *
4103  * hw - Struct containing variables accessed by shared code
4104  *****************************************************************************/
4105 int32_t
4106 e1000_init_eeprom_params(struct e1000_hw *hw)
4107 {
4108     struct e1000_eeprom_info *eeprom = &hw->eeprom;
4109     uint32_t eecd = E1000_READ_REG(hw, EECD);
4110     int32_t ret_val = E1000_SUCCESS;
4111     uint16_t eeprom_size;
4112
4113     DEBUGFUNC("e1000_init_eeprom_params");
4114
4115     switch (hw->mac_type) {
4116     case e1000_82542_rev2_0:
4117     case e1000_82542_rev2_1:
4118     case e1000_82543:
4119     case e1000_82544:
4120         eeprom->type = e1000_eeprom_microwire;
4121         eeprom->word_size = 64;
4122         eeprom->opcode_bits = 3;
4123         eeprom->address_bits = 6;
4124         eeprom->delay_usec = 50;
4125         eeprom->use_eerd = FALSE;
4126         eeprom->use_eewr = FALSE;
4127         break;
4128     case e1000_82540:
4129     case e1000_82545:
4130     case e1000_82545_rev_3:
4131     case e1000_82546:
4132     case e1000_82546_rev_3:
4133         eeprom->type = e1000_eeprom_microwire;
4134         eeprom->opcode_bits = 3;
4135         eeprom->delay_usec = 50;
4136         if(eecd & E1000_EECD_SIZE) {
4137             eeprom->word_size = 256;
4138             eeprom->address_bits = 8;
4139         } else {
4140             eeprom->word_size = 64;
4141             eeprom->address_bits = 6;
4142         }
4143         eeprom->use_eerd = FALSE;
4144         eeprom->use_eewr = FALSE;
4145         break;
4146     case e1000_82541:
4147     case e1000_82541_rev_2:
4148     case e1000_82547:
4149     case e1000_82547_rev_2:
4150         if (eecd & E1000_EECD_TYPE) {
4151             eeprom->type = e1000_eeprom_spi;
4152             eeprom->opcode_bits = 8;
4153             eeprom->delay_usec = 1;
4154             if (eecd & E1000_EECD_ADDR_BITS) {
4155                 eeprom->page_size = 32;
4156                 eeprom->address_bits = 16;
4157             } else {
4158                 eeprom->page_size = 8;
4159                 eeprom->address_bits = 8;
4160             }
4161         } else {
4162             eeprom->type = e1000_eeprom_microwire;
4163             eeprom->opcode_bits = 3;
4164             eeprom->delay_usec = 50;
4165             if (eecd & E1000_EECD_ADDR_BITS) {
4166                 eeprom->word_size = 256;
4167                 eeprom->address_bits = 8;
4168             } else {
4169                 eeprom->word_size = 64;
4170                 eeprom->address_bits = 6;
4171             }
4172         }
4173         eeprom->use_eerd = FALSE;
4174         eeprom->use_eewr = FALSE;
4175         break;
4176     case e1000_82571:
4177     case e1000_82572:
4178         eeprom->type = e1000_eeprom_spi;
4179         eeprom->opcode_bits = 8;
4180         eeprom->delay_usec = 1;
4181         if (eecd & E1000_EECD_ADDR_BITS) {
4182             eeprom->page_size = 32;
4183             eeprom->address_bits = 16;
4184         } else {
4185             eeprom->page_size = 8;
4186             eeprom->address_bits = 8;
4187         }
4188         eeprom->use_eerd = FALSE;
4189         eeprom->use_eewr = FALSE;
4190         break;
4191     case e1000_82573:
4192         eeprom->type = e1000_eeprom_spi;
4193         eeprom->opcode_bits = 8;
4194         eeprom->delay_usec = 1;
4195         if (eecd & E1000_EECD_ADDR_BITS) {
4196             eeprom->page_size = 32;
4197             eeprom->address_bits = 16;
4198         } else {
4199             eeprom->page_size = 8;
4200             eeprom->address_bits = 8;
4201         }
4202         eeprom->use_eerd = TRUE;
4203         eeprom->use_eewr = TRUE;
4204         if(e1000_is_onboard_nvm_eeprom(hw) == FALSE) {
4205             eeprom->type = e1000_eeprom_flash;
4206             eeprom->word_size = 2048;
4207
4208             /* Ensure that the Autonomous FLASH update bit is cleared due to
4209              * Flash update issue on parts which use a FLASH for NVM. */
4210             eecd &= ~E1000_EECD_AUPDEN;
4211             E1000_WRITE_REG(hw, EECD, eecd);
4212         }
4213         break;
4214     case e1000_80003es2lan:
4215         eeprom->type = e1000_eeprom_spi;
4216         eeprom->opcode_bits = 8;
4217         eeprom->delay_usec = 1;
4218         if (eecd & E1000_EECD_ADDR_BITS) {
4219             eeprom->page_size = 32;
4220             eeprom->address_bits = 16;
4221         } else {
4222             eeprom->page_size = 8;
4223             eeprom->address_bits = 8;
4224         }
4225         eeprom->use_eerd = TRUE;
4226         eeprom->use_eewr = FALSE;
4227         break;
4228     default:
4229         break;
4230     }
4231
4232     if (eeprom->type == e1000_eeprom_spi) {
4233         /* eeprom_size will be an enum [0..8] that maps to eeprom sizes 128B to
4234          * 32KB (incremented by powers of 2).
4235          */
4236         if(hw->mac_type <= e1000_82547_rev_2) {
4237             /* Set to default value for initial eeprom read. */
4238             eeprom->word_size = 64;
4239             ret_val = e1000_read_eeprom(hw, EEPROM_CFG, 1, &eeprom_size);
4240             if(ret_val)
4241                 return ret_val;
4242             eeprom_size = (eeprom_size & EEPROM_SIZE_MASK) >> EEPROM_SIZE_SHIFT;
4243             /* 256B eeprom size was not supported in earlier hardware, so we
4244              * bump eeprom_size up one to ensure that "1" (which maps to 256B)
4245              * is never the result used in the shifting logic below. */
4246             if(eeprom_size)
4247                 eeprom_size++;
4248         } else {
4249             eeprom_size = (uint16_t)((eecd & E1000_EECD_SIZE_EX_MASK) >>
4250                           E1000_EECD_SIZE_EX_SHIFT);
4251         }
4252
4253         eeprom->word_size = 1 << (eeprom_size + EEPROM_WORD_SIZE_SHIFT);
4254     }
4255     return ret_val;
4256 }
4257
4258 /******************************************************************************
4259  * Raises the EEPROM's clock input.
4260  *
4261  * hw - Struct containing variables accessed by shared code
4262  * eecd - EECD's current value
4263  *****************************************************************************/
4264 static void
4265 e1000_raise_ee_clk(struct e1000_hw *hw,
4266                    uint32_t *eecd)
4267 {
4268     /* Raise the clock input to the EEPROM (by setting the SK bit), and then
4269      * wait <delay> microseconds.
4270      */
4271     *eecd = *eecd | E1000_EECD_SK;
4272     E1000_WRITE_REG(hw, EECD, *eecd);
4273     E1000_WRITE_FLUSH(hw);
4274     udelay(hw->eeprom.delay_usec);
4275 }
4276
4277 /******************************************************************************
4278  * Lowers the EEPROM's clock input.
4279  *
4280  * hw - Struct containing variables accessed by shared code
4281  * eecd - EECD's current value
4282  *****************************************************************************/
4283 static void
4284 e1000_lower_ee_clk(struct e1000_hw *hw,
4285                    uint32_t *eecd)
4286 {
4287     /* Lower the clock input to the EEPROM (by clearing the SK bit), and then
4288      * wait 50 microseconds.
4289      */
4290     *eecd = *eecd & ~E1000_EECD_SK;
4291     E1000_WRITE_REG(hw, EECD, *eecd);
4292     E1000_WRITE_FLUSH(hw);
4293     udelay(hw->eeprom.delay_usec);
4294 }
4295
4296 /******************************************************************************
4297  * Shift data bits out to the EEPROM.
4298  *
4299  * hw - Struct containing variables accessed by shared code
4300  * data - data to send to the EEPROM
4301  * count - number of bits to shift out
4302  *****************************************************************************/
4303 static void
4304 e1000_shift_out_ee_bits(struct e1000_hw *hw,
4305                         uint16_t data,
4306                         uint16_t count)
4307 {
4308     struct e1000_eeprom_info *eeprom = &hw->eeprom;
4309     uint32_t eecd;
4310     uint32_t mask;
4311
4312     /* We need to shift "count" bits out to the EEPROM. So, value in the
4313      * "data" parameter will be shifted out to the EEPROM one bit at a time.
4314      * In order to do this, "data" must be broken down into bits.
4315      */
4316     mask = 0x01 << (count - 1);
4317     eecd = E1000_READ_REG(hw, EECD);
4318     if (eeprom->type == e1000_eeprom_microwire) {
4319         eecd &= ~E1000_EECD_DO;
4320     } else if (eeprom->type == e1000_eeprom_spi) {
4321         eecd |= E1000_EECD_DO;
4322     }
4323     do {
4324         /* A "1" is shifted out to the EEPROM by setting bit "DI" to a "1",
4325          * and then raising and then lowering the clock (the SK bit controls
4326          * the clock input to the EEPROM).  A "0" is shifted out to the EEPROM
4327          * by setting "DI" to "0" and then raising and then lowering the clock.
4328          */
4329         eecd &= ~E1000_EECD_DI;
4330
4331         if(data & mask)
4332             eecd |= E1000_EECD_DI;
4333
4334         E1000_WRITE_REG(hw, EECD, eecd);
4335         E1000_WRITE_FLUSH(hw);
4336
4337         udelay(eeprom->delay_usec);
4338
4339         e1000_raise_ee_clk(hw, &eecd);
4340         e1000_lower_ee_clk(hw, &eecd);
4341
4342         mask = mask >> 1;
4343
4344     } while(mask);
4345
4346     /* We leave the "DI" bit set to "0" when we leave this routine. */
4347     eecd &= ~E1000_EECD_DI;
4348     E1000_WRITE_REG(hw, EECD, eecd);
4349 }
4350
4351 /******************************************************************************
4352  * Shift data bits in from the EEPROM
4353  *
4354  * hw - Struct containing variables accessed by shared code
4355  *****************************************************************************/
4356 static uint16_t
4357 e1000_shift_in_ee_bits(struct e1000_hw *hw,
4358                        uint16_t count)
4359 {
4360     uint32_t eecd;
4361     uint32_t i;
4362     uint16_t data;
4363
4364     /* In order to read a register from the EEPROM, we need to shift 'count'
4365      * bits in from the EEPROM. Bits are "shifted in" by raising the clock
4366      * input to the EEPROM (setting the SK bit), and then reading the value of
4367      * the "DO" bit.  During this "shifting in" process the "DI" bit should
4368      * always be clear.
4369      */
4370
4371     eecd = E1000_READ_REG(hw, EECD);
4372
4373     eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
4374     data = 0;
4375
4376     for(i = 0; i < count; i++) {
4377         data = data << 1;
4378         e1000_raise_ee_clk(hw, &eecd);
4379
4380         eecd = E1000_READ_REG(hw, EECD);
4381
4382         eecd &= ~(E1000_EECD_DI);
4383         if(eecd & E1000_EECD_DO)
4384             data |= 1;
4385
4386         e1000_lower_ee_clk(hw, &eecd);
4387     }
4388
4389     return data;
4390 }
4391
4392 /******************************************************************************
4393  * Prepares EEPROM for access
4394  *
4395  * hw - Struct containing variables accessed by shared code
4396  *
4397  * Lowers EEPROM clock. Clears input pin. Sets the chip select pin. This
4398  * function should be called before issuing a command to the EEPROM.
4399  *****************************************************************************/
4400 static int32_t
4401 e1000_acquire_eeprom(struct e1000_hw *hw)
4402 {
4403     struct e1000_eeprom_info *eeprom = &hw->eeprom;
4404     uint32_t eecd, i=0;
4405
4406     DEBUGFUNC("e1000_acquire_eeprom");
4407
4408     if (e1000_swfw_sync_acquire(hw, E1000_SWFW_EEP_SM))
4409         return -E1000_ERR_SWFW_SYNC;
4410     eecd = E1000_READ_REG(hw, EECD);
4411
4412     if (hw->mac_type != e1000_82573) {
4413         /* Request EEPROM Access */
4414         if(hw->mac_type > e1000_82544) {
4415             eecd |= E1000_EECD_REQ;
4416             E1000_WRITE_REG(hw, EECD, eecd);
4417             eecd = E1000_READ_REG(hw, EECD);
4418             while((!(eecd & E1000_EECD_GNT)) &&
4419                   (i < E1000_EEPROM_GRANT_ATTEMPTS)) {
4420                 i++;
4421                 udelay(5);
4422                 eecd = E1000_READ_REG(hw, EECD);
4423             }
4424             if(!(eecd & E1000_EECD_GNT)) {
4425                 eecd &= ~E1000_EECD_REQ;
4426                 E1000_WRITE_REG(hw, EECD, eecd);
4427                 DEBUGOUT("Could not acquire EEPROM grant\n");
4428                 e1000_swfw_sync_release(hw, E1000_SWFW_EEP_SM);
4429                 return -E1000_ERR_EEPROM;
4430             }
4431         }
4432     }
4433
4434     /* Setup EEPROM for Read/Write */
4435
4436     if (eeprom->type == e1000_eeprom_microwire) {
4437         /* Clear SK and DI */
4438         eecd &= ~(E1000_EECD_DI | E1000_EECD_SK);
4439         E1000_WRITE_REG(hw, EECD, eecd);
4440
4441         /* Set CS */
4442         eecd |= E1000_EECD_CS;
4443         E1000_WRITE_REG(hw, EECD, eecd);
4444     } else if (eeprom->type == e1000_eeprom_spi) {
4445         /* Clear SK and CS */
4446         eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
4447         E1000_WRITE_REG(hw, EECD, eecd);
4448         udelay(1);
4449     }
4450
4451     return E1000_SUCCESS;
4452 }
4453
4454 /******************************************************************************
4455  * Returns EEPROM to a "standby" state
4456  *
4457  * hw - Struct containing variables accessed by shared code
4458  *****************************************************************************/
4459 static void
4460 e1000_standby_eeprom(struct e1000_hw *hw)
4461 {
4462     struct e1000_eeprom_info *eeprom = &hw->eeprom;
4463     uint32_t eecd;
4464
4465     eecd = E1000_READ_REG(hw, EECD);
4466
4467     if(eeprom->type == e1000_eeprom_microwire) {
4468         eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
4469         E1000_WRITE_REG(hw, EECD, eecd);
4470         E1000_WRITE_FLUSH(hw);
4471         udelay(eeprom->delay_usec);
4472
4473         /* Clock high */
4474         eecd |= E1000_EECD_SK;
4475         E1000_WRITE_REG(hw, EECD, eecd);
4476         E1000_WRITE_FLUSH(hw);
4477         udelay(eeprom->delay_usec);
4478
4479         /* Select EEPROM */
4480         eecd |= E1000_EECD_CS;
4481         E1000_WRITE_REG(hw, EECD, eecd);
4482         E1000_WRITE_FLUSH(hw);
4483         udelay(eeprom->delay_usec);
4484
4485         /* Clock low */
4486         eecd &= ~E1000_EECD_SK;
4487         E1000_WRITE_REG(hw, EECD, eecd);
4488         E1000_WRITE_FLUSH(hw);
4489         udelay(eeprom->delay_usec);
4490     } else if(eeprom->type == e1000_eeprom_spi) {
4491         /* Toggle CS to flush commands */
4492         eecd |= E1000_EECD_CS;
4493         E1000_WRITE_REG(hw, EECD, eecd);
4494         E1000_WRITE_FLUSH(hw);
4495         udelay(eeprom->delay_usec);
4496         eecd &= ~E1000_EECD_CS;
4497         E1000_WRITE_REG(hw, EECD, eecd);
4498         E1000_WRITE_FLUSH(hw);
4499         udelay(eeprom->delay_usec);
4500     }
4501 }
4502
4503 /******************************************************************************
4504  * Terminates a command by inverting the EEPROM's chip select pin
4505  *
4506  * hw - Struct containing variables accessed by shared code
4507  *****************************************************************************/
4508 static void
4509 e1000_release_eeprom(struct e1000_hw *hw)
4510 {
4511     uint32_t eecd;
4512
4513     DEBUGFUNC("e1000_release_eeprom");
4514
4515     eecd = E1000_READ_REG(hw, EECD);
4516
4517     if (hw->eeprom.type == e1000_eeprom_spi) {
4518         eecd |= E1000_EECD_CS;  /* Pull CS high */
4519         eecd &= ~E1000_EECD_SK; /* Lower SCK */
4520
4521         E1000_WRITE_REG(hw, EECD, eecd);
4522
4523         udelay(hw->eeprom.delay_usec);
4524     } else if(hw->eeprom.type == e1000_eeprom_microwire) {
4525         /* cleanup eeprom */
4526
4527         /* CS on Microwire is active-high */
4528         eecd &= ~(E1000_EECD_CS | E1000_EECD_DI);
4529
4530         E1000_WRITE_REG(hw, EECD, eecd);
4531
4532         /* Rising edge of clock */
4533         eecd |= E1000_EECD_SK;
4534         E1000_WRITE_REG(hw, EECD, eecd);
4535         E1000_WRITE_FLUSH(hw);
4536         udelay(hw->eeprom.delay_usec);
4537
4538         /* Falling edge of clock */
4539         eecd &= ~E1000_EECD_SK;
4540         E1000_WRITE_REG(hw, EECD, eecd);
4541         E1000_WRITE_FLUSH(hw);
4542         udelay(hw->eeprom.delay_usec);
4543     }
4544
4545     /* Stop requesting EEPROM access */
4546     if(hw->mac_type > e1000_82544) {
4547         eecd &= ~E1000_EECD_REQ;
4548         E1000_WRITE_REG(hw, EECD, eecd);
4549     }
4550
4551     e1000_swfw_sync_release(hw, E1000_SWFW_EEP_SM);
4552 }
4553
4554 /******************************************************************************
4555  * Reads a 16 bit word from the EEPROM.
4556  *
4557  * hw - Struct containing variables accessed by shared code
4558  *****************************************************************************/
4559 int32_t
4560 e1000_spi_eeprom_ready(struct e1000_hw *hw)
4561 {
4562     uint16_t retry_count = 0;
4563     uint8_t spi_stat_reg;
4564
4565     DEBUGFUNC("e1000_spi_eeprom_ready");
4566
4567     /* Read "Status Register" repeatedly until the LSB is cleared.  The
4568      * EEPROM will signal that the command has been completed by clearing
4569      * bit 0 of the internal status register.  If it's not cleared within
4570      * 5 milliseconds, then error out.
4571      */
4572     retry_count = 0;
4573     do {
4574         e1000_shift_out_ee_bits(hw, EEPROM_RDSR_OPCODE_SPI,
4575                                 hw->eeprom.opcode_bits);
4576         spi_stat_reg = (uint8_t)e1000_shift_in_ee_bits(hw, 8);
4577         if (!(spi_stat_reg & EEPROM_STATUS_RDY_SPI))
4578             break;
4579
4580         udelay(5);
4581         retry_count += 5;
4582
4583         e1000_standby_eeprom(hw);
4584     } while(retry_count < EEPROM_MAX_RETRY_SPI);
4585
4586     /* ATMEL SPI write time could vary from 0-20mSec on 3.3V devices (and
4587      * only 0-5mSec on 5V devices)
4588      */
4589     if(retry_count >= EEPROM_MAX_RETRY_SPI) {
4590         DEBUGOUT("SPI EEPROM Status error\n");
4591         return -E1000_ERR_EEPROM;
4592     }
4593
4594     return E1000_SUCCESS;
4595 }
4596
4597 /******************************************************************************
4598  * Reads a 16 bit word from the EEPROM.
4599  *
4600  * hw - Struct containing variables accessed by shared code
4601  * offset - offset of  word in the EEPROM to read
4602  * data - word read from the EEPROM
4603  * words - number of words to read
4604  *****************************************************************************/
4605 int32_t
4606 e1000_read_eeprom(struct e1000_hw *hw,
4607                   uint16_t offset,
4608                   uint16_t words,
4609                   uint16_t *data)
4610 {
4611     struct e1000_eeprom_info *eeprom = &hw->eeprom;
4612     uint32_t i = 0;
4613     int32_t ret_val;
4614
4615     DEBUGFUNC("e1000_read_eeprom");
4616
4617     /* A check for invalid values:  offset too large, too many words, and not
4618      * enough words.
4619      */
4620     if((offset >= eeprom->word_size) || (words > eeprom->word_size - offset) ||
4621        (words == 0)) {
4622         DEBUGOUT("\"words\" parameter out of bounds\n");
4623         return -E1000_ERR_EEPROM;
4624     }
4625
4626     /* FLASH reads without acquiring the semaphore are safe */
4627     if (e1000_is_onboard_nvm_eeprom(hw) == TRUE &&
4628     hw->eeprom.use_eerd == FALSE) {
4629         switch (hw->mac_type) {
4630         case e1000_80003es2lan:
4631             break;
4632         default:
4633             /* Prepare the EEPROM for reading  */
4634             if (e1000_acquire_eeprom(hw) != E1000_SUCCESS)
4635                 return -E1000_ERR_EEPROM;
4636             break;
4637         }
4638     }
4639
4640     if (eeprom->use_eerd == TRUE) {
4641         ret_val = e1000_read_eeprom_eerd(hw, offset, words, data);
4642         if ((e1000_is_onboard_nvm_eeprom(hw) == TRUE) ||
4643             (hw->mac_type != e1000_82573))
4644             e1000_release_eeprom(hw);
4645         return ret_val;
4646     }
4647
4648     if(eeprom->type == e1000_eeprom_spi) {
4649         uint16_t word_in;
4650         uint8_t read_opcode = EEPROM_READ_OPCODE_SPI;
4651
4652         if(e1000_spi_eeprom_ready(hw)) {
4653             e1000_release_eeprom(hw);
4654             return -E1000_ERR_EEPROM;
4655         }
4656
4657         e1000_standby_eeprom(hw);
4658
4659         /* Some SPI eeproms use the 8th address bit embedded in the opcode */
4660         if((eeprom->address_bits == 8) && (offset >= 128))
4661             read_opcode |= EEPROM_A8_OPCODE_SPI;
4662
4663         /* Send the READ command (opcode + addr)  */
4664         e1000_shift_out_ee_bits(hw, read_opcode, eeprom->opcode_bits);
4665         e1000_shift_out_ee_bits(hw, (uint16_t)(offset*2), eeprom->address_bits);
4666
4667         /* Read the data.  The address of the eeprom internally increments with
4668          * each byte (spi) being read, saving on the overhead of eeprom setup
4669          * and tear-down.  The address counter will roll over if reading beyond
4670          * the size of the eeprom, thus allowing the entire memory to be read
4671          * starting from any offset. */
4672         for (i = 0; i < words; i++) {
4673             word_in = e1000_shift_in_ee_bits(hw, 16);
4674             data[i] = (word_in >> 8) | (word_in << 8);
4675         }
4676     } else if(eeprom->type == e1000_eeprom_microwire) {
4677         for (i = 0; i < words; i++) {
4678             /* Send the READ command (opcode + addr)  */
4679             e1000_shift_out_ee_bits(hw, EEPROM_READ_OPCODE_MICROWIRE,
4680                                     eeprom->opcode_bits);
4681             e1000_shift_out_ee_bits(hw, (uint16_t)(offset + i),
4682                                     eeprom->address_bits);
4683
4684             /* Read the data.  For microwire, each word requires the overhead
4685              * of eeprom setup and tear-down. */
4686             data[i] = e1000_shift_in_ee_bits(hw, 16);
4687             e1000_standby_eeprom(hw);
4688         }
4689     }
4690
4691     /* End this read operation */
4692     e1000_release_eeprom(hw);
4693
4694     return E1000_SUCCESS;
4695 }
4696
4697 /******************************************************************************
4698  * Reads a 16 bit word from the EEPROM using the EERD register.
4699  *
4700  * hw - Struct containing variables accessed by shared code
4701  * offset - offset of  word in the EEPROM to read
4702  * data - word read from the EEPROM
4703  * words - number of words to read
4704  *****************************************************************************/
4705 static int32_t
4706 e1000_read_eeprom_eerd(struct e1000_hw *hw,
4707                   uint16_t offset,
4708                   uint16_t words,
4709                   uint16_t *data)
4710 {
4711     uint32_t i, eerd = 0;
4712     int32_t error = 0;
4713
4714     for (i = 0; i < words; i++) {
4715         eerd = ((offset+i) << E1000_EEPROM_RW_ADDR_SHIFT) +
4716                          E1000_EEPROM_RW_REG_START;
4717
4718         E1000_WRITE_REG(hw, EERD, eerd);
4719         error = e1000_poll_eerd_eewr_done(hw, E1000_EEPROM_POLL_READ);
4720
4721         if(error) {
4722             break;
4723         }
4724         data[i] = (E1000_READ_REG(hw, EERD) >> E1000_EEPROM_RW_REG_DATA);
4725
4726     }
4727
4728     return error;
4729 }
4730
4731 /******************************************************************************
4732  * Writes a 16 bit word from the EEPROM using the EEWR register.
4733  *
4734  * hw - Struct containing variables accessed by shared code
4735  * offset - offset of  word in the EEPROM to read
4736  * data - word read from the EEPROM
4737  * words - number of words to read
4738  *****************************************************************************/
4739 static int32_t
4740 e1000_write_eeprom_eewr(struct e1000_hw *hw,
4741                    uint16_t offset,
4742                    uint16_t words,
4743                    uint16_t *data)
4744 {
4745     uint32_t    register_value = 0;
4746     uint32_t    i              = 0;
4747     int32_t     error          = 0;
4748
4749     if (e1000_swfw_sync_acquire(hw, E1000_SWFW_EEP_SM))
4750         return -E1000_ERR_SWFW_SYNC;
4751
4752     for (i = 0; i < words; i++) {
4753         register_value = (data[i] << E1000_EEPROM_RW_REG_DATA) |
4754                          ((offset+i) << E1000_EEPROM_RW_ADDR_SHIFT) |
4755                          E1000_EEPROM_RW_REG_START;
4756
4757         error = e1000_poll_eerd_eewr_done(hw, E1000_EEPROM_POLL_WRITE);
4758         if(error) {
4759             break;
4760         }
4761
4762         E1000_WRITE_REG(hw, EEWR, register_value);
4763
4764         error = e1000_poll_eerd_eewr_done(hw, E1000_EEPROM_POLL_WRITE);
4765
4766         if(error) {
4767             break;
4768         }
4769     }
4770
4771     e1000_swfw_sync_release(hw, E1000_SWFW_EEP_SM);
4772     return error;
4773 }
4774
4775 /******************************************************************************
4776  * Polls the status bit (bit 1) of the EERD to determine when the read is done.
4777  *
4778  * hw - Struct containing variables accessed by shared code
4779  *****************************************************************************/
4780 static int32_t
4781 e1000_poll_eerd_eewr_done(struct e1000_hw *hw, int eerd)
4782 {
4783     uint32_t attempts = 100000;
4784     uint32_t i, reg = 0;
4785     int32_t done = E1000_ERR_EEPROM;
4786
4787     for(i = 0; i < attempts; i++) {
4788         if(eerd == E1000_EEPROM_POLL_READ)
4789             reg = E1000_READ_REG(hw, EERD);
4790         else
4791             reg = E1000_READ_REG(hw, EEWR);
4792
4793         if(reg & E1000_EEPROM_RW_REG_DONE) {
4794             done = E1000_SUCCESS;
4795             break;
4796         }
4797         udelay(5);
4798     }
4799
4800     return done;
4801 }
4802
4803 /***************************************************************************
4804 * Description:     Determines if the onboard NVM is FLASH or EEPROM.
4805 *
4806 * hw - Struct containing variables accessed by shared code
4807 ****************************************************************************/
4808 static boolean_t
4809 e1000_is_onboard_nvm_eeprom(struct e1000_hw *hw)
4810 {
4811     uint32_t eecd = 0;
4812
4813     DEBUGFUNC("e1000_is_onboard_nvm_eeprom");
4814
4815     if(hw->mac_type == e1000_82573) {
4816         eecd = E1000_READ_REG(hw, EECD);
4817
4818         /* Isolate bits 15 & 16 */
4819         eecd = ((eecd >> 15) & 0x03);
4820
4821         /* If both bits are set, device is Flash type */
4822         if(eecd == 0x03) {
4823             return FALSE;
4824         }
4825     }
4826     return TRUE;
4827 }
4828
4829 /******************************************************************************
4830  * Verifies that the EEPROM has a valid checksum
4831  *
4832  * hw - Struct containing variables accessed by shared code
4833  *
4834  * Reads the first 64 16 bit words of the EEPROM and sums the values read.
4835  * If the the sum of the 64 16 bit words is 0xBABA, the EEPROM's checksum is
4836  * valid.
4837  *****************************************************************************/
4838 int32_t
4839 e1000_validate_eeprom_checksum(struct e1000_hw *hw)
4840 {
4841     uint16_t checksum = 0;
4842     uint16_t i, eeprom_data;
4843
4844     DEBUGFUNC("e1000_validate_eeprom_checksum");
4845
4846     if ((hw->mac_type == e1000_82573) &&
4847         (e1000_is_onboard_nvm_eeprom(hw) == FALSE)) {
4848         /* Check bit 4 of word 10h.  If it is 0, firmware is done updating
4849          * 10h-12h.  Checksum may need to be fixed. */
4850         e1000_read_eeprom(hw, 0x10, 1, &eeprom_data);
4851         if ((eeprom_data & 0x10) == 0) {
4852             /* Read 0x23 and check bit 15.  This bit is a 1 when the checksum
4853              * has already been fixed.  If the checksum is still wrong and this
4854              * bit is a 1, we need to return bad checksum.  Otherwise, we need
4855              * to set this bit to a 1 and update the checksum. */
4856             e1000_read_eeprom(hw, 0x23, 1, &eeprom_data);
4857             if ((eeprom_data & 0x8000) == 0) {
4858                 eeprom_data |= 0x8000;
4859                 e1000_write_eeprom(hw, 0x23, 1, &eeprom_data);
4860                 e1000_update_eeprom_checksum(hw);
4861             }
4862         }
4863     }
4864
4865     for(i = 0; i < (EEPROM_CHECKSUM_REG + 1); i++) {
4866         if(e1000_read_eeprom(hw, i, 1, &eeprom_data) < 0) {
4867             DEBUGOUT("EEPROM Read Error\n");
4868             return -E1000_ERR_EEPROM;
4869         }
4870         checksum += eeprom_data;
4871     }
4872
4873     if(checksum == (uint16_t) EEPROM_SUM)
4874         return E1000_SUCCESS;
4875     else {
4876         DEBUGOUT("EEPROM Checksum Invalid\n");
4877         return -E1000_ERR_EEPROM;
4878     }
4879 }
4880
4881 /******************************************************************************
4882  * Calculates the EEPROM checksum and writes it to the EEPROM
4883  *
4884  * hw - Struct containing variables accessed by shared code
4885  *
4886  * Sums the first 63 16 bit words of the EEPROM. Subtracts the sum from 0xBABA.
4887  * Writes the difference to word offset 63 of the EEPROM.
4888  *****************************************************************************/
4889 int32_t
4890 e1000_update_eeprom_checksum(struct e1000_hw *hw)
4891 {
4892     uint16_t checksum = 0;
4893     uint16_t i, eeprom_data;
4894
4895     DEBUGFUNC("e1000_update_eeprom_checksum");
4896
4897     for(i = 0; i < EEPROM_CHECKSUM_REG; i++) {
4898         if(e1000_read_eeprom(hw, i, 1, &eeprom_data) < 0) {
4899             DEBUGOUT("EEPROM Read Error\n");
4900             return -E1000_ERR_EEPROM;
4901         }
4902         checksum += eeprom_data;
4903     }
4904     checksum = (uint16_t) EEPROM_SUM - checksum;
4905     if(e1000_write_eeprom(hw, EEPROM_CHECKSUM_REG, 1, &checksum) < 0) {
4906         DEBUGOUT("EEPROM Write Error\n");
4907         return -E1000_ERR_EEPROM;
4908     } else if (hw->eeprom.type == e1000_eeprom_flash) {
4909         e1000_commit_shadow_ram(hw);
4910     }
4911     return E1000_SUCCESS;
4912 }
4913
4914 /******************************************************************************
4915  * Parent function for writing words to the different EEPROM types.
4916  *
4917  * hw - Struct containing variables accessed by shared code
4918  * offset - offset within the EEPROM to be written to
4919  * words - number of words to write
4920  * data - 16 bit word to be written to the EEPROM
4921  *
4922  * If e1000_update_eeprom_checksum is not called after this function, the
4923  * EEPROM will most likely contain an invalid checksum.
4924  *****************************************************************************/
4925 int32_t
4926 e1000_write_eeprom(struct e1000_hw *hw,
4927                    uint16_t offset,
4928                    uint16_t words,
4929                    uint16_t *data)
4930 {
4931     struct e1000_eeprom_info *eeprom = &hw->eeprom;
4932     int32_t status = 0;
4933
4934     DEBUGFUNC("e1000_write_eeprom");
4935
4936     /* A check for invalid values:  offset too large, too many words, and not
4937      * enough words.
4938      */
4939     if((offset >= eeprom->word_size) || (words > eeprom->word_size - offset) ||
4940        (words == 0)) {
4941         DEBUGOUT("\"words\" parameter out of bounds\n");
4942         return -E1000_ERR_EEPROM;
4943     }
4944
4945     /* 82573 writes only through eewr */
4946     if(eeprom->use_eewr == TRUE)
4947         return e1000_write_eeprom_eewr(hw, offset, words, data);
4948
4949     /* Prepare the EEPROM for writing  */
4950     if (e1000_acquire_eeprom(hw) != E1000_SUCCESS)
4951         return -E1000_ERR_EEPROM;
4952
4953     if(eeprom->type == e1000_eeprom_microwire) {
4954         status = e1000_write_eeprom_microwire(hw, offset, words, data);
4955     } else {
4956         status = e1000_write_eeprom_spi(hw, offset, words, data);
4957         msec_delay(10);
4958     }
4959
4960     /* Done with writing */
4961     e1000_release_eeprom(hw);
4962
4963     return status;
4964 }
4965
4966 /******************************************************************************
4967  * Writes a 16 bit word to a given offset in an SPI EEPROM.
4968  *
4969  * hw - Struct containing variables accessed by shared code
4970  * offset - offset within the EEPROM to be written to
4971  * words - number of words to write
4972  * data - pointer to array of 8 bit words to be written to the EEPROM
4973  *
4974  *****************************************************************************/
4975 int32_t
4976 e1000_write_eeprom_spi(struct e1000_hw *hw,
4977                        uint16_t offset,
4978                        uint16_t words,
4979                        uint16_t *data)
4980 {
4981     struct e1000_eeprom_info *eeprom = &hw->eeprom;
4982     uint16_t widx = 0;
4983
4984     DEBUGFUNC("e1000_write_eeprom_spi");
4985
4986     while (widx < words) {
4987         uint8_t write_opcode = EEPROM_WRITE_OPCODE_SPI;
4988
4989         if(e1000_spi_eeprom_ready(hw)) return -E1000_ERR_EEPROM;
4990
4991         e1000_standby_eeprom(hw);
4992
4993         /*  Send the WRITE ENABLE command (8 bit opcode )  */
4994         e1000_shift_out_ee_bits(hw, EEPROM_WREN_OPCODE_SPI,
4995                                     eeprom->opcode_bits);
4996
4997         e1000_standby_eeprom(hw);
4998
4999         /* Some SPI eeproms use the 8th address bit embedded in the opcode */
5000         if((eeprom->address_bits == 8) && (offset >= 128))
5001             write_opcode |= EEPROM_A8_OPCODE_SPI;
5002
5003         /* Send the Write command (8-bit opcode + addr) */
5004         e1000_shift_out_ee_bits(hw, write_opcode, eeprom->opcode_bits);
5005
5006         e1000_shift_out_ee_bits(hw, (uint16_t)((offset + widx)*2),
5007                                 eeprom->address_bits);
5008
5009         /* Send the data */
5010
5011         /* Loop to allow for up to whole page write (32 bytes) of eeprom */
5012         while (widx < words) {
5013             uint16_t word_out = data[widx];
5014             word_out = (word_out >> 8) | (word_out << 8);
5015             e1000_shift_out_ee_bits(hw, word_out, 16);
5016             widx++;
5017
5018             /* Some larger eeprom sizes are capable of a 32-byte PAGE WRITE
5019              * operation, while the smaller eeproms are capable of an 8-byte
5020              * PAGE WRITE operation.  Break the inner loop to pass new address
5021              */
5022             if((((offset + widx)*2) % eeprom->page_size) == 0) {
5023                 e1000_standby_eeprom(hw);
5024                 break;
5025             }
5026         }
5027     }
5028
5029     return E1000_SUCCESS;
5030 }
5031
5032 /******************************************************************************
5033  * Writes a 16 bit word to a given offset in a Microwire EEPROM.
5034  *
5035  * hw - Struct containing variables accessed by shared code
5036  * offset - offset within the EEPROM to be written to
5037  * words - number of words to write
5038  * data - pointer to array of 16 bit words to be written to the EEPROM
5039  *
5040  *****************************************************************************/
5041 int32_t
5042 e1000_write_eeprom_microwire(struct e1000_hw *hw,
5043                              uint16_t offset,
5044                              uint16_t words,
5045                              uint16_t *data)
5046 {
5047     struct e1000_eeprom_info *eeprom = &hw->eeprom;
5048     uint32_t eecd;
5049     uint16_t words_written = 0;
5050     uint16_t i = 0;
5051
5052     DEBUGFUNC("e1000_write_eeprom_microwire");
5053
5054     /* Send the write enable command to the EEPROM (3-bit opcode plus
5055      * 6/8-bit dummy address beginning with 11).  It's less work to include
5056      * the 11 of the dummy address as part of the opcode than it is to shift
5057      * it over the correct number of bits for the address.  This puts the
5058      * EEPROM into write/erase mode.
5059      */
5060     e1000_shift_out_ee_bits(hw, EEPROM_EWEN_OPCODE_MICROWIRE,
5061                             (uint16_t)(eeprom->opcode_bits + 2));
5062
5063     e1000_shift_out_ee_bits(hw, 0, (uint16_t)(eeprom->address_bits - 2));
5064
5065     /* Prepare the EEPROM */
5066     e1000_standby_eeprom(hw);
5067
5068     while (words_written < words) {
5069         /* Send the Write command (3-bit opcode + addr) */
5070         e1000_shift_out_ee_bits(hw, EEPROM_WRITE_OPCODE_MICROWIRE,
5071                                 eeprom->opcode_bits);
5072
5073         e1000_shift_out_ee_bits(hw, (uint16_t)(offset + words_written),
5074                                 eeprom->address_bits);
5075
5076         /* Send the data */
5077         e1000_shift_out_ee_bits(hw, data[words_written], 16);
5078
5079         /* Toggle the CS line.  This in effect tells the EEPROM to execute
5080          * the previous command.
5081          */
5082         e1000_standby_eeprom(hw);
5083
5084         /* Read DO repeatedly until it is high (equal to '1').  The EEPROM will
5085          * signal that the command has been completed by raising the DO signal.
5086          * If DO does not go high in 10 milliseconds, then error out.
5087          */
5088         for(i = 0; i < 200; i++) {
5089             eecd = E1000_READ_REG(hw, EECD);
5090             if(eecd & E1000_EECD_DO) break;
5091             udelay(50);
5092         }
5093         if(i == 200) {
5094             DEBUGOUT("EEPROM Write did not complete\n");
5095             return -E1000_ERR_EEPROM;
5096         }
5097
5098         /* Recover from write */
5099         e1000_standby_eeprom(hw);
5100
5101         words_written++;
5102     }
5103
5104     /* Send the write disable command to the EEPROM (3-bit opcode plus
5105      * 6/8-bit dummy address beginning with 10).  It's less work to include
5106      * the 10 of the dummy address as part of the opcode than it is to shift
5107      * it over the correct number of bits for the address.  This takes the
5108      * EEPROM out of write/erase mode.
5109      */
5110     e1000_shift_out_ee_bits(hw, EEPROM_EWDS_OPCODE_MICROWIRE,
5111                             (uint16_t)(eeprom->opcode_bits + 2));
5112
5113     e1000_shift_out_ee_bits(hw, 0, (uint16_t)(eeprom->address_bits - 2));
5114
5115     return E1000_SUCCESS;
5116 }
5117
5118 /******************************************************************************
5119  * Flushes the cached eeprom to NVM. This is done by saving the modified values
5120  * in the eeprom cache and the non modified values in the currently active bank
5121  * to the new bank.
5122  *
5123  * hw - Struct containing variables accessed by shared code
5124  * offset - offset of  word in the EEPROM to read
5125  * data - word read from the EEPROM
5126  * words - number of words to read
5127  *****************************************************************************/
5128 static int32_t
5129 e1000_commit_shadow_ram(struct e1000_hw *hw)
5130 {
5131     uint32_t attempts = 100000;
5132     uint32_t eecd = 0;
5133     uint32_t flop = 0;
5134     uint32_t i = 0;
5135     int32_t error = E1000_SUCCESS;
5136
5137     /* The flop register will be used to determine if flash type is STM */
5138     flop = E1000_READ_REG(hw, FLOP);
5139
5140     if (hw->mac_type == e1000_82573) {
5141         for (i=0; i < attempts; i++) {
5142             eecd = E1000_READ_REG(hw, EECD);
5143             if ((eecd & E1000_EECD_FLUPD) == 0) {
5144                 break;
5145             }
5146             udelay(5);
5147         }
5148
5149         if (i == attempts) {
5150             return -E1000_ERR_EEPROM;
5151         }
5152
5153         /* If STM opcode located in bits 15:8 of flop, reset firmware */
5154         if ((flop & 0xFF00) == E1000_STM_OPCODE) {
5155             E1000_WRITE_REG(hw, HICR, E1000_HICR_FW_RESET);
5156         }
5157
5158         /* Perform the flash update */
5159         E1000_WRITE_REG(hw, EECD, eecd | E1000_EECD_FLUPD);
5160
5161         for (i=0; i < attempts; i++) {
5162             eecd = E1000_READ_REG(hw, EECD);
5163             if ((eecd & E1000_EECD_FLUPD) == 0) {
5164                 break;
5165             }
5166             udelay(5);
5167         }
5168
5169         if (i == attempts) {
5170             return -E1000_ERR_EEPROM;
5171         }
5172     }
5173
5174     return error;
5175 }
5176
5177 /******************************************************************************
5178  * Reads the adapter's part number from the EEPROM
5179  *
5180  * hw - Struct containing variables accessed by shared code
5181  * part_num - Adapter's part number
5182  *****************************************************************************/
5183 int32_t
5184 e1000_read_part_num(struct e1000_hw *hw,
5185                     uint32_t *part_num)
5186 {
5187     uint16_t offset = EEPROM_PBA_BYTE_1;
5188     uint16_t eeprom_data;
5189
5190     DEBUGFUNC("e1000_read_part_num");
5191
5192     /* Get word 0 from EEPROM */
5193     if(e1000_read_eeprom(hw, offset, 1, &eeprom_data) < 0) {
5194         DEBUGOUT("EEPROM Read Error\n");
5195         return -E1000_ERR_EEPROM;
5196     }
5197     /* Save word 0 in upper half of part_num */
5198     *part_num = (uint32_t) (eeprom_data << 16);
5199
5200     /* Get word 1 from EEPROM */
5201     if(e1000_read_eeprom(hw, ++offset, 1, &eeprom_data) < 0) {
5202         DEBUGOUT("EEPROM Read Error\n");
5203         return -E1000_ERR_EEPROM;
5204     }
5205     /* Save word 1 in lower half of part_num */
5206     *part_num |= eeprom_data;
5207
5208     return E1000_SUCCESS;
5209 }
5210
5211 /******************************************************************************
5212  * Reads the adapter's MAC address from the EEPROM and inverts the LSB for the
5213  * second function of dual function devices
5214  *
5215  * hw - Struct containing variables accessed by shared code
5216  *****************************************************************************/
5217 int32_t
5218 e1000_read_mac_addr(struct e1000_hw * hw)
5219 {
5220     uint16_t offset;
5221     uint16_t eeprom_data, i;
5222
5223     DEBUGFUNC("e1000_read_mac_addr");
5224
5225     for(i = 0; i < NODE_ADDRESS_SIZE; i += 2) {
5226         offset = i >> 1;
5227         if(e1000_read_eeprom(hw, offset, 1, &eeprom_data) < 0) {
5228             DEBUGOUT("EEPROM Read Error\n");
5229             return -E1000_ERR_EEPROM;
5230         }
5231         hw->perm_mac_addr[i] = (uint8_t) (eeprom_data & 0x00FF);
5232         hw->perm_mac_addr[i+1] = (uint8_t) (eeprom_data >> 8);
5233     }
5234
5235     switch (hw->mac_type) {
5236     default:
5237         break;
5238     case e1000_82546:
5239     case e1000_82546_rev_3:
5240     case e1000_82571:
5241     case e1000_80003es2lan:
5242         if(E1000_READ_REG(hw, STATUS) & E1000_STATUS_FUNC_1)
5243             hw->perm_mac_addr[5] ^= 0x01;
5244         break;
5245     }
5246
5247     for(i = 0; i < NODE_ADDRESS_SIZE; i++)
5248         hw->mac_addr[i] = hw->perm_mac_addr[i];
5249     return E1000_SUCCESS;
5250 }
5251
5252 /******************************************************************************
5253  * Initializes receive address filters.
5254  *
5255  * hw - Struct containing variables accessed by shared code
5256  *
5257  * Places the MAC address in receive address register 0 and clears the rest
5258  * of the receive addresss registers. Clears the multicast table. Assumes
5259  * the receiver is in reset when the routine is called.
5260  *****************************************************************************/
5261 static void
5262 e1000_init_rx_addrs(struct e1000_hw *hw)
5263 {
5264     uint32_t i;
5265     uint32_t rar_num;
5266
5267     DEBUGFUNC("e1000_init_rx_addrs");
5268
5269     /* Setup the receive address. */
5270     DEBUGOUT("Programming MAC Address into RAR[0]\n");
5271
5272     e1000_rar_set(hw, hw->mac_addr, 0);
5273
5274     rar_num = E1000_RAR_ENTRIES;
5275
5276     /* Reserve a spot for the Locally Administered Address to work around
5277      * an 82571 issue in which a reset on one port will reload the MAC on
5278      * the other port. */
5279     if ((hw->mac_type == e1000_82571) && (hw->laa_is_present == TRUE))
5280         rar_num -= 1;
5281     /* Zero out the other 15 receive addresses. */
5282     DEBUGOUT("Clearing RAR[1-15]\n");
5283     for(i = 1; i < rar_num; i++) {
5284         E1000_WRITE_REG_ARRAY(hw, RA, (i << 1), 0);
5285         E1000_WRITE_FLUSH(hw);
5286         E1000_WRITE_REG_ARRAY(hw, RA, ((i << 1) + 1), 0);
5287         E1000_WRITE_FLUSH(hw);
5288     }
5289 }
5290
5291 #if 0
5292 /******************************************************************************
5293  * Updates the MAC's list of multicast addresses.
5294  *
5295  * hw - Struct containing variables accessed by shared code
5296  * mc_addr_list - the list of new multicast addresses
5297  * mc_addr_count - number of addresses
5298  * pad - number of bytes between addresses in the list
5299  * rar_used_count - offset where to start adding mc addresses into the RAR's
5300  *
5301  * The given list replaces any existing list. Clears the last 15 receive
5302  * address registers and the multicast table. Uses receive address registers
5303  * for the first 15 multicast addresses, and hashes the rest into the
5304  * multicast table.
5305  *****************************************************************************/
5306 void
5307 e1000_mc_addr_list_update(struct e1000_hw *hw,
5308                           uint8_t *mc_addr_list,
5309                           uint32_t mc_addr_count,
5310                           uint32_t pad,
5311                           uint32_t rar_used_count)
5312 {
5313     uint32_t hash_value;
5314     uint32_t i;
5315     uint32_t num_rar_entry;
5316     uint32_t num_mta_entry;
5317
5318     DEBUGFUNC("e1000_mc_addr_list_update");
5319
5320     /* Set the new number of MC addresses that we are being requested to use. */
5321     hw->num_mc_addrs = mc_addr_count;
5322
5323     /* Clear RAR[1-15] */
5324     DEBUGOUT(" Clearing RAR[1-15]\n");
5325     num_rar_entry = E1000_RAR_ENTRIES;
5326     /* Reserve a spot for the Locally Administered Address to work around
5327      * an 82571 issue in which a reset on one port will reload the MAC on
5328      * the other port. */
5329     if ((hw->mac_type == e1000_82571) && (hw->laa_is_present == TRUE))
5330         num_rar_entry -= 1;
5331
5332     for(i = rar_used_count; i < num_rar_entry; i++) {
5333         E1000_WRITE_REG_ARRAY(hw, RA, (i << 1), 0);
5334         E1000_WRITE_FLUSH(hw);
5335         E1000_WRITE_REG_ARRAY(hw, RA, ((i << 1) + 1), 0);
5336         E1000_WRITE_FLUSH(hw);
5337     }
5338
5339     /* Clear the MTA */
5340     DEBUGOUT(" Clearing MTA\n");
5341     num_mta_entry = E1000_NUM_MTA_REGISTERS;
5342     for(i = 0; i < num_mta_entry; i++) {
5343         E1000_WRITE_REG_ARRAY(hw, MTA, i, 0);
5344         E1000_WRITE_FLUSH(hw);
5345     }
5346
5347     /* Add the new addresses */
5348     for(i = 0; i < mc_addr_count; i++) {
5349         DEBUGOUT(" Adding the multicast addresses:\n");
5350         DEBUGOUT7(" MC Addr #%d =%.2X %.2X %.2X %.2X %.2X %.2X\n", i,
5351                   mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad)],
5352                   mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad) + 1],
5353                   mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad) + 2],
5354                   mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad) + 3],
5355                   mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad) + 4],
5356                   mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad) + 5]);
5357
5358         hash_value = e1000_hash_mc_addr(hw,
5359                                         mc_addr_list +
5360                                         (i * (ETH_LENGTH_OF_ADDRESS + pad)));
5361
5362         DEBUGOUT1(" Hash value = 0x%03X\n", hash_value);
5363
5364         /* Place this multicast address in the RAR if there is room, *
5365          * else put it in the MTA
5366          */
5367         if (rar_used_count < num_rar_entry) {
5368             e1000_rar_set(hw,
5369                           mc_addr_list + (i * (ETH_LENGTH_OF_ADDRESS + pad)),
5370                           rar_used_count);
5371             rar_used_count++;
5372         } else {
5373             e1000_mta_set(hw, hash_value);
5374         }
5375     }
5376     DEBUGOUT("MC Update Complete\n");
5377 }
5378 #endif  /*  0  */
5379
5380 /******************************************************************************
5381  * Hashes an address to determine its location in the multicast table
5382  *
5383  * hw - Struct containing variables accessed by shared code
5384  * mc_addr - the multicast address to hash
5385  *****************************************************************************/
5386 uint32_t
5387 e1000_hash_mc_addr(struct e1000_hw *hw,
5388                    uint8_t *mc_addr)
5389 {
5390     uint32_t hash_value = 0;
5391
5392     /* The portion of the address that is used for the hash table is
5393      * determined by the mc_filter_type setting.
5394      */
5395     switch (hw->mc_filter_type) {
5396     /* [0] [1] [2] [3] [4] [5]
5397      * 01  AA  00  12  34  56
5398      * LSB                 MSB
5399      */
5400     case 0:
5401         /* [47:36] i.e. 0x563 for above example address */
5402         hash_value = ((mc_addr[4] >> 4) | (((uint16_t) mc_addr[5]) << 4));
5403         break;
5404     case 1:
5405         /* [46:35] i.e. 0xAC6 for above example address */
5406         hash_value = ((mc_addr[4] >> 3) | (((uint16_t) mc_addr[5]) << 5));
5407         break;
5408     case 2:
5409         /* [45:34] i.e. 0x5D8 for above example address */
5410         hash_value = ((mc_addr[4] >> 2) | (((uint16_t) mc_addr[5]) << 6));
5411         break;
5412     case 3:
5413         /* [43:32] i.e. 0x634 for above example address */
5414         hash_value = ((mc_addr[4]) | (((uint16_t) mc_addr[5]) << 8));
5415         break;
5416     }
5417
5418     hash_value &= 0xFFF;
5419
5420     return hash_value;
5421 }
5422
5423 /******************************************************************************
5424  * Sets the bit in the multicast table corresponding to the hash value.
5425  *
5426  * hw - Struct containing variables accessed by shared code
5427  * hash_value - Multicast address hash value
5428  *****************************************************************************/
5429 void
5430 e1000_mta_set(struct e1000_hw *hw,
5431               uint32_t hash_value)
5432 {
5433     uint32_t hash_bit, hash_reg;
5434     uint32_t mta;
5435     uint32_t temp;
5436
5437     /* The MTA is a register array of 128 32-bit registers.
5438      * It is treated like an array of 4096 bits.  We want to set
5439      * bit BitArray[hash_value]. So we figure out what register
5440      * the bit is in, read it, OR in the new bit, then write
5441      * back the new value.  The register is determined by the
5442      * upper 7 bits of the hash value and the bit within that
5443      * register are determined by the lower 5 bits of the value.
5444      */
5445     hash_reg = (hash_value >> 5) & 0x7F;
5446     hash_bit = hash_value & 0x1F;
5447
5448     mta = E1000_READ_REG_ARRAY(hw, MTA, hash_reg);
5449
5450     mta |= (1 << hash_bit);
5451
5452     /* If we are on an 82544 and we are trying to write an odd offset
5453      * in the MTA, save off the previous entry before writing and
5454      * restore the old value after writing.
5455      */
5456     if((hw->mac_type == e1000_82544) && ((hash_reg & 0x1) == 1)) {
5457         temp = E1000_READ_REG_ARRAY(hw, MTA, (hash_reg - 1));
5458         E1000_WRITE_REG_ARRAY(hw, MTA, hash_reg, mta);
5459         E1000_WRITE_FLUSH(hw);
5460         E1000_WRITE_REG_ARRAY(hw, MTA, (hash_reg - 1), temp);
5461         E1000_WRITE_FLUSH(hw);
5462     } else {
5463         E1000_WRITE_REG_ARRAY(hw, MTA, hash_reg, mta);
5464         E1000_WRITE_FLUSH(hw);
5465     }
5466 }
5467
5468 /******************************************************************************
5469  * Puts an ethernet address into a receive address register.
5470  *
5471  * hw - Struct containing variables accessed by shared code
5472  * addr - Address to put into receive address register
5473  * index - Receive address register to write
5474  *****************************************************************************/
5475 void
5476 e1000_rar_set(struct e1000_hw *hw,
5477               uint8_t *addr,
5478               uint32_t index)
5479 {
5480     uint32_t rar_low, rar_high;
5481
5482     /* HW expects these in little endian so we reverse the byte order
5483      * from network order (big endian) to little endian
5484      */
5485     rar_low = ((uint32_t) addr[0] |
5486                ((uint32_t) addr[1] << 8) |
5487                ((uint32_t) addr[2] << 16) | ((uint32_t) addr[3] << 24));
5488     rar_high = ((uint32_t) addr[4] | ((uint32_t) addr[5] << 8));
5489
5490     /* Disable Rx and flush all Rx frames before enabling RSS to avoid Rx
5491      * unit hang.
5492      *
5493      * Description:
5494      * If there are any Rx frames queued up or otherwise present in the HW
5495      * before RSS is enabled, and then we enable RSS, the HW Rx unit will
5496      * hang.  To work around this issue, we have to disable receives and
5497      * flush out all Rx frames before we enable RSS. To do so, we modify we
5498      * redirect all Rx traffic to manageability and then reset the HW.
5499      * This flushes away Rx frames, and (since the redirections to
5500      * manageability persists across resets) keeps new ones from coming in
5501      * while we work.  Then, we clear the Address Valid AV bit for all MAC
5502      * addresses and undo the re-direction to manageability.
5503      * Now, frames are coming in again, but the MAC won't accept them, so
5504      * far so good.  We now proceed to initialize RSS (if necessary) and
5505      * configure the Rx unit.  Last, we re-enable the AV bits and continue
5506      * on our merry way.
5507      */
5508     switch (hw->mac_type) {
5509     case e1000_82571:
5510     case e1000_82572:
5511     case e1000_80003es2lan:
5512         if (hw->leave_av_bit_off == TRUE)
5513             break;
5514     default:
5515         /* Indicate to hardware the Address is Valid. */
5516         rar_high |= E1000_RAH_AV;
5517         break;
5518     }
5519
5520     E1000_WRITE_REG_ARRAY(hw, RA, (index << 1), rar_low);
5521     E1000_WRITE_FLUSH(hw);
5522     E1000_WRITE_REG_ARRAY(hw, RA, ((index << 1) + 1), rar_high);
5523     E1000_WRITE_FLUSH(hw);
5524 }
5525
5526 /******************************************************************************
5527  * Writes a value to the specified offset in the VLAN filter table.
5528  *
5529  * hw - Struct containing variables accessed by shared code
5530  * offset - Offset in VLAN filer table to write
5531  * value - Value to write into VLAN filter table
5532  *****************************************************************************/
5533 void
5534 e1000_write_vfta(struct e1000_hw *hw,
5535                  uint32_t offset,
5536                  uint32_t value)
5537 {
5538     uint32_t temp;
5539
5540     if((hw->mac_type == e1000_82544) && ((offset & 0x1) == 1)) {
5541         temp = E1000_READ_REG_ARRAY(hw, VFTA, (offset - 1));
5542         E1000_WRITE_REG_ARRAY(hw, VFTA, offset, value);
5543         E1000_WRITE_FLUSH(hw);
5544         E1000_WRITE_REG_ARRAY(hw, VFTA, (offset - 1), temp);
5545         E1000_WRITE_FLUSH(hw);
5546     } else {
5547         E1000_WRITE_REG_ARRAY(hw, VFTA, offset, value);
5548         E1000_WRITE_FLUSH(hw);
5549     }
5550 }
5551
5552 /******************************************************************************
5553  * Clears the VLAN filer table
5554  *
5555  * hw - Struct containing variables accessed by shared code
5556  *****************************************************************************/
5557 static void
5558 e1000_clear_vfta(struct e1000_hw *hw)
5559 {
5560     uint32_t offset;
5561     uint32_t vfta_value = 0;
5562     uint32_t vfta_offset = 0;
5563     uint32_t vfta_bit_in_reg = 0;
5564
5565     if (hw->mac_type == e1000_82573) {
5566         if (hw->mng_cookie.vlan_id != 0) {
5567             /* The VFTA is a 4096b bit-field, each identifying a single VLAN
5568              * ID.  The following operations determine which 32b entry
5569              * (i.e. offset) into the array we want to set the VLAN ID
5570              * (i.e. bit) of the manageability unit. */
5571             vfta_offset = (hw->mng_cookie.vlan_id >>
5572                            E1000_VFTA_ENTRY_SHIFT) &
5573                           E1000_VFTA_ENTRY_MASK;
5574             vfta_bit_in_reg = 1 << (hw->mng_cookie.vlan_id &
5575                                     E1000_VFTA_ENTRY_BIT_SHIFT_MASK);
5576         }
5577     }
5578     for (offset = 0; offset < E1000_VLAN_FILTER_TBL_SIZE; offset++) {
5579         /* If the offset we want to clear is the same offset of the
5580          * manageability VLAN ID, then clear all bits except that of the
5581          * manageability unit */
5582         vfta_value = (offset == vfta_offset) ? vfta_bit_in_reg : 0;
5583         E1000_WRITE_REG_ARRAY(hw, VFTA, offset, vfta_value);
5584         E1000_WRITE_FLUSH(hw);
5585     }
5586 }
5587
5588 static int32_t
5589 e1000_id_led_init(struct e1000_hw * hw)
5590 {
5591     uint32_t ledctl;
5592     const uint32_t ledctl_mask = 0x000000FF;
5593     const uint32_t ledctl_on = E1000_LEDCTL_MODE_LED_ON;
5594     const uint32_t ledctl_off = E1000_LEDCTL_MODE_LED_OFF;
5595     uint16_t eeprom_data, i, temp;
5596     const uint16_t led_mask = 0x0F;
5597
5598     DEBUGFUNC("e1000_id_led_init");
5599
5600     if(hw->mac_type < e1000_82540) {
5601         /* Nothing to do */
5602         return E1000_SUCCESS;
5603     }
5604
5605     ledctl = E1000_READ_REG(hw, LEDCTL);
5606     hw->ledctl_default = ledctl;
5607     hw->ledctl_mode1 = hw->ledctl_default;
5608     hw->ledctl_mode2 = hw->ledctl_default;
5609
5610     if(e1000_read_eeprom(hw, EEPROM_ID_LED_SETTINGS, 1, &eeprom_data) < 0) {
5611         DEBUGOUT("EEPROM Read Error\n");
5612         return -E1000_ERR_EEPROM;
5613     }
5614     if((eeprom_data== ID_LED_RESERVED_0000) ||
5615        (eeprom_data == ID_LED_RESERVED_FFFF)) eeprom_data = ID_LED_DEFAULT;
5616     for(i = 0; i < 4; i++) {
5617         temp = (eeprom_data >> (i << 2)) & led_mask;
5618         switch(temp) {
5619         case ID_LED_ON1_DEF2:
5620         case ID_LED_ON1_ON2:
5621         case ID_LED_ON1_OFF2:
5622             hw->ledctl_mode1 &= ~(ledctl_mask << (i << 3));
5623             hw->ledctl_mode1 |= ledctl_on << (i << 3);
5624             break;
5625         case ID_LED_OFF1_DEF2:
5626         case ID_LED_OFF1_ON2:
5627         case ID_LED_OFF1_OFF2:
5628             hw->ledctl_mode1 &= ~(ledctl_mask << (i << 3));
5629             hw->ledctl_mode1 |= ledctl_off << (i << 3);
5630             break;
5631         default:
5632             /* Do nothing */
5633             break;
5634         }
5635         switch(temp) {
5636         case ID_LED_DEF1_ON2:
5637         case ID_LED_ON1_ON2:
5638         case ID_LED_OFF1_ON2:
5639             hw->ledctl_mode2 &= ~(ledctl_mask << (i << 3));
5640             hw->ledctl_mode2 |= ledctl_on << (i << 3);
5641             break;
5642         case ID_LED_DEF1_OFF2:
5643         case ID_LED_ON1_OFF2:
5644         case ID_LED_OFF1_OFF2:
5645             hw->ledctl_mode2 &= ~(ledctl_mask << (i << 3));
5646             hw->ledctl_mode2 |= ledctl_off << (i << 3);
5647             break;
5648         default:
5649             /* Do nothing */
5650             break;
5651         }
5652     }
5653     return E1000_SUCCESS;
5654 }
5655
5656 /******************************************************************************
5657  * Prepares SW controlable LED for use and saves the current state of the LED.
5658  *
5659  * hw - Struct containing variables accessed by shared code
5660  *****************************************************************************/
5661 int32_t
5662 e1000_setup_led(struct e1000_hw *hw)
5663 {
5664     uint32_t ledctl;
5665     int32_t ret_val = E1000_SUCCESS;
5666
5667     DEBUGFUNC("e1000_setup_led");
5668
5669     switch(hw->mac_type) {
5670     case e1000_82542_rev2_0:
5671     case e1000_82542_rev2_1:
5672     case e1000_82543:
5673     case e1000_82544:
5674         /* No setup necessary */
5675         break;
5676     case e1000_82541:
5677     case e1000_82547:
5678     case e1000_82541_rev_2:
5679     case e1000_82547_rev_2:
5680         /* Turn off PHY Smart Power Down (if enabled) */
5681         ret_val = e1000_read_phy_reg(hw, IGP01E1000_GMII_FIFO,
5682                                      &hw->phy_spd_default);
5683         if(ret_val)
5684             return ret_val;
5685         ret_val = e1000_write_phy_reg(hw, IGP01E1000_GMII_FIFO,
5686                                       (uint16_t)(hw->phy_spd_default &
5687                                       ~IGP01E1000_GMII_SPD));
5688         if(ret_val)
5689             return ret_val;
5690         /* Fall Through */
5691     default:
5692         if(hw->media_type == e1000_media_type_fiber) {
5693             ledctl = E1000_READ_REG(hw, LEDCTL);
5694             /* Save current LEDCTL settings */
5695             hw->ledctl_default = ledctl;
5696             /* Turn off LED0 */
5697             ledctl &= ~(E1000_LEDCTL_LED0_IVRT |
5698                         E1000_LEDCTL_LED0_BLINK |
5699                         E1000_LEDCTL_LED0_MODE_MASK);
5700             ledctl |= (E1000_LEDCTL_MODE_LED_OFF <<
5701                        E1000_LEDCTL_LED0_MODE_SHIFT);
5702             E1000_WRITE_REG(hw, LEDCTL, ledctl);
5703         } else if(hw->media_type == e1000_media_type_copper)
5704             E1000_WRITE_REG(hw, LEDCTL, hw->ledctl_mode1);
5705         break;
5706     }
5707
5708     return E1000_SUCCESS;
5709 }
5710
5711 /******************************************************************************
5712  * Used on 82571 and later Si that has LED blink bits.
5713  * Callers must use their own timer and should have already called
5714  * e1000_id_led_init()
5715  * Call e1000_cleanup led() to stop blinking
5716  *
5717  * hw - Struct containing variables accessed by shared code
5718  *****************************************************************************/
5719 int32_t
5720 e1000_blink_led_start(struct e1000_hw *hw)
5721 {
5722     int16_t  i;
5723     uint32_t ledctl_blink = 0;
5724
5725     DEBUGFUNC("e1000_id_led_blink_on");
5726
5727     if (hw->mac_type < e1000_82571) {
5728         /* Nothing to do */
5729         return E1000_SUCCESS;
5730     }
5731     if (hw->media_type == e1000_media_type_fiber) {
5732         /* always blink LED0 for PCI-E fiber */
5733         ledctl_blink = E1000_LEDCTL_LED0_BLINK |
5734                      (E1000_LEDCTL_MODE_LED_ON << E1000_LEDCTL_LED0_MODE_SHIFT);
5735     } else {
5736         /* set the blink bit for each LED that's "on" (0x0E) in ledctl_mode2 */
5737         ledctl_blink = hw->ledctl_mode2;
5738         for (i=0; i < 4; i++)
5739             if (((hw->ledctl_mode2 >> (i * 8)) & 0xFF) ==
5740                 E1000_LEDCTL_MODE_LED_ON)
5741                 ledctl_blink |= (E1000_LEDCTL_LED0_BLINK << (i * 8));
5742     }
5743
5744     E1000_WRITE_REG(hw, LEDCTL, ledctl_blink);
5745
5746     return E1000_SUCCESS;
5747 }
5748
5749 /******************************************************************************
5750  * Restores the saved state of the SW controlable LED.
5751  *
5752  * hw - Struct containing variables accessed by shared code
5753  *****************************************************************************/
5754 int32_t
5755 e1000_cleanup_led(struct e1000_hw *hw)
5756 {
5757     int32_t ret_val = E1000_SUCCESS;
5758
5759     DEBUGFUNC("e1000_cleanup_led");
5760
5761     switch(hw->mac_type) {
5762     case e1000_82542_rev2_0:
5763     case e1000_82542_rev2_1:
5764     case e1000_82543:
5765     case e1000_82544:
5766         /* No cleanup necessary */
5767         break;
5768     case e1000_82541:
5769     case e1000_82547:
5770     case e1000_82541_rev_2:
5771     case e1000_82547_rev_2:
5772         /* Turn on PHY Smart Power Down (if previously enabled) */
5773         ret_val = e1000_write_phy_reg(hw, IGP01E1000_GMII_FIFO,
5774                                       hw->phy_spd_default);
5775         if(ret_val)
5776             return ret_val;
5777         /* Fall Through */
5778     default:
5779         /* Restore LEDCTL settings */
5780         E1000_WRITE_REG(hw, LEDCTL, hw->ledctl_default);
5781         break;
5782     }
5783
5784     return E1000_SUCCESS;
5785 }
5786
5787 /******************************************************************************
5788  * Turns on the software controllable LED
5789  *
5790  * hw - Struct containing variables accessed by shared code
5791  *****************************************************************************/
5792 int32_t
5793 e1000_led_on(struct e1000_hw *hw)
5794 {
5795     uint32_t ctrl = E1000_READ_REG(hw, CTRL);
5796
5797     DEBUGFUNC("e1000_led_on");
5798
5799     switch(hw->mac_type) {
5800     case e1000_82542_rev2_0:
5801     case e1000_82542_rev2_1:
5802     case e1000_82543:
5803         /* Set SW Defineable Pin 0 to turn on the LED */
5804         ctrl |= E1000_CTRL_SWDPIN0;
5805         ctrl |= E1000_CTRL_SWDPIO0;
5806         break;
5807     case e1000_82544:
5808         if(hw->media_type == e1000_media_type_fiber) {
5809             /* Set SW Defineable Pin 0 to turn on the LED */
5810             ctrl |= E1000_CTRL_SWDPIN0;
5811             ctrl |= E1000_CTRL_SWDPIO0;
5812         } else {
5813             /* Clear SW Defineable Pin 0 to turn on the LED */
5814             ctrl &= ~E1000_CTRL_SWDPIN0;
5815             ctrl |= E1000_CTRL_SWDPIO0;
5816         }
5817         break;
5818     default:
5819         if(hw->media_type == e1000_media_type_fiber) {
5820             /* Clear SW Defineable Pin 0 to turn on the LED */
5821             ctrl &= ~E1000_CTRL_SWDPIN0;
5822             ctrl |= E1000_CTRL_SWDPIO0;
5823         } else if(hw->media_type == e1000_media_type_copper) {
5824             E1000_WRITE_REG(hw, LEDCTL, hw->ledctl_mode2);
5825             return E1000_SUCCESS;
5826         }
5827         break;
5828     }
5829
5830     E1000_WRITE_REG(hw, CTRL, ctrl);
5831
5832     return E1000_SUCCESS;
5833 }
5834
5835 /******************************************************************************
5836  * Turns off the software controllable LED
5837  *
5838  * hw - Struct containing variables accessed by shared code
5839  *****************************************************************************/
5840 int32_t
5841 e1000_led_off(struct e1000_hw *hw)
5842 {
5843     uint32_t ctrl = E1000_READ_REG(hw, CTRL);
5844
5845     DEBUGFUNC("e1000_led_off");
5846
5847     switch(hw->mac_type) {
5848     case e1000_82542_rev2_0:
5849     case e1000_82542_rev2_1:
5850     case e1000_82543:
5851         /* Clear SW Defineable Pin 0 to turn off the LED */
5852         ctrl &= ~E1000_CTRL_SWDPIN0;
5853         ctrl |= E1000_CTRL_SWDPIO0;
5854         break;
5855     case e1000_82544:
5856         if(hw->media_type == e1000_media_type_fiber) {
5857             /* Clear SW Defineable Pin 0 to turn off the LED */
5858             ctrl &= ~E1000_CTRL_SWDPIN0;
5859             ctrl |= E1000_CTRL_SWDPIO0;
5860         } else {
5861             /* Set SW Defineable Pin 0 to turn off the LED */
5862             ctrl |= E1000_CTRL_SWDPIN0;
5863             ctrl |= E1000_CTRL_SWDPIO0;
5864         }
5865         break;
5866     default:
5867         if(hw->media_type == e1000_media_type_fiber) {
5868             /* Set SW Defineable Pin 0 to turn off the LED */
5869             ctrl |= E1000_CTRL_SWDPIN0;
5870             ctrl |= E1000_CTRL_SWDPIO0;
5871         } else if(hw->media_type == e1000_media_type_copper) {
5872             E1000_WRITE_REG(hw, LEDCTL, hw->ledctl_mode1);
5873             return E1000_SUCCESS;
5874         }
5875         break;
5876     }
5877
5878     E1000_WRITE_REG(hw, CTRL, ctrl);
5879
5880     return E1000_SUCCESS;
5881 }
5882
5883 /******************************************************************************
5884  * Clears all hardware statistics counters.
5885  *
5886  * hw - Struct containing variables accessed by shared code
5887  *****************************************************************************/
5888 static void
5889 e1000_clear_hw_cntrs(struct e1000_hw *hw)
5890 {
5891     volatile uint32_t temp;
5892
5893     temp = E1000_READ_REG(hw, CRCERRS);
5894     temp = E1000_READ_REG(hw, SYMERRS);
5895     temp = E1000_READ_REG(hw, MPC);
5896     temp = E1000_READ_REG(hw, SCC);
5897     temp = E1000_READ_REG(hw, ECOL);
5898     temp = E1000_READ_REG(hw, MCC);
5899     temp = E1000_READ_REG(hw, LATECOL);
5900     temp = E1000_READ_REG(hw, COLC);
5901     temp = E1000_READ_REG(hw, DC);
5902     temp = E1000_READ_REG(hw, SEC);
5903     temp = E1000_READ_REG(hw, RLEC);
5904     temp = E1000_READ_REG(hw, XONRXC);
5905     temp = E1000_READ_REG(hw, XONTXC);
5906     temp = E1000_READ_REG(hw, XOFFRXC);
5907     temp = E1000_READ_REG(hw, XOFFTXC);
5908     temp = E1000_READ_REG(hw, FCRUC);
5909     temp = E1000_READ_REG(hw, PRC64);
5910     temp = E1000_READ_REG(hw, PRC127);
5911     temp = E1000_READ_REG(hw, PRC255);
5912     temp = E1000_READ_REG(hw, PRC511);
5913     temp = E1000_READ_REG(hw, PRC1023);
5914     temp = E1000_READ_REG(hw, PRC1522);
5915     temp = E1000_READ_REG(hw, GPRC);
5916     temp = E1000_READ_REG(hw, BPRC);
5917     temp = E1000_READ_REG(hw, MPRC);
5918     temp = E1000_READ_REG(hw, GPTC);
5919     temp = E1000_READ_REG(hw, GORCL);
5920     temp = E1000_READ_REG(hw, GORCH);
5921     temp = E1000_READ_REG(hw, GOTCL);
5922     temp = E1000_READ_REG(hw, GOTCH);
5923     temp = E1000_READ_REG(hw, RNBC);
5924     temp = E1000_READ_REG(hw, RUC);
5925     temp = E1000_READ_REG(hw, RFC);
5926     temp = E1000_READ_REG(hw, ROC);
5927     temp = E1000_READ_REG(hw, RJC);
5928     temp = E1000_READ_REG(hw, TORL);
5929     temp = E1000_READ_REG(hw, TORH);
5930     temp = E1000_READ_REG(hw, TOTL);
5931     temp = E1000_READ_REG(hw, TOTH);
5932     temp = E1000_READ_REG(hw, TPR);
5933     temp = E1000_READ_REG(hw, TPT);
5934     temp = E1000_READ_REG(hw, PTC64);
5935     temp = E1000_READ_REG(hw, PTC127);
5936     temp = E1000_READ_REG(hw, PTC255);
5937     temp = E1000_READ_REG(hw, PTC511);
5938     temp = E1000_READ_REG(hw, PTC1023);
5939     temp = E1000_READ_REG(hw, PTC1522);
5940     temp = E1000_READ_REG(hw, MPTC);
5941     temp = E1000_READ_REG(hw, BPTC);
5942
5943     if(hw->mac_type < e1000_82543) return;
5944
5945     temp = E1000_READ_REG(hw, ALGNERRC);
5946     temp = E1000_READ_REG(hw, RXERRC);
5947     temp = E1000_READ_REG(hw, TNCRS);
5948     temp = E1000_READ_REG(hw, CEXTERR);
5949     temp = E1000_READ_REG(hw, TSCTC);
5950     temp = E1000_READ_REG(hw, TSCTFC);
5951
5952     if(hw->mac_type <= e1000_82544) return;
5953
5954     temp = E1000_READ_REG(hw, MGTPRC);
5955     temp = E1000_READ_REG(hw, MGTPDC);
5956     temp = E1000_READ_REG(hw, MGTPTC);
5957
5958     if(hw->mac_type <= e1000_82547_rev_2) return;
5959
5960     temp = E1000_READ_REG(hw, IAC);
5961     temp = E1000_READ_REG(hw, ICRXOC);
5962     temp = E1000_READ_REG(hw, ICRXPTC);
5963     temp = E1000_READ_REG(hw, ICRXATC);
5964     temp = E1000_READ_REG(hw, ICTXPTC);
5965     temp = E1000_READ_REG(hw, ICTXATC);
5966     temp = E1000_READ_REG(hw, ICTXQEC);
5967     temp = E1000_READ_REG(hw, ICTXQMTC);
5968     temp = E1000_READ_REG(hw, ICRXDMTC);
5969 }
5970
5971 /******************************************************************************
5972  * Resets Adaptive IFS to its default state.
5973  *
5974  * hw - Struct containing variables accessed by shared code
5975  *
5976  * Call this after e1000_init_hw. You may override the IFS defaults by setting
5977  * hw->ifs_params_forced to TRUE. However, you must initialize hw->
5978  * current_ifs_val, ifs_min_val, ifs_max_val, ifs_step_size, and ifs_ratio
5979  * before calling this function.
5980  *****************************************************************************/
5981 void
5982 e1000_reset_adaptive(struct e1000_hw *hw)
5983 {
5984     DEBUGFUNC("e1000_reset_adaptive");
5985
5986     if(hw->adaptive_ifs) {
5987         if(!hw->ifs_params_forced) {
5988             hw->current_ifs_val = 0;
5989             hw->ifs_min_val = IFS_MIN;
5990             hw->ifs_max_val = IFS_MAX;
5991             hw->ifs_step_size = IFS_STEP;
5992             hw->ifs_ratio = IFS_RATIO;
5993         }
5994         hw->in_ifs_mode = FALSE;
5995         E1000_WRITE_REG(hw, AIT, 0);
5996     } else {
5997         DEBUGOUT("Not in Adaptive IFS mode!\n");
5998     }
5999 }
6000
6001 /******************************************************************************
6002  * Called during the callback/watchdog routine to update IFS value based on
6003  * the ratio of transmits to collisions.
6004  *
6005  * hw - Struct containing variables accessed by shared code
6006  * tx_packets - Number of transmits since last callback
6007  * total_collisions - Number of collisions since last callback
6008  *****************************************************************************/
6009 void
6010 e1000_update_adaptive(struct e1000_hw *hw)
6011 {
6012     DEBUGFUNC("e1000_update_adaptive");
6013
6014     if(hw->adaptive_ifs) {
6015         if((hw->collision_delta * hw->ifs_ratio) > hw->tx_packet_delta) {
6016             if(hw->tx_packet_delta > MIN_NUM_XMITS) {
6017                 hw->in_ifs_mode = TRUE;
6018                 if(hw->current_ifs_val < hw->ifs_max_val) {
6019                     if(hw->current_ifs_val == 0)
6020                         hw->current_ifs_val = hw->ifs_min_val;
6021                     else
6022                         hw->current_ifs_val += hw->ifs_step_size;
6023                     E1000_WRITE_REG(hw, AIT, hw->current_ifs_val);
6024                 }
6025             }
6026         } else {
6027             if(hw->in_ifs_mode && (hw->tx_packet_delta <= MIN_NUM_XMITS)) {
6028                 hw->current_ifs_val = 0;
6029                 hw->in_ifs_mode = FALSE;
6030                 E1000_WRITE_REG(hw, AIT, 0);
6031             }
6032         }
6033     } else {
6034         DEBUGOUT("Not in Adaptive IFS mode!\n");
6035     }
6036 }
6037
6038 /******************************************************************************
6039  * Adjusts the statistic counters when a frame is accepted by TBI_ACCEPT
6040  *
6041  * hw - Struct containing variables accessed by shared code
6042  * frame_len - The length of the frame in question
6043  * mac_addr - The Ethernet destination address of the frame in question
6044  *****************************************************************************/
6045 void
6046 e1000_tbi_adjust_stats(struct e1000_hw *hw,
6047                        struct e1000_hw_stats *stats,
6048                        uint32_t frame_len,
6049                        uint8_t *mac_addr)
6050 {
6051     uint64_t carry_bit;
6052
6053     /* First adjust the frame length. */
6054     frame_len--;
6055     /* We need to adjust the statistics counters, since the hardware
6056      * counters overcount this packet as a CRC error and undercount
6057      * the packet as a good packet
6058      */
6059     /* This packet should not be counted as a CRC error.    */
6060     stats->crcerrs--;
6061     /* This packet does count as a Good Packet Received.    */
6062     stats->gprc++;
6063
6064     /* Adjust the Good Octets received counters             */
6065     carry_bit = 0x80000000 & stats->gorcl;
6066     stats->gorcl += frame_len;
6067     /* If the high bit of Gorcl (the low 32 bits of the Good Octets
6068      * Received Count) was one before the addition,
6069      * AND it is zero after, then we lost the carry out,
6070      * need to add one to Gorch (Good Octets Received Count High).
6071      * This could be simplified if all environments supported
6072      * 64-bit integers.
6073      */
6074     if(carry_bit && ((stats->gorcl & 0x80000000) == 0))
6075         stats->gorch++;
6076     /* Is this a broadcast or multicast?  Check broadcast first,
6077      * since the test for a multicast frame will test positive on
6078      * a broadcast frame.
6079      */
6080     if((mac_addr[0] == (uint8_t) 0xff) && (mac_addr[1] == (uint8_t) 0xff))
6081         /* Broadcast packet */
6082         stats->bprc++;
6083     else if(*mac_addr & 0x01)
6084         /* Multicast packet */
6085         stats->mprc++;
6086
6087     if(frame_len == hw->max_frame_size) {
6088         /* In this case, the hardware has overcounted the number of
6089          * oversize frames.
6090          */
6091         if(stats->roc > 0)
6092             stats->roc--;
6093     }
6094
6095     /* Adjust the bin counters when the extra byte put the frame in the
6096      * wrong bin. Remember that the frame_len was adjusted above.
6097      */
6098     if(frame_len == 64) {
6099         stats->prc64++;
6100         stats->prc127--;
6101     } else if(frame_len == 127) {
6102         stats->prc127++;
6103         stats->prc255--;
6104     } else if(frame_len == 255) {
6105         stats->prc255++;
6106         stats->prc511--;
6107     } else if(frame_len == 511) {
6108         stats->prc511++;
6109         stats->prc1023--;
6110     } else if(frame_len == 1023) {
6111         stats->prc1023++;
6112         stats->prc1522--;
6113     } else if(frame_len == 1522) {
6114         stats->prc1522++;
6115     }
6116 }
6117
6118 /******************************************************************************
6119  * Gets the current PCI bus type, speed, and width of the hardware
6120  *
6121  * hw - Struct containing variables accessed by shared code
6122  *****************************************************************************/
6123 void
6124 e1000_get_bus_info(struct e1000_hw *hw)
6125 {
6126     uint32_t status;
6127
6128     switch (hw->mac_type) {
6129     case e1000_82542_rev2_0:
6130     case e1000_82542_rev2_1:
6131         hw->bus_type = e1000_bus_type_unknown;
6132         hw->bus_speed = e1000_bus_speed_unknown;
6133         hw->bus_width = e1000_bus_width_unknown;
6134         break;
6135     case e1000_82572:
6136     case e1000_82573:
6137         hw->bus_type = e1000_bus_type_pci_express;
6138         hw->bus_speed = e1000_bus_speed_2500;
6139         hw->bus_width = e1000_bus_width_pciex_1;
6140         break;
6141     case e1000_82571:
6142     case e1000_80003es2lan:
6143         hw->bus_type = e1000_bus_type_pci_express;
6144         hw->bus_speed = e1000_bus_speed_2500;
6145         hw->bus_width = e1000_bus_width_pciex_4;
6146         break;
6147     default:
6148         status = E1000_READ_REG(hw, STATUS);
6149         hw->bus_type = (status & E1000_STATUS_PCIX_MODE) ?
6150                        e1000_bus_type_pcix : e1000_bus_type_pci;
6151
6152         if(hw->device_id == E1000_DEV_ID_82546EB_QUAD_COPPER) {
6153             hw->bus_speed = (hw->bus_type == e1000_bus_type_pci) ?
6154                             e1000_bus_speed_66 : e1000_bus_speed_120;
6155         } else if(hw->bus_type == e1000_bus_type_pci) {
6156             hw->bus_speed = (status & E1000_STATUS_PCI66) ?
6157                             e1000_bus_speed_66 : e1000_bus_speed_33;
6158         } else {
6159             switch (status & E1000_STATUS_PCIX_SPEED) {
6160             case E1000_STATUS_PCIX_SPEED_66:
6161                 hw->bus_speed = e1000_bus_speed_66;
6162                 break;
6163             case E1000_STATUS_PCIX_SPEED_100:
6164                 hw->bus_speed = e1000_bus_speed_100;
6165                 break;
6166             case E1000_STATUS_PCIX_SPEED_133:
6167                 hw->bus_speed = e1000_bus_speed_133;
6168                 break;
6169             default:
6170                 hw->bus_speed = e1000_bus_speed_reserved;
6171                 break;
6172             }
6173         }
6174         hw->bus_width = (status & E1000_STATUS_BUS64) ?
6175                         e1000_bus_width_64 : e1000_bus_width_32;
6176         break;
6177     }
6178 }
6179
6180 #if 0
6181 /******************************************************************************
6182  * Reads a value from one of the devices registers using port I/O (as opposed
6183  * memory mapped I/O). Only 82544 and newer devices support port I/O.
6184  *
6185  * hw - Struct containing variables accessed by shared code
6186  * offset - offset to read from
6187  *****************************************************************************/
6188 uint32_t
6189 e1000_read_reg_io(struct e1000_hw *hw,
6190                   uint32_t offset)
6191 {
6192     unsigned long io_addr = hw->io_base;
6193     unsigned long io_data = hw->io_base + 4;
6194
6195     e1000_io_write(hw, io_addr, offset);
6196     return e1000_io_read(hw, io_data);
6197 }
6198 #endif  /*  0  */
6199
6200 /******************************************************************************
6201  * Writes a value to one of the devices registers using port I/O (as opposed to
6202  * memory mapped I/O). Only 82544 and newer devices support port I/O.
6203  *
6204  * hw - Struct containing variables accessed by shared code
6205  * offset - offset to write to
6206  * value - value to write
6207  *****************************************************************************/
6208 static void
6209 e1000_write_reg_io(struct e1000_hw *hw,
6210                    uint32_t offset,
6211                    uint32_t value)
6212 {
6213     unsigned long io_addr = hw->io_base;
6214     unsigned long io_data = hw->io_base + 4;
6215
6216     e1000_io_write(hw, io_addr, offset);
6217     e1000_io_write(hw, io_data, value);
6218 }
6219
6220
6221 /******************************************************************************
6222  * Estimates the cable length.
6223  *
6224  * hw - Struct containing variables accessed by shared code
6225  * min_length - The estimated minimum length
6226  * max_length - The estimated maximum length
6227  *
6228  * returns: - E1000_ERR_XXX
6229  *            E1000_SUCCESS
6230  *
6231  * This function always returns a ranged length (minimum & maximum).
6232  * So for M88 phy's, this function interprets the one value returned from the
6233  * register to the minimum and maximum range.
6234  * For IGP phy's, the function calculates the range by the AGC registers.
6235  *****************************************************************************/
6236 static int32_t
6237 e1000_get_cable_length(struct e1000_hw *hw,
6238                        uint16_t *min_length,
6239                        uint16_t *max_length)
6240 {
6241     int32_t ret_val;
6242     uint16_t agc_value = 0;
6243     uint16_t cur_agc, min_agc = IGP01E1000_AGC_LENGTH_TABLE_SIZE;
6244     uint16_t max_agc = 0;
6245     uint16_t i, phy_data;
6246     uint16_t cable_length;
6247
6248     DEBUGFUNC("e1000_get_cable_length");
6249
6250     *min_length = *max_length = 0;
6251
6252     /* Use old method for Phy older than IGP */
6253     if(hw->phy_type == e1000_phy_m88) {
6254
6255         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS,
6256                                      &phy_data);
6257         if(ret_val)
6258             return ret_val;
6259         cable_length = (phy_data & M88E1000_PSSR_CABLE_LENGTH) >>
6260                        M88E1000_PSSR_CABLE_LENGTH_SHIFT;
6261
6262         /* Convert the enum value to ranged values */
6263         switch (cable_length) {
6264         case e1000_cable_length_50:
6265             *min_length = 0;
6266             *max_length = e1000_igp_cable_length_50;
6267             break;
6268         case e1000_cable_length_50_80:
6269             *min_length = e1000_igp_cable_length_50;
6270             *max_length = e1000_igp_cable_length_80;
6271             break;
6272         case e1000_cable_length_80_110:
6273             *min_length = e1000_igp_cable_length_80;
6274             *max_length = e1000_igp_cable_length_110;
6275             break;
6276         case e1000_cable_length_110_140:
6277             *min_length = e1000_igp_cable_length_110;
6278             *max_length = e1000_igp_cable_length_140;
6279             break;
6280         case e1000_cable_length_140:
6281             *min_length = e1000_igp_cable_length_140;
6282             *max_length = e1000_igp_cable_length_170;
6283             break;
6284         default:
6285             return -E1000_ERR_PHY;
6286             break;
6287         }
6288     } else if (hw->phy_type == e1000_phy_gg82563) {
6289         ret_val = e1000_read_phy_reg(hw, GG82563_PHY_DSP_DISTANCE,
6290                                      &phy_data);
6291         if (ret_val)
6292             return ret_val;
6293         cable_length = phy_data & GG82563_DSPD_CABLE_LENGTH;
6294
6295         switch (cable_length) {
6296         case e1000_gg_cable_length_60:
6297             *min_length = 0;
6298             *max_length = e1000_igp_cable_length_60;
6299             break;
6300         case e1000_gg_cable_length_60_115:
6301             *min_length = e1000_igp_cable_length_60;
6302             *max_length = e1000_igp_cable_length_115;
6303             break;
6304         case e1000_gg_cable_length_115_150:
6305             *min_length = e1000_igp_cable_length_115;
6306             *max_length = e1000_igp_cable_length_150;
6307             break;
6308         case e1000_gg_cable_length_150:
6309             *min_length = e1000_igp_cable_length_150;
6310             *max_length = e1000_igp_cable_length_180;
6311             break;
6312         default:
6313             return -E1000_ERR_PHY;
6314             break;
6315         }
6316     } else if(hw->phy_type == e1000_phy_igp) { /* For IGP PHY */
6317         uint16_t agc_reg_array[IGP01E1000_PHY_CHANNEL_NUM] =
6318                                                          {IGP01E1000_PHY_AGC_A,
6319                                                           IGP01E1000_PHY_AGC_B,
6320                                                           IGP01E1000_PHY_AGC_C,
6321                                                           IGP01E1000_PHY_AGC_D};
6322         /* Read the AGC registers for all channels */
6323         for(i = 0; i < IGP01E1000_PHY_CHANNEL_NUM; i++) {
6324
6325             ret_val = e1000_read_phy_reg(hw, agc_reg_array[i], &phy_data);
6326             if(ret_val)
6327                 return ret_val;
6328
6329             cur_agc = phy_data >> IGP01E1000_AGC_LENGTH_SHIFT;
6330
6331             /* Array bound check. */
6332             if((cur_agc >= IGP01E1000_AGC_LENGTH_TABLE_SIZE - 1) ||
6333                (cur_agc == 0))
6334                 return -E1000_ERR_PHY;
6335
6336             agc_value += cur_agc;
6337
6338             /* Update minimal AGC value. */
6339             if(min_agc > cur_agc)
6340                 min_agc = cur_agc;
6341         }
6342
6343         /* Remove the minimal AGC result for length < 50m */
6344         if(agc_value < IGP01E1000_PHY_CHANNEL_NUM * e1000_igp_cable_length_50) {
6345             agc_value -= min_agc;
6346
6347             /* Get the average length of the remaining 3 channels */
6348             agc_value /= (IGP01E1000_PHY_CHANNEL_NUM - 1);
6349         } else {
6350             /* Get the average length of all the 4 channels. */
6351             agc_value /= IGP01E1000_PHY_CHANNEL_NUM;
6352         }
6353
6354         /* Set the range of the calculated length. */
6355         *min_length = ((e1000_igp_cable_length_table[agc_value] -
6356                        IGP01E1000_AGC_RANGE) > 0) ?
6357                        (e1000_igp_cable_length_table[agc_value] -
6358                        IGP01E1000_AGC_RANGE) : 0;
6359         *max_length = e1000_igp_cable_length_table[agc_value] +
6360                       IGP01E1000_AGC_RANGE;
6361     } else if (hw->phy_type == e1000_phy_igp_2) {
6362         uint16_t agc_reg_array[IGP02E1000_PHY_CHANNEL_NUM] =
6363                                                          {IGP02E1000_PHY_AGC_A,
6364                                                           IGP02E1000_PHY_AGC_B,
6365                                                           IGP02E1000_PHY_AGC_C,
6366                                                           IGP02E1000_PHY_AGC_D};
6367         /* Read the AGC registers for all channels */
6368         for (i = 0; i < IGP02E1000_PHY_CHANNEL_NUM; i++) {
6369             ret_val = e1000_read_phy_reg(hw, agc_reg_array[i], &phy_data);
6370             if (ret_val)
6371                 return ret_val;
6372
6373             /* Getting bits 15:9, which represent the combination of course and
6374              * fine gain values.  The result is a number that can be put into
6375              * the lookup table to obtain the approximate cable length. */
6376             cur_agc = (phy_data >> IGP02E1000_AGC_LENGTH_SHIFT) &
6377                       IGP02E1000_AGC_LENGTH_MASK;
6378
6379             /* Remove min & max AGC values from calculation. */
6380             if (e1000_igp_2_cable_length_table[min_agc] > e1000_igp_2_cable_length_table[cur_agc])
6381                 min_agc = cur_agc;
6382             if (e1000_igp_2_cable_length_table[max_agc] < e1000_igp_2_cable_length_table[cur_agc])
6383                 max_agc = cur_agc;
6384
6385             agc_value += e1000_igp_2_cable_length_table[cur_agc];
6386         }
6387
6388         agc_value -= (e1000_igp_2_cable_length_table[min_agc] + e1000_igp_2_cable_length_table[max_agc]);
6389         agc_value /= (IGP02E1000_PHY_CHANNEL_NUM - 2);
6390
6391         /* Calculate cable length with the error range of +/- 10 meters. */
6392         *min_length = ((agc_value - IGP02E1000_AGC_RANGE) > 0) ?
6393                        (agc_value - IGP02E1000_AGC_RANGE) : 0;
6394         *max_length = agc_value + IGP02E1000_AGC_RANGE;
6395     }
6396
6397     return E1000_SUCCESS;
6398 }
6399
6400 /******************************************************************************
6401  * Check the cable polarity
6402  *
6403  * hw - Struct containing variables accessed by shared code
6404  * polarity - output parameter : 0 - Polarity is not reversed
6405  *                               1 - Polarity is reversed.
6406  *
6407  * returns: - E1000_ERR_XXX
6408  *            E1000_SUCCESS
6409  *
6410  * For phy's older then IGP, this function simply reads the polarity bit in the
6411  * Phy Status register.  For IGP phy's, this bit is valid only if link speed is
6412  * 10 Mbps.  If the link speed is 100 Mbps there is no polarity so this bit will
6413  * return 0.  If the link speed is 1000 Mbps the polarity status is in the
6414  * IGP01E1000_PHY_PCS_INIT_REG.
6415  *****************************************************************************/
6416 static int32_t
6417 e1000_check_polarity(struct e1000_hw *hw,
6418                      uint16_t *polarity)
6419 {
6420     int32_t ret_val;
6421     uint16_t phy_data;
6422
6423     DEBUGFUNC("e1000_check_polarity");
6424
6425     if ((hw->phy_type == e1000_phy_m88) ||
6426         (hw->phy_type == e1000_phy_gg82563)) {
6427         /* return the Polarity bit in the Status register. */
6428         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS,
6429                                      &phy_data);
6430         if(ret_val)
6431             return ret_val;
6432         *polarity = (phy_data & M88E1000_PSSR_REV_POLARITY) >>
6433                     M88E1000_PSSR_REV_POLARITY_SHIFT;
6434     } else if(hw->phy_type == e1000_phy_igp ||
6435               hw->phy_type == e1000_phy_igp_2) {
6436         /* Read the Status register to check the speed */
6437         ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_STATUS,
6438                                      &phy_data);
6439         if(ret_val)
6440             return ret_val;
6441
6442         /* If speed is 1000 Mbps, must read the IGP01E1000_PHY_PCS_INIT_REG to
6443          * find the polarity status */
6444         if((phy_data & IGP01E1000_PSSR_SPEED_MASK) ==
6445            IGP01E1000_PSSR_SPEED_1000MBPS) {
6446
6447             /* Read the GIG initialization PCS register (0x00B4) */
6448             ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PCS_INIT_REG,
6449                                          &phy_data);
6450             if(ret_val)
6451                 return ret_val;
6452
6453             /* Check the polarity bits */
6454             *polarity = (phy_data & IGP01E1000_PHY_POLARITY_MASK) ? 1 : 0;
6455         } else {
6456             /* For 10 Mbps, read the polarity bit in the status register. (for
6457              * 100 Mbps this bit is always 0) */
6458             *polarity = phy_data & IGP01E1000_PSSR_POLARITY_REVERSED;
6459         }
6460     }
6461     return E1000_SUCCESS;
6462 }
6463
6464 /******************************************************************************
6465  * Check if Downshift occured
6466  *
6467  * hw - Struct containing variables accessed by shared code
6468  * downshift - output parameter : 0 - No Downshift ocured.
6469  *                                1 - Downshift ocured.
6470  *
6471  * returns: - E1000_ERR_XXX
6472  *            E1000_SUCCESS
6473  *
6474  * For phy's older then IGP, this function reads the Downshift bit in the Phy
6475  * Specific Status register.  For IGP phy's, it reads the Downgrade bit in the
6476  * Link Health register.  In IGP this bit is latched high, so the driver must
6477  * read it immediately after link is established.
6478  *****************************************************************************/
6479 static int32_t
6480 e1000_check_downshift(struct e1000_hw *hw)
6481 {
6482     int32_t ret_val;
6483     uint16_t phy_data;
6484
6485     DEBUGFUNC("e1000_check_downshift");
6486
6487     if(hw->phy_type == e1000_phy_igp ||
6488         hw->phy_type == e1000_phy_igp_2) {
6489         ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_LINK_HEALTH,
6490                                      &phy_data);
6491         if(ret_val)
6492             return ret_val;
6493
6494         hw->speed_downgraded = (phy_data & IGP01E1000_PLHR_SS_DOWNGRADE) ? 1 : 0;
6495     } else if ((hw->phy_type == e1000_phy_m88) ||
6496                (hw->phy_type == e1000_phy_gg82563)) {
6497         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS,
6498                                      &phy_data);
6499         if(ret_val)
6500             return ret_val;
6501
6502         hw->speed_downgraded = (phy_data & M88E1000_PSSR_DOWNSHIFT) >>
6503                                M88E1000_PSSR_DOWNSHIFT_SHIFT;
6504     }
6505
6506     return E1000_SUCCESS;
6507 }
6508
6509 /*****************************************************************************
6510  *
6511  * 82541_rev_2 & 82547_rev_2 have the capability to configure the DSP when a
6512  * gigabit link is achieved to improve link quality.
6513  *
6514  * hw: Struct containing variables accessed by shared code
6515  *
6516  * returns: - E1000_ERR_PHY if fail to read/write the PHY
6517  *            E1000_SUCCESS at any other case.
6518  *
6519  ****************************************************************************/
6520
6521 static int32_t
6522 e1000_config_dsp_after_link_change(struct e1000_hw *hw,
6523                                    boolean_t link_up)
6524 {
6525     int32_t ret_val;
6526     uint16_t phy_data, phy_saved_data, speed, duplex, i;
6527     uint16_t dsp_reg_array[IGP01E1000_PHY_CHANNEL_NUM] =
6528                                         {IGP01E1000_PHY_AGC_PARAM_A,
6529                                         IGP01E1000_PHY_AGC_PARAM_B,
6530                                         IGP01E1000_PHY_AGC_PARAM_C,
6531                                         IGP01E1000_PHY_AGC_PARAM_D};
6532     uint16_t min_length, max_length;
6533
6534     DEBUGFUNC("e1000_config_dsp_after_link_change");
6535
6536     if(hw->phy_type != e1000_phy_igp)
6537         return E1000_SUCCESS;
6538
6539     if(link_up) {
6540         ret_val = e1000_get_speed_and_duplex(hw, &speed, &duplex);
6541         if(ret_val) {
6542             DEBUGOUT("Error getting link speed and duplex\n");
6543             return ret_val;
6544         }
6545
6546         if(speed == SPEED_1000) {
6547
6548             e1000_get_cable_length(hw, &min_length, &max_length);
6549
6550             if((hw->dsp_config_state == e1000_dsp_config_enabled) &&
6551                 min_length >= e1000_igp_cable_length_50) {
6552
6553                 for(i = 0; i < IGP01E1000_PHY_CHANNEL_NUM; i++) {
6554                     ret_val = e1000_read_phy_reg(hw, dsp_reg_array[i],
6555                                                  &phy_data);
6556                     if(ret_val)
6557                         return ret_val;
6558
6559                     phy_data &= ~IGP01E1000_PHY_EDAC_MU_INDEX;
6560
6561                     ret_val = e1000_write_phy_reg(hw, dsp_reg_array[i],
6562                                                   phy_data);
6563                     if(ret_val)
6564                         return ret_val;
6565                 }
6566                 hw->dsp_config_state = e1000_dsp_config_activated;
6567             }
6568
6569             if((hw->ffe_config_state == e1000_ffe_config_enabled) &&
6570                (min_length < e1000_igp_cable_length_50)) {
6571
6572                 uint16_t ffe_idle_err_timeout = FFE_IDLE_ERR_COUNT_TIMEOUT_20;
6573                 uint32_t idle_errs = 0;
6574
6575                 /* clear previous idle error counts */
6576                 ret_val = e1000_read_phy_reg(hw, PHY_1000T_STATUS,
6577                                              &phy_data);
6578                 if(ret_val)
6579                     return ret_val;
6580
6581                 for(i = 0; i < ffe_idle_err_timeout; i++) {
6582                     udelay(1000);
6583                     ret_val = e1000_read_phy_reg(hw, PHY_1000T_STATUS,
6584                                                  &phy_data);
6585                     if(ret_val)
6586                         return ret_val;
6587
6588                     idle_errs += (phy_data & SR_1000T_IDLE_ERROR_CNT);
6589                     if(idle_errs > SR_1000T_PHY_EXCESSIVE_IDLE_ERR_COUNT) {
6590                         hw->ffe_config_state = e1000_ffe_config_active;
6591
6592                         ret_val = e1000_write_phy_reg(hw,
6593                                     IGP01E1000_PHY_DSP_FFE,
6594                                     IGP01E1000_PHY_DSP_FFE_CM_CP);
6595                         if(ret_val)
6596                             return ret_val;
6597                         break;
6598                     }
6599
6600                     if(idle_errs)
6601                         ffe_idle_err_timeout = FFE_IDLE_ERR_COUNT_TIMEOUT_100;
6602                 }
6603             }
6604         }
6605     } else {
6606         if(hw->dsp_config_state == e1000_dsp_config_activated) {
6607             /* Save off the current value of register 0x2F5B to be restored at
6608              * the end of the routines. */
6609             ret_val = e1000_read_phy_reg(hw, 0x2F5B, &phy_saved_data);
6610
6611             if(ret_val)
6612                 return ret_val;
6613
6614             /* Disable the PHY transmitter */
6615             ret_val = e1000_write_phy_reg(hw, 0x2F5B, 0x0003);
6616
6617             if(ret_val)
6618                 return ret_val;
6619
6620             msec_delay_irq(20);
6621
6622             ret_val = e1000_write_phy_reg(hw, 0x0000,
6623                                           IGP01E1000_IEEE_FORCE_GIGA);
6624             if(ret_val)
6625                 return ret_val;
6626             for(i = 0; i < IGP01E1000_PHY_CHANNEL_NUM; i++) {
6627                 ret_val = e1000_read_phy_reg(hw, dsp_reg_array[i], &phy_data);
6628                 if(ret_val)
6629                     return ret_val;
6630
6631                 phy_data &= ~IGP01E1000_PHY_EDAC_MU_INDEX;
6632                 phy_data |=  IGP01E1000_PHY_EDAC_SIGN_EXT_9_BITS;
6633
6634                 ret_val = e1000_write_phy_reg(hw,dsp_reg_array[i], phy_data);
6635                 if(ret_val)
6636                     return ret_val;
6637             }
6638
6639             ret_val = e1000_write_phy_reg(hw, 0x0000,
6640                                           IGP01E1000_IEEE_RESTART_AUTONEG);
6641             if(ret_val)
6642                 return ret_val;
6643
6644             msec_delay_irq(20);
6645
6646             /* Now enable the transmitter */
6647             ret_val = e1000_write_phy_reg(hw, 0x2F5B, phy_saved_data);
6648
6649             if(ret_val)
6650                 return ret_val;
6651
6652             hw->dsp_config_state = e1000_dsp_config_enabled;
6653         }
6654
6655         if(hw->ffe_config_state == e1000_ffe_config_active) {
6656             /* Save off the current value of register 0x2F5B to be restored at
6657              * the end of the routines. */
6658             ret_val = e1000_read_phy_reg(hw, 0x2F5B, &phy_saved_data);
6659
6660             if(ret_val)
6661                 return ret_val;
6662
6663             /* Disable the PHY transmitter */
6664             ret_val = e1000_write_phy_reg(hw, 0x2F5B, 0x0003);
6665
6666             if(ret_val)
6667                 return ret_val;
6668
6669             msec_delay_irq(20);
6670
6671             ret_val = e1000_write_phy_reg(hw, 0x0000,
6672                                           IGP01E1000_IEEE_FORCE_GIGA);
6673             if(ret_val)
6674                 return ret_val;
6675             ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_DSP_FFE,
6676                                           IGP01E1000_PHY_DSP_FFE_DEFAULT);
6677             if(ret_val)
6678                 return ret_val;
6679
6680             ret_val = e1000_write_phy_reg(hw, 0x0000,
6681                                           IGP01E1000_IEEE_RESTART_AUTONEG);
6682             if(ret_val)
6683                 return ret_val;
6684
6685             msec_delay_irq(20);
6686
6687             /* Now enable the transmitter */
6688             ret_val = e1000_write_phy_reg(hw, 0x2F5B, phy_saved_data);
6689
6690             if(ret_val)
6691                 return ret_val;
6692
6693             hw->ffe_config_state = e1000_ffe_config_enabled;
6694         }
6695     }
6696     return E1000_SUCCESS;
6697 }
6698
6699 /*****************************************************************************
6700  * Set PHY to class A mode
6701  * Assumes the following operations will follow to enable the new class mode.
6702  *  1. Do a PHY soft reset
6703  *  2. Restart auto-negotiation or force link.
6704  *
6705  * hw - Struct containing variables accessed by shared code
6706  ****************************************************************************/
6707 static int32_t
6708 e1000_set_phy_mode(struct e1000_hw *hw)
6709 {
6710     int32_t ret_val;
6711     uint16_t eeprom_data;
6712
6713     DEBUGFUNC("e1000_set_phy_mode");
6714
6715     if((hw->mac_type == e1000_82545_rev_3) &&
6716        (hw->media_type == e1000_media_type_copper)) {
6717         ret_val = e1000_read_eeprom(hw, EEPROM_PHY_CLASS_WORD, 1, &eeprom_data);
6718         if(ret_val) {
6719             return ret_val;
6720         }
6721
6722         if((eeprom_data != EEPROM_RESERVED_WORD) &&
6723            (eeprom_data & EEPROM_PHY_CLASS_A)) {
6724             ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x000B);
6725             if(ret_val)
6726                 return ret_val;
6727             ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, 0x8104);
6728             if(ret_val)
6729                 return ret_val;
6730
6731             hw->phy_reset_disable = FALSE;
6732         }
6733     }
6734
6735     return E1000_SUCCESS;
6736 }
6737
6738 /*****************************************************************************
6739  *
6740  * This function sets the lplu state according to the active flag.  When
6741  * activating lplu this function also disables smart speed and vise versa.
6742  * lplu will not be activated unless the device autonegotiation advertisment
6743  * meets standards of either 10 or 10/100 or 10/100/1000 at all duplexes.
6744  * hw: Struct containing variables accessed by shared code
6745  * active - true to enable lplu false to disable lplu.
6746  *
6747  * returns: - E1000_ERR_PHY if fail to read/write the PHY
6748  *            E1000_SUCCESS at any other case.
6749  *
6750  ****************************************************************************/
6751
6752 static int32_t
6753 e1000_set_d3_lplu_state(struct e1000_hw *hw,
6754                         boolean_t active)
6755 {
6756     int32_t ret_val;
6757     uint16_t phy_data;
6758     DEBUGFUNC("e1000_set_d3_lplu_state");
6759
6760     if(hw->phy_type != e1000_phy_igp && hw->phy_type != e1000_phy_igp_2)
6761         return E1000_SUCCESS;
6762
6763     /* During driver activity LPLU should not be used or it will attain link
6764      * from the lowest speeds starting from 10Mbps. The capability is used for
6765      * Dx transitions and states */
6766     if(hw->mac_type == e1000_82541_rev_2 || hw->mac_type == e1000_82547_rev_2) {
6767         ret_val = e1000_read_phy_reg(hw, IGP01E1000_GMII_FIFO, &phy_data);
6768         if(ret_val)
6769             return ret_val;
6770     } else {
6771         ret_val = e1000_read_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT, &phy_data);
6772         if(ret_val)
6773             return ret_val;
6774     }
6775
6776     if(!active) {
6777         if(hw->mac_type == e1000_82541_rev_2 ||
6778            hw->mac_type == e1000_82547_rev_2) {
6779             phy_data &= ~IGP01E1000_GMII_FLEX_SPD;
6780             ret_val = e1000_write_phy_reg(hw, IGP01E1000_GMII_FIFO, phy_data);
6781             if(ret_val)
6782                 return ret_val;
6783         } else {
6784                 phy_data &= ~IGP02E1000_PM_D3_LPLU;
6785                 ret_val = e1000_write_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT,
6786                                               phy_data);
6787                 if (ret_val)
6788                     return ret_val;
6789         }
6790
6791         /* LPLU and SmartSpeed are mutually exclusive.  LPLU is used during
6792          * Dx states where the power conservation is most important.  During
6793          * driver activity we should enable SmartSpeed, so performance is
6794          * maintained. */
6795         if (hw->smart_speed == e1000_smart_speed_on) {
6796             ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
6797                                          &phy_data);
6798             if(ret_val)
6799                 return ret_val;
6800
6801             phy_data |= IGP01E1000_PSCFR_SMART_SPEED;
6802             ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
6803                                           phy_data);
6804             if(ret_val)
6805                 return ret_val;
6806         } else if (hw->smart_speed == e1000_smart_speed_off) {
6807             ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
6808                                          &phy_data);
6809             if (ret_val)
6810                 return ret_val;
6811
6812             phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
6813             ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
6814                                           phy_data);
6815             if(ret_val)
6816                 return ret_val;
6817         }
6818
6819     } else if((hw->autoneg_advertised == AUTONEG_ADVERTISE_SPEED_DEFAULT) ||
6820               (hw->autoneg_advertised == AUTONEG_ADVERTISE_10_ALL ) ||
6821               (hw->autoneg_advertised == AUTONEG_ADVERTISE_10_100_ALL)) {
6822
6823         if(hw->mac_type == e1000_82541_rev_2 ||
6824            hw->mac_type == e1000_82547_rev_2) {
6825             phy_data |= IGP01E1000_GMII_FLEX_SPD;
6826             ret_val = e1000_write_phy_reg(hw, IGP01E1000_GMII_FIFO, phy_data);
6827             if(ret_val)
6828                 return ret_val;
6829         } else {
6830                 phy_data |= IGP02E1000_PM_D3_LPLU;
6831                 ret_val = e1000_write_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT,
6832                                               phy_data);
6833                 if (ret_val)
6834                     return ret_val;
6835         }
6836
6837         /* When LPLU is enabled we should disable SmartSpeed */
6838         ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG, &phy_data);
6839         if(ret_val)
6840             return ret_val;
6841
6842         phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
6843         ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG, phy_data);
6844         if(ret_val)
6845             return ret_val;
6846
6847     }
6848     return E1000_SUCCESS;
6849 }
6850
6851 /*****************************************************************************
6852  *
6853  * This function sets the lplu d0 state according to the active flag.  When
6854  * activating lplu this function also disables smart speed and vise versa.
6855  * lplu will not be activated unless the device autonegotiation advertisment
6856  * meets standards of either 10 or 10/100 or 10/100/1000 at all duplexes.
6857  * hw: Struct containing variables accessed by shared code
6858  * active - true to enable lplu false to disable lplu.
6859  *
6860  * returns: - E1000_ERR_PHY if fail to read/write the PHY
6861  *            E1000_SUCCESS at any other case.
6862  *
6863  ****************************************************************************/
6864
6865 static int32_t
6866 e1000_set_d0_lplu_state(struct e1000_hw *hw,
6867                         boolean_t active)
6868 {
6869     int32_t ret_val;
6870     uint16_t phy_data;
6871     DEBUGFUNC("e1000_set_d0_lplu_state");
6872
6873     if(hw->mac_type <= e1000_82547_rev_2)
6874         return E1000_SUCCESS;
6875
6876         ret_val = e1000_read_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT, &phy_data);
6877         if(ret_val)
6878             return ret_val;
6879
6880     if (!active) {
6881             phy_data &= ~IGP02E1000_PM_D0_LPLU;
6882             ret_val = e1000_write_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT, phy_data);
6883             if (ret_val)
6884                 return ret_val;
6885
6886         /* LPLU and SmartSpeed are mutually exclusive.  LPLU is used during
6887          * Dx states where the power conservation is most important.  During
6888          * driver activity we should enable SmartSpeed, so performance is
6889          * maintained. */
6890         if (hw->smart_speed == e1000_smart_speed_on) {
6891             ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
6892                                          &phy_data);
6893             if(ret_val)
6894                 return ret_val;
6895
6896             phy_data |= IGP01E1000_PSCFR_SMART_SPEED;
6897             ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
6898                                           phy_data);
6899             if(ret_val)
6900                 return ret_val;
6901         } else if (hw->smart_speed == e1000_smart_speed_off) {
6902             ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
6903                                          &phy_data);
6904             if (ret_val)
6905                 return ret_val;
6906
6907             phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
6908             ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
6909                                           phy_data);
6910             if(ret_val)
6911                 return ret_val;
6912         }
6913
6914
6915     } else {
6916
6917             phy_data |= IGP02E1000_PM_D0_LPLU;
6918             ret_val = e1000_write_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT, phy_data);
6919             if (ret_val)
6920                 return ret_val;
6921
6922         /* When LPLU is enabled we should disable SmartSpeed */
6923         ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG, &phy_data);
6924         if(ret_val)
6925             return ret_val;
6926
6927         phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
6928         ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG, phy_data);
6929         if(ret_val)
6930             return ret_val;
6931
6932     }
6933     return E1000_SUCCESS;
6934 }
6935
6936 /******************************************************************************
6937  * Change VCO speed register to improve Bit Error Rate performance of SERDES.
6938  *
6939  * hw - Struct containing variables accessed by shared code
6940  *****************************************************************************/
6941 static int32_t
6942 e1000_set_vco_speed(struct e1000_hw *hw)
6943 {
6944     int32_t  ret_val;
6945     uint16_t default_page = 0;
6946     uint16_t phy_data;
6947
6948     DEBUGFUNC("e1000_set_vco_speed");
6949
6950     switch(hw->mac_type) {
6951     case e1000_82545_rev_3:
6952     case e1000_82546_rev_3:
6953        break;
6954     default:
6955         return E1000_SUCCESS;
6956     }
6957
6958     /* Set PHY register 30, page 5, bit 8 to 0 */
6959
6960     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, &default_page);
6961     if(ret_val)
6962         return ret_val;
6963
6964     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x0005);
6965     if(ret_val)
6966         return ret_val;
6967
6968     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, &phy_data);
6969     if(ret_val)
6970         return ret_val;
6971
6972     phy_data &= ~M88E1000_PHY_VCO_REG_BIT8;
6973     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, phy_data);
6974     if(ret_val)
6975         return ret_val;
6976
6977     /* Set PHY register 30, page 4, bit 11 to 1 */
6978
6979     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x0004);
6980     if(ret_val)
6981         return ret_val;
6982
6983     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, &phy_data);
6984     if(ret_val)
6985         return ret_val;
6986
6987     phy_data |= M88E1000_PHY_VCO_REG_BIT11;
6988     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, phy_data);
6989     if(ret_val)
6990         return ret_val;
6991
6992     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, default_page);
6993     if(ret_val)
6994         return ret_val;
6995
6996     return E1000_SUCCESS;
6997 }
6998
6999
7000 /*****************************************************************************
7001  * This function reads the cookie from ARC ram.
7002  *
7003  * returns: - E1000_SUCCESS .
7004  ****************************************************************************/
7005 int32_t
7006 e1000_host_if_read_cookie(struct e1000_hw * hw, uint8_t *buffer)
7007 {
7008     uint8_t i;
7009     uint32_t offset = E1000_MNG_DHCP_COOKIE_OFFSET;
7010     uint8_t length = E1000_MNG_DHCP_COOKIE_LENGTH;
7011
7012     length = (length >> 2);
7013     offset = (offset >> 2);
7014
7015     for (i = 0; i < length; i++) {
7016         *((uint32_t *) buffer + i) =
7017             E1000_READ_REG_ARRAY_DWORD(hw, HOST_IF, offset + i);
7018     }
7019     return E1000_SUCCESS;
7020 }
7021
7022
7023 /*****************************************************************************
7024  * This function checks whether the HOST IF is enabled for command operaton
7025  * and also checks whether the previous command is completed.
7026  * It busy waits in case of previous command is not completed.
7027  *
7028  * returns: - E1000_ERR_HOST_INTERFACE_COMMAND in case if is not ready or
7029  *            timeout
7030  *          - E1000_SUCCESS for success.
7031  ****************************************************************************/
7032 static int32_t
7033 e1000_mng_enable_host_if(struct e1000_hw * hw)
7034 {
7035     uint32_t hicr;
7036     uint8_t i;
7037
7038     /* Check that the host interface is enabled. */
7039     hicr = E1000_READ_REG(hw, HICR);
7040     if ((hicr & E1000_HICR_EN) == 0) {
7041         DEBUGOUT("E1000_HOST_EN bit disabled.\n");
7042         return -E1000_ERR_HOST_INTERFACE_COMMAND;
7043     }
7044     /* check the previous command is completed */
7045     for (i = 0; i < E1000_MNG_DHCP_COMMAND_TIMEOUT; i++) {
7046         hicr = E1000_READ_REG(hw, HICR);
7047         if (!(hicr & E1000_HICR_C))
7048             break;
7049         msec_delay_irq(1);
7050     }
7051
7052     if (i == E1000_MNG_DHCP_COMMAND_TIMEOUT) {
7053         DEBUGOUT("Previous command timeout failed .\n");
7054         return -E1000_ERR_HOST_INTERFACE_COMMAND;
7055     }
7056     return E1000_SUCCESS;
7057 }
7058
7059 /*****************************************************************************
7060  * This function writes the buffer content at the offset given on the host if.
7061  * It also does alignment considerations to do the writes in most efficient way.
7062  * Also fills up the sum of the buffer in *buffer parameter.
7063  *
7064  * returns  - E1000_SUCCESS for success.
7065  ****************************************************************************/
7066 static int32_t
7067 e1000_mng_host_if_write(struct e1000_hw * hw, uint8_t *buffer,
7068                         uint16_t length, uint16_t offset, uint8_t *sum)
7069 {
7070     uint8_t *tmp;
7071     uint8_t *bufptr = buffer;
7072     uint32_t data;
7073     uint16_t remaining, i, j, prev_bytes;
7074
7075     /* sum = only sum of the data and it is not checksum */
7076
7077     if (length == 0 || offset + length > E1000_HI_MAX_MNG_DATA_LENGTH) {
7078         return -E1000_ERR_PARAM;
7079     }
7080
7081     tmp = (uint8_t *)&data;
7082     prev_bytes = offset & 0x3;
7083     offset &= 0xFFFC;
7084     offset >>= 2;
7085
7086     if (prev_bytes) {
7087         data = E1000_READ_REG_ARRAY_DWORD(hw, HOST_IF, offset);
7088         for (j = prev_bytes; j < sizeof(uint32_t); j++) {
7089             *(tmp + j) = *bufptr++;
7090             *sum += *(tmp + j);
7091         }
7092         E1000_WRITE_REG_ARRAY_DWORD(hw, HOST_IF, offset, data);
7093         length -= j - prev_bytes;
7094         offset++;
7095     }
7096
7097     remaining = length & 0x3;
7098     length -= remaining;
7099
7100     /* Calculate length in DWORDs */
7101     length >>= 2;
7102
7103     /* The device driver writes the relevant command block into the
7104      * ram area. */
7105     for (i = 0; i < length; i++) {
7106         for (j = 0; j < sizeof(uint32_t); j++) {
7107             *(tmp + j) = *bufptr++;
7108             *sum += *(tmp + j);
7109         }
7110
7111         E1000_WRITE_REG_ARRAY_DWORD(hw, HOST_IF, offset + i, data);
7112     }
7113     if (remaining) {
7114         for (j = 0; j < sizeof(uint32_t); j++) {
7115             if (j < remaining)
7116                 *(tmp + j) = *bufptr++;
7117             else
7118                 *(tmp + j) = 0;
7119
7120             *sum += *(tmp + j);
7121         }
7122         E1000_WRITE_REG_ARRAY_DWORD(hw, HOST_IF, offset + i, data);
7123     }
7124
7125     return E1000_SUCCESS;
7126 }
7127
7128
7129 /*****************************************************************************
7130  * This function writes the command header after does the checksum calculation.
7131  *
7132  * returns  - E1000_SUCCESS for success.
7133  ****************************************************************************/
7134 static int32_t
7135 e1000_mng_write_cmd_header(struct e1000_hw * hw,
7136                            struct e1000_host_mng_command_header * hdr)
7137 {
7138     uint16_t i;
7139     uint8_t sum;
7140     uint8_t *buffer;
7141
7142     /* Write the whole command header structure which includes sum of
7143      * the buffer */
7144
7145     uint16_t length = sizeof(struct e1000_host_mng_command_header);
7146
7147     sum = hdr->checksum;
7148     hdr->checksum = 0;
7149
7150     buffer = (uint8_t *) hdr;
7151     i = length;
7152     while(i--)
7153         sum += buffer[i];
7154
7155     hdr->checksum = 0 - sum;
7156
7157     length >>= 2;
7158     /* The device driver writes the relevant command block into the ram area. */
7159     for (i = 0; i < length; i++) {
7160         E1000_WRITE_REG_ARRAY_DWORD(hw, HOST_IF, i, *((uint32_t *) hdr + i));
7161         E1000_WRITE_FLUSH(hw);
7162     }
7163
7164     return E1000_SUCCESS;
7165 }
7166
7167
7168 /*****************************************************************************
7169  * This function indicates to ARC that a new command is pending which completes
7170  * one write operation by the driver.
7171  *
7172  * returns  - E1000_SUCCESS for success.
7173  ****************************************************************************/
7174 static int32_t
7175 e1000_mng_write_commit(
7176     struct e1000_hw * hw)
7177 {
7178     uint32_t hicr;
7179
7180     hicr = E1000_READ_REG(hw, HICR);
7181     /* Setting this bit tells the ARC that a new command is pending. */
7182     E1000_WRITE_REG(hw, HICR, hicr | E1000_HICR_C);
7183
7184     return E1000_SUCCESS;
7185 }
7186
7187
7188 /*****************************************************************************
7189  * This function checks the mode of the firmware.
7190  *
7191  * returns  - TRUE when the mode is IAMT or FALSE.
7192  ****************************************************************************/
7193 boolean_t
7194 e1000_check_mng_mode(
7195     struct e1000_hw *hw)
7196 {
7197     uint32_t fwsm;
7198
7199     fwsm = E1000_READ_REG(hw, FWSM);
7200
7201     if((fwsm & E1000_FWSM_MODE_MASK) ==
7202         (E1000_MNG_IAMT_MODE << E1000_FWSM_MODE_SHIFT))
7203         return TRUE;
7204
7205     return FALSE;
7206 }
7207
7208
7209 /*****************************************************************************
7210  * This function writes the dhcp info .
7211  ****************************************************************************/
7212 int32_t
7213 e1000_mng_write_dhcp_info(struct e1000_hw * hw, uint8_t *buffer,
7214                           uint16_t length)
7215 {
7216     int32_t ret_val;
7217     struct e1000_host_mng_command_header hdr;
7218
7219     hdr.command_id = E1000_MNG_DHCP_TX_PAYLOAD_CMD;
7220     hdr.command_length = length;
7221     hdr.reserved1 = 0;
7222     hdr.reserved2 = 0;
7223     hdr.checksum = 0;
7224
7225     ret_val = e1000_mng_enable_host_if(hw);
7226     if (ret_val == E1000_SUCCESS) {
7227         ret_val = e1000_mng_host_if_write(hw, buffer, length, sizeof(hdr),
7228                                           &(hdr.checksum));
7229         if (ret_val == E1000_SUCCESS) {
7230             ret_val = e1000_mng_write_cmd_header(hw, &hdr);
7231             if (ret_val == E1000_SUCCESS)
7232                 ret_val = e1000_mng_write_commit(hw);
7233         }
7234     }
7235     return ret_val;
7236 }
7237
7238
7239 /*****************************************************************************
7240  * This function calculates the checksum.
7241  *
7242  * returns  - checksum of buffer contents.
7243  ****************************************************************************/
7244 uint8_t
7245 e1000_calculate_mng_checksum(char *buffer, uint32_t length)
7246 {
7247     uint8_t sum = 0;
7248     uint32_t i;
7249
7250     if (!buffer)
7251         return 0;
7252
7253     for (i=0; i < length; i++)
7254         sum += buffer[i];
7255
7256     return (uint8_t) (0 - sum);
7257 }
7258
7259 /*****************************************************************************
7260  * This function checks whether tx pkt filtering needs to be enabled or not.
7261  *
7262  * returns  - TRUE for packet filtering or FALSE.
7263  ****************************************************************************/
7264 boolean_t
7265 e1000_enable_tx_pkt_filtering(struct e1000_hw *hw)
7266 {
7267     /* called in init as well as watchdog timer functions */
7268
7269     int32_t ret_val, checksum;
7270     boolean_t tx_filter = FALSE;
7271     struct e1000_host_mng_dhcp_cookie *hdr = &(hw->mng_cookie);
7272     uint8_t *buffer = (uint8_t *) &(hw->mng_cookie);
7273
7274     if (e1000_check_mng_mode(hw)) {
7275         ret_val = e1000_mng_enable_host_if(hw);
7276         if (ret_val == E1000_SUCCESS) {
7277             ret_val = e1000_host_if_read_cookie(hw, buffer);
7278             if (ret_val == E1000_SUCCESS) {
7279                 checksum = hdr->checksum;
7280                 hdr->checksum = 0;
7281                 if ((hdr->signature == E1000_IAMT_SIGNATURE) &&
7282                     checksum == e1000_calculate_mng_checksum((char *)buffer,
7283                                                E1000_MNG_DHCP_COOKIE_LENGTH)) {
7284                     if (hdr->status &
7285                         E1000_MNG_DHCP_COOKIE_STATUS_PARSING_SUPPORT)
7286                         tx_filter = TRUE;
7287                 } else
7288                     tx_filter = TRUE;
7289             } else
7290                 tx_filter = TRUE;
7291         }
7292     }
7293
7294     hw->tx_pkt_filtering = tx_filter;
7295     return tx_filter;
7296 }
7297
7298 /******************************************************************************
7299  * Verifies the hardware needs to allow ARPs to be processed by the host
7300  *
7301  * hw - Struct containing variables accessed by shared code
7302  *
7303  * returns: - TRUE/FALSE
7304  *
7305  *****************************************************************************/
7306 uint32_t
7307 e1000_enable_mng_pass_thru(struct e1000_hw *hw)
7308 {
7309     uint32_t manc;
7310     uint32_t fwsm, factps;
7311
7312     if (hw->asf_firmware_present) {
7313         manc = E1000_READ_REG(hw, MANC);
7314
7315         if (!(manc & E1000_MANC_RCV_TCO_EN) ||
7316             !(manc & E1000_MANC_EN_MAC_ADDR_FILTER))
7317             return FALSE;
7318         if (e1000_arc_subsystem_valid(hw) == TRUE) {
7319             fwsm = E1000_READ_REG(hw, FWSM);
7320             factps = E1000_READ_REG(hw, FACTPS);
7321
7322             if (((fwsm & E1000_FWSM_MODE_MASK) ==
7323                 (e1000_mng_mode_pt << E1000_FWSM_MODE_SHIFT)) &&
7324                 (factps & E1000_FACTPS_MNGCG))
7325                 return TRUE;
7326         } else
7327             if ((manc & E1000_MANC_SMBUS_EN) && !(manc & E1000_MANC_ASF_EN))
7328                 return TRUE;
7329     }
7330     return FALSE;
7331 }
7332
7333 static int32_t
7334 e1000_polarity_reversal_workaround(struct e1000_hw *hw)
7335 {
7336     int32_t ret_val;
7337     uint16_t mii_status_reg;
7338     uint16_t i;
7339
7340     /* Polarity reversal workaround for forced 10F/10H links. */
7341
7342     /* Disable the transmitter on the PHY */
7343
7344     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x0019);
7345     if(ret_val)
7346         return ret_val;
7347     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, 0xFFFF);
7348     if(ret_val)
7349         return ret_val;
7350
7351     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x0000);
7352     if(ret_val)
7353         return ret_val;
7354
7355     /* This loop will early-out if the NO link condition has been met. */
7356     for(i = PHY_FORCE_TIME; i > 0; i--) {
7357         /* Read the MII Status Register and wait for Link Status bit
7358          * to be clear.
7359          */
7360
7361         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
7362         if(ret_val)
7363             return ret_val;
7364
7365         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
7366         if(ret_val)
7367             return ret_val;
7368
7369         if((mii_status_reg & ~MII_SR_LINK_STATUS) == 0) break;
7370         msec_delay_irq(100);
7371     }
7372
7373     /* Recommended delay time after link has been lost */
7374     msec_delay_irq(1000);
7375
7376     /* Now we will re-enable th transmitter on the PHY */
7377
7378     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x0019);
7379     if(ret_val)
7380         return ret_val;
7381     msec_delay_irq(50);
7382     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, 0xFFF0);
7383     if(ret_val)
7384         return ret_val;
7385     msec_delay_irq(50);
7386     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, 0xFF00);
7387     if(ret_val)
7388         return ret_val;
7389     msec_delay_irq(50);
7390     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, 0x0000);
7391     if(ret_val)
7392         return ret_val;
7393
7394     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x0000);
7395     if(ret_val)
7396         return ret_val;
7397
7398     /* This loop will early-out if the link condition has been met. */
7399     for(i = PHY_FORCE_TIME; i > 0; i--) {
7400         /* Read the MII Status Register and wait for Link Status bit
7401          * to be set.
7402          */
7403
7404         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
7405         if(ret_val)
7406             return ret_val;
7407
7408         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
7409         if(ret_val)
7410             return ret_val;
7411
7412         if(mii_status_reg & MII_SR_LINK_STATUS) break;
7413         msec_delay_irq(100);
7414     }
7415     return E1000_SUCCESS;
7416 }
7417
7418 /***************************************************************************
7419  *
7420  * Disables PCI-Express master access.
7421  *
7422  * hw: Struct containing variables accessed by shared code
7423  *
7424  * returns: - none.
7425  *
7426  ***************************************************************************/
7427 static void
7428 e1000_set_pci_express_master_disable(struct e1000_hw *hw)
7429 {
7430     uint32_t ctrl;
7431
7432     DEBUGFUNC("e1000_set_pci_express_master_disable");
7433
7434     if (hw->bus_type != e1000_bus_type_pci_express)
7435         return;
7436
7437     ctrl = E1000_READ_REG(hw, CTRL);
7438     ctrl |= E1000_CTRL_GIO_MASTER_DISABLE;
7439     E1000_WRITE_REG(hw, CTRL, ctrl);
7440 }
7441
7442 #if 0
7443 /***************************************************************************
7444  *
7445  * Enables PCI-Express master access.
7446  *
7447  * hw: Struct containing variables accessed by shared code
7448  *
7449  * returns: - none.
7450  *
7451  ***************************************************************************/
7452 void
7453 e1000_enable_pciex_master(struct e1000_hw *hw)
7454 {
7455     uint32_t ctrl;
7456
7457     DEBUGFUNC("e1000_enable_pciex_master");
7458
7459     if (hw->bus_type != e1000_bus_type_pci_express)
7460         return;
7461
7462     ctrl = E1000_READ_REG(hw, CTRL);
7463     ctrl &= ~E1000_CTRL_GIO_MASTER_DISABLE;
7464     E1000_WRITE_REG(hw, CTRL, ctrl);
7465 }
7466 #endif  /*  0  */
7467
7468 /*******************************************************************************
7469  *
7470  * Disables PCI-Express master access and verifies there are no pending requests
7471  *
7472  * hw: Struct containing variables accessed by shared code
7473  *
7474  * returns: - E1000_ERR_MASTER_REQUESTS_PENDING if master disable bit hasn't
7475  *            caused the master requests to be disabled.
7476  *            E1000_SUCCESS master requests disabled.
7477  *
7478  ******************************************************************************/
7479 int32_t
7480 e1000_disable_pciex_master(struct e1000_hw *hw)
7481 {
7482     int32_t timeout = MASTER_DISABLE_TIMEOUT;   /* 80ms */
7483
7484     DEBUGFUNC("e1000_disable_pciex_master");
7485
7486     if (hw->bus_type != e1000_bus_type_pci_express)
7487         return E1000_SUCCESS;
7488
7489     e1000_set_pci_express_master_disable(hw);
7490
7491     while(timeout) {
7492         if(!(E1000_READ_REG(hw, STATUS) & E1000_STATUS_GIO_MASTER_ENABLE))
7493             break;
7494         else
7495             udelay(100);
7496         timeout--;
7497     }
7498
7499     if(!timeout) {
7500         DEBUGOUT("Master requests are pending.\n");
7501         return -E1000_ERR_MASTER_REQUESTS_PENDING;
7502     }
7503
7504     return E1000_SUCCESS;
7505 }
7506
7507 /*******************************************************************************
7508  *
7509  * Check for EEPROM Auto Read bit done.
7510  *
7511  * hw: Struct containing variables accessed by shared code
7512  *
7513  * returns: - E1000_ERR_RESET if fail to reset MAC
7514  *            E1000_SUCCESS at any other case.
7515  *
7516  ******************************************************************************/
7517 static int32_t
7518 e1000_get_auto_rd_done(struct e1000_hw *hw)
7519 {
7520     int32_t timeout = AUTO_READ_DONE_TIMEOUT;
7521
7522     DEBUGFUNC("e1000_get_auto_rd_done");
7523
7524     switch (hw->mac_type) {
7525     default:
7526         msec_delay(5);
7527         break;
7528     case e1000_82571:
7529     case e1000_82572:
7530     case e1000_82573:
7531     case e1000_80003es2lan:
7532         while(timeout) {
7533             if (E1000_READ_REG(hw, EECD) & E1000_EECD_AUTO_RD) break;
7534             else msec_delay(1);
7535             timeout--;
7536         }
7537
7538         if(!timeout) {
7539             DEBUGOUT("Auto read by HW from EEPROM has not completed.\n");
7540             return -E1000_ERR_RESET;
7541         }
7542         break;
7543     }
7544
7545     /* PHY configuration from NVM just starts after EECD_AUTO_RD sets to high.
7546      * Need to wait for PHY configuration completion before accessing NVM
7547      * and PHY. */
7548     if (hw->mac_type == e1000_82573)
7549         msec_delay(25);
7550
7551     return E1000_SUCCESS;
7552 }
7553
7554 /***************************************************************************
7555  * Checks if the PHY configuration is done
7556  *
7557  * hw: Struct containing variables accessed by shared code
7558  *
7559  * returns: - E1000_ERR_RESET if fail to reset MAC
7560  *            E1000_SUCCESS at any other case.
7561  *
7562  ***************************************************************************/
7563 static int32_t
7564 e1000_get_phy_cfg_done(struct e1000_hw *hw)
7565 {
7566     int32_t timeout = PHY_CFG_TIMEOUT;
7567     uint32_t cfg_mask = E1000_EEPROM_CFG_DONE;
7568
7569     DEBUGFUNC("e1000_get_phy_cfg_done");
7570
7571     switch (hw->mac_type) {
7572     default:
7573         msec_delay(10);
7574         break;
7575     case e1000_80003es2lan:
7576         /* Separate *_CFG_DONE_* bit for each port */
7577         if (E1000_READ_REG(hw, STATUS) & E1000_STATUS_FUNC_1)
7578             cfg_mask = E1000_EEPROM_CFG_DONE_PORT_1;
7579         /* Fall Through */
7580     case e1000_82571:
7581     case e1000_82572:
7582         while (timeout) {
7583             if (E1000_READ_REG(hw, EEMNGCTL) & cfg_mask)
7584                 break;
7585             else
7586                 msec_delay(1);
7587             timeout--;
7588         }
7589
7590         if (!timeout) {
7591             DEBUGOUT("MNG configuration cycle has not completed.\n");
7592             return -E1000_ERR_RESET;
7593         }
7594         break;
7595     }
7596
7597     return E1000_SUCCESS;
7598 }
7599
7600 /***************************************************************************
7601  *
7602  * Using the combination of SMBI and SWESMBI semaphore bits when resetting
7603  * adapter or Eeprom access.
7604  *
7605  * hw: Struct containing variables accessed by shared code
7606  *
7607  * returns: - E1000_ERR_EEPROM if fail to access EEPROM.
7608  *            E1000_SUCCESS at any other case.
7609  *
7610  ***************************************************************************/
7611 static int32_t
7612 e1000_get_hw_eeprom_semaphore(struct e1000_hw *hw)
7613 {
7614     int32_t timeout;
7615     uint32_t swsm;
7616
7617     DEBUGFUNC("e1000_get_hw_eeprom_semaphore");
7618
7619     if(!hw->eeprom_semaphore_present)
7620         return E1000_SUCCESS;
7621
7622     if (hw->mac_type == e1000_80003es2lan) {
7623         /* Get the SW semaphore. */
7624         if (e1000_get_software_semaphore(hw) != E1000_SUCCESS)
7625             return -E1000_ERR_EEPROM;
7626     }
7627
7628     /* Get the FW semaphore. */
7629     timeout = hw->eeprom.word_size + 1;
7630     while(timeout) {
7631         swsm = E1000_READ_REG(hw, SWSM);
7632         swsm |= E1000_SWSM_SWESMBI;
7633         E1000_WRITE_REG(hw, SWSM, swsm);
7634         /* if we managed to set the bit we got the semaphore. */
7635         swsm = E1000_READ_REG(hw, SWSM);
7636         if(swsm & E1000_SWSM_SWESMBI)
7637             break;
7638
7639         udelay(50);
7640         timeout--;
7641     }
7642
7643     if(!timeout) {
7644         /* Release semaphores */
7645         e1000_put_hw_eeprom_semaphore(hw);
7646         DEBUGOUT("Driver can't access the Eeprom - SWESMBI bit is set.\n");
7647         return -E1000_ERR_EEPROM;
7648     }
7649
7650     return E1000_SUCCESS;
7651 }
7652
7653 /***************************************************************************
7654  * This function clears HW semaphore bits.
7655  *
7656  * hw: Struct containing variables accessed by shared code
7657  *
7658  * returns: - None.
7659  *
7660  ***************************************************************************/
7661 static void
7662 e1000_put_hw_eeprom_semaphore(struct e1000_hw *hw)
7663 {
7664     uint32_t swsm;
7665
7666     DEBUGFUNC("e1000_put_hw_eeprom_semaphore");
7667
7668     if(!hw->eeprom_semaphore_present)
7669         return;
7670
7671     swsm = E1000_READ_REG(hw, SWSM);
7672     if (hw->mac_type == e1000_80003es2lan) {
7673         /* Release both semaphores. */
7674         swsm &= ~(E1000_SWSM_SMBI | E1000_SWSM_SWESMBI);
7675     } else
7676         swsm &= ~(E1000_SWSM_SWESMBI);
7677     E1000_WRITE_REG(hw, SWSM, swsm);
7678 }
7679
7680 /***************************************************************************
7681  *
7682  * Obtaining software semaphore bit (SMBI) before resetting PHY.
7683  *
7684  * hw: Struct containing variables accessed by shared code
7685  *
7686  * returns: - E1000_ERR_RESET if fail to obtain semaphore.
7687  *            E1000_SUCCESS at any other case.
7688  *
7689  ***************************************************************************/
7690 int32_t
7691 e1000_get_software_semaphore(struct e1000_hw *hw)
7692 {
7693     int32_t timeout = hw->eeprom.word_size + 1;
7694     uint32_t swsm;
7695
7696     DEBUGFUNC("e1000_get_software_semaphore");
7697
7698     if (hw->mac_type != e1000_80003es2lan)
7699         return E1000_SUCCESS;
7700
7701     while(timeout) {
7702         swsm = E1000_READ_REG(hw, SWSM);
7703         /* If SMBI bit cleared, it is now set and we hold the semaphore */
7704         if(!(swsm & E1000_SWSM_SMBI))
7705             break;
7706         msec_delay_irq(1);
7707         timeout--;
7708     }
7709
7710     if(!timeout) {
7711         DEBUGOUT("Driver can't access device - SMBI bit is set.\n");
7712         return -E1000_ERR_RESET;
7713     }
7714
7715     return E1000_SUCCESS;
7716 }
7717
7718 /***************************************************************************
7719  *
7720  * Release semaphore bit (SMBI).
7721  *
7722  * hw: Struct containing variables accessed by shared code
7723  *
7724  ***************************************************************************/
7725 void
7726 e1000_release_software_semaphore(struct e1000_hw *hw)
7727 {
7728     uint32_t swsm;
7729
7730     DEBUGFUNC("e1000_release_software_semaphore");
7731
7732     if (hw->mac_type != e1000_80003es2lan)
7733         return;
7734
7735     swsm = E1000_READ_REG(hw, SWSM);
7736     /* Release the SW semaphores.*/
7737     swsm &= ~E1000_SWSM_SMBI;
7738     E1000_WRITE_REG(hw, SWSM, swsm);
7739 }
7740
7741 /******************************************************************************
7742  * Checks if PHY reset is blocked due to SOL/IDER session, for example.
7743  * Returning E1000_BLK_PHY_RESET isn't necessarily an error.  But it's up to
7744  * the caller to figure out how to deal with it.
7745  *
7746  * hw - Struct containing variables accessed by shared code
7747  *
7748  * returns: - E1000_BLK_PHY_RESET
7749  *            E1000_SUCCESS
7750  *
7751  *****************************************************************************/
7752 int32_t
7753 e1000_check_phy_reset_block(struct e1000_hw *hw)
7754 {
7755     uint32_t manc = 0;
7756
7757     if (hw->mac_type > e1000_82547_rev_2)
7758         manc = E1000_READ_REG(hw, MANC);
7759     return (manc & E1000_MANC_BLK_PHY_RST_ON_IDE) ?
7760             E1000_BLK_PHY_RESET : E1000_SUCCESS;
7761 }
7762
7763 static uint8_t
7764 e1000_arc_subsystem_valid(struct e1000_hw *hw)
7765 {
7766     uint32_t fwsm;
7767
7768     /* On 8257x silicon, registers in the range of 0x8800 - 0x8FFC
7769      * may not be provided a DMA clock when no manageability features are
7770      * enabled.  We do not want to perform any reads/writes to these registers
7771      * if this is the case.  We read FWSM to determine the manageability mode.
7772      */
7773     switch (hw->mac_type) {
7774     case e1000_82571:
7775     case e1000_82572:
7776     case e1000_82573:
7777     case e1000_80003es2lan:
7778         fwsm = E1000_READ_REG(hw, FWSM);
7779         if((fwsm & E1000_FWSM_MODE_MASK) != 0)
7780             return TRUE;
7781         break;
7782     default:
7783         break;
7784     }
7785     return FALSE;
7786 }
7787
7788
7789 /******************************************************************************
7790  * Configure PCI-Ex no-snoop
7791  *
7792  * hw - Struct containing variables accessed by shared code.
7793  * no_snoop - Bitmap of no-snoop events.
7794  *
7795  * returns: E1000_SUCCESS
7796  *
7797  *****************************************************************************/
7798 int32_t
7799 e1000_set_pci_ex_no_snoop(struct e1000_hw *hw, uint32_t no_snoop)
7800 {
7801     uint32_t gcr_reg = 0;
7802
7803     DEBUGFUNC("e1000_set_pci_ex_no_snoop");
7804
7805     if (hw->bus_type == e1000_bus_type_unknown)
7806         e1000_get_bus_info(hw);
7807
7808     if (hw->bus_type != e1000_bus_type_pci_express)
7809         return E1000_SUCCESS;
7810
7811     if (no_snoop) {
7812         gcr_reg = E1000_READ_REG(hw, GCR);
7813         gcr_reg &= ~(PCI_EX_NO_SNOOP_ALL);
7814         gcr_reg |= no_snoop;
7815         E1000_WRITE_REG(hw, GCR, gcr_reg);
7816     }
7817     if (hw->mac_type == e1000_ich8lan) {
7818         uint32_t ctrl_ext;
7819
7820         E1000_WRITE_REG(hw, GCR, PCI_EX_82566_SNOOP_ALL);
7821
7822         ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
7823         ctrl_ext |= E1000_CTRL_EXT_RO_DIS;
7824         E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
7825     }
7826
7827     return E1000_SUCCESS;
7828 }
7829
7830 /***************************************************************************
7831  *
7832  * Get software semaphore FLAG bit (SWFLAG).
7833  * SWFLAG is used to synchronize the access to all shared resource between
7834  * SW, FW and HW.
7835  *
7836  * hw: Struct containing variables accessed by shared code
7837  *
7838  ***************************************************************************/
7839 int32_t
7840 e1000_get_software_flag(struct e1000_hw *hw)
7841 {
7842     int32_t timeout = PHY_CFG_TIMEOUT;
7843     uint32_t extcnf_ctrl;
7844
7845     DEBUGFUNC("e1000_get_software_flag");
7846
7847     if (hw->mac_type == e1000_ich8lan) {
7848         while (timeout) {
7849             extcnf_ctrl = E1000_READ_REG(hw, EXTCNF_CTRL);
7850             extcnf_ctrl |= E1000_EXTCNF_CTRL_SWFLAG;
7851             E1000_WRITE_REG(hw, EXTCNF_CTRL, extcnf_ctrl);
7852
7853             extcnf_ctrl = E1000_READ_REG(hw, EXTCNF_CTRL);
7854             if (extcnf_ctrl & E1000_EXTCNF_CTRL_SWFLAG)
7855                 break;
7856             msec_delay_irq(1);
7857             timeout--;
7858         }
7859
7860         if (!timeout) {
7861             DEBUGOUT("FW or HW locks the resource too long.\n");
7862             return -E1000_ERR_CONFIG;
7863         }
7864     }
7865
7866     return E1000_SUCCESS;
7867 }
7868
7869 /***************************************************************************
7870  *
7871  * Release software semaphore FLAG bit (SWFLAG).
7872  * SWFLAG is used to synchronize the access to all shared resource between
7873  * SW, FW and HW.
7874  *
7875  * hw: Struct containing variables accessed by shared code
7876  *
7877  ***************************************************************************/
7878 void
7879 e1000_release_software_flag(struct e1000_hw *hw)
7880 {
7881     uint32_t extcnf_ctrl;
7882
7883     DEBUGFUNC("e1000_release_software_flag");
7884
7885     if (hw->mac_type == e1000_ich8lan) {
7886         extcnf_ctrl= E1000_READ_REG(hw, EXTCNF_CTRL);
7887         extcnf_ctrl &= ~E1000_EXTCNF_CTRL_SWFLAG;
7888         E1000_WRITE_REG(hw, EXTCNF_CTRL, extcnf_ctrl);
7889     }
7890
7891     return;
7892 }
7893
7894 /***************************************************************************
7895  *
7896  * Disable dynamic power down mode in ife PHY.
7897  * It can be used to workaround band-gap problem.
7898  *
7899  * hw: Struct containing variables accessed by shared code
7900  *
7901  ***************************************************************************/
7902 int32_t
7903 e1000_ife_disable_dynamic_power_down(struct e1000_hw *hw)
7904 {
7905     uint16_t phy_data;
7906     int32_t ret_val = E1000_SUCCESS;
7907
7908     DEBUGFUNC("e1000_ife_disable_dynamic_power_down");
7909
7910     if (hw->phy_type == e1000_phy_ife) {
7911         ret_val = e1000_read_phy_reg(hw, IFE_PHY_SPECIAL_CONTROL, &phy_data);
7912         if (ret_val)
7913             return ret_val;
7914
7915         phy_data |=  IFE_PSC_DISABLE_DYNAMIC_POWER_DOWN;
7916         ret_val = e1000_write_phy_reg(hw, IFE_PHY_SPECIAL_CONTROL, phy_data);
7917     }
7918
7919     return ret_val;
7920 }
7921
7922 /***************************************************************************
7923  *
7924  * Enable dynamic power down mode in ife PHY.
7925  * It can be used to workaround band-gap problem.
7926  *
7927  * hw: Struct containing variables accessed by shared code
7928  *
7929  ***************************************************************************/
7930 int32_t
7931 e1000_ife_enable_dynamic_power_down(struct e1000_hw *hw)
7932 {
7933     uint16_t phy_data;
7934     int32_t ret_val = E1000_SUCCESS;
7935
7936     DEBUGFUNC("e1000_ife_enable_dynamic_power_down");
7937
7938     if (hw->phy_type == e1000_phy_ife) {
7939         ret_val = e1000_read_phy_reg(hw, IFE_PHY_SPECIAL_CONTROL, &phy_data);
7940         if (ret_val)
7941             return ret_val;
7942
7943         phy_data &=  ~IFE_PSC_DISABLE_DYNAMIC_POWER_DOWN;
7944         ret_val = e1000_write_phy_reg(hw, IFE_PHY_SPECIAL_CONTROL, phy_data);
7945     }
7946
7947     return ret_val;
7948 }
7949
7950 /******************************************************************************
7951  * Reads a 16 bit word or words from the EEPROM using the ICH8's flash access
7952  * register.
7953  *
7954  * hw - Struct containing variables accessed by shared code
7955  * offset - offset of word in the EEPROM to read
7956  * data - word read from the EEPROM
7957  * words - number of words to read
7958  *****************************************************************************/
7959 int32_t
7960 e1000_read_eeprom_ich8(struct e1000_hw *hw, uint16_t offset, uint16_t words,
7961                        uint16_t *data)
7962 {
7963     int32_t  error = E1000_SUCCESS;
7964     uint32_t flash_bank = 0;
7965     uint32_t act_offset = 0;
7966     uint32_t bank_offset = 0;
7967     uint16_t word = 0;
7968     uint16_t i = 0;
7969
7970     /* We need to know which is the valid flash bank.  In the event
7971      * that we didn't allocate eeprom_shadow_ram, we may not be
7972      * managing flash_bank.  So it cannot be trusted and needs
7973      * to be updated with each read.
7974      */
7975     /* Value of bit 22 corresponds to the flash bank we're on. */
7976     flash_bank = (E1000_READ_REG(hw, EECD) & E1000_EECD_SEC1VAL) ? 1 : 0;
7977
7978     /* Adjust offset appropriately if we're on bank 1 - adjust for word size */
7979     bank_offset = flash_bank * (hw->flash_bank_size * 2);
7980
7981     error = e1000_get_software_flag(hw);
7982     if (error != E1000_SUCCESS)
7983         return error;
7984
7985     for (i = 0; i < words; i++) {
7986         if (hw->eeprom_shadow_ram != NULL &&
7987             hw->eeprom_shadow_ram[offset+i].modified == TRUE) {
7988             data[i] = hw->eeprom_shadow_ram[offset+i].eeprom_word;
7989         } else {
7990             /* The NVM part needs a byte offset, hence * 2 */
7991             act_offset = bank_offset + ((offset + i) * 2);
7992             error = e1000_read_ich8_word(hw, act_offset, &word);
7993             if (error != E1000_SUCCESS)
7994                 break;
7995             data[i] = word;
7996         }
7997     }
7998
7999     e1000_release_software_flag(hw);
8000
8001     return error;
8002 }
8003
8004 /******************************************************************************
8005  * Writes a 16 bit word or words to the EEPROM using the ICH8's flash access
8006  * register.  Actually, writes are written to the shadow ram cache in the hw
8007  * structure hw->e1000_shadow_ram.  e1000_commit_shadow_ram flushes this to
8008  * the NVM, which occurs when the NVM checksum is updated.
8009  *
8010  * hw - Struct containing variables accessed by shared code
8011  * offset - offset of word in the EEPROM to write
8012  * words - number of words to write
8013  * data - words to write to the EEPROM
8014  *****************************************************************************/
8015 int32_t
8016 e1000_write_eeprom_ich8(struct e1000_hw *hw, uint16_t offset, uint16_t words,
8017                         uint16_t *data)
8018 {
8019     uint32_t i = 0;
8020     int32_t error = E1000_SUCCESS;
8021
8022     error = e1000_get_software_flag(hw);
8023     if (error != E1000_SUCCESS)
8024         return error;
8025
8026     /* A driver can write to the NVM only if it has eeprom_shadow_ram
8027      * allocated.  Subsequent reads to the modified words are read from
8028      * this cached structure as well.  Writes will only go into this
8029      * cached structure unless it's followed by a call to
8030      * e1000_update_eeprom_checksum() where it will commit the changes
8031      * and clear the "modified" field.
8032      */
8033     if (hw->eeprom_shadow_ram != NULL) {
8034         for (i = 0; i < words; i++) {
8035             if ((offset + i) < E1000_SHADOW_RAM_WORDS) {
8036                 hw->eeprom_shadow_ram[offset+i].modified = TRUE;
8037                 hw->eeprom_shadow_ram[offset+i].eeprom_word = data[i];
8038             } else {
8039                 error = -E1000_ERR_EEPROM;
8040                 break;
8041             }
8042         }
8043     } else {
8044         /* Drivers have the option to not allocate eeprom_shadow_ram as long
8045          * as they don't perform any NVM writes.  An attempt in doing so
8046          * will result in this error.
8047          */
8048         error = -E1000_ERR_EEPROM;
8049     }
8050
8051     e1000_release_software_flag(hw);
8052
8053     return error;
8054 }
8055
8056 /******************************************************************************
8057  * This function does initial flash setup so that a new read/write/erase cycle
8058  * can be started.
8059  *
8060  * hw - The pointer to the hw structure
8061  ****************************************************************************/
8062 int32_t
8063 e1000_ich8_cycle_init(struct e1000_hw *hw)
8064 {
8065     union ich8_hws_flash_status hsfsts;
8066     int32_t error = E1000_ERR_EEPROM;
8067     int32_t i     = 0;
8068
8069     DEBUGFUNC("e1000_ich8_cycle_init");
8070
8071     hsfsts.regval = E1000_READ_ICH8_REG16(hw, ICH8_FLASH_HSFSTS);
8072
8073     /* May be check the Flash Des Valid bit in Hw status */
8074     if (hsfsts.hsf_status.fldesvalid == 0) {
8075         DEBUGOUT("Flash descriptor invalid.  SW Sequencing must be used.");
8076         return error;
8077     }
8078
8079     /* Clear FCERR in Hw status by writing 1 */
8080     /* Clear DAEL in Hw status by writing a 1 */
8081     hsfsts.hsf_status.flcerr = 1;
8082     hsfsts.hsf_status.dael = 1;
8083
8084     E1000_WRITE_ICH8_REG16(hw, ICH8_FLASH_HSFSTS, hsfsts.regval);
8085
8086     /* Either we should have a hardware SPI cycle in progress bit to check
8087      * against, in order to start a new cycle or FDONE bit should be changed
8088      * in the hardware so that it is 1 after harware reset, which can then be
8089      * used as an indication whether a cycle is in progress or has been
8090      * completed .. we should also have some software semaphore mechanism to
8091      * guard FDONE or the cycle in progress bit so that two threads access to
8092      * those bits can be sequentiallized or a way so that 2 threads dont
8093      * start the cycle at the same time */
8094
8095     if (hsfsts.hsf_status.flcinprog == 0) {
8096         /* There is no cycle running at present, so we can start a cycle */
8097         /* Begin by setting Flash Cycle Done. */
8098         hsfsts.hsf_status.flcdone = 1;
8099         E1000_WRITE_ICH8_REG16(hw, ICH8_FLASH_HSFSTS, hsfsts.regval);
8100         error = E1000_SUCCESS;
8101     } else {
8102         /* otherwise poll for sometime so the current cycle has a chance
8103          * to end before giving up. */
8104         for (i = 0; i < ICH8_FLASH_COMMAND_TIMEOUT; i++) {
8105             hsfsts.regval = E1000_READ_ICH8_REG16(hw, ICH8_FLASH_HSFSTS);
8106             if (hsfsts.hsf_status.flcinprog == 0) {
8107                 error = E1000_SUCCESS;
8108                 break;
8109             }
8110             udelay(1);
8111         }
8112         if (error == E1000_SUCCESS) {
8113             /* Successful in waiting for previous cycle to timeout,
8114              * now set the Flash Cycle Done. */
8115             hsfsts.hsf_status.flcdone = 1;
8116             E1000_WRITE_ICH8_REG16(hw, ICH8_FLASH_HSFSTS, hsfsts.regval);
8117         } else {
8118             DEBUGOUT("Flash controller busy, cannot get access");
8119         }
8120     }
8121     return error;
8122 }
8123
8124 /******************************************************************************
8125  * This function starts a flash cycle and waits for its completion
8126  *
8127  * hw - The pointer to the hw structure
8128  ****************************************************************************/
8129 int32_t
8130 e1000_ich8_flash_cycle(struct e1000_hw *hw, uint32_t timeout)
8131 {
8132     union ich8_hws_flash_ctrl hsflctl;
8133     union ich8_hws_flash_status hsfsts;
8134     int32_t error = E1000_ERR_EEPROM;
8135     uint32_t i = 0;
8136
8137     /* Start a cycle by writing 1 in Flash Cycle Go in Hw Flash Control */
8138     hsflctl.regval = E1000_READ_ICH8_REG16(hw, ICH8_FLASH_HSFCTL);
8139     hsflctl.hsf_ctrl.flcgo = 1;
8140     E1000_WRITE_ICH8_REG16(hw, ICH8_FLASH_HSFCTL, hsflctl.regval);
8141
8142     /* wait till FDONE bit is set to 1 */
8143     do {
8144         hsfsts.regval = E1000_READ_ICH8_REG16(hw, ICH8_FLASH_HSFSTS);
8145         if (hsfsts.hsf_status.flcdone == 1)
8146             break;
8147         udelay(1);
8148         i++;
8149     } while (i < timeout);
8150     if (hsfsts.hsf_status.flcdone == 1 && hsfsts.hsf_status.flcerr == 0) {
8151         error = E1000_SUCCESS;
8152     }
8153     return error;
8154 }
8155
8156 /******************************************************************************
8157  * Reads a byte or word from the NVM using the ICH8 flash access registers.
8158  *
8159  * hw - The pointer to the hw structure
8160  * index - The index of the byte or word to read.
8161  * size - Size of data to read, 1=byte 2=word
8162  * data - Pointer to the word to store the value read.
8163  *****************************************************************************/
8164 int32_t
8165 e1000_read_ich8_data(struct e1000_hw *hw, uint32_t index,
8166                      uint32_t size, uint16_t* data)
8167 {
8168     union ich8_hws_flash_status hsfsts;
8169     union ich8_hws_flash_ctrl hsflctl;
8170     uint32_t flash_linear_address;
8171     uint32_t flash_data = 0;
8172     int32_t error = -E1000_ERR_EEPROM;
8173     int32_t count = 0;
8174
8175     DEBUGFUNC("e1000_read_ich8_data");
8176
8177     if (size < 1  || size > 2 || data == 0x0 ||
8178         index > ICH8_FLASH_LINEAR_ADDR_MASK)
8179         return error;
8180
8181     flash_linear_address = (ICH8_FLASH_LINEAR_ADDR_MASK & index) +
8182                            hw->flash_base_addr;
8183
8184     do {
8185         udelay(1);
8186         /* Steps */
8187         error = e1000_ich8_cycle_init(hw);
8188         if (error != E1000_SUCCESS)
8189             break;
8190
8191         hsflctl.regval = E1000_READ_ICH8_REG16(hw, ICH8_FLASH_HSFCTL);
8192         /* 0b/1b corresponds to 1 or 2 byte size, respectively. */
8193         hsflctl.hsf_ctrl.fldbcount = size - 1;
8194         hsflctl.hsf_ctrl.flcycle = ICH8_CYCLE_READ;
8195         E1000_WRITE_ICH8_REG16(hw, ICH8_FLASH_HSFCTL, hsflctl.regval);
8196
8197         /* Write the last 24 bits of index into Flash Linear address field in
8198          * Flash Address */
8199         /* TODO: TBD maybe check the index against the size of flash */
8200
8201         E1000_WRITE_ICH8_REG(hw, ICH8_FLASH_FADDR, flash_linear_address);
8202
8203         error = e1000_ich8_flash_cycle(hw, ICH8_FLASH_COMMAND_TIMEOUT);
8204
8205         /* Check if FCERR is set to 1, if set to 1, clear it and try the whole
8206          * sequence a few more times, else read in (shift in) the Flash Data0,
8207          * the order is least significant byte first msb to lsb */
8208         if (error == E1000_SUCCESS) {
8209             flash_data = E1000_READ_ICH8_REG(hw, ICH8_FLASH_FDATA0);
8210             if (size == 1) {
8211                 *data = (uint8_t)(flash_data & 0x000000FF);
8212             } else if (size == 2) {
8213                 *data = (uint16_t)(flash_data & 0x0000FFFF);
8214             }
8215             break;
8216         } else {
8217             /* If we've gotten here, then things are probably completely hosed,
8218              * but if the error condition is detected, it won't hurt to give
8219              * it another try...ICH8_FLASH_CYCLE_REPEAT_COUNT times.
8220              */
8221             hsfsts.regval = E1000_READ_ICH8_REG16(hw, ICH8_FLASH_HSFSTS);
8222             if (hsfsts.hsf_status.flcerr == 1) {
8223                 /* Repeat for some time before giving up. */
8224                 continue;
8225             } else if (hsfsts.hsf_status.flcdone == 0) {
8226                 DEBUGOUT("Timeout error - flash cycle did not complete.");
8227                 break;
8228             }
8229         }
8230     } while (count++ < ICH8_FLASH_CYCLE_REPEAT_COUNT);
8231
8232     return error;
8233 }
8234
8235 /******************************************************************************
8236  * Writes One /two bytes to the NVM using the ICH8 flash access registers.
8237  *
8238  * hw - The pointer to the hw structure
8239  * index - The index of the byte/word to read.
8240  * size - Size of data to read, 1=byte 2=word
8241  * data - The byte(s) to write to the NVM.
8242  *****************************************************************************/
8243 int32_t
8244 e1000_write_ich8_data(struct e1000_hw *hw, uint32_t index, uint32_t size,
8245                       uint16_t data)
8246 {
8247     union ich8_hws_flash_status hsfsts;
8248     union ich8_hws_flash_ctrl hsflctl;
8249     uint32_t flash_linear_address;
8250     uint32_t flash_data = 0;
8251     int32_t error = -E1000_ERR_EEPROM;
8252     int32_t count = 0;
8253
8254     DEBUGFUNC("e1000_write_ich8_data");
8255
8256     if (size < 1  || size > 2 || data > size * 0xff ||
8257         index > ICH8_FLASH_LINEAR_ADDR_MASK)
8258         return error;
8259
8260     flash_linear_address = (ICH8_FLASH_LINEAR_ADDR_MASK & index) +
8261                            hw->flash_base_addr;
8262
8263     do {
8264         udelay(1);
8265         /* Steps */
8266         error = e1000_ich8_cycle_init(hw);
8267         if (error != E1000_SUCCESS)
8268             break;
8269
8270         hsflctl.regval = E1000_READ_ICH8_REG16(hw, ICH8_FLASH_HSFCTL);
8271         /* 0b/1b corresponds to 1 or 2 byte size, respectively. */
8272         hsflctl.hsf_ctrl.fldbcount = size -1;
8273         hsflctl.hsf_ctrl.flcycle = ICH8_CYCLE_WRITE;
8274         E1000_WRITE_ICH8_REG16(hw, ICH8_FLASH_HSFCTL, hsflctl.regval);
8275
8276         /* Write the last 24 bits of index into Flash Linear address field in
8277          * Flash Address */
8278         E1000_WRITE_ICH8_REG(hw, ICH8_FLASH_FADDR, flash_linear_address);
8279
8280         if (size == 1)
8281             flash_data = (uint32_t)data & 0x00FF;
8282         else
8283             flash_data = (uint32_t)data;
8284
8285         E1000_WRITE_ICH8_REG(hw, ICH8_FLASH_FDATA0, flash_data);
8286
8287         /* check if FCERR is set to 1 , if set to 1, clear it and try the whole
8288          * sequence a few more times else done */
8289         error = e1000_ich8_flash_cycle(hw, ICH8_FLASH_COMMAND_TIMEOUT);
8290         if (error == E1000_SUCCESS) {
8291             break;
8292         } else {
8293             /* If we're here, then things are most likely completely hosed,
8294              * but if the error condition is detected, it won't hurt to give
8295              * it another try...ICH8_FLASH_CYCLE_REPEAT_COUNT times.
8296              */
8297             hsfsts.regval = E1000_READ_ICH8_REG16(hw, ICH8_FLASH_HSFSTS);
8298             if (hsfsts.hsf_status.flcerr == 1) {
8299                 /* Repeat for some time before giving up. */
8300                 continue;
8301             } else if (hsfsts.hsf_status.flcdone == 0) {
8302                 DEBUGOUT("Timeout error - flash cycle did not complete.");
8303                 break;
8304             }
8305         }
8306     } while (count++ < ICH8_FLASH_CYCLE_REPEAT_COUNT);
8307
8308     return error;
8309 }
8310
8311 /******************************************************************************
8312  * Reads a single byte from the NVM using the ICH8 flash access registers.
8313  *
8314  * hw - pointer to e1000_hw structure
8315  * index - The index of the byte to read.
8316  * data - Pointer to a byte to store the value read.
8317  *****************************************************************************/
8318 int32_t
8319 e1000_read_ich8_byte(struct e1000_hw *hw, uint32_t index, uint8_t* data)
8320 {
8321     int32_t status = E1000_SUCCESS;
8322     uint16_t word = 0;
8323
8324     status = e1000_read_ich8_data(hw, index, 1, &word);
8325     if (status == E1000_SUCCESS) {
8326         *data = (uint8_t)word;
8327     }
8328
8329     return status;
8330 }
8331
8332 /******************************************************************************
8333  * Writes a single byte to the NVM using the ICH8 flash access registers.
8334  * Performs verification by reading back the value and then going through
8335  * a retry algorithm before giving up.
8336  *
8337  * hw - pointer to e1000_hw structure
8338  * index - The index of the byte to write.
8339  * byte - The byte to write to the NVM.
8340  *****************************************************************************/
8341 int32_t
8342 e1000_verify_write_ich8_byte(struct e1000_hw *hw, uint32_t index, uint8_t byte)
8343 {
8344     int32_t error = E1000_SUCCESS;
8345     int32_t program_retries;
8346     uint8_t temp_byte;
8347
8348     e1000_write_ich8_byte(hw, index, byte);
8349     udelay(100);
8350
8351     for (program_retries = 0; program_retries < 100; program_retries++) {
8352         e1000_read_ich8_byte(hw, index, &temp_byte);
8353         if (temp_byte == byte)
8354             break;
8355         udelay(10);
8356         e1000_write_ich8_byte(hw, index, byte);
8357         udelay(100);
8358     }
8359     if (program_retries == 100)
8360         error = E1000_ERR_EEPROM;
8361
8362     return error;
8363 }
8364
8365 /******************************************************************************
8366  * Writes a single byte to the NVM using the ICH8 flash access registers.
8367  *
8368  * hw - pointer to e1000_hw structure
8369  * index - The index of the byte to read.
8370  * data - The byte to write to the NVM.
8371  *****************************************************************************/
8372 int32_t
8373 e1000_write_ich8_byte(struct e1000_hw *hw, uint32_t index, uint8_t data)
8374 {
8375     int32_t status = E1000_SUCCESS;
8376     uint16_t word = (uint16_t)data;
8377
8378     status = e1000_write_ich8_data(hw, index, 1, word);
8379
8380     return status;
8381 }
8382
8383 /******************************************************************************
8384  * Reads a word from the NVM using the ICH8 flash access registers.
8385  *
8386  * hw - pointer to e1000_hw structure
8387  * index - The starting byte index of the word to read.
8388  * data - Pointer to a word to store the value read.
8389  *****************************************************************************/
8390 int32_t
8391 e1000_read_ich8_word(struct e1000_hw *hw, uint32_t index, uint16_t *data)
8392 {
8393     int32_t status = E1000_SUCCESS;
8394     status = e1000_read_ich8_data(hw, index, 2, data);
8395     return status;
8396 }
8397
8398 /******************************************************************************
8399  * Writes a word to the NVM using the ICH8 flash access registers.
8400  *
8401  * hw - pointer to e1000_hw structure
8402  * index - The starting byte index of the word to read.
8403  * data - The word to write to the NVM.
8404  *****************************************************************************/
8405 int32_t
8406 e1000_write_ich8_word(struct e1000_hw *hw, uint32_t index, uint16_t data)
8407 {
8408     int32_t status = E1000_SUCCESS;
8409     status = e1000_write_ich8_data(hw, index, 2, data);
8410     return status;
8411 }
8412
8413 /******************************************************************************
8414  * Erases the bank specified. Each bank is a 4k block. Segments are 0 based.
8415  * segment N is 4096 * N + flash_reg_addr.
8416  *
8417  * hw - pointer to e1000_hw structure
8418  * segment - 0 for first segment, 1 for second segment, etc.
8419  *****************************************************************************/
8420 int32_t
8421 e1000_erase_ich8_4k_segment(struct e1000_hw *hw, uint32_t segment)
8422 {
8423     union ich8_hws_flash_status hsfsts;
8424     union ich8_hws_flash_ctrl hsflctl;
8425     uint32_t flash_linear_address;
8426     int32_t  count = 0;
8427     int32_t  error = E1000_ERR_EEPROM;
8428     int32_t  iteration, seg_size;
8429     int32_t  sector_size;
8430     int32_t  j = 0;
8431     int32_t  error_flag = 0;
8432
8433     hsfsts.regval = E1000_READ_ICH8_REG16(hw, ICH8_FLASH_HSFSTS);
8434
8435     /* Determine HW Sector size: Read BERASE bits of Hw flash Status register */
8436     /* 00: The Hw sector is 256 bytes, hence we need to erase 16
8437      *     consecutive sectors.  The start index for the nth Hw sector can be
8438      *     calculated as = segment * 4096 + n * 256
8439      * 01: The Hw sector is 4K bytes, hence we need to erase 1 sector.
8440      *     The start index for the nth Hw sector can be calculated
8441      *     as = segment * 4096
8442      * 10: Error condition
8443      * 11: The Hw sector size is much bigger than the size asked to
8444      *     erase...error condition */
8445     if (hsfsts.hsf_status.berasesz == 0x0) {
8446         /* Hw sector size 256 */
8447         sector_size = seg_size = ICH8_FLASH_SEG_SIZE_256;
8448         iteration = ICH8_FLASH_SECTOR_SIZE / ICH8_FLASH_SEG_SIZE_256;
8449     } else if (hsfsts.hsf_status.berasesz == 0x1) {
8450         sector_size = seg_size = ICH8_FLASH_SEG_SIZE_4K;
8451         iteration = 1;
8452     } else if (hsfsts.hsf_status.berasesz == 0x3) {
8453         sector_size = seg_size = ICH8_FLASH_SEG_SIZE_64K;
8454         iteration = 1;
8455     } else {
8456         return error;
8457     }
8458
8459     for (j = 0; j < iteration ; j++) {
8460         do {
8461             count++;
8462             /* Steps */
8463             error = e1000_ich8_cycle_init(hw);
8464             if (error != E1000_SUCCESS) {
8465                 error_flag = 1;
8466                 break;
8467             }
8468
8469             /* Write a value 11 (block Erase) in Flash Cycle field in Hw flash
8470              * Control */
8471             hsflctl.regval = E1000_READ_ICH8_REG16(hw, ICH8_FLASH_HSFCTL);
8472             hsflctl.hsf_ctrl.flcycle = ICH8_CYCLE_ERASE;
8473             E1000_WRITE_ICH8_REG16(hw, ICH8_FLASH_HSFCTL, hsflctl.regval);
8474
8475             /* Write the last 24 bits of an index within the block into Flash
8476              * Linear address field in Flash Address.  This probably needs to
8477              * be calculated here based off the on-chip segment size and the
8478              * software segment size assumed (4K) */
8479             /* TBD */
8480             flash_linear_address = segment * sector_size + j * seg_size;
8481             flash_linear_address &= ICH8_FLASH_LINEAR_ADDR_MASK;
8482             flash_linear_address += hw->flash_base_addr;
8483
8484             E1000_WRITE_ICH8_REG(hw, ICH8_FLASH_FADDR, flash_linear_address);
8485
8486             error = e1000_ich8_flash_cycle(hw, 1000000);
8487             /* Check if FCERR is set to 1.  If 1, clear it and try the whole
8488              * sequence a few more times else Done */
8489             if (error == E1000_SUCCESS) {
8490                 break;
8491             } else {
8492                 hsfsts.regval = E1000_READ_ICH8_REG16(hw, ICH8_FLASH_HSFSTS);
8493                 if (hsfsts.hsf_status.flcerr == 1) {
8494                     /* repeat for some time before giving up */
8495                     continue;
8496                 } else if (hsfsts.hsf_status.flcdone == 0) {
8497                     error_flag = 1;
8498                     break;
8499                 }
8500             }
8501         } while ((count < ICH8_FLASH_CYCLE_REPEAT_COUNT) && !error_flag);
8502         if (error_flag == 1)
8503             break;
8504     }
8505     if (error_flag != 1)
8506         error = E1000_SUCCESS;
8507     return error;
8508 }
8509
8510 /******************************************************************************
8511  *
8512  * Reverse duplex setting without breaking the link.
8513  *
8514  * hw: Struct containing variables accessed by shared code
8515  *
8516  *****************************************************************************/
8517 int32_t
8518 e1000_duplex_reversal(struct e1000_hw *hw)
8519 {
8520     int32_t ret_val;
8521     uint16_t phy_data;
8522
8523     if (hw->phy_type != e1000_phy_igp_3)
8524         return E1000_SUCCESS;
8525
8526     ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
8527     if (ret_val)
8528         return ret_val;
8529
8530     phy_data ^= MII_CR_FULL_DUPLEX;
8531
8532     ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
8533     if (ret_val)
8534         return ret_val;
8535
8536     ret_val = e1000_read_phy_reg(hw, IGP3E1000_PHY_MISC_CTRL, &phy_data);
8537     if (ret_val)
8538         return ret_val;
8539
8540     phy_data |= IGP3_PHY_MISC_DUPLEX_MANUAL_SET;
8541     ret_val = e1000_write_phy_reg(hw, IGP3E1000_PHY_MISC_CTRL, phy_data);
8542
8543     return ret_val;
8544 }
8545
8546 int32_t
8547 e1000_init_lcd_from_nvm_config_region(struct e1000_hw *hw,
8548                                       uint32_t cnf_base_addr, uint32_t cnf_size)
8549 {
8550     uint32_t ret_val = E1000_SUCCESS;
8551     uint16_t word_addr, reg_data, reg_addr;
8552     uint16_t i;
8553
8554     /* cnf_base_addr is in DWORD */
8555     word_addr = (uint16_t)(cnf_base_addr << 1);
8556
8557     /* cnf_size is returned in size of dwords */
8558     for (i = 0; i < cnf_size; i++) {
8559         ret_val = e1000_read_eeprom(hw, (word_addr + i*2), 1, &reg_data);
8560         if (ret_val)
8561             return ret_val;
8562
8563         ret_val = e1000_read_eeprom(hw, (word_addr + i*2 + 1), 1, &reg_addr);
8564         if (ret_val)
8565             return ret_val;
8566
8567         ret_val = e1000_get_software_flag(hw);
8568         if (ret_val != E1000_SUCCESS)
8569             return ret_val;
8570
8571         ret_val = e1000_write_phy_reg_ex(hw, (uint32_t)reg_addr, reg_data);
8572
8573         e1000_release_software_flag(hw);
8574     }
8575
8576     return ret_val;
8577 }
8578
8579
8580 int32_t
8581 e1000_init_lcd_from_nvm(struct e1000_hw *hw)
8582 {
8583     uint32_t reg_data, cnf_base_addr, cnf_size, ret_val, loop;
8584
8585     if (hw->phy_type != e1000_phy_igp_3)
8586           return E1000_SUCCESS;
8587
8588     /* Check if SW needs configure the PHY */
8589     reg_data = E1000_READ_REG(hw, FEXTNVM);
8590     if (!(reg_data & FEXTNVM_SW_CONFIG))
8591         return E1000_SUCCESS;
8592
8593     /* Wait for basic configuration completes before proceeding*/
8594     loop = 0;
8595     do {
8596         reg_data = E1000_READ_REG(hw, STATUS) & E1000_STATUS_LAN_INIT_DONE;
8597         udelay(100);
8598         loop++;
8599     } while ((!reg_data) && (loop < 50));
8600
8601     /* Clear the Init Done bit for the next init event */
8602     reg_data = E1000_READ_REG(hw, STATUS);
8603     reg_data &= ~E1000_STATUS_LAN_INIT_DONE;
8604     E1000_WRITE_REG(hw, STATUS, reg_data);
8605
8606     /* Make sure HW does not configure LCD from PHY extended configuration
8607        before SW configuration */
8608     reg_data = E1000_READ_REG(hw, EXTCNF_CTRL);
8609     if ((reg_data & E1000_EXTCNF_CTRL_LCD_WRITE_ENABLE) == 0x0000) {
8610         reg_data = E1000_READ_REG(hw, EXTCNF_SIZE);
8611         cnf_size = reg_data & E1000_EXTCNF_SIZE_EXT_PCIE_LENGTH;
8612         cnf_size >>= 16;
8613         if (cnf_size) {
8614             reg_data = E1000_READ_REG(hw, EXTCNF_CTRL);
8615             cnf_base_addr = reg_data & E1000_EXTCNF_CTRL_EXT_CNF_POINTER;
8616             /* cnf_base_addr is in DWORD */
8617             cnf_base_addr >>= 16;
8618
8619             /* Configure LCD from extended configuration region. */
8620             ret_val = e1000_init_lcd_from_nvm_config_region(hw, cnf_base_addr,
8621                                                             cnf_size);
8622             if (ret_val)
8623                 return ret_val;
8624         }
8625     }
8626
8627     return E1000_SUCCESS;
8628 }
8629
8630
8631