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