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