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