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