e1000: drop dead pcie code from e1000
[safe/jmp/linux-2.6] / 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 s32 e1000_swfw_sync_acquire(struct e1000_hw *hw, u16 mask);
37 static void e1000_swfw_sync_release(struct e1000_hw *hw, u16 mask);
38
39 static s32 e1000_check_downshift(struct e1000_hw *hw);
40 static s32 e1000_check_polarity(struct e1000_hw *hw,
41                                 e1000_rev_polarity *polarity);
42 static void e1000_clear_hw_cntrs(struct e1000_hw *hw);
43 static void e1000_clear_vfta(struct e1000_hw *hw);
44 static s32 e1000_config_dsp_after_link_change(struct e1000_hw *hw,
45                                               bool link_up);
46 static s32 e1000_config_fc_after_link_up(struct e1000_hw *hw);
47 static s32 e1000_detect_gig_phy(struct e1000_hw *hw);
48 static s32 e1000_get_auto_rd_done(struct e1000_hw *hw);
49 static s32 e1000_get_cable_length(struct e1000_hw *hw, u16 *min_length,
50                                   u16 *max_length);
51 static s32 e1000_get_hw_eeprom_semaphore(struct e1000_hw *hw);
52 static s32 e1000_get_phy_cfg_done(struct e1000_hw *hw);
53 static s32 e1000_id_led_init(struct e1000_hw *hw);
54 static void e1000_init_rx_addrs(struct e1000_hw *hw);
55 static s32 e1000_phy_igp_get_info(struct e1000_hw *hw,
56                                   struct e1000_phy_info *phy_info);
57 static s32 e1000_read_eeprom_eerd(struct e1000_hw *hw, u16 offset, u16 words,
58                                   u16 *data);
59 static s32 e1000_write_eeprom_eewr(struct e1000_hw *hw, u16 offset, u16 words,
60                                    u16 *data);
61 static s32 e1000_poll_eerd_eewr_done(struct e1000_hw *hw, int eerd);
62 static s32 e1000_phy_m88_get_info(struct e1000_hw *hw,
63                                   struct e1000_phy_info *phy_info);
64 static void e1000_put_hw_eeprom_semaphore(struct e1000_hw *hw);
65 static s32 e1000_set_d3_lplu_state(struct e1000_hw *hw, bool active);
66 static s32 e1000_wait_autoneg(struct e1000_hw *hw);
67 static void e1000_write_reg_io(struct e1000_hw *hw, u32 offset, u32 value);
68 static s32 e1000_set_phy_type(struct e1000_hw *hw);
69 static void e1000_phy_init_script(struct e1000_hw *hw);
70 static s32 e1000_setup_copper_link(struct e1000_hw *hw);
71 static s32 e1000_setup_fiber_serdes_link(struct e1000_hw *hw);
72 static s32 e1000_adjust_serdes_amplitude(struct e1000_hw *hw);
73 static s32 e1000_phy_force_speed_duplex(struct e1000_hw *hw);
74 static s32 e1000_config_mac_to_phy(struct e1000_hw *hw);
75 static void e1000_raise_mdi_clk(struct e1000_hw *hw, u32 *ctrl);
76 static void e1000_lower_mdi_clk(struct e1000_hw *hw, u32 *ctrl);
77 static void e1000_shift_out_mdi_bits(struct e1000_hw *hw, u32 data,
78                                      u16 count);
79 static u16 e1000_shift_in_mdi_bits(struct e1000_hw *hw);
80 static s32 e1000_phy_reset_dsp(struct e1000_hw *hw);
81 static s32 e1000_write_eeprom_spi(struct e1000_hw *hw, u16 offset,
82                                       u16 words, u16 *data);
83 static s32 e1000_write_eeprom_microwire(struct e1000_hw *hw, u16 offset,
84                                         u16 words, u16 *data);
85 static s32 e1000_spi_eeprom_ready(struct e1000_hw *hw);
86 static void e1000_raise_ee_clk(struct e1000_hw *hw, u32 *eecd);
87 static void e1000_lower_ee_clk(struct e1000_hw *hw, u32 *eecd);
88 static void e1000_shift_out_ee_bits(struct e1000_hw *hw, u16 data, u16 count);
89 static s32 e1000_write_phy_reg_ex(struct e1000_hw *hw, u32 reg_addr,
90                                   u16 phy_data);
91 static s32 e1000_read_phy_reg_ex(struct e1000_hw *hw,u32 reg_addr,
92                                  u16 *phy_data);
93 static u16 e1000_shift_in_ee_bits(struct e1000_hw *hw, u16 count);
94 static s32 e1000_acquire_eeprom(struct e1000_hw *hw);
95 static void e1000_release_eeprom(struct e1000_hw *hw);
96 static void e1000_standby_eeprom(struct e1000_hw *hw);
97 static s32 e1000_set_vco_speed(struct e1000_hw *hw);
98 static s32 e1000_polarity_reversal_workaround(struct e1000_hw *hw);
99 static s32 e1000_set_phy_mode(struct e1000_hw *hw);
100 static s32 e1000_do_read_eeprom(struct e1000_hw *hw, u16 offset, u16 words, u16 *data);
101 static s32 e1000_do_write_eeprom(struct e1000_hw *hw, u16 offset, u16 words, u16 *data);
102
103 /* IGP cable length table */
104 static const
105 u16 e1000_igp_cable_length_table[IGP01E1000_AGC_LENGTH_TABLE_SIZE] =
106     { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
107       5, 10, 10, 10, 10, 10, 10, 10, 20, 20, 20, 20, 20, 25, 25, 25,
108       25, 25, 25, 25, 30, 30, 30, 30, 40, 40, 40, 40, 40, 40, 40, 40,
109       40, 50, 50, 50, 50, 50, 50, 50, 60, 60, 60, 60, 60, 60, 60, 60,
110       60, 70, 70, 70, 70, 70, 70, 80, 80, 80, 80, 80, 80, 90, 90, 90,
111       90, 90, 90, 90, 90, 90, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
112       100, 100, 100, 100, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110,
113       110, 110, 110, 110, 110, 110, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120};
114
115 static DEFINE_SPINLOCK(e1000_eeprom_lock);
116
117 /******************************************************************************
118  * Set the phy type member in the hw struct.
119  *
120  * hw - Struct containing variables accessed by shared code
121  *****************************************************************************/
122 static s32 e1000_set_phy_type(struct e1000_hw *hw)
123 {
124     DEBUGFUNC("e1000_set_phy_type");
125
126     if (hw->mac_type == e1000_undefined)
127         return -E1000_ERR_PHY_TYPE;
128
129     switch (hw->phy_id) {
130     case M88E1000_E_PHY_ID:
131     case M88E1000_I_PHY_ID:
132     case M88E1011_I_PHY_ID:
133     case M88E1111_I_PHY_ID:
134         hw->phy_type = e1000_phy_m88;
135         break;
136     case IGP01E1000_I_PHY_ID:
137         if (hw->mac_type == e1000_82541 ||
138             hw->mac_type == e1000_82541_rev_2 ||
139             hw->mac_type == e1000_82547 ||
140             hw->mac_type == e1000_82547_rev_2) {
141             hw->phy_type = e1000_phy_igp;
142             break;
143         }
144     default:
145         /* Should never have loaded on this device */
146         hw->phy_type = e1000_phy_undefined;
147         return -E1000_ERR_PHY_TYPE;
148     }
149
150     return E1000_SUCCESS;
151 }
152
153 /******************************************************************************
154  * IGP phy init script - initializes the GbE PHY
155  *
156  * hw - Struct containing variables accessed by shared code
157  *****************************************************************************/
158 static void e1000_phy_init_script(struct e1000_hw *hw)
159 {
160     u32 ret_val;
161     u16 phy_saved_data;
162
163     DEBUGFUNC("e1000_phy_init_script");
164
165     if (hw->phy_init_script) {
166         msleep(20);
167
168         /* Save off the current value of register 0x2F5B to be restored at
169          * the end of this routine. */
170         ret_val = e1000_read_phy_reg(hw, 0x2F5B, &phy_saved_data);
171
172         /* Disabled the PHY transmitter */
173         e1000_write_phy_reg(hw, 0x2F5B, 0x0003);
174
175         msleep(20);
176
177         e1000_write_phy_reg(hw,0x0000,0x0140);
178
179         msleep(5);
180
181         switch (hw->mac_type) {
182         case e1000_82541:
183         case e1000_82547:
184             e1000_write_phy_reg(hw, 0x1F95, 0x0001);
185
186             e1000_write_phy_reg(hw, 0x1F71, 0xBD21);
187
188             e1000_write_phy_reg(hw, 0x1F79, 0x0018);
189
190             e1000_write_phy_reg(hw, 0x1F30, 0x1600);
191
192             e1000_write_phy_reg(hw, 0x1F31, 0x0014);
193
194             e1000_write_phy_reg(hw, 0x1F32, 0x161C);
195
196             e1000_write_phy_reg(hw, 0x1F94, 0x0003);
197
198             e1000_write_phy_reg(hw, 0x1F96, 0x003F);
199
200             e1000_write_phy_reg(hw, 0x2010, 0x0008);
201             break;
202
203         case e1000_82541_rev_2:
204         case e1000_82547_rev_2:
205             e1000_write_phy_reg(hw, 0x1F73, 0x0099);
206             break;
207         default:
208             break;
209         }
210
211         e1000_write_phy_reg(hw, 0x0000, 0x3300);
212
213         msleep(20);
214
215         /* Now enable the transmitter */
216         e1000_write_phy_reg(hw, 0x2F5B, phy_saved_data);
217
218         if (hw->mac_type == e1000_82547) {
219             u16 fused, fine, coarse;
220
221             /* Move to analog registers page */
222             e1000_read_phy_reg(hw, IGP01E1000_ANALOG_SPARE_FUSE_STATUS, &fused);
223
224             if (!(fused & IGP01E1000_ANALOG_SPARE_FUSE_ENABLED)) {
225                 e1000_read_phy_reg(hw, IGP01E1000_ANALOG_FUSE_STATUS, &fused);
226
227                 fine = fused & IGP01E1000_ANALOG_FUSE_FINE_MASK;
228                 coarse = fused & IGP01E1000_ANALOG_FUSE_COARSE_MASK;
229
230                 if (coarse > IGP01E1000_ANALOG_FUSE_COARSE_THRESH) {
231                     coarse -= IGP01E1000_ANALOG_FUSE_COARSE_10;
232                     fine -= IGP01E1000_ANALOG_FUSE_FINE_1;
233                 } else if (coarse == IGP01E1000_ANALOG_FUSE_COARSE_THRESH)
234                     fine -= IGP01E1000_ANALOG_FUSE_FINE_10;
235
236                 fused = (fused & IGP01E1000_ANALOG_FUSE_POLY_MASK) |
237                         (fine & IGP01E1000_ANALOG_FUSE_FINE_MASK) |
238                         (coarse & IGP01E1000_ANALOG_FUSE_COARSE_MASK);
239
240                 e1000_write_phy_reg(hw, IGP01E1000_ANALOG_FUSE_CONTROL, fused);
241                 e1000_write_phy_reg(hw, IGP01E1000_ANALOG_FUSE_BYPASS,
242                                     IGP01E1000_ANALOG_FUSE_ENABLE_SW_CONTROL);
243             }
244         }
245     }
246 }
247
248 /******************************************************************************
249  * Set the mac type member in the hw struct.
250  *
251  * hw - Struct containing variables accessed by shared code
252  *****************************************************************************/
253 s32 e1000_set_mac_type(struct e1000_hw *hw)
254 {
255         DEBUGFUNC("e1000_set_mac_type");
256
257         switch (hw->device_id) {
258         case E1000_DEV_ID_82542:
259                 switch (hw->revision_id) {
260                 case E1000_82542_2_0_REV_ID:
261                         hw->mac_type = e1000_82542_rev2_0;
262                         break;
263                 case E1000_82542_2_1_REV_ID:
264                         hw->mac_type = e1000_82542_rev2_1;
265                         break;
266                 default:
267                         /* Invalid 82542 revision ID */
268                         return -E1000_ERR_MAC_TYPE;
269                 }
270                 break;
271         case E1000_DEV_ID_82543GC_FIBER:
272         case E1000_DEV_ID_82543GC_COPPER:
273                 hw->mac_type = e1000_82543;
274                 break;
275         case E1000_DEV_ID_82544EI_COPPER:
276         case E1000_DEV_ID_82544EI_FIBER:
277         case E1000_DEV_ID_82544GC_COPPER:
278         case E1000_DEV_ID_82544GC_LOM:
279                 hw->mac_type = e1000_82544;
280                 break;
281         case E1000_DEV_ID_82540EM:
282         case E1000_DEV_ID_82540EM_LOM:
283         case E1000_DEV_ID_82540EP:
284         case E1000_DEV_ID_82540EP_LOM:
285         case E1000_DEV_ID_82540EP_LP:
286                 hw->mac_type = e1000_82540;
287                 break;
288         case E1000_DEV_ID_82545EM_COPPER:
289         case E1000_DEV_ID_82545EM_FIBER:
290                 hw->mac_type = e1000_82545;
291                 break;
292         case E1000_DEV_ID_82545GM_COPPER:
293         case E1000_DEV_ID_82545GM_FIBER:
294         case E1000_DEV_ID_82545GM_SERDES:
295                 hw->mac_type = e1000_82545_rev_3;
296                 break;
297         case E1000_DEV_ID_82546EB_COPPER:
298         case E1000_DEV_ID_82546EB_FIBER:
299         case E1000_DEV_ID_82546EB_QUAD_COPPER:
300                 hw->mac_type = e1000_82546;
301                 break;
302         case E1000_DEV_ID_82546GB_COPPER:
303         case E1000_DEV_ID_82546GB_FIBER:
304         case E1000_DEV_ID_82546GB_SERDES:
305         case E1000_DEV_ID_82546GB_PCIE:
306         case E1000_DEV_ID_82546GB_QUAD_COPPER:
307         case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
308                 hw->mac_type = e1000_82546_rev_3;
309                 break;
310         case E1000_DEV_ID_82541EI:
311         case E1000_DEV_ID_82541EI_MOBILE:
312         case E1000_DEV_ID_82541ER_LOM:
313                 hw->mac_type = e1000_82541;
314                 break;
315         case E1000_DEV_ID_82541ER:
316         case E1000_DEV_ID_82541GI:
317         case E1000_DEV_ID_82541GI_LF:
318         case E1000_DEV_ID_82541GI_MOBILE:
319                 hw->mac_type = e1000_82541_rev_2;
320                 break;
321         case E1000_DEV_ID_82547EI:
322         case E1000_DEV_ID_82547EI_MOBILE:
323                 hw->mac_type = e1000_82547;
324                 break;
325         case E1000_DEV_ID_82547GI:
326                 hw->mac_type = e1000_82547_rev_2;
327                 break;
328         default:
329                 /* Should never have loaded on this device */
330                 return -E1000_ERR_MAC_TYPE;
331         }
332
333         switch (hw->mac_type) {
334         case e1000_82541:
335         case e1000_82547:
336         case e1000_82541_rev_2:
337         case e1000_82547_rev_2:
338                 hw->asf_firmware_present = true;
339                 break;
340         default:
341                 break;
342         }
343
344         /* The 82543 chip does not count tx_carrier_errors properly in
345          * FD mode
346          */
347         if (hw->mac_type == e1000_82543)
348                 hw->bad_tx_carr_stats_fd = true;
349
350         if (hw->mac_type > e1000_82544)
351                 hw->has_smbus = true;
352
353         return E1000_SUCCESS;
354 }
355
356 /*****************************************************************************
357  * Set media type and TBI compatibility.
358  *
359  * hw - Struct containing variables accessed by shared code
360  * **************************************************************************/
361 void e1000_set_media_type(struct e1000_hw *hw)
362 {
363     u32 status;
364
365     DEBUGFUNC("e1000_set_media_type");
366
367     if (hw->mac_type != e1000_82543) {
368         /* tbi_compatibility is only valid on 82543 */
369         hw->tbi_compatibility_en = false;
370     }
371
372     switch (hw->device_id) {
373     case E1000_DEV_ID_82545GM_SERDES:
374     case E1000_DEV_ID_82546GB_SERDES:
375         hw->media_type = e1000_media_type_internal_serdes;
376         break;
377     default:
378         switch (hw->mac_type) {
379         case e1000_82542_rev2_0:
380         case e1000_82542_rev2_1:
381             hw->media_type = e1000_media_type_fiber;
382             break;
383         default:
384             status = er32(STATUS);
385             if (status & E1000_STATUS_TBIMODE) {
386                 hw->media_type = e1000_media_type_fiber;
387                 /* tbi_compatibility not valid on fiber */
388                 hw->tbi_compatibility_en = false;
389             } else {
390                 hw->media_type = e1000_media_type_copper;
391             }
392             break;
393         }
394     }
395 }
396
397 /******************************************************************************
398  * Reset the transmit and receive units; mask and clear all interrupts.
399  *
400  * hw - Struct containing variables accessed by shared code
401  *****************************************************************************/
402 s32 e1000_reset_hw(struct e1000_hw *hw)
403 {
404     u32 ctrl;
405     u32 ctrl_ext;
406     u32 icr;
407     u32 manc;
408     u32 led_ctrl;
409     s32 ret_val;
410
411     DEBUGFUNC("e1000_reset_hw");
412
413     /* For 82542 (rev 2.0), disable MWI before issuing a device reset */
414     if (hw->mac_type == e1000_82542_rev2_0) {
415         DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
416         e1000_pci_clear_mwi(hw);
417     }
418
419     /* Clear interrupt mask to stop board from generating interrupts */
420     DEBUGOUT("Masking off all interrupts\n");
421     ew32(IMC, 0xffffffff);
422
423     /* Disable the Transmit and Receive units.  Then delay to allow
424      * any pending transactions to complete before we hit the MAC with
425      * the global reset.
426      */
427     ew32(RCTL, 0);
428     ew32(TCTL, E1000_TCTL_PSP);
429     E1000_WRITE_FLUSH();
430
431     /* The tbi_compatibility_on Flag must be cleared when Rctl is cleared. */
432     hw->tbi_compatibility_on = false;
433
434     /* Delay to allow any outstanding PCI transactions to complete before
435      * resetting the device
436      */
437     msleep(10);
438
439     ctrl = er32(CTRL);
440
441     /* Must reset the PHY before resetting the MAC */
442     if ((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
443         ew32(CTRL, (ctrl | E1000_CTRL_PHY_RST));
444         msleep(5);
445     }
446
447     /* Issue a global reset to the MAC.  This will reset the chip's
448      * transmit, receive, DMA, and link units.  It will not effect
449      * the current PCI configuration.  The global reset bit is self-
450      * clearing, and should clear within a microsecond.
451      */
452     DEBUGOUT("Issuing a global reset to MAC\n");
453
454     switch (hw->mac_type) {
455         case e1000_82544:
456         case e1000_82540:
457         case e1000_82545:
458         case e1000_82546:
459         case e1000_82541:
460         case e1000_82541_rev_2:
461             /* These controllers can't ack the 64-bit write when issuing the
462              * reset, so use IO-mapping as a workaround to issue the reset */
463             E1000_WRITE_REG_IO(hw, CTRL, (ctrl | E1000_CTRL_RST));
464             break;
465         case e1000_82545_rev_3:
466         case e1000_82546_rev_3:
467             /* Reset is performed on a shadow of the control register */
468             ew32(CTRL_DUP, (ctrl | E1000_CTRL_RST));
469             break;
470         default:
471             ew32(CTRL, (ctrl | E1000_CTRL_RST));
472             break;
473     }
474
475     /* After MAC reset, force reload of EEPROM to restore power-on settings to
476      * device.  Later controllers reload the EEPROM automatically, so just wait
477      * for reload to complete.
478      */
479     switch (hw->mac_type) {
480         case e1000_82542_rev2_0:
481         case e1000_82542_rev2_1:
482         case e1000_82543:
483         case e1000_82544:
484             /* Wait for reset to complete */
485             udelay(10);
486             ctrl_ext = er32(CTRL_EXT);
487             ctrl_ext |= E1000_CTRL_EXT_EE_RST;
488             ew32(CTRL_EXT, ctrl_ext);
489             E1000_WRITE_FLUSH();
490             /* Wait for EEPROM reload */
491             msleep(2);
492             break;
493         case e1000_82541:
494         case e1000_82541_rev_2:
495         case e1000_82547:
496         case e1000_82547_rev_2:
497             /* Wait for EEPROM reload */
498             msleep(20);
499             break;
500         default:
501             /* Auto read done will delay 5ms or poll based on mac type */
502             ret_val = e1000_get_auto_rd_done(hw);
503             if (ret_val)
504                 return ret_val;
505             break;
506     }
507
508     /* Disable HW ARPs on ASF enabled adapters */
509     if (hw->mac_type >= e1000_82540) {
510         manc = er32(MANC);
511         manc &= ~(E1000_MANC_ARP_EN);
512         ew32(MANC, manc);
513     }
514
515     if ((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
516         e1000_phy_init_script(hw);
517
518         /* Configure activity LED after PHY reset */
519         led_ctrl = er32(LEDCTL);
520         led_ctrl &= IGP_ACTIVITY_LED_MASK;
521         led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
522         ew32(LEDCTL, led_ctrl);
523     }
524
525     /* Clear interrupt mask to stop board from generating interrupts */
526     DEBUGOUT("Masking off all interrupts\n");
527     ew32(IMC, 0xffffffff);
528
529     /* Clear any pending interrupt events. */
530     icr = er32(ICR);
531
532     /* If MWI was previously enabled, reenable it. */
533     if (hw->mac_type == e1000_82542_rev2_0) {
534         if (hw->pci_cmd_word & PCI_COMMAND_INVALIDATE)
535             e1000_pci_set_mwi(hw);
536     }
537
538     return E1000_SUCCESS;
539 }
540
541 /******************************************************************************
542  * Performs basic configuration of the adapter.
543  *
544  * hw - Struct containing variables accessed by shared code
545  *
546  * Assumes that the controller has previously been reset and is in a
547  * post-reset uninitialized state. Initializes the receive address registers,
548  * multicast table, and VLAN filter table. Calls routines to setup link
549  * configuration and flow control settings. Clears all on-chip counters. Leaves
550  * the transmit and receive units disabled and uninitialized.
551  *****************************************************************************/
552 s32 e1000_init_hw(struct e1000_hw *hw)
553 {
554     u32 ctrl;
555     u32 i;
556     s32 ret_val;
557     u32 mta_size;
558     u32 ctrl_ext;
559
560     DEBUGFUNC("e1000_init_hw");
561
562     /* Initialize Identification LED */
563     ret_val = e1000_id_led_init(hw);
564     if (ret_val) {
565         DEBUGOUT("Error Initializing Identification LED\n");
566         return ret_val;
567     }
568
569     /* Set the media type and TBI compatibility */
570     e1000_set_media_type(hw);
571
572     /* Disabling VLAN filtering. */
573     DEBUGOUT("Initializing the IEEE VLAN\n");
574     if (hw->mac_type < e1000_82545_rev_3)
575         ew32(VET, 0);
576     e1000_clear_vfta(hw);
577
578     /* For 82542 (rev 2.0), disable MWI and put the receiver into reset */
579     if (hw->mac_type == e1000_82542_rev2_0) {
580         DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
581         e1000_pci_clear_mwi(hw);
582         ew32(RCTL, E1000_RCTL_RST);
583         E1000_WRITE_FLUSH();
584         msleep(5);
585     }
586
587     /* Setup the receive address. This involves initializing all of the Receive
588      * Address Registers (RARs 0 - 15).
589      */
590     e1000_init_rx_addrs(hw);
591
592     /* For 82542 (rev 2.0), take the receiver out of reset and enable MWI */
593     if (hw->mac_type == e1000_82542_rev2_0) {
594         ew32(RCTL, 0);
595         E1000_WRITE_FLUSH();
596         msleep(1);
597         if (hw->pci_cmd_word & PCI_COMMAND_INVALIDATE)
598             e1000_pci_set_mwi(hw);
599     }
600
601     /* Zero out the Multicast HASH table */
602     DEBUGOUT("Zeroing the MTA\n");
603     mta_size = E1000_MC_TBL_SIZE;
604     for (i = 0; i < mta_size; i++) {
605         E1000_WRITE_REG_ARRAY(hw, MTA, i, 0);
606         /* use write flush to prevent Memory Write Block (MWB) from
607          * occuring when accessing our register space */
608         E1000_WRITE_FLUSH();
609     }
610
611     /* Set the PCI priority bit correctly in the CTRL register.  This
612      * determines if the adapter gives priority to receives, or if it
613      * gives equal priority to transmits and receives.  Valid only on
614      * 82542 and 82543 silicon.
615      */
616     if (hw->dma_fairness && hw->mac_type <= e1000_82543) {
617         ctrl = er32(CTRL);
618         ew32(CTRL, ctrl | E1000_CTRL_PRIOR);
619     }
620
621     switch (hw->mac_type) {
622     case e1000_82545_rev_3:
623     case e1000_82546_rev_3:
624         break;
625     default:
626         /* Workaround for PCI-X problem when BIOS sets MMRBC incorrectly. */
627         if (hw->bus_type == e1000_bus_type_pcix && e1000_pcix_get_mmrbc(hw) > 2048)
628                 e1000_pcix_set_mmrbc(hw, 2048);
629         break;
630     }
631
632     /* Call a subroutine to configure the link and setup flow control. */
633     ret_val = e1000_setup_link(hw);
634
635     /* Set the transmit descriptor write-back policy */
636     if (hw->mac_type > e1000_82544) {
637         ctrl = er32(TXDCTL);
638         ctrl = (ctrl & ~E1000_TXDCTL_WTHRESH) | E1000_TXDCTL_FULL_TX_DESC_WB;
639         ew32(TXDCTL, ctrl);
640     }
641
642     /* Clear all of the statistics registers (clear on read).  It is
643      * important that we do this after we have tried to establish link
644      * because the symbol error count will increment wildly if there
645      * is no link.
646      */
647     e1000_clear_hw_cntrs(hw);
648
649     if (hw->device_id == E1000_DEV_ID_82546GB_QUAD_COPPER ||
650         hw->device_id == E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3) {
651         ctrl_ext = er32(CTRL_EXT);
652         /* Relaxed ordering must be disabled to avoid a parity
653          * error crash in a PCI slot. */
654         ctrl_ext |= E1000_CTRL_EXT_RO_DIS;
655         ew32(CTRL_EXT, ctrl_ext);
656     }
657
658     return ret_val;
659 }
660
661 /******************************************************************************
662  * Adjust SERDES output amplitude based on EEPROM setting.
663  *
664  * hw - Struct containing variables accessed by shared code.
665  *****************************************************************************/
666 static s32 e1000_adjust_serdes_amplitude(struct e1000_hw *hw)
667 {
668     u16 eeprom_data;
669     s32  ret_val;
670
671     DEBUGFUNC("e1000_adjust_serdes_amplitude");
672
673     if (hw->media_type != e1000_media_type_internal_serdes)
674         return E1000_SUCCESS;
675
676     switch (hw->mac_type) {
677     case e1000_82545_rev_3:
678     case e1000_82546_rev_3:
679         break;
680     default:
681         return E1000_SUCCESS;
682     }
683
684     ret_val = e1000_read_eeprom(hw, EEPROM_SERDES_AMPLITUDE, 1, &eeprom_data);
685     if (ret_val) {
686         return ret_val;
687     }
688
689     if (eeprom_data != EEPROM_RESERVED_WORD) {
690         /* Adjust SERDES output amplitude only. */
691         eeprom_data &= EEPROM_SERDES_AMPLITUDE_MASK;
692         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_EXT_CTRL, eeprom_data);
693         if (ret_val)
694             return ret_val;
695     }
696
697     return E1000_SUCCESS;
698 }
699
700 /******************************************************************************
701  * Configures flow control and link settings.
702  *
703  * hw - Struct containing variables accessed by shared code
704  *
705  * Determines which flow control settings to use. Calls the apropriate media-
706  * specific link configuration function. Configures the flow control settings.
707  * Assuming the adapter has a valid link partner, a valid link should be
708  * established. Assumes the hardware has previously been reset and the
709  * transmitter and receiver are not enabled.
710  *****************************************************************************/
711 s32 e1000_setup_link(struct e1000_hw *hw)
712 {
713     u32 ctrl_ext;
714     s32 ret_val;
715     u16 eeprom_data;
716
717     DEBUGFUNC("e1000_setup_link");
718
719     /* Read and store word 0x0F of the EEPROM. This word contains bits
720      * that determine the hardware's default PAUSE (flow control) mode,
721      * a bit that determines whether the HW defaults to enabling or
722      * disabling auto-negotiation, and the direction of the
723      * SW defined pins. If there is no SW over-ride of the flow
724      * control setting, then the variable hw->fc will
725      * be initialized based on a value in the EEPROM.
726      */
727     if (hw->fc == E1000_FC_DEFAULT) {
728         ret_val = e1000_read_eeprom(hw, EEPROM_INIT_CONTROL2_REG,
729                                     1, &eeprom_data);
730         if (ret_val) {
731             DEBUGOUT("EEPROM Read Error\n");
732             return -E1000_ERR_EEPROM;
733         }
734         if ((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) == 0)
735             hw->fc = E1000_FC_NONE;
736         else if ((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) ==
737                  EEPROM_WORD0F_ASM_DIR)
738             hw->fc = E1000_FC_TX_PAUSE;
739         else
740             hw->fc = E1000_FC_FULL;
741     }
742
743     /* We want to save off the original Flow Control configuration just
744      * in case we get disconnected and then reconnected into a different
745      * hub or switch with different Flow Control capabilities.
746      */
747     if (hw->mac_type == e1000_82542_rev2_0)
748         hw->fc &= (~E1000_FC_TX_PAUSE);
749
750     if ((hw->mac_type < e1000_82543) && (hw->report_tx_early == 1))
751         hw->fc &= (~E1000_FC_RX_PAUSE);
752
753     hw->original_fc = hw->fc;
754
755     DEBUGOUT1("After fix-ups FlowControl is now = %x\n", hw->fc);
756
757     /* Take the 4 bits from EEPROM word 0x0F that determine the initial
758      * polarity value for the SW controlled pins, and setup the
759      * Extended Device Control reg with that info.
760      * This is needed because one of the SW controlled pins is used for
761      * signal detection.  So this should be done before e1000_setup_pcs_link()
762      * or e1000_phy_setup() is called.
763      */
764     if (hw->mac_type == e1000_82543) {
765         ret_val = e1000_read_eeprom(hw, EEPROM_INIT_CONTROL2_REG,
766                                     1, &eeprom_data);
767         if (ret_val) {
768             DEBUGOUT("EEPROM Read Error\n");
769             return -E1000_ERR_EEPROM;
770         }
771         ctrl_ext = ((eeprom_data & EEPROM_WORD0F_SWPDIO_EXT) <<
772                     SWDPIO__EXT_SHIFT);
773         ew32(CTRL_EXT, ctrl_ext);
774     }
775
776     /* Call the necessary subroutine to configure the link. */
777     ret_val = (hw->media_type == e1000_media_type_copper) ?
778               e1000_setup_copper_link(hw) :
779               e1000_setup_fiber_serdes_link(hw);
780
781     /* Initialize the flow control address, type, and PAUSE timer
782      * registers to their default values.  This is done even if flow
783      * control is disabled, because it does not hurt anything to
784      * initialize these registers.
785      */
786     DEBUGOUT("Initializing the Flow Control address, type and timer regs\n");
787
788     ew32(FCT, FLOW_CONTROL_TYPE);
789     ew32(FCAH, FLOW_CONTROL_ADDRESS_HIGH);
790     ew32(FCAL, FLOW_CONTROL_ADDRESS_LOW);
791
792     ew32(FCTTV, hw->fc_pause_time);
793
794     /* Set the flow control receive threshold registers.  Normally,
795      * these registers will be set to a default threshold that may be
796      * adjusted later by the driver's runtime code.  However, if the
797      * ability to transmit pause frames in not enabled, then these
798      * registers will be set to 0.
799      */
800     if (!(hw->fc & E1000_FC_TX_PAUSE)) {
801         ew32(FCRTL, 0);
802         ew32(FCRTH, 0);
803     } else {
804         /* We need to set up the Receive Threshold high and low water marks
805          * as well as (optionally) enabling the transmission of XON frames.
806          */
807         if (hw->fc_send_xon) {
808             ew32(FCRTL, (hw->fc_low_water | E1000_FCRTL_XONE));
809             ew32(FCRTH, hw->fc_high_water);
810         } else {
811             ew32(FCRTL, hw->fc_low_water);
812             ew32(FCRTH, hw->fc_high_water);
813         }
814     }
815     return ret_val;
816 }
817
818 /******************************************************************************
819  * Sets up link for a fiber based or serdes based adapter
820  *
821  * hw - Struct containing variables accessed by shared code
822  *
823  * Manipulates Physical Coding Sublayer functions in order to configure
824  * link. Assumes the hardware has been previously reset and the transmitter
825  * and receiver are not enabled.
826  *****************************************************************************/
827 static s32 e1000_setup_fiber_serdes_link(struct e1000_hw *hw)
828 {
829     u32 ctrl;
830     u32 status;
831     u32 txcw = 0;
832     u32 i;
833     u32 signal = 0;
834     s32 ret_val;
835
836     DEBUGFUNC("e1000_setup_fiber_serdes_link");
837
838     /* On adapters with a MAC newer than 82544, SWDP 1 will be
839      * set when the optics detect a signal. On older adapters, it will be
840      * cleared when there is a signal.  This applies to fiber media only.
841      * If we're on serdes media, adjust the output amplitude to value
842      * set in the EEPROM.
843      */
844     ctrl = er32(CTRL);
845     if (hw->media_type == e1000_media_type_fiber)
846         signal = (hw->mac_type > e1000_82544) ? E1000_CTRL_SWDPIN1 : 0;
847
848     ret_val = e1000_adjust_serdes_amplitude(hw);
849     if (ret_val)
850         return ret_val;
851
852     /* Take the link out of reset */
853     ctrl &= ~(E1000_CTRL_LRST);
854
855     /* Adjust VCO speed to improve BER performance */
856     ret_val = e1000_set_vco_speed(hw);
857     if (ret_val)
858         return ret_val;
859
860     e1000_config_collision_dist(hw);
861
862     /* Check for a software override of the flow control settings, and setup
863      * the device accordingly.  If auto-negotiation is enabled, then software
864      * will have to set the "PAUSE" bits to the correct value in the Tranmsit
865      * Config Word Register (TXCW) and re-start auto-negotiation.  However, if
866      * auto-negotiation is disabled, then software will have to manually
867      * configure the two flow control enable bits in the CTRL register.
868      *
869      * The possible values of the "fc" parameter are:
870      *      0:  Flow control is completely disabled
871      *      1:  Rx flow control is enabled (we can receive pause frames, but
872      *          not send pause frames).
873      *      2:  Tx flow control is enabled (we can send pause frames but we do
874      *          not support receiving pause frames).
875      *      3:  Both Rx and TX flow control (symmetric) are enabled.
876      */
877     switch (hw->fc) {
878     case E1000_FC_NONE:
879         /* Flow control is completely disabled by a software over-ride. */
880         txcw = (E1000_TXCW_ANE | E1000_TXCW_FD);
881         break;
882     case E1000_FC_RX_PAUSE:
883         /* RX Flow control is enabled and TX Flow control is disabled by a
884          * software over-ride. Since there really isn't a way to advertise
885          * that we are capable of RX Pause ONLY, we will advertise that we
886          * support both symmetric and asymmetric RX PAUSE. Later, we will
887          *  disable the adapter's ability to send PAUSE frames.
888          */
889         txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
890         break;
891     case E1000_FC_TX_PAUSE:
892         /* TX Flow control is enabled, and RX Flow control is disabled, by a
893          * software over-ride.
894          */
895         txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_ASM_DIR);
896         break;
897     case E1000_FC_FULL:
898         /* Flow control (both RX and TX) is enabled by a software over-ride. */
899         txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
900         break;
901     default:
902         DEBUGOUT("Flow control param set incorrectly\n");
903         return -E1000_ERR_CONFIG;
904         break;
905     }
906
907     /* Since auto-negotiation is enabled, take the link out of reset (the link
908      * will be in reset, because we previously reset the chip). This will
909      * restart auto-negotiation.  If auto-neogtiation is successful then the
910      * link-up status bit will be set and the flow control enable bits (RFCE
911      * and TFCE) will be set according to their negotiated value.
912      */
913     DEBUGOUT("Auto-negotiation enabled\n");
914
915     ew32(TXCW, txcw);
916     ew32(CTRL, ctrl);
917     E1000_WRITE_FLUSH();
918
919     hw->txcw = txcw;
920     msleep(1);
921
922     /* If we have a signal (the cable is plugged in) then poll for a "Link-Up"
923      * indication in the Device Status Register.  Time-out if a link isn't
924      * seen in 500 milliseconds seconds (Auto-negotiation should complete in
925      * less than 500 milliseconds even if the other end is doing it in SW).
926      * For internal serdes, we just assume a signal is present, then poll.
927      */
928     if (hw->media_type == e1000_media_type_internal_serdes ||
929        (er32(CTRL) & E1000_CTRL_SWDPIN1) == signal) {
930         DEBUGOUT("Looking for Link\n");
931         for (i = 0; i < (LINK_UP_TIMEOUT / 10); i++) {
932             msleep(10);
933             status = er32(STATUS);
934             if (status & E1000_STATUS_LU) break;
935         }
936         if (i == (LINK_UP_TIMEOUT / 10)) {
937             DEBUGOUT("Never got a valid link from auto-neg!!!\n");
938             hw->autoneg_failed = 1;
939             /* AutoNeg failed to achieve a link, so we'll call
940              * e1000_check_for_link. This routine will force the link up if
941              * we detect a signal. This will allow us to communicate with
942              * non-autonegotiating link partners.
943              */
944             ret_val = e1000_check_for_link(hw);
945             if (ret_val) {
946                 DEBUGOUT("Error while checking for link\n");
947                 return ret_val;
948             }
949             hw->autoneg_failed = 0;
950         } else {
951             hw->autoneg_failed = 0;
952             DEBUGOUT("Valid Link Found\n");
953         }
954     } else {
955         DEBUGOUT("No Signal Detected\n");
956     }
957     return E1000_SUCCESS;
958 }
959
960 /******************************************************************************
961 * Make sure we have a valid PHY and change PHY mode before link setup.
962 *
963 * hw - Struct containing variables accessed by shared code
964 ******************************************************************************/
965 static s32 e1000_copper_link_preconfig(struct e1000_hw *hw)
966 {
967     u32 ctrl;
968     s32 ret_val;
969     u16 phy_data;
970
971     DEBUGFUNC("e1000_copper_link_preconfig");
972
973     ctrl = er32(CTRL);
974     /* With 82543, we need to force speed and duplex on the MAC equal to what
975      * the PHY speed and duplex configuration is. In addition, we need to
976      * perform a hardware reset on the PHY to take it out of reset.
977      */
978     if (hw->mac_type > e1000_82543) {
979         ctrl |= E1000_CTRL_SLU;
980         ctrl &= ~(E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
981         ew32(CTRL, ctrl);
982     } else {
983         ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX | E1000_CTRL_SLU);
984         ew32(CTRL, ctrl);
985         ret_val = e1000_phy_hw_reset(hw);
986         if (ret_val)
987             return ret_val;
988     }
989
990     /* Make sure we have a valid PHY */
991     ret_val = e1000_detect_gig_phy(hw);
992     if (ret_val) {
993         DEBUGOUT("Error, did not detect valid phy.\n");
994         return ret_val;
995     }
996     DEBUGOUT1("Phy ID = %x \n", hw->phy_id);
997
998     /* Set PHY to class A mode (if necessary) */
999     ret_val = e1000_set_phy_mode(hw);
1000     if (ret_val)
1001         return ret_val;
1002
1003     if ((hw->mac_type == e1000_82545_rev_3) ||
1004        (hw->mac_type == e1000_82546_rev_3)) {
1005         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
1006         phy_data |= 0x00000008;
1007         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
1008     }
1009
1010     if (hw->mac_type <= e1000_82543 ||
1011         hw->mac_type == e1000_82541 || hw->mac_type == e1000_82547 ||
1012         hw->mac_type == e1000_82541_rev_2 || hw->mac_type == e1000_82547_rev_2)
1013         hw->phy_reset_disable = false;
1014
1015    return E1000_SUCCESS;
1016 }
1017
1018
1019 /********************************************************************
1020 * Copper link setup for e1000_phy_igp series.
1021 *
1022 * hw - Struct containing variables accessed by shared code
1023 *********************************************************************/
1024 static s32 e1000_copper_link_igp_setup(struct e1000_hw *hw)
1025 {
1026     u32 led_ctrl;
1027     s32 ret_val;
1028     u16 phy_data;
1029
1030     DEBUGFUNC("e1000_copper_link_igp_setup");
1031
1032     if (hw->phy_reset_disable)
1033         return E1000_SUCCESS;
1034
1035     ret_val = e1000_phy_reset(hw);
1036     if (ret_val) {
1037         DEBUGOUT("Error Resetting the PHY\n");
1038         return ret_val;
1039     }
1040
1041     /* Wait 15ms for MAC to configure PHY from eeprom settings */
1042     msleep(15);
1043     /* Configure activity LED after PHY reset */
1044     led_ctrl = er32(LEDCTL);
1045     led_ctrl &= IGP_ACTIVITY_LED_MASK;
1046     led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
1047     ew32(LEDCTL, led_ctrl);
1048
1049     /* The NVM settings will configure LPLU in D3 for IGP2 and IGP3 PHYs */
1050     if (hw->phy_type == e1000_phy_igp) {
1051         /* disable lplu d3 during driver init */
1052         ret_val = e1000_set_d3_lplu_state(hw, false);
1053         if (ret_val) {
1054             DEBUGOUT("Error Disabling LPLU D3\n");
1055             return ret_val;
1056         }
1057     }
1058
1059     /* Configure mdi-mdix settings */
1060     ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, &phy_data);
1061     if (ret_val)
1062         return ret_val;
1063
1064     if ((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
1065         hw->dsp_config_state = e1000_dsp_config_disabled;
1066         /* Force MDI for earlier revs of the IGP PHY */
1067         phy_data &= ~(IGP01E1000_PSCR_AUTO_MDIX | IGP01E1000_PSCR_FORCE_MDI_MDIX);
1068         hw->mdix = 1;
1069
1070     } else {
1071         hw->dsp_config_state = e1000_dsp_config_enabled;
1072         phy_data &= ~IGP01E1000_PSCR_AUTO_MDIX;
1073
1074         switch (hw->mdix) {
1075         case 1:
1076             phy_data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
1077             break;
1078         case 2:
1079             phy_data |= IGP01E1000_PSCR_FORCE_MDI_MDIX;
1080             break;
1081         case 0:
1082         default:
1083             phy_data |= IGP01E1000_PSCR_AUTO_MDIX;
1084             break;
1085         }
1086     }
1087     ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, phy_data);
1088     if (ret_val)
1089         return ret_val;
1090
1091     /* set auto-master slave resolution settings */
1092     if (hw->autoneg) {
1093         e1000_ms_type phy_ms_setting = hw->master_slave;
1094
1095         if (hw->ffe_config_state == e1000_ffe_config_active)
1096             hw->ffe_config_state = e1000_ffe_config_enabled;
1097
1098         if (hw->dsp_config_state == e1000_dsp_config_activated)
1099             hw->dsp_config_state = e1000_dsp_config_enabled;
1100
1101         /* when autonegotiation advertisment is only 1000Mbps then we
1102           * should disable SmartSpeed and enable Auto MasterSlave
1103           * resolution as hardware default. */
1104         if (hw->autoneg_advertised == ADVERTISE_1000_FULL) {
1105             /* Disable SmartSpeed */
1106             ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
1107                                          &phy_data);
1108             if (ret_val)
1109                 return ret_val;
1110             phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
1111             ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
1112                                           phy_data);
1113             if (ret_val)
1114                 return ret_val;
1115             /* Set auto Master/Slave resolution process */
1116             ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_data);
1117             if (ret_val)
1118                 return ret_val;
1119             phy_data &= ~CR_1000T_MS_ENABLE;
1120             ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_data);
1121             if (ret_val)
1122                 return ret_val;
1123         }
1124
1125         ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_data);
1126         if (ret_val)
1127             return ret_val;
1128
1129         /* load defaults for future use */
1130         hw->original_master_slave = (phy_data & CR_1000T_MS_ENABLE) ?
1131                                         ((phy_data & CR_1000T_MS_VALUE) ?
1132                                          e1000_ms_force_master :
1133                                          e1000_ms_force_slave) :
1134                                          e1000_ms_auto;
1135
1136         switch (phy_ms_setting) {
1137         case e1000_ms_force_master:
1138             phy_data |= (CR_1000T_MS_ENABLE | CR_1000T_MS_VALUE);
1139             break;
1140         case e1000_ms_force_slave:
1141             phy_data |= CR_1000T_MS_ENABLE;
1142             phy_data &= ~(CR_1000T_MS_VALUE);
1143             break;
1144         case e1000_ms_auto:
1145             phy_data &= ~CR_1000T_MS_ENABLE;
1146             default:
1147             break;
1148         }
1149         ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_data);
1150         if (ret_val)
1151             return ret_val;
1152     }
1153
1154     return E1000_SUCCESS;
1155 }
1156
1157 /********************************************************************
1158 * Copper link setup for e1000_phy_m88 series.
1159 *
1160 * hw - Struct containing variables accessed by shared code
1161 *********************************************************************/
1162 static s32 e1000_copper_link_mgp_setup(struct e1000_hw *hw)
1163 {
1164     s32 ret_val;
1165     u16 phy_data;
1166
1167     DEBUGFUNC("e1000_copper_link_mgp_setup");
1168
1169     if (hw->phy_reset_disable)
1170         return E1000_SUCCESS;
1171
1172     /* Enable CRS on TX. This must be set for half-duplex operation. */
1173     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
1174     if (ret_val)
1175         return ret_val;
1176
1177     phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
1178
1179     /* Options:
1180      *   MDI/MDI-X = 0 (default)
1181      *   0 - Auto for all speeds
1182      *   1 - MDI mode
1183      *   2 - MDI-X mode
1184      *   3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
1185      */
1186     phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
1187
1188     switch (hw->mdix) {
1189     case 1:
1190         phy_data |= M88E1000_PSCR_MDI_MANUAL_MODE;
1191         break;
1192     case 2:
1193         phy_data |= M88E1000_PSCR_MDIX_MANUAL_MODE;
1194         break;
1195     case 3:
1196         phy_data |= M88E1000_PSCR_AUTO_X_1000T;
1197         break;
1198     case 0:
1199     default:
1200         phy_data |= M88E1000_PSCR_AUTO_X_MODE;
1201         break;
1202     }
1203
1204     /* Options:
1205      *   disable_polarity_correction = 0 (default)
1206      *       Automatic Correction for Reversed Cable Polarity
1207      *   0 - Disabled
1208      *   1 - Enabled
1209      */
1210     phy_data &= ~M88E1000_PSCR_POLARITY_REVERSAL;
1211     if (hw->disable_polarity_correction == 1)
1212         phy_data |= M88E1000_PSCR_POLARITY_REVERSAL;
1213     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
1214     if (ret_val)
1215         return ret_val;
1216
1217     if (hw->phy_revision < M88E1011_I_REV_4) {
1218         /* Force TX_CLK in the Extended PHY Specific Control Register
1219          * to 25MHz clock.
1220          */
1221         ret_val = e1000_read_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, &phy_data);
1222         if (ret_val)
1223             return ret_val;
1224
1225         phy_data |= M88E1000_EPSCR_TX_CLK_25;
1226
1227         if ((hw->phy_revision == E1000_REVISION_2) &&
1228             (hw->phy_id == M88E1111_I_PHY_ID)) {
1229             /* Vidalia Phy, set the downshift counter to 5x */
1230             phy_data &= ~(M88EC018_EPSCR_DOWNSHIFT_COUNTER_MASK);
1231             phy_data |= M88EC018_EPSCR_DOWNSHIFT_COUNTER_5X;
1232             ret_val = e1000_write_phy_reg(hw,
1233                                         M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
1234             if (ret_val)
1235                 return ret_val;
1236         } else {
1237             /* Configure Master and Slave downshift values */
1238             phy_data &= ~(M88E1000_EPSCR_MASTER_DOWNSHIFT_MASK |
1239                               M88E1000_EPSCR_SLAVE_DOWNSHIFT_MASK);
1240             phy_data |= (M88E1000_EPSCR_MASTER_DOWNSHIFT_1X |
1241                              M88E1000_EPSCR_SLAVE_DOWNSHIFT_1X);
1242             ret_val = e1000_write_phy_reg(hw,
1243                                         M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
1244             if (ret_val)
1245                return ret_val;
1246         }
1247     }
1248
1249     /* SW Reset the PHY so all changes take effect */
1250     ret_val = e1000_phy_reset(hw);
1251     if (ret_val) {
1252         DEBUGOUT("Error Resetting the PHY\n");
1253         return ret_val;
1254     }
1255
1256    return E1000_SUCCESS;
1257 }
1258
1259 /********************************************************************
1260 * Setup auto-negotiation and flow control advertisements,
1261 * and then perform auto-negotiation.
1262 *
1263 * hw - Struct containing variables accessed by shared code
1264 *********************************************************************/
1265 static s32 e1000_copper_link_autoneg(struct e1000_hw *hw)
1266 {
1267     s32 ret_val;
1268     u16 phy_data;
1269
1270     DEBUGFUNC("e1000_copper_link_autoneg");
1271
1272     /* Perform some bounds checking on the hw->autoneg_advertised
1273      * parameter.  If this variable is zero, then set it to the default.
1274      */
1275     hw->autoneg_advertised &= AUTONEG_ADVERTISE_SPEED_DEFAULT;
1276
1277     /* If autoneg_advertised is zero, we assume it was not defaulted
1278      * by the calling code so we set to advertise full capability.
1279      */
1280     if (hw->autoneg_advertised == 0)
1281         hw->autoneg_advertised = AUTONEG_ADVERTISE_SPEED_DEFAULT;
1282
1283     DEBUGOUT("Reconfiguring auto-neg advertisement params\n");
1284     ret_val = e1000_phy_setup_autoneg(hw);
1285     if (ret_val) {
1286         DEBUGOUT("Error Setting up Auto-Negotiation\n");
1287         return ret_val;
1288     }
1289     DEBUGOUT("Restarting Auto-Neg\n");
1290
1291     /* Restart auto-negotiation by setting the Auto Neg Enable bit and
1292      * the Auto Neg Restart bit in the PHY control register.
1293      */
1294     ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
1295     if (ret_val)
1296         return ret_val;
1297
1298     phy_data |= (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
1299     ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
1300     if (ret_val)
1301         return ret_val;
1302
1303     /* Does the user want to wait for Auto-Neg to complete here, or
1304      * check at a later time (for example, callback routine).
1305      */
1306     if (hw->wait_autoneg_complete) {
1307         ret_val = e1000_wait_autoneg(hw);
1308         if (ret_val) {
1309             DEBUGOUT("Error while waiting for autoneg to complete\n");
1310             return ret_val;
1311         }
1312     }
1313
1314     hw->get_link_status = true;
1315
1316     return E1000_SUCCESS;
1317 }
1318
1319 /******************************************************************************
1320 * Config the MAC and the PHY after link is up.
1321 *   1) Set up the MAC to the current PHY speed/duplex
1322 *      if we are on 82543.  If we
1323 *      are on newer silicon, we only need to configure
1324 *      collision distance in the Transmit Control Register.
1325 *   2) Set up flow control on the MAC to that established with
1326 *      the link partner.
1327 *   3) Config DSP to improve Gigabit link quality for some PHY revisions.
1328 *
1329 * hw - Struct containing variables accessed by shared code
1330 ******************************************************************************/
1331 static s32 e1000_copper_link_postconfig(struct e1000_hw *hw)
1332 {
1333     s32 ret_val;
1334     DEBUGFUNC("e1000_copper_link_postconfig");
1335
1336     if (hw->mac_type >= e1000_82544) {
1337         e1000_config_collision_dist(hw);
1338     } else {
1339         ret_val = e1000_config_mac_to_phy(hw);
1340         if (ret_val) {
1341             DEBUGOUT("Error configuring MAC to PHY settings\n");
1342             return ret_val;
1343         }
1344     }
1345     ret_val = e1000_config_fc_after_link_up(hw);
1346     if (ret_val) {
1347         DEBUGOUT("Error Configuring Flow Control\n");
1348         return ret_val;
1349     }
1350
1351     /* Config DSP to improve Giga link quality */
1352     if (hw->phy_type == e1000_phy_igp) {
1353         ret_val = e1000_config_dsp_after_link_change(hw, true);
1354         if (ret_val) {
1355             DEBUGOUT("Error Configuring DSP after link up\n");
1356             return ret_val;
1357         }
1358     }
1359
1360     return E1000_SUCCESS;
1361 }
1362
1363 /******************************************************************************
1364 * Detects which PHY is present and setup the speed and duplex
1365 *
1366 * hw - Struct containing variables accessed by shared code
1367 ******************************************************************************/
1368 static s32 e1000_setup_copper_link(struct e1000_hw *hw)
1369 {
1370     s32 ret_val;
1371     u16 i;
1372     u16 phy_data;
1373
1374     DEBUGFUNC("e1000_setup_copper_link");
1375
1376     /* Check if it is a valid PHY and set PHY mode if necessary. */
1377     ret_val = e1000_copper_link_preconfig(hw);
1378     if (ret_val)
1379         return ret_val;
1380
1381     if (hw->phy_type == e1000_phy_igp) {
1382         ret_val = e1000_copper_link_igp_setup(hw);
1383         if (ret_val)
1384             return ret_val;
1385     } else if (hw->phy_type == e1000_phy_m88) {
1386         ret_val = e1000_copper_link_mgp_setup(hw);
1387         if (ret_val)
1388             return ret_val;
1389     }
1390
1391     if (hw->autoneg) {
1392         /* Setup autoneg and flow control advertisement
1393           * and perform autonegotiation */
1394         ret_val = e1000_copper_link_autoneg(hw);
1395         if (ret_val)
1396             return ret_val;
1397     } else {
1398         /* PHY will be set to 10H, 10F, 100H,or 100F
1399           * depending on value from forced_speed_duplex. */
1400         DEBUGOUT("Forcing speed and duplex\n");
1401         ret_val = e1000_phy_force_speed_duplex(hw);
1402         if (ret_val) {
1403             DEBUGOUT("Error Forcing Speed and Duplex\n");
1404             return ret_val;
1405         }
1406     }
1407
1408     /* Check link status. Wait up to 100 microseconds for link to become
1409      * valid.
1410      */
1411     for (i = 0; i < 10; i++) {
1412         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
1413         if (ret_val)
1414             return ret_val;
1415         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
1416         if (ret_val)
1417             return ret_val;
1418
1419         if (phy_data & MII_SR_LINK_STATUS) {
1420             /* Config the MAC and PHY after link is up */
1421             ret_val = e1000_copper_link_postconfig(hw);
1422             if (ret_val)
1423                 return ret_val;
1424
1425             DEBUGOUT("Valid link established!!!\n");
1426             return E1000_SUCCESS;
1427         }
1428         udelay(10);
1429     }
1430
1431     DEBUGOUT("Unable to establish link!!!\n");
1432     return E1000_SUCCESS;
1433 }
1434
1435 /******************************************************************************
1436 * Configures PHY autoneg and flow control advertisement settings
1437 *
1438 * hw - Struct containing variables accessed by shared code
1439 ******************************************************************************/
1440 s32 e1000_phy_setup_autoneg(struct e1000_hw *hw)
1441 {
1442     s32 ret_val;
1443     u16 mii_autoneg_adv_reg;
1444     u16 mii_1000t_ctrl_reg;
1445
1446     DEBUGFUNC("e1000_phy_setup_autoneg");
1447
1448     /* Read the MII Auto-Neg Advertisement Register (Address 4). */
1449     ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_ADV, &mii_autoneg_adv_reg);
1450     if (ret_val)
1451         return ret_val;
1452
1453     /* Read the MII 1000Base-T Control Register (Address 9). */
1454     ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL, &mii_1000t_ctrl_reg);
1455     if (ret_val)
1456         return ret_val;
1457
1458     /* Need to parse both autoneg_advertised and fc and set up
1459      * the appropriate PHY registers.  First we will parse for
1460      * autoneg_advertised software override.  Since we can advertise
1461      * a plethora of combinations, we need to check each bit
1462      * individually.
1463      */
1464
1465     /* First we clear all the 10/100 mb speed bits in the Auto-Neg
1466      * Advertisement Register (Address 4) and the 1000 mb speed bits in
1467      * the  1000Base-T Control Register (Address 9).
1468      */
1469     mii_autoneg_adv_reg &= ~REG4_SPEED_MASK;
1470     mii_1000t_ctrl_reg &= ~REG9_SPEED_MASK;
1471
1472     DEBUGOUT1("autoneg_advertised %x\n", hw->autoneg_advertised);
1473
1474     /* Do we want to advertise 10 Mb Half Duplex? */
1475     if (hw->autoneg_advertised & ADVERTISE_10_HALF) {
1476         DEBUGOUT("Advertise 10mb Half duplex\n");
1477         mii_autoneg_adv_reg |= NWAY_AR_10T_HD_CAPS;
1478     }
1479
1480     /* Do we want to advertise 10 Mb Full Duplex? */
1481     if (hw->autoneg_advertised & ADVERTISE_10_FULL) {
1482         DEBUGOUT("Advertise 10mb Full duplex\n");
1483         mii_autoneg_adv_reg |= NWAY_AR_10T_FD_CAPS;
1484     }
1485
1486     /* Do we want to advertise 100 Mb Half Duplex? */
1487     if (hw->autoneg_advertised & ADVERTISE_100_HALF) {
1488         DEBUGOUT("Advertise 100mb Half duplex\n");
1489         mii_autoneg_adv_reg |= NWAY_AR_100TX_HD_CAPS;
1490     }
1491
1492     /* Do we want to advertise 100 Mb Full Duplex? */
1493     if (hw->autoneg_advertised & ADVERTISE_100_FULL) {
1494         DEBUGOUT("Advertise 100mb Full duplex\n");
1495         mii_autoneg_adv_reg |= NWAY_AR_100TX_FD_CAPS;
1496     }
1497
1498     /* We do not allow the Phy to advertise 1000 Mb Half Duplex */
1499     if (hw->autoneg_advertised & ADVERTISE_1000_HALF) {
1500         DEBUGOUT("Advertise 1000mb Half duplex requested, request denied!\n");
1501     }
1502
1503     /* Do we want to advertise 1000 Mb Full Duplex? */
1504     if (hw->autoneg_advertised & ADVERTISE_1000_FULL) {
1505         DEBUGOUT("Advertise 1000mb Full duplex\n");
1506         mii_1000t_ctrl_reg |= CR_1000T_FD_CAPS;
1507     }
1508
1509     /* Check for a software override of the flow control settings, and
1510      * setup the PHY advertisement registers accordingly.  If
1511      * auto-negotiation is enabled, then software will have to set the
1512      * "PAUSE" bits to the correct value in the Auto-Negotiation
1513      * Advertisement Register (PHY_AUTONEG_ADV) and re-start auto-negotiation.
1514      *
1515      * The possible values of the "fc" parameter are:
1516      *      0:  Flow control is completely disabled
1517      *      1:  Rx flow control is enabled (we can receive pause frames
1518      *          but not send pause frames).
1519      *      2:  Tx flow control is enabled (we can send pause frames
1520      *          but we do not support receiving pause frames).
1521      *      3:  Both Rx and TX flow control (symmetric) are enabled.
1522      *  other:  No software override.  The flow control configuration
1523      *          in the EEPROM is used.
1524      */
1525     switch (hw->fc) {
1526     case E1000_FC_NONE: /* 0 */
1527         /* Flow control (RX & TX) is completely disabled by a
1528          * software over-ride.
1529          */
1530         mii_autoneg_adv_reg &= ~(NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
1531         break;
1532     case E1000_FC_RX_PAUSE: /* 1 */
1533         /* RX Flow control is enabled, and TX Flow control is
1534          * disabled, by a software over-ride.
1535          */
1536         /* Since there really isn't a way to advertise that we are
1537          * capable of RX Pause ONLY, we will advertise that we
1538          * support both symmetric and asymmetric RX PAUSE.  Later
1539          * (in e1000_config_fc_after_link_up) we will disable the
1540          *hw's ability to send PAUSE frames.
1541          */
1542         mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
1543         break;
1544     case E1000_FC_TX_PAUSE: /* 2 */
1545         /* TX Flow control is enabled, and RX Flow control is
1546          * disabled, by a software over-ride.
1547          */
1548         mii_autoneg_adv_reg |= NWAY_AR_ASM_DIR;
1549         mii_autoneg_adv_reg &= ~NWAY_AR_PAUSE;
1550         break;
1551     case E1000_FC_FULL: /* 3 */
1552         /* Flow control (both RX and TX) is enabled by a software
1553          * over-ride.
1554          */
1555         mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
1556         break;
1557     default:
1558         DEBUGOUT("Flow control param set incorrectly\n");
1559         return -E1000_ERR_CONFIG;
1560     }
1561
1562     ret_val = e1000_write_phy_reg(hw, PHY_AUTONEG_ADV, mii_autoneg_adv_reg);
1563     if (ret_val)
1564         return ret_val;
1565
1566     DEBUGOUT1("Auto-Neg Advertising %x\n", mii_autoneg_adv_reg);
1567
1568     ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL, mii_1000t_ctrl_reg);
1569     if (ret_val)
1570         return ret_val;
1571
1572     return E1000_SUCCESS;
1573 }
1574
1575 /******************************************************************************
1576 * Force PHY speed and duplex settings to hw->forced_speed_duplex
1577 *
1578 * hw - Struct containing variables accessed by shared code
1579 ******************************************************************************/
1580 static s32 e1000_phy_force_speed_duplex(struct e1000_hw *hw)
1581 {
1582     u32 ctrl;
1583     s32 ret_val;
1584     u16 mii_ctrl_reg;
1585     u16 mii_status_reg;
1586     u16 phy_data;
1587     u16 i;
1588
1589     DEBUGFUNC("e1000_phy_force_speed_duplex");
1590
1591     /* Turn off Flow control if we are forcing speed and duplex. */
1592     hw->fc = E1000_FC_NONE;
1593
1594     DEBUGOUT1("hw->fc = %d\n", hw->fc);
1595
1596     /* Read the Device Control Register. */
1597     ctrl = er32(CTRL);
1598
1599     /* Set the bits to Force Speed and Duplex in the Device Ctrl Reg. */
1600     ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
1601     ctrl &= ~(DEVICE_SPEED_MASK);
1602
1603     /* Clear the Auto Speed Detect Enable bit. */
1604     ctrl &= ~E1000_CTRL_ASDE;
1605
1606     /* Read the MII Control Register. */
1607     ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &mii_ctrl_reg);
1608     if (ret_val)
1609         return ret_val;
1610
1611     /* We need to disable autoneg in order to force link and duplex. */
1612
1613     mii_ctrl_reg &= ~MII_CR_AUTO_NEG_EN;
1614
1615     /* Are we forcing Full or Half Duplex? */
1616     if (hw->forced_speed_duplex == e1000_100_full ||
1617         hw->forced_speed_duplex == e1000_10_full) {
1618         /* We want to force full duplex so we SET the full duplex bits in the
1619          * Device and MII Control Registers.
1620          */
1621         ctrl |= E1000_CTRL_FD;
1622         mii_ctrl_reg |= MII_CR_FULL_DUPLEX;
1623         DEBUGOUT("Full Duplex\n");
1624     } else {
1625         /* We want to force half duplex so we CLEAR the full duplex bits in
1626          * the Device and MII Control Registers.
1627          */
1628         ctrl &= ~E1000_CTRL_FD;
1629         mii_ctrl_reg &= ~MII_CR_FULL_DUPLEX;
1630         DEBUGOUT("Half Duplex\n");
1631     }
1632
1633     /* Are we forcing 100Mbps??? */
1634     if (hw->forced_speed_duplex == e1000_100_full ||
1635        hw->forced_speed_duplex == e1000_100_half) {
1636         /* Set the 100Mb bit and turn off the 1000Mb and 10Mb bits. */
1637         ctrl |= E1000_CTRL_SPD_100;
1638         mii_ctrl_reg |= MII_CR_SPEED_100;
1639         mii_ctrl_reg &= ~(MII_CR_SPEED_1000 | MII_CR_SPEED_10);
1640         DEBUGOUT("Forcing 100mb ");
1641     } else {
1642         /* Set the 10Mb bit and turn off the 1000Mb and 100Mb bits. */
1643         ctrl &= ~(E1000_CTRL_SPD_1000 | E1000_CTRL_SPD_100);
1644         mii_ctrl_reg |= MII_CR_SPEED_10;
1645         mii_ctrl_reg &= ~(MII_CR_SPEED_1000 | MII_CR_SPEED_100);
1646         DEBUGOUT("Forcing 10mb ");
1647     }
1648
1649     e1000_config_collision_dist(hw);
1650
1651     /* Write the configured values back to the Device Control Reg. */
1652     ew32(CTRL, ctrl);
1653
1654     if (hw->phy_type == e1000_phy_m88) {
1655         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
1656         if (ret_val)
1657             return ret_val;
1658
1659         /* Clear Auto-Crossover to force MDI manually. M88E1000 requires MDI
1660          * forced whenever speed are duplex are forced.
1661          */
1662         phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
1663         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
1664         if (ret_val)
1665             return ret_val;
1666
1667         DEBUGOUT1("M88E1000 PSCR: %x \n", phy_data);
1668
1669         /* Need to reset the PHY or these changes will be ignored */
1670         mii_ctrl_reg |= MII_CR_RESET;
1671
1672     } else {
1673         /* Clear Auto-Crossover to force MDI manually.  IGP requires MDI
1674          * forced whenever speed or duplex are forced.
1675          */
1676         ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, &phy_data);
1677         if (ret_val)
1678             return ret_val;
1679
1680         phy_data &= ~IGP01E1000_PSCR_AUTO_MDIX;
1681         phy_data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
1682
1683         ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, phy_data);
1684         if (ret_val)
1685             return ret_val;
1686     }
1687
1688     /* Write back the modified PHY MII control register. */
1689     ret_val = e1000_write_phy_reg(hw, PHY_CTRL, mii_ctrl_reg);
1690     if (ret_val)
1691         return ret_val;
1692
1693     udelay(1);
1694
1695     /* The wait_autoneg_complete flag may be a little misleading here.
1696      * Since we are forcing speed and duplex, Auto-Neg is not enabled.
1697      * But we do want to delay for a period while forcing only so we
1698      * don't generate false No Link messages.  So we will wait here
1699      * only if the user has set wait_autoneg_complete to 1, which is
1700      * the default.
1701      */
1702     if (hw->wait_autoneg_complete) {
1703         /* We will wait for autoneg to complete. */
1704         DEBUGOUT("Waiting for forced speed/duplex link.\n");
1705         mii_status_reg = 0;
1706
1707         /* We will wait for autoneg to complete or 4.5 seconds to expire. */
1708         for (i = PHY_FORCE_TIME; i > 0; i--) {
1709             /* Read the MII Status Register and wait for Auto-Neg Complete bit
1710              * to be set.
1711              */
1712             ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
1713             if (ret_val)
1714                 return ret_val;
1715
1716             ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
1717             if (ret_val)
1718                 return ret_val;
1719
1720             if (mii_status_reg & MII_SR_LINK_STATUS) break;
1721             msleep(100);
1722         }
1723         if ((i == 0) &&
1724            (hw->phy_type == e1000_phy_m88)) {
1725             /* We didn't get link.  Reset the DSP and wait again for link. */
1726             ret_val = e1000_phy_reset_dsp(hw);
1727             if (ret_val) {
1728                 DEBUGOUT("Error Resetting PHY DSP\n");
1729                 return ret_val;
1730             }
1731         }
1732         /* This loop will early-out if the link condition has been met.  */
1733         for (i = PHY_FORCE_TIME; i > 0; i--) {
1734             if (mii_status_reg & MII_SR_LINK_STATUS) break;
1735             msleep(100);
1736             /* Read the MII Status Register and wait for Auto-Neg Complete bit
1737              * to be set.
1738              */
1739             ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
1740             if (ret_val)
1741                 return ret_val;
1742
1743             ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
1744             if (ret_val)
1745                 return ret_val;
1746         }
1747     }
1748
1749     if (hw->phy_type == e1000_phy_m88) {
1750         /* Because we reset the PHY above, we need to re-force TX_CLK in the
1751          * Extended PHY Specific Control Register to 25MHz clock.  This value
1752          * defaults back to a 2.5MHz clock when the PHY is reset.
1753          */
1754         ret_val = e1000_read_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, &phy_data);
1755         if (ret_val)
1756             return ret_val;
1757
1758         phy_data |= M88E1000_EPSCR_TX_CLK_25;
1759         ret_val = e1000_write_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
1760         if (ret_val)
1761             return ret_val;
1762
1763         /* In addition, because of the s/w reset above, we need to enable CRS on
1764          * TX.  This must be set for both full and half duplex operation.
1765          */
1766         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
1767         if (ret_val)
1768             return ret_val;
1769
1770         phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
1771         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
1772         if (ret_val)
1773             return ret_val;
1774
1775         if ((hw->mac_type == e1000_82544 || hw->mac_type == e1000_82543) &&
1776             (!hw->autoneg) && (hw->forced_speed_duplex == e1000_10_full ||
1777              hw->forced_speed_duplex == e1000_10_half)) {
1778             ret_val = e1000_polarity_reversal_workaround(hw);
1779             if (ret_val)
1780                 return ret_val;
1781         }
1782     }
1783     return E1000_SUCCESS;
1784 }
1785
1786 /******************************************************************************
1787 * Sets the collision distance in the Transmit Control register
1788 *
1789 * hw - Struct containing variables accessed by shared code
1790 *
1791 * Link should have been established previously. Reads the speed and duplex
1792 * information from the Device Status register.
1793 ******************************************************************************/
1794 void e1000_config_collision_dist(struct e1000_hw *hw)
1795 {
1796     u32 tctl, coll_dist;
1797
1798     DEBUGFUNC("e1000_config_collision_dist");
1799
1800     if (hw->mac_type < e1000_82543)
1801         coll_dist = E1000_COLLISION_DISTANCE_82542;
1802     else
1803         coll_dist = E1000_COLLISION_DISTANCE;
1804
1805     tctl = er32(TCTL);
1806
1807     tctl &= ~E1000_TCTL_COLD;
1808     tctl |= coll_dist << E1000_COLD_SHIFT;
1809
1810     ew32(TCTL, tctl);
1811     E1000_WRITE_FLUSH();
1812 }
1813
1814 /******************************************************************************
1815 * Sets MAC speed and duplex settings to reflect the those in the PHY
1816 *
1817 * hw - Struct containing variables accessed by shared code
1818 * mii_reg - data to write to the MII control register
1819 *
1820 * The contents of the PHY register containing the needed information need to
1821 * be passed in.
1822 ******************************************************************************/
1823 static s32 e1000_config_mac_to_phy(struct e1000_hw *hw)
1824 {
1825     u32 ctrl;
1826     s32 ret_val;
1827     u16 phy_data;
1828
1829     DEBUGFUNC("e1000_config_mac_to_phy");
1830
1831     /* 82544 or newer MAC, Auto Speed Detection takes care of
1832     * MAC speed/duplex configuration.*/
1833     if (hw->mac_type >= e1000_82544)
1834         return E1000_SUCCESS;
1835
1836     /* Read the Device Control Register and set the bits to Force Speed
1837      * and Duplex.
1838      */
1839     ctrl = er32(CTRL);
1840     ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
1841     ctrl &= ~(E1000_CTRL_SPD_SEL | E1000_CTRL_ILOS);
1842
1843     /* Set up duplex in the Device Control and Transmit Control
1844      * registers depending on negotiated values.
1845      */
1846     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data);
1847     if (ret_val)
1848         return ret_val;
1849
1850     if (phy_data & M88E1000_PSSR_DPLX)
1851         ctrl |= E1000_CTRL_FD;
1852     else
1853         ctrl &= ~E1000_CTRL_FD;
1854
1855     e1000_config_collision_dist(hw);
1856
1857     /* Set up speed in the Device Control register depending on
1858      * negotiated values.
1859      */
1860     if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_1000MBS)
1861         ctrl |= E1000_CTRL_SPD_1000;
1862     else if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_100MBS)
1863         ctrl |= E1000_CTRL_SPD_100;
1864
1865     /* Write the configured values back to the Device Control Reg. */
1866     ew32(CTRL, ctrl);
1867     return E1000_SUCCESS;
1868 }
1869
1870 /******************************************************************************
1871  * Forces the MAC's flow control settings.
1872  *
1873  * hw - Struct containing variables accessed by shared code
1874  *
1875  * Sets the TFCE and RFCE bits in the device control register to reflect
1876  * the adapter settings. TFCE and RFCE need to be explicitly set by
1877  * software when a Copper PHY is used because autonegotiation is managed
1878  * by the PHY rather than the MAC. Software must also configure these
1879  * bits when link is forced on a fiber connection.
1880  *****************************************************************************/
1881 s32 e1000_force_mac_fc(struct e1000_hw *hw)
1882 {
1883     u32 ctrl;
1884
1885     DEBUGFUNC("e1000_force_mac_fc");
1886
1887     /* Get the current configuration of the Device Control Register */
1888     ctrl = er32(CTRL);
1889
1890     /* Because we didn't get link via the internal auto-negotiation
1891      * mechanism (we either forced link or we got link via PHY
1892      * auto-neg), we have to manually enable/disable transmit an
1893      * receive flow control.
1894      *
1895      * The "Case" statement below enables/disable flow control
1896      * according to the "hw->fc" parameter.
1897      *
1898      * The possible values of the "fc" parameter are:
1899      *      0:  Flow control is completely disabled
1900      *      1:  Rx flow control is enabled (we can receive pause
1901      *          frames but not send pause frames).
1902      *      2:  Tx flow control is enabled (we can send pause frames
1903      *          frames but we do not receive pause frames).
1904      *      3:  Both Rx and TX flow control (symmetric) is enabled.
1905      *  other:  No other values should be possible at this point.
1906      */
1907
1908     switch (hw->fc) {
1909     case E1000_FC_NONE:
1910         ctrl &= (~(E1000_CTRL_TFCE | E1000_CTRL_RFCE));
1911         break;
1912     case E1000_FC_RX_PAUSE:
1913         ctrl &= (~E1000_CTRL_TFCE);
1914         ctrl |= E1000_CTRL_RFCE;
1915         break;
1916     case E1000_FC_TX_PAUSE:
1917         ctrl &= (~E1000_CTRL_RFCE);
1918         ctrl |= E1000_CTRL_TFCE;
1919         break;
1920     case E1000_FC_FULL:
1921         ctrl |= (E1000_CTRL_TFCE | E1000_CTRL_RFCE);
1922         break;
1923     default:
1924         DEBUGOUT("Flow control param set incorrectly\n");
1925         return -E1000_ERR_CONFIG;
1926     }
1927
1928     /* Disable TX Flow Control for 82542 (rev 2.0) */
1929     if (hw->mac_type == e1000_82542_rev2_0)
1930         ctrl &= (~E1000_CTRL_TFCE);
1931
1932     ew32(CTRL, ctrl);
1933     return E1000_SUCCESS;
1934 }
1935
1936 /******************************************************************************
1937  * Configures flow control settings after link is established
1938  *
1939  * hw - Struct containing variables accessed by shared code
1940  *
1941  * Should be called immediately after a valid link has been established.
1942  * Forces MAC flow control settings if link was forced. When in MII/GMII mode
1943  * and autonegotiation is enabled, the MAC flow control settings will be set
1944  * based on the flow control negotiated by the PHY. In TBI mode, the TFCE
1945  * and RFCE bits will be automaticaly set to the negotiated flow control mode.
1946  *****************************************************************************/
1947 static s32 e1000_config_fc_after_link_up(struct e1000_hw *hw)
1948 {
1949     s32 ret_val;
1950     u16 mii_status_reg;
1951     u16 mii_nway_adv_reg;
1952     u16 mii_nway_lp_ability_reg;
1953     u16 speed;
1954     u16 duplex;
1955
1956     DEBUGFUNC("e1000_config_fc_after_link_up");
1957
1958     /* Check for the case where we have fiber media and auto-neg failed
1959      * so we had to force link.  In this case, we need to force the
1960      * configuration of the MAC to match the "fc" parameter.
1961      */
1962     if (((hw->media_type == e1000_media_type_fiber) && (hw->autoneg_failed)) ||
1963         ((hw->media_type == e1000_media_type_internal_serdes) &&
1964          (hw->autoneg_failed)) ||
1965         ((hw->media_type == e1000_media_type_copper) && (!hw->autoneg))) {
1966         ret_val = e1000_force_mac_fc(hw);
1967         if (ret_val) {
1968             DEBUGOUT("Error forcing flow control settings\n");
1969             return ret_val;
1970         }
1971     }
1972
1973     /* Check for the case where we have copper media and auto-neg is
1974      * enabled.  In this case, we need to check and see if Auto-Neg
1975      * has completed, and if so, how the PHY and link partner has
1976      * flow control configured.
1977      */
1978     if ((hw->media_type == e1000_media_type_copper) && hw->autoneg) {
1979         /* Read the MII Status Register and check to see if AutoNeg
1980          * has completed.  We read this twice because this reg has
1981          * some "sticky" (latched) bits.
1982          */
1983         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
1984         if (ret_val)
1985             return ret_val;
1986         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
1987         if (ret_val)
1988             return ret_val;
1989
1990         if (mii_status_reg & MII_SR_AUTONEG_COMPLETE) {
1991             /* The AutoNeg process has completed, so we now need to
1992              * read both the Auto Negotiation Advertisement Register
1993              * (Address 4) and the Auto_Negotiation Base Page Ability
1994              * Register (Address 5) to determine how flow control was
1995              * negotiated.
1996              */
1997             ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_ADV,
1998                                          &mii_nway_adv_reg);
1999             if (ret_val)
2000                 return ret_val;
2001             ret_val = e1000_read_phy_reg(hw, PHY_LP_ABILITY,
2002                                          &mii_nway_lp_ability_reg);
2003             if (ret_val)
2004                 return ret_val;
2005
2006             /* Two bits in the Auto Negotiation Advertisement Register
2007              * (Address 4) and two bits in the Auto Negotiation Base
2008              * Page Ability Register (Address 5) determine flow control
2009              * for both the PHY and the link partner.  The following
2010              * table, taken out of the IEEE 802.3ab/D6.0 dated March 25,
2011              * 1999, describes these PAUSE resolution bits and how flow
2012              * control is determined based upon these settings.
2013              * NOTE:  DC = Don't Care
2014              *
2015              *   LOCAL DEVICE  |   LINK PARTNER
2016              * PAUSE | ASM_DIR | PAUSE | ASM_DIR | NIC Resolution
2017              *-------|---------|-------|---------|--------------------
2018              *   0   |    0    |  DC   |   DC    | E1000_FC_NONE
2019              *   0   |    1    |   0   |   DC    | E1000_FC_NONE
2020              *   0   |    1    |   1   |    0    | E1000_FC_NONE
2021              *   0   |    1    |   1   |    1    | E1000_FC_TX_PAUSE
2022              *   1   |    0    |   0   |   DC    | E1000_FC_NONE
2023              *   1   |   DC    |   1   |   DC    | E1000_FC_FULL
2024              *   1   |    1    |   0   |    0    | E1000_FC_NONE
2025              *   1   |    1    |   0   |    1    | E1000_FC_RX_PAUSE
2026              *
2027              */
2028             /* Are both PAUSE bits set to 1?  If so, this implies
2029              * Symmetric Flow Control is enabled at both ends.  The
2030              * ASM_DIR bits are irrelevant per the spec.
2031              *
2032              * For Symmetric Flow Control:
2033              *
2034              *   LOCAL DEVICE  |   LINK PARTNER
2035              * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
2036              *-------|---------|-------|---------|--------------------
2037              *   1   |   DC    |   1   |   DC    | E1000_FC_FULL
2038              *
2039              */
2040             if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
2041                 (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE)) {
2042                 /* Now we need to check if the user selected RX ONLY
2043                  * of pause frames.  In this case, we had to advertise
2044                  * FULL flow control because we could not advertise RX
2045                  * ONLY. Hence, we must now check to see if we need to
2046                  * turn OFF  the TRANSMISSION of PAUSE frames.
2047                  */
2048                 if (hw->original_fc == E1000_FC_FULL) {
2049                     hw->fc = E1000_FC_FULL;
2050                     DEBUGOUT("Flow Control = FULL.\n");
2051                 } else {
2052                     hw->fc = E1000_FC_RX_PAUSE;
2053                     DEBUGOUT("Flow Control = RX PAUSE frames only.\n");
2054                 }
2055             }
2056             /* For receiving PAUSE frames ONLY.
2057              *
2058              *   LOCAL DEVICE  |   LINK PARTNER
2059              * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
2060              *-------|---------|-------|---------|--------------------
2061              *   0   |    1    |   1   |    1    | E1000_FC_TX_PAUSE
2062              *
2063              */
2064             else if (!(mii_nway_adv_reg & NWAY_AR_PAUSE) &&
2065                      (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
2066                      (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
2067                      (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
2068                 hw->fc = E1000_FC_TX_PAUSE;
2069                 DEBUGOUT("Flow Control = TX PAUSE frames only.\n");
2070             }
2071             /* For transmitting PAUSE frames ONLY.
2072              *
2073              *   LOCAL DEVICE  |   LINK PARTNER
2074              * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
2075              *-------|---------|-------|---------|--------------------
2076              *   1   |    1    |   0   |    1    | E1000_FC_RX_PAUSE
2077              *
2078              */
2079             else if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
2080                      (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
2081                      !(mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
2082                      (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
2083                 hw->fc = E1000_FC_RX_PAUSE;
2084                 DEBUGOUT("Flow Control = RX PAUSE frames only.\n");
2085             }
2086             /* Per the IEEE spec, at this point flow control should be
2087              * disabled.  However, we want to consider that we could
2088              * be connected to a legacy switch that doesn't advertise
2089              * desired flow control, but can be forced on the link
2090              * partner.  So if we advertised no flow control, that is
2091              * what we will resolve to.  If we advertised some kind of
2092              * receive capability (Rx Pause Only or Full Flow Control)
2093              * and the link partner advertised none, we will configure
2094              * ourselves to enable Rx Flow Control only.  We can do
2095              * this safely for two reasons:  If the link partner really
2096              * didn't want flow control enabled, and we enable Rx, no
2097              * harm done since we won't be receiving any PAUSE frames
2098              * anyway.  If the intent on the link partner was to have
2099              * flow control enabled, then by us enabling RX only, we
2100              * can at least receive pause frames and process them.
2101              * This is a good idea because in most cases, since we are
2102              * predominantly a server NIC, more times than not we will
2103              * be asked to delay transmission of packets than asking
2104              * our link partner to pause transmission of frames.
2105              */
2106             else if ((hw->original_fc == E1000_FC_NONE ||
2107                       hw->original_fc == E1000_FC_TX_PAUSE) ||
2108                       hw->fc_strict_ieee) {
2109                 hw->fc = E1000_FC_NONE;
2110                 DEBUGOUT("Flow Control = NONE.\n");
2111             } else {
2112                 hw->fc = E1000_FC_RX_PAUSE;
2113                 DEBUGOUT("Flow Control = RX PAUSE frames only.\n");
2114             }
2115
2116             /* Now we need to do one last check...  If we auto-
2117              * negotiated to HALF DUPLEX, flow control should not be
2118              * enabled per IEEE 802.3 spec.
2119              */
2120             ret_val = e1000_get_speed_and_duplex(hw, &speed, &duplex);
2121             if (ret_val) {
2122                 DEBUGOUT("Error getting link speed and duplex\n");
2123                 return ret_val;
2124             }
2125
2126             if (duplex == HALF_DUPLEX)
2127                 hw->fc = E1000_FC_NONE;
2128
2129             /* Now we call a subroutine to actually force the MAC
2130              * controller to use the correct flow control settings.
2131              */
2132             ret_val = e1000_force_mac_fc(hw);
2133             if (ret_val) {
2134                 DEBUGOUT("Error forcing flow control settings\n");
2135                 return ret_val;
2136             }
2137         } else {
2138             DEBUGOUT("Copper PHY and Auto Neg has not completed.\n");
2139         }
2140     }
2141     return E1000_SUCCESS;
2142 }
2143
2144 /******************************************************************************
2145  * Checks to see if the link status of the hardware has changed.
2146  *
2147  * hw - Struct containing variables accessed by shared code
2148  *
2149  * Called by any function that needs to check the link status of the adapter.
2150  *****************************************************************************/
2151 s32 e1000_check_for_link(struct e1000_hw *hw)
2152 {
2153     u32 rxcw = 0;
2154     u32 ctrl;
2155     u32 status;
2156     u32 rctl;
2157     u32 icr;
2158     u32 signal = 0;
2159     s32 ret_val;
2160     u16 phy_data;
2161
2162     DEBUGFUNC("e1000_check_for_link");
2163
2164     ctrl = er32(CTRL);
2165     status = er32(STATUS);
2166
2167     /* On adapters with a MAC newer than 82544, SW Defineable pin 1 will be
2168      * set when the optics detect a signal. On older adapters, it will be
2169      * cleared when there is a signal.  This applies to fiber media only.
2170      */
2171     if ((hw->media_type == e1000_media_type_fiber) ||
2172         (hw->media_type == e1000_media_type_internal_serdes)) {
2173         rxcw = er32(RXCW);
2174
2175         if (hw->media_type == e1000_media_type_fiber) {
2176             signal = (hw->mac_type > e1000_82544) ? E1000_CTRL_SWDPIN1 : 0;
2177             if (status & E1000_STATUS_LU)
2178                 hw->get_link_status = false;
2179         }
2180     }
2181
2182     /* If we have a copper PHY then we only want to go out to the PHY
2183      * registers to see if Auto-Neg has completed and/or if our link
2184      * status has changed.  The get_link_status flag will be set if we
2185      * receive a Link Status Change interrupt or we have Rx Sequence
2186      * Errors.
2187      */
2188     if ((hw->media_type == e1000_media_type_copper) && hw->get_link_status) {
2189         /* First we want to see if the MII Status Register reports
2190          * link.  If so, then we want to get the current speed/duplex
2191          * of the PHY.
2192          * Read the register twice since the link bit is sticky.
2193          */
2194         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
2195         if (ret_val)
2196             return ret_val;
2197         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
2198         if (ret_val)
2199             return ret_val;
2200
2201         if (phy_data & MII_SR_LINK_STATUS) {
2202             hw->get_link_status = false;
2203             /* Check if there was DownShift, must be checked immediately after
2204              * link-up */
2205             e1000_check_downshift(hw);
2206
2207             /* If we are on 82544 or 82543 silicon and speed/duplex
2208              * are forced to 10H or 10F, then we will implement the polarity
2209              * reversal workaround.  We disable interrupts first, and upon
2210              * returning, place the devices interrupt state to its previous
2211              * value except for the link status change interrupt which will
2212              * happen due to the execution of this workaround.
2213              */
2214
2215             if ((hw->mac_type == e1000_82544 || hw->mac_type == e1000_82543) &&
2216                 (!hw->autoneg) &&
2217                 (hw->forced_speed_duplex == e1000_10_full ||
2218                  hw->forced_speed_duplex == e1000_10_half)) {
2219                 ew32(IMC, 0xffffffff);
2220                 ret_val = e1000_polarity_reversal_workaround(hw);
2221                 icr = er32(ICR);
2222                 ew32(ICS, (icr & ~E1000_ICS_LSC));
2223                 ew32(IMS, IMS_ENABLE_MASK);
2224             }
2225
2226         } else {
2227             /* No link detected */
2228             e1000_config_dsp_after_link_change(hw, false);
2229             return 0;
2230         }
2231
2232         /* If we are forcing speed/duplex, then we simply return since
2233          * we have already determined whether we have link or not.
2234          */
2235         if (!hw->autoneg) return -E1000_ERR_CONFIG;
2236
2237         /* optimize the dsp settings for the igp phy */
2238         e1000_config_dsp_after_link_change(hw, true);
2239
2240         /* We have a M88E1000 PHY and Auto-Neg is enabled.  If we
2241          * have Si on board that is 82544 or newer, Auto
2242          * Speed Detection takes care of MAC speed/duplex
2243          * configuration.  So we only need to configure Collision
2244          * Distance in the MAC.  Otherwise, we need to force
2245          * speed/duplex on the MAC to the current PHY speed/duplex
2246          * settings.
2247          */
2248         if (hw->mac_type >= e1000_82544)
2249             e1000_config_collision_dist(hw);
2250         else {
2251             ret_val = e1000_config_mac_to_phy(hw);
2252             if (ret_val) {
2253                 DEBUGOUT("Error configuring MAC to PHY settings\n");
2254                 return ret_val;
2255             }
2256         }
2257
2258         /* Configure Flow Control now that Auto-Neg has completed. First, we
2259          * need to restore the desired flow control settings because we may
2260          * have had to re-autoneg with a different link partner.
2261          */
2262         ret_val = e1000_config_fc_after_link_up(hw);
2263         if (ret_val) {
2264             DEBUGOUT("Error configuring flow control\n");
2265             return ret_val;
2266         }
2267
2268         /* At this point we know that we are on copper and we have
2269          * auto-negotiated link.  These are conditions for checking the link
2270          * partner capability register.  We use the link speed to determine if
2271          * TBI compatibility needs to be turned on or off.  If the link is not
2272          * at gigabit speed, then TBI compatibility is not needed.  If we are
2273          * at gigabit speed, we turn on TBI compatibility.
2274          */
2275         if (hw->tbi_compatibility_en) {
2276             u16 speed, duplex;
2277             ret_val = e1000_get_speed_and_duplex(hw, &speed, &duplex);
2278             if (ret_val) {
2279                 DEBUGOUT("Error getting link speed and duplex\n");
2280                 return ret_val;
2281             }
2282             if (speed != SPEED_1000) {
2283                 /* If link speed is not set to gigabit speed, we do not need
2284                  * to enable TBI compatibility.
2285                  */
2286                 if (hw->tbi_compatibility_on) {
2287                     /* If we previously were in the mode, turn it off. */
2288                     rctl = er32(RCTL);
2289                     rctl &= ~E1000_RCTL_SBP;
2290                     ew32(RCTL, rctl);
2291                     hw->tbi_compatibility_on = false;
2292                 }
2293             } else {
2294                 /* If TBI compatibility is was previously off, turn it on. For
2295                  * compatibility with a TBI link partner, we will store bad
2296                  * packets. Some frames have an additional byte on the end and
2297                  * will look like CRC errors to the hardware.
2298                  */
2299                 if (!hw->tbi_compatibility_on) {
2300                     hw->tbi_compatibility_on = true;
2301                     rctl = er32(RCTL);
2302                     rctl |= E1000_RCTL_SBP;
2303                     ew32(RCTL, rctl);
2304                 }
2305             }
2306         }
2307     }
2308     /* If we don't have link (auto-negotiation failed or link partner cannot
2309      * auto-negotiate), the cable is plugged in (we have signal), and our
2310      * link partner is not trying to auto-negotiate with us (we are receiving
2311      * idles or data), we need to force link up. We also need to give
2312      * auto-negotiation time to complete, in case the cable was just plugged
2313      * in. The autoneg_failed flag does this.
2314      */
2315     else if ((((hw->media_type == e1000_media_type_fiber) &&
2316               ((ctrl & E1000_CTRL_SWDPIN1) == signal)) ||
2317               (hw->media_type == e1000_media_type_internal_serdes)) &&
2318               (!(status & E1000_STATUS_LU)) &&
2319               (!(rxcw & E1000_RXCW_C))) {
2320         if (hw->autoneg_failed == 0) {
2321             hw->autoneg_failed = 1;
2322             return 0;
2323         }
2324         DEBUGOUT("NOT RXing /C/, disable AutoNeg and force link.\n");
2325
2326         /* Disable auto-negotiation in the TXCW register */
2327         ew32(TXCW, (hw->txcw & ~E1000_TXCW_ANE));
2328
2329         /* Force link-up and also force full-duplex. */
2330         ctrl = er32(CTRL);
2331         ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FD);
2332         ew32(CTRL, ctrl);
2333
2334         /* Configure Flow Control after forcing link up. */
2335         ret_val = e1000_config_fc_after_link_up(hw);
2336         if (ret_val) {
2337             DEBUGOUT("Error configuring flow control\n");
2338             return ret_val;
2339         }
2340     }
2341     /* If we are forcing link and we are receiving /C/ ordered sets, re-enable
2342      * auto-negotiation in the TXCW register and disable forced link in the
2343      * Device Control register in an attempt to auto-negotiate with our link
2344      * partner.
2345      */
2346     else if (((hw->media_type == e1000_media_type_fiber) ||
2347               (hw->media_type == e1000_media_type_internal_serdes)) &&
2348               (ctrl & E1000_CTRL_SLU) && (rxcw & E1000_RXCW_C)) {
2349         DEBUGOUT("RXing /C/, enable AutoNeg and stop forcing link.\n");
2350         ew32(TXCW, hw->txcw);
2351         ew32(CTRL, (ctrl & ~E1000_CTRL_SLU));
2352
2353         hw->serdes_link_down = false;
2354     }
2355     /* If we force link for non-auto-negotiation switch, check link status
2356      * based on MAC synchronization for internal serdes media type.
2357      */
2358     else if ((hw->media_type == e1000_media_type_internal_serdes) &&
2359              !(E1000_TXCW_ANE & er32(TXCW))) {
2360         /* SYNCH bit and IV bit are sticky. */
2361         udelay(10);
2362         if (E1000_RXCW_SYNCH & er32(RXCW)) {
2363             if (!(rxcw & E1000_RXCW_IV)) {
2364                 hw->serdes_link_down = false;
2365                 DEBUGOUT("SERDES: Link is up.\n");
2366             }
2367         } else {
2368             hw->serdes_link_down = true;
2369             DEBUGOUT("SERDES: Link is down.\n");
2370         }
2371     }
2372     if ((hw->media_type == e1000_media_type_internal_serdes) &&
2373         (E1000_TXCW_ANE & er32(TXCW))) {
2374         hw->serdes_link_down = !(E1000_STATUS_LU & er32(STATUS));
2375     }
2376     return E1000_SUCCESS;
2377 }
2378
2379 /******************************************************************************
2380  * Detects the current speed and duplex settings of the hardware.
2381  *
2382  * hw - Struct containing variables accessed by shared code
2383  * speed - Speed of the connection
2384  * duplex - Duplex setting of the connection
2385  *****************************************************************************/
2386 s32 e1000_get_speed_and_duplex(struct e1000_hw *hw, u16 *speed, u16 *duplex)
2387 {
2388     u32 status;
2389     s32 ret_val;
2390     u16 phy_data;
2391
2392     DEBUGFUNC("e1000_get_speed_and_duplex");
2393
2394     if (hw->mac_type >= e1000_82543) {
2395         status = er32(STATUS);
2396         if (status & E1000_STATUS_SPEED_1000) {
2397             *speed = SPEED_1000;
2398             DEBUGOUT("1000 Mbs, ");
2399         } else if (status & E1000_STATUS_SPEED_100) {
2400             *speed = SPEED_100;
2401             DEBUGOUT("100 Mbs, ");
2402         } else {
2403             *speed = SPEED_10;
2404             DEBUGOUT("10 Mbs, ");
2405         }
2406
2407         if (status & E1000_STATUS_FD) {
2408             *duplex = FULL_DUPLEX;
2409             DEBUGOUT("Full Duplex\n");
2410         } else {
2411             *duplex = HALF_DUPLEX;
2412             DEBUGOUT(" Half Duplex\n");
2413         }
2414     } else {
2415         DEBUGOUT("1000 Mbs, Full Duplex\n");
2416         *speed = SPEED_1000;
2417         *duplex = FULL_DUPLEX;
2418     }
2419
2420     /* IGP01 PHY may advertise full duplex operation after speed downgrade even
2421      * if it is operating at half duplex.  Here we set the duplex settings to
2422      * match the duplex in the link partner's capabilities.
2423      */
2424     if (hw->phy_type == e1000_phy_igp && hw->speed_downgraded) {
2425         ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_EXP, &phy_data);
2426         if (ret_val)
2427             return ret_val;
2428
2429         if (!(phy_data & NWAY_ER_LP_NWAY_CAPS))
2430             *duplex = HALF_DUPLEX;
2431         else {
2432             ret_val = e1000_read_phy_reg(hw, PHY_LP_ABILITY, &phy_data);
2433             if (ret_val)
2434                 return ret_val;
2435             if ((*speed == SPEED_100 && !(phy_data & NWAY_LPAR_100TX_FD_CAPS)) ||
2436                (*speed == SPEED_10 && !(phy_data & NWAY_LPAR_10T_FD_CAPS)))
2437                 *duplex = HALF_DUPLEX;
2438         }
2439     }
2440
2441     return E1000_SUCCESS;
2442 }
2443
2444 /******************************************************************************
2445 * Blocks until autoneg completes or times out (~4.5 seconds)
2446 *
2447 * hw - Struct containing variables accessed by shared code
2448 ******************************************************************************/
2449 static s32 e1000_wait_autoneg(struct e1000_hw *hw)
2450 {
2451     s32 ret_val;
2452     u16 i;
2453     u16 phy_data;
2454
2455     DEBUGFUNC("e1000_wait_autoneg");
2456     DEBUGOUT("Waiting for Auto-Neg to complete.\n");
2457
2458     /* We will wait for autoneg to complete or 4.5 seconds to expire. */
2459     for (i = PHY_AUTO_NEG_TIME; i > 0; i--) {
2460         /* Read the MII Status Register and wait for Auto-Neg
2461          * Complete bit to be set.
2462          */
2463         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
2464         if (ret_val)
2465             return ret_val;
2466         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
2467         if (ret_val)
2468             return ret_val;
2469         if (phy_data & MII_SR_AUTONEG_COMPLETE) {
2470             return E1000_SUCCESS;
2471         }
2472         msleep(100);
2473     }
2474     return E1000_SUCCESS;
2475 }
2476
2477 /******************************************************************************
2478 * Raises the Management Data Clock
2479 *
2480 * hw - Struct containing variables accessed by shared code
2481 * ctrl - Device control register's current value
2482 ******************************************************************************/
2483 static void e1000_raise_mdi_clk(struct e1000_hw *hw, u32 *ctrl)
2484 {
2485     /* Raise the clock input to the Management Data Clock (by setting the MDC
2486      * bit), and then delay 10 microseconds.
2487      */
2488     ew32(CTRL, (*ctrl | E1000_CTRL_MDC));
2489     E1000_WRITE_FLUSH();
2490     udelay(10);
2491 }
2492
2493 /******************************************************************************
2494 * Lowers the Management Data Clock
2495 *
2496 * hw - Struct containing variables accessed by shared code
2497 * ctrl - Device control register's current value
2498 ******************************************************************************/
2499 static void e1000_lower_mdi_clk(struct e1000_hw *hw, u32 *ctrl)
2500 {
2501     /* Lower the clock input to the Management Data Clock (by clearing the MDC
2502      * bit), and then delay 10 microseconds.
2503      */
2504     ew32(CTRL, (*ctrl & ~E1000_CTRL_MDC));
2505     E1000_WRITE_FLUSH();
2506     udelay(10);
2507 }
2508
2509 /******************************************************************************
2510 * Shifts data bits out to the PHY
2511 *
2512 * hw - Struct containing variables accessed by shared code
2513 * data - Data to send out to the PHY
2514 * count - Number of bits to shift out
2515 *
2516 * Bits are shifted out in MSB to LSB order.
2517 ******************************************************************************/
2518 static void e1000_shift_out_mdi_bits(struct e1000_hw *hw, u32 data, u16 count)
2519 {
2520     u32 ctrl;
2521     u32 mask;
2522
2523     /* We need to shift "count" number of bits out to the PHY. So, the value
2524      * in the "data" parameter will be shifted out to the PHY one bit at a
2525      * time. In order to do this, "data" must be broken down into bits.
2526      */
2527     mask = 0x01;
2528     mask <<= (count - 1);
2529
2530     ctrl = er32(CTRL);
2531
2532     /* Set MDIO_DIR and MDC_DIR direction bits to be used as output pins. */
2533     ctrl |= (E1000_CTRL_MDIO_DIR | E1000_CTRL_MDC_DIR);
2534
2535     while (mask) {
2536         /* A "1" is shifted out to the PHY by setting the MDIO bit to "1" and
2537          * then raising and lowering the Management Data Clock. A "0" is
2538          * shifted out to the PHY by setting the MDIO bit to "0" and then
2539          * raising and lowering the clock.
2540          */
2541         if (data & mask)
2542             ctrl |= E1000_CTRL_MDIO;
2543         else
2544             ctrl &= ~E1000_CTRL_MDIO;
2545
2546         ew32(CTRL, ctrl);
2547         E1000_WRITE_FLUSH();
2548
2549         udelay(10);
2550
2551         e1000_raise_mdi_clk(hw, &ctrl);
2552         e1000_lower_mdi_clk(hw, &ctrl);
2553
2554         mask = mask >> 1;
2555     }
2556 }
2557
2558 /******************************************************************************
2559 * Shifts data bits in from the PHY
2560 *
2561 * hw - Struct containing variables accessed by shared code
2562 *
2563 * Bits are shifted in in MSB to LSB order.
2564 ******************************************************************************/
2565 static u16 e1000_shift_in_mdi_bits(struct e1000_hw *hw)
2566 {
2567     u32 ctrl;
2568     u16 data = 0;
2569     u8 i;
2570
2571     /* In order to read a register from the PHY, we need to shift in a total
2572      * of 18 bits from the PHY. The first two bit (turnaround) times are used
2573      * to avoid contention on the MDIO pin when a read operation is performed.
2574      * These two bits are ignored by us and thrown away. Bits are "shifted in"
2575      * by raising the input to the Management Data Clock (setting the MDC bit),
2576      * and then reading the value of the MDIO bit.
2577      */
2578     ctrl = er32(CTRL);
2579
2580     /* Clear MDIO_DIR (SWDPIO1) to indicate this bit is to be used as input. */
2581     ctrl &= ~E1000_CTRL_MDIO_DIR;
2582     ctrl &= ~E1000_CTRL_MDIO;
2583
2584     ew32(CTRL, ctrl);
2585     E1000_WRITE_FLUSH();
2586
2587     /* Raise and Lower the clock before reading in the data. This accounts for
2588      * the turnaround bits. The first clock occurred when we clocked out the
2589      * last bit of the Register Address.
2590      */
2591     e1000_raise_mdi_clk(hw, &ctrl);
2592     e1000_lower_mdi_clk(hw, &ctrl);
2593
2594     for (data = 0, i = 0; i < 16; i++) {
2595         data = data << 1;
2596         e1000_raise_mdi_clk(hw, &ctrl);
2597         ctrl = er32(CTRL);
2598         /* Check to see if we shifted in a "1". */
2599         if (ctrl & E1000_CTRL_MDIO)
2600             data |= 1;
2601         e1000_lower_mdi_clk(hw, &ctrl);
2602     }
2603
2604     e1000_raise_mdi_clk(hw, &ctrl);
2605     e1000_lower_mdi_clk(hw, &ctrl);
2606
2607     return data;
2608 }
2609
2610 static s32 e1000_swfw_sync_acquire(struct e1000_hw *hw, u16 mask)
2611 {
2612     u32 swfw_sync = 0;
2613     u32 swmask = mask;
2614     u32 fwmask = mask << 16;
2615     s32 timeout = 200;
2616
2617     DEBUGFUNC("e1000_swfw_sync_acquire");
2618
2619     if (!hw->swfw_sync_present)
2620         return e1000_get_hw_eeprom_semaphore(hw);
2621
2622     while (timeout) {
2623             if (e1000_get_hw_eeprom_semaphore(hw))
2624                 return -E1000_ERR_SWFW_SYNC;
2625
2626             swfw_sync = er32(SW_FW_SYNC);
2627             if (!(swfw_sync & (fwmask | swmask))) {
2628                 break;
2629             }
2630
2631             /* firmware currently using resource (fwmask) */
2632             /* or other software thread currently using resource (swmask) */
2633             e1000_put_hw_eeprom_semaphore(hw);
2634             mdelay(5);
2635             timeout--;
2636     }
2637
2638     if (!timeout) {
2639         DEBUGOUT("Driver can't access resource, SW_FW_SYNC timeout.\n");
2640         return -E1000_ERR_SWFW_SYNC;
2641     }
2642
2643     swfw_sync |= swmask;
2644     ew32(SW_FW_SYNC, swfw_sync);
2645
2646     e1000_put_hw_eeprom_semaphore(hw);
2647     return E1000_SUCCESS;
2648 }
2649
2650 static void e1000_swfw_sync_release(struct e1000_hw *hw, u16 mask)
2651 {
2652     u32 swfw_sync;
2653     u32 swmask = mask;
2654
2655     DEBUGFUNC("e1000_swfw_sync_release");
2656
2657     if (!hw->swfw_sync_present) {
2658         e1000_put_hw_eeprom_semaphore(hw);
2659         return;
2660     }
2661
2662     /* if (e1000_get_hw_eeprom_semaphore(hw))
2663      *    return -E1000_ERR_SWFW_SYNC; */
2664     while (e1000_get_hw_eeprom_semaphore(hw) != E1000_SUCCESS);
2665         /* empty */
2666
2667     swfw_sync = er32(SW_FW_SYNC);
2668     swfw_sync &= ~swmask;
2669     ew32(SW_FW_SYNC, swfw_sync);
2670
2671     e1000_put_hw_eeprom_semaphore(hw);
2672 }
2673
2674 /*****************************************************************************
2675 * Reads the value from a PHY register, if the value is on a specific non zero
2676 * page, sets the page first.
2677 * hw - Struct containing variables accessed by shared code
2678 * reg_addr - address of the PHY register to read
2679 ******************************************************************************/
2680 s32 e1000_read_phy_reg(struct e1000_hw *hw, u32 reg_addr, u16 *phy_data)
2681 {
2682     u32 ret_val;
2683     u16 swfw;
2684
2685     DEBUGFUNC("e1000_read_phy_reg");
2686
2687     swfw = E1000_SWFW_PHY0_SM;
2688     if (e1000_swfw_sync_acquire(hw, swfw))
2689         return -E1000_ERR_SWFW_SYNC;
2690
2691     if ((hw->phy_type == e1000_phy_igp) &&
2692         (reg_addr > MAX_PHY_MULTI_PAGE_REG)) {
2693         ret_val = e1000_write_phy_reg_ex(hw, IGP01E1000_PHY_PAGE_SELECT,
2694                                          (u16)reg_addr);
2695         if (ret_val) {
2696             e1000_swfw_sync_release(hw, swfw);
2697             return ret_val;
2698         }
2699     }
2700
2701     ret_val = e1000_read_phy_reg_ex(hw, MAX_PHY_REG_ADDRESS & reg_addr,
2702                                     phy_data);
2703
2704     e1000_swfw_sync_release(hw, swfw);
2705     return ret_val;
2706 }
2707
2708 static s32 e1000_read_phy_reg_ex(struct e1000_hw *hw, u32 reg_addr,
2709                                  u16 *phy_data)
2710 {
2711     u32 i;
2712     u32 mdic = 0;
2713     const u32 phy_addr = 1;
2714
2715     DEBUGFUNC("e1000_read_phy_reg_ex");
2716
2717     if (reg_addr > MAX_PHY_REG_ADDRESS) {
2718         DEBUGOUT1("PHY Address %d is out of range\n", reg_addr);
2719         return -E1000_ERR_PARAM;
2720     }
2721
2722     if (hw->mac_type > e1000_82543) {
2723         /* Set up Op-code, Phy Address, and register address in the MDI
2724          * Control register.  The MAC will take care of interfacing with the
2725          * PHY to retrieve the desired data.
2726          */
2727         mdic = ((reg_addr << E1000_MDIC_REG_SHIFT) |
2728                 (phy_addr << E1000_MDIC_PHY_SHIFT) |
2729                 (E1000_MDIC_OP_READ));
2730
2731         ew32(MDIC, mdic);
2732
2733         /* Poll the ready bit to see if the MDI read completed */
2734         for (i = 0; i < 64; i++) {
2735             udelay(50);
2736             mdic = er32(MDIC);
2737             if (mdic & E1000_MDIC_READY) break;
2738         }
2739         if (!(mdic & E1000_MDIC_READY)) {
2740             DEBUGOUT("MDI Read did not complete\n");
2741             return -E1000_ERR_PHY;
2742         }
2743         if (mdic & E1000_MDIC_ERROR) {
2744             DEBUGOUT("MDI Error\n");
2745             return -E1000_ERR_PHY;
2746         }
2747         *phy_data = (u16)mdic;
2748     } else {
2749         /* We must first send a preamble through the MDIO pin to signal the
2750          * beginning of an MII instruction.  This is done by sending 32
2751          * consecutive "1" bits.
2752          */
2753         e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
2754
2755         /* Now combine the next few fields that are required for a read
2756          * operation.  We use this method instead of calling the
2757          * e1000_shift_out_mdi_bits routine five different times. The format of
2758          * a MII read instruction consists of a shift out of 14 bits and is
2759          * defined as follows:
2760          *    <Preamble><SOF><Op Code><Phy Addr><Reg Addr>
2761          * followed by a shift in of 18 bits.  This first two bits shifted in
2762          * are TurnAround bits used to avoid contention on the MDIO pin when a
2763          * READ operation is performed.  These two bits are thrown away
2764          * followed by a shift in of 16 bits which contains the desired data.
2765          */
2766         mdic = ((reg_addr) | (phy_addr << 5) |
2767                 (PHY_OP_READ << 10) | (PHY_SOF << 12));
2768
2769         e1000_shift_out_mdi_bits(hw, mdic, 14);
2770
2771         /* Now that we've shifted out the read command to the MII, we need to
2772          * "shift in" the 16-bit value (18 total bits) of the requested PHY
2773          * register address.
2774          */
2775         *phy_data = e1000_shift_in_mdi_bits(hw);
2776     }
2777     return E1000_SUCCESS;
2778 }
2779
2780 /******************************************************************************
2781 * Writes a value to a PHY register
2782 *
2783 * hw - Struct containing variables accessed by shared code
2784 * reg_addr - address of the PHY register to write
2785 * data - data to write to the PHY
2786 ******************************************************************************/
2787 s32 e1000_write_phy_reg(struct e1000_hw *hw, u32 reg_addr, u16 phy_data)
2788 {
2789     u32 ret_val;
2790     u16 swfw;
2791
2792     DEBUGFUNC("e1000_write_phy_reg");
2793
2794     swfw = E1000_SWFW_PHY0_SM;
2795     if (e1000_swfw_sync_acquire(hw, swfw))
2796         return -E1000_ERR_SWFW_SYNC;
2797
2798     if ((hw->phy_type == e1000_phy_igp) &&
2799         (reg_addr > MAX_PHY_MULTI_PAGE_REG)) {
2800         ret_val = e1000_write_phy_reg_ex(hw, IGP01E1000_PHY_PAGE_SELECT,
2801                                          (u16)reg_addr);
2802         if (ret_val) {
2803             e1000_swfw_sync_release(hw, swfw);
2804             return ret_val;
2805         }
2806     }
2807
2808     ret_val = e1000_write_phy_reg_ex(hw, MAX_PHY_REG_ADDRESS & reg_addr,
2809                                      phy_data);
2810
2811     e1000_swfw_sync_release(hw, swfw);
2812     return ret_val;
2813 }
2814
2815 static s32 e1000_write_phy_reg_ex(struct e1000_hw *hw, u32 reg_addr,
2816                                   u16 phy_data)
2817 {
2818     u32 i;
2819     u32 mdic = 0;
2820     const u32 phy_addr = 1;
2821
2822     DEBUGFUNC("e1000_write_phy_reg_ex");
2823
2824     if (reg_addr > MAX_PHY_REG_ADDRESS) {
2825         DEBUGOUT1("PHY Address %d is out of range\n", reg_addr);
2826         return -E1000_ERR_PARAM;
2827     }
2828
2829     if (hw->mac_type > e1000_82543) {
2830         /* Set up Op-code, Phy Address, register address, and data intended
2831          * for the PHY register in the MDI Control register.  The MAC will take
2832          * care of interfacing with the PHY to send the desired data.
2833          */
2834         mdic = (((u32)phy_data) |
2835                 (reg_addr << E1000_MDIC_REG_SHIFT) |
2836                 (phy_addr << E1000_MDIC_PHY_SHIFT) |
2837                 (E1000_MDIC_OP_WRITE));
2838
2839         ew32(MDIC, mdic);
2840
2841         /* Poll the ready bit to see if the MDI read completed */
2842         for (i = 0; i < 641; i++) {
2843             udelay(5);
2844             mdic = er32(MDIC);
2845             if (mdic & E1000_MDIC_READY) break;
2846         }
2847         if (!(mdic & E1000_MDIC_READY)) {
2848             DEBUGOUT("MDI Write did not complete\n");
2849             return -E1000_ERR_PHY;
2850         }
2851     } else {
2852         /* We'll need to use the SW defined pins to shift the write command
2853          * out to the PHY. We first send a preamble to the PHY to signal the
2854          * beginning of the MII instruction.  This is done by sending 32
2855          * consecutive "1" bits.
2856          */
2857         e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
2858
2859         /* Now combine the remaining required fields that will indicate a
2860          * write operation. We use this method instead of calling the
2861          * e1000_shift_out_mdi_bits routine for each field in the command. The
2862          * format of a MII write instruction is as follows:
2863          * <Preamble><SOF><Op Code><Phy Addr><Reg Addr><Turnaround><Data>.
2864          */
2865         mdic = ((PHY_TURNAROUND) | (reg_addr << 2) | (phy_addr << 7) |
2866                 (PHY_OP_WRITE << 12) | (PHY_SOF << 14));
2867         mdic <<= 16;
2868         mdic |= (u32)phy_data;
2869
2870         e1000_shift_out_mdi_bits(hw, mdic, 32);
2871     }
2872
2873     return E1000_SUCCESS;
2874 }
2875
2876 /******************************************************************************
2877 * Returns the PHY to the power-on reset state
2878 *
2879 * hw - Struct containing variables accessed by shared code
2880 ******************************************************************************/
2881 s32 e1000_phy_hw_reset(struct e1000_hw *hw)
2882 {
2883     u32 ctrl, ctrl_ext;
2884     u32 led_ctrl;
2885     s32 ret_val;
2886     u16 swfw;
2887
2888     DEBUGFUNC("e1000_phy_hw_reset");
2889
2890     DEBUGOUT("Resetting Phy...\n");
2891
2892     if (hw->mac_type > e1000_82543) {
2893         swfw = E1000_SWFW_PHY0_SM;
2894         if (e1000_swfw_sync_acquire(hw, swfw)) {
2895             DEBUGOUT("Unable to acquire swfw sync\n");
2896             return -E1000_ERR_SWFW_SYNC;
2897         }
2898         /* Read the device control register and assert the E1000_CTRL_PHY_RST
2899          * bit. Then, take it out of reset.
2900          * For e1000 hardware, we delay for 10ms between the assert
2901          * and deassert.
2902          */
2903         ctrl = er32(CTRL);
2904         ew32(CTRL, ctrl | E1000_CTRL_PHY_RST);
2905         E1000_WRITE_FLUSH();
2906
2907         msleep(10);
2908
2909         ew32(CTRL, ctrl);
2910         E1000_WRITE_FLUSH();
2911
2912         e1000_swfw_sync_release(hw, swfw);
2913     } else {
2914         /* Read the Extended Device Control Register, assert the PHY_RESET_DIR
2915          * bit to put the PHY into reset. Then, take it out of reset.
2916          */
2917         ctrl_ext = er32(CTRL_EXT);
2918         ctrl_ext |= E1000_CTRL_EXT_SDP4_DIR;
2919         ctrl_ext &= ~E1000_CTRL_EXT_SDP4_DATA;
2920         ew32(CTRL_EXT, ctrl_ext);
2921         E1000_WRITE_FLUSH();
2922         msleep(10);
2923         ctrl_ext |= E1000_CTRL_EXT_SDP4_DATA;
2924         ew32(CTRL_EXT, ctrl_ext);
2925         E1000_WRITE_FLUSH();
2926     }
2927     udelay(150);
2928
2929     if ((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
2930         /* Configure activity LED after PHY reset */
2931         led_ctrl = er32(LEDCTL);
2932         led_ctrl &= IGP_ACTIVITY_LED_MASK;
2933         led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
2934         ew32(LEDCTL, led_ctrl);
2935     }
2936
2937     /* Wait for FW to finish PHY configuration. */
2938     ret_val = e1000_get_phy_cfg_done(hw);
2939     if (ret_val != E1000_SUCCESS)
2940         return ret_val;
2941
2942     return ret_val;
2943 }
2944
2945 /******************************************************************************
2946 * Resets the PHY
2947 *
2948 * hw - Struct containing variables accessed by shared code
2949 *
2950 * Sets bit 15 of the MII Control register
2951 ******************************************************************************/
2952 s32 e1000_phy_reset(struct e1000_hw *hw)
2953 {
2954     s32 ret_val;
2955     u16 phy_data;
2956
2957     DEBUGFUNC("e1000_phy_reset");
2958
2959     switch (hw->phy_type) {
2960     case e1000_phy_igp:
2961         ret_val = e1000_phy_hw_reset(hw);
2962         if (ret_val)
2963             return ret_val;
2964         break;
2965     default:
2966         ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
2967         if (ret_val)
2968             return ret_val;
2969
2970         phy_data |= MII_CR_RESET;
2971         ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
2972         if (ret_val)
2973             return ret_val;
2974
2975         udelay(1);
2976         break;
2977     }
2978
2979     if (hw->phy_type == e1000_phy_igp)
2980         e1000_phy_init_script(hw);
2981
2982     return E1000_SUCCESS;
2983 }
2984
2985 /******************************************************************************
2986 * Probes the expected PHY address for known PHY IDs
2987 *
2988 * hw - Struct containing variables accessed by shared code
2989 ******************************************************************************/
2990 static s32 e1000_detect_gig_phy(struct e1000_hw *hw)
2991 {
2992     s32 phy_init_status, ret_val;
2993     u16 phy_id_high, phy_id_low;
2994     bool match = false;
2995
2996     DEBUGFUNC("e1000_detect_gig_phy");
2997
2998     if (hw->phy_id != 0)
2999         return E1000_SUCCESS;
3000
3001     /* Read the PHY ID Registers to identify which PHY is onboard. */
3002     ret_val = e1000_read_phy_reg(hw, PHY_ID1, &phy_id_high);
3003     if (ret_val)
3004         return ret_val;
3005
3006     hw->phy_id = (u32)(phy_id_high << 16);
3007     udelay(20);
3008     ret_val = e1000_read_phy_reg(hw, PHY_ID2, &phy_id_low);
3009     if (ret_val)
3010         return ret_val;
3011
3012     hw->phy_id |= (u32)(phy_id_low & PHY_REVISION_MASK);
3013     hw->phy_revision = (u32)phy_id_low & ~PHY_REVISION_MASK;
3014
3015     switch (hw->mac_type) {
3016     case e1000_82543:
3017         if (hw->phy_id == M88E1000_E_PHY_ID) match = true;
3018         break;
3019     case e1000_82544:
3020         if (hw->phy_id == M88E1000_I_PHY_ID) match = true;
3021         break;
3022     case e1000_82540:
3023     case e1000_82545:
3024     case e1000_82545_rev_3:
3025     case e1000_82546:
3026     case e1000_82546_rev_3:
3027         if (hw->phy_id == M88E1011_I_PHY_ID) match = true;
3028         break;
3029     case e1000_82541:
3030     case e1000_82541_rev_2:
3031     case e1000_82547:
3032     case e1000_82547_rev_2:
3033         if (hw->phy_id == IGP01E1000_I_PHY_ID) match = true;
3034         break;
3035     default:
3036         DEBUGOUT1("Invalid MAC type %d\n", hw->mac_type);
3037         return -E1000_ERR_CONFIG;
3038     }
3039     phy_init_status = e1000_set_phy_type(hw);
3040
3041     if ((match) && (phy_init_status == E1000_SUCCESS)) {
3042         DEBUGOUT1("PHY ID 0x%X detected\n", hw->phy_id);
3043         return E1000_SUCCESS;
3044     }
3045     DEBUGOUT1("Invalid PHY ID 0x%X\n", hw->phy_id);
3046     return -E1000_ERR_PHY;
3047 }
3048
3049 /******************************************************************************
3050 * Resets the PHY's DSP
3051 *
3052 * hw - Struct containing variables accessed by shared code
3053 ******************************************************************************/
3054 static s32 e1000_phy_reset_dsp(struct e1000_hw *hw)
3055 {
3056     s32 ret_val;
3057     DEBUGFUNC("e1000_phy_reset_dsp");
3058
3059     do {
3060         ret_val = e1000_write_phy_reg(hw, 29, 0x001d);
3061         if (ret_val) break;
3062         ret_val = e1000_write_phy_reg(hw, 30, 0x00c1);
3063         if (ret_val) break;
3064         ret_val = e1000_write_phy_reg(hw, 30, 0x0000);
3065         if (ret_val) break;
3066         ret_val = E1000_SUCCESS;
3067     } while (0);
3068
3069     return ret_val;
3070 }
3071
3072 /******************************************************************************
3073 * Get PHY information from various PHY registers for igp PHY only.
3074 *
3075 * hw - Struct containing variables accessed by shared code
3076 * phy_info - PHY information structure
3077 ******************************************************************************/
3078 static s32 e1000_phy_igp_get_info(struct e1000_hw *hw,
3079                                   struct e1000_phy_info *phy_info)
3080 {
3081     s32 ret_val;
3082     u16 phy_data, min_length, max_length, average;
3083     e1000_rev_polarity polarity;
3084
3085     DEBUGFUNC("e1000_phy_igp_get_info");
3086
3087     /* The downshift status is checked only once, after link is established,
3088      * and it stored in the hw->speed_downgraded parameter. */
3089     phy_info->downshift = (e1000_downshift)hw->speed_downgraded;
3090
3091     /* IGP01E1000 does not need to support it. */
3092     phy_info->extended_10bt_distance = e1000_10bt_ext_dist_enable_normal;
3093
3094     /* IGP01E1000 always correct polarity reversal */
3095     phy_info->polarity_correction = e1000_polarity_reversal_enabled;
3096
3097     /* Check polarity status */
3098     ret_val = e1000_check_polarity(hw, &polarity);
3099     if (ret_val)
3100         return ret_val;
3101
3102     phy_info->cable_polarity = polarity;
3103
3104     ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_STATUS, &phy_data);
3105     if (ret_val)
3106         return ret_val;
3107
3108     phy_info->mdix_mode = (e1000_auto_x_mode)((phy_data & IGP01E1000_PSSR_MDIX) >>
3109                           IGP01E1000_PSSR_MDIX_SHIFT);
3110
3111     if ((phy_data & IGP01E1000_PSSR_SPEED_MASK) ==
3112        IGP01E1000_PSSR_SPEED_1000MBPS) {
3113         /* Local/Remote Receiver Information are only valid at 1000 Mbps */
3114         ret_val = e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_data);
3115         if (ret_val)
3116             return ret_val;
3117
3118         phy_info->local_rx = ((phy_data & SR_1000T_LOCAL_RX_STATUS) >>
3119                              SR_1000T_LOCAL_RX_STATUS_SHIFT) ?
3120                              e1000_1000t_rx_status_ok : e1000_1000t_rx_status_not_ok;
3121         phy_info->remote_rx = ((phy_data & SR_1000T_REMOTE_RX_STATUS) >>
3122                               SR_1000T_REMOTE_RX_STATUS_SHIFT) ?
3123                               e1000_1000t_rx_status_ok : e1000_1000t_rx_status_not_ok;
3124
3125         /* Get cable length */
3126         ret_val = e1000_get_cable_length(hw, &min_length, &max_length);
3127         if (ret_val)
3128             return ret_val;
3129
3130         /* Translate to old method */
3131         average = (max_length + min_length) / 2;
3132
3133         if (average <= e1000_igp_cable_length_50)
3134             phy_info->cable_length = e1000_cable_length_50;
3135         else if (average <= e1000_igp_cable_length_80)
3136             phy_info->cable_length = e1000_cable_length_50_80;
3137         else if (average <= e1000_igp_cable_length_110)
3138             phy_info->cable_length = e1000_cable_length_80_110;
3139         else if (average <= e1000_igp_cable_length_140)
3140             phy_info->cable_length = e1000_cable_length_110_140;
3141         else
3142             phy_info->cable_length = e1000_cable_length_140;
3143     }
3144
3145     return E1000_SUCCESS;
3146 }
3147
3148
3149 /******************************************************************************
3150 * Get PHY information from various PHY registers fot m88 PHY only.
3151 *
3152 * hw - Struct containing variables accessed by shared code
3153 * phy_info - PHY information structure
3154 ******************************************************************************/
3155 static s32 e1000_phy_m88_get_info(struct e1000_hw *hw,
3156                                   struct e1000_phy_info *phy_info)
3157 {
3158     s32 ret_val;
3159     u16 phy_data;
3160     e1000_rev_polarity polarity;
3161
3162     DEBUGFUNC("e1000_phy_m88_get_info");
3163
3164     /* The downshift status is checked only once, after link is established,
3165      * and it stored in the hw->speed_downgraded parameter. */
3166     phy_info->downshift = (e1000_downshift)hw->speed_downgraded;
3167
3168     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
3169     if (ret_val)
3170         return ret_val;
3171
3172     phy_info->extended_10bt_distance =
3173         ((phy_data & M88E1000_PSCR_10BT_EXT_DIST_ENABLE) >>
3174         M88E1000_PSCR_10BT_EXT_DIST_ENABLE_SHIFT) ?
3175         e1000_10bt_ext_dist_enable_lower : e1000_10bt_ext_dist_enable_normal;
3176
3177     phy_info->polarity_correction =
3178         ((phy_data & M88E1000_PSCR_POLARITY_REVERSAL) >>
3179         M88E1000_PSCR_POLARITY_REVERSAL_SHIFT) ?
3180         e1000_polarity_reversal_disabled : e1000_polarity_reversal_enabled;
3181
3182     /* Check polarity status */
3183     ret_val = e1000_check_polarity(hw, &polarity);
3184     if (ret_val)
3185         return ret_val;
3186     phy_info->cable_polarity = polarity;
3187
3188     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data);
3189     if (ret_val)
3190         return ret_val;
3191
3192     phy_info->mdix_mode = (e1000_auto_x_mode)((phy_data & M88E1000_PSSR_MDIX) >>
3193                           M88E1000_PSSR_MDIX_SHIFT);
3194
3195     if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_1000MBS) {
3196         /* Cable Length Estimation and Local/Remote Receiver Information
3197          * are only valid at 1000 Mbps.
3198          */
3199         phy_info->cable_length = (e1000_cable_length)((phy_data & M88E1000_PSSR_CABLE_LENGTH) >>
3200                                   M88E1000_PSSR_CABLE_LENGTH_SHIFT);
3201
3202         ret_val = e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_data);
3203         if (ret_val)
3204             return ret_val;
3205
3206         phy_info->local_rx = ((phy_data & SR_1000T_LOCAL_RX_STATUS) >>
3207                              SR_1000T_LOCAL_RX_STATUS_SHIFT) ?
3208                              e1000_1000t_rx_status_ok : e1000_1000t_rx_status_not_ok;
3209         phy_info->remote_rx = ((phy_data & SR_1000T_REMOTE_RX_STATUS) >>
3210                               SR_1000T_REMOTE_RX_STATUS_SHIFT) ?
3211                               e1000_1000t_rx_status_ok : e1000_1000t_rx_status_not_ok;
3212
3213     }
3214
3215     return E1000_SUCCESS;
3216 }
3217
3218 /******************************************************************************
3219 * Get PHY information from various PHY registers
3220 *
3221 * hw - Struct containing variables accessed by shared code
3222 * phy_info - PHY information structure
3223 ******************************************************************************/
3224 s32 e1000_phy_get_info(struct e1000_hw *hw, struct e1000_phy_info *phy_info)
3225 {
3226     s32 ret_val;
3227     u16 phy_data;
3228
3229     DEBUGFUNC("e1000_phy_get_info");
3230
3231     phy_info->cable_length = e1000_cable_length_undefined;
3232     phy_info->extended_10bt_distance = e1000_10bt_ext_dist_enable_undefined;
3233     phy_info->cable_polarity = e1000_rev_polarity_undefined;
3234     phy_info->downshift = e1000_downshift_undefined;
3235     phy_info->polarity_correction = e1000_polarity_reversal_undefined;
3236     phy_info->mdix_mode = e1000_auto_x_mode_undefined;
3237     phy_info->local_rx = e1000_1000t_rx_status_undefined;
3238     phy_info->remote_rx = e1000_1000t_rx_status_undefined;
3239
3240     if (hw->media_type != e1000_media_type_copper) {
3241         DEBUGOUT("PHY info is only valid for copper media\n");
3242         return -E1000_ERR_CONFIG;
3243     }
3244
3245     ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
3246     if (ret_val)
3247         return ret_val;
3248
3249     ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
3250     if (ret_val)
3251         return ret_val;
3252
3253     if ((phy_data & MII_SR_LINK_STATUS) != MII_SR_LINK_STATUS) {
3254         DEBUGOUT("PHY info is only valid if link is up\n");
3255         return -E1000_ERR_CONFIG;
3256     }
3257
3258     if (hw->phy_type == e1000_phy_igp)
3259         return e1000_phy_igp_get_info(hw, phy_info);
3260     else
3261         return e1000_phy_m88_get_info(hw, phy_info);
3262 }
3263
3264 s32 e1000_validate_mdi_setting(struct e1000_hw *hw)
3265 {
3266     DEBUGFUNC("e1000_validate_mdi_settings");
3267
3268     if (!hw->autoneg && (hw->mdix == 0 || hw->mdix == 3)) {
3269         DEBUGOUT("Invalid MDI setting detected\n");
3270         hw->mdix = 1;
3271         return -E1000_ERR_CONFIG;
3272     }
3273     return E1000_SUCCESS;
3274 }
3275
3276
3277 /******************************************************************************
3278  * Sets up eeprom variables in the hw struct.  Must be called after mac_type
3279  * is configured.
3280  *
3281  * hw - Struct containing variables accessed by shared code
3282  *****************************************************************************/
3283 s32 e1000_init_eeprom_params(struct e1000_hw *hw)
3284 {
3285     struct e1000_eeprom_info *eeprom = &hw->eeprom;
3286     u32 eecd = er32(EECD);
3287     s32 ret_val = E1000_SUCCESS;
3288     u16 eeprom_size;
3289
3290     DEBUGFUNC("e1000_init_eeprom_params");
3291
3292     switch (hw->mac_type) {
3293     case e1000_82542_rev2_0:
3294     case e1000_82542_rev2_1:
3295     case e1000_82543:
3296     case e1000_82544:
3297         eeprom->type = e1000_eeprom_microwire;
3298         eeprom->word_size = 64;
3299         eeprom->opcode_bits = 3;
3300         eeprom->address_bits = 6;
3301         eeprom->delay_usec = 50;
3302         eeprom->use_eerd = false;
3303         eeprom->use_eewr = false;
3304         break;
3305     case e1000_82540:
3306     case e1000_82545:
3307     case e1000_82545_rev_3:
3308     case e1000_82546:
3309     case e1000_82546_rev_3:
3310         eeprom->type = e1000_eeprom_microwire;
3311         eeprom->opcode_bits = 3;
3312         eeprom->delay_usec = 50;
3313         if (eecd & E1000_EECD_SIZE) {
3314             eeprom->word_size = 256;
3315             eeprom->address_bits = 8;
3316         } else {
3317             eeprom->word_size = 64;
3318             eeprom->address_bits = 6;
3319         }
3320         eeprom->use_eerd = false;
3321         eeprom->use_eewr = false;
3322         break;
3323     case e1000_82541:
3324     case e1000_82541_rev_2:
3325     case e1000_82547:
3326     case e1000_82547_rev_2:
3327         if (eecd & E1000_EECD_TYPE) {
3328             eeprom->type = e1000_eeprom_spi;
3329             eeprom->opcode_bits = 8;
3330             eeprom->delay_usec = 1;
3331             if (eecd & E1000_EECD_ADDR_BITS) {
3332                 eeprom->page_size = 32;
3333                 eeprom->address_bits = 16;
3334             } else {
3335                 eeprom->page_size = 8;
3336                 eeprom->address_bits = 8;
3337             }
3338         } else {
3339             eeprom->type = e1000_eeprom_microwire;
3340             eeprom->opcode_bits = 3;
3341             eeprom->delay_usec = 50;
3342             if (eecd & E1000_EECD_ADDR_BITS) {
3343                 eeprom->word_size = 256;
3344                 eeprom->address_bits = 8;
3345             } else {
3346                 eeprom->word_size = 64;
3347                 eeprom->address_bits = 6;
3348             }
3349         }
3350         eeprom->use_eerd = false;
3351         eeprom->use_eewr = false;
3352         break;
3353     default:
3354         break;
3355     }
3356
3357     if (eeprom->type == e1000_eeprom_spi) {
3358         /* eeprom_size will be an enum [0..8] that maps to eeprom sizes 128B to
3359          * 32KB (incremented by powers of 2).
3360          */
3361         /* Set to default value for initial eeprom read. */
3362         eeprom->word_size = 64;
3363         ret_val = e1000_read_eeprom(hw, EEPROM_CFG, 1, &eeprom_size);
3364         if (ret_val)
3365             return ret_val;
3366         eeprom_size = (eeprom_size & EEPROM_SIZE_MASK) >> EEPROM_SIZE_SHIFT;
3367         /* 256B eeprom size was not supported in earlier hardware, so we
3368          * bump eeprom_size up one to ensure that "1" (which maps to 256B)
3369          * is never the result used in the shifting logic below. */
3370         if (eeprom_size)
3371             eeprom_size++;
3372
3373         eeprom->word_size = 1 << (eeprom_size + EEPROM_WORD_SIZE_SHIFT);
3374     }
3375     return ret_val;
3376 }
3377
3378 /******************************************************************************
3379  * Raises the EEPROM's clock input.
3380  *
3381  * hw - Struct containing variables accessed by shared code
3382  * eecd - EECD's current value
3383  *****************************************************************************/
3384 static void e1000_raise_ee_clk(struct e1000_hw *hw, u32 *eecd)
3385 {
3386     /* Raise the clock input to the EEPROM (by setting the SK bit), and then
3387      * wait <delay> microseconds.
3388      */
3389     *eecd = *eecd | E1000_EECD_SK;
3390     ew32(EECD, *eecd);
3391     E1000_WRITE_FLUSH();
3392     udelay(hw->eeprom.delay_usec);
3393 }
3394
3395 /******************************************************************************
3396  * Lowers the EEPROM's clock input.
3397  *
3398  * hw - Struct containing variables accessed by shared code
3399  * eecd - EECD's current value
3400  *****************************************************************************/
3401 static void e1000_lower_ee_clk(struct e1000_hw *hw, u32 *eecd)
3402 {
3403     /* Lower the clock input to the EEPROM (by clearing the SK bit), and then
3404      * wait 50 microseconds.
3405      */
3406     *eecd = *eecd & ~E1000_EECD_SK;
3407     ew32(EECD, *eecd);
3408     E1000_WRITE_FLUSH();
3409     udelay(hw->eeprom.delay_usec);
3410 }
3411
3412 /******************************************************************************
3413  * Shift data bits out to the EEPROM.
3414  *
3415  * hw - Struct containing variables accessed by shared code
3416  * data - data to send to the EEPROM
3417  * count - number of bits to shift out
3418  *****************************************************************************/
3419 static void e1000_shift_out_ee_bits(struct e1000_hw *hw, u16 data, u16 count)
3420 {
3421     struct e1000_eeprom_info *eeprom = &hw->eeprom;
3422     u32 eecd;
3423     u32 mask;
3424
3425     /* We need to shift "count" bits out to the EEPROM. So, value in the
3426      * "data" parameter will be shifted out to the EEPROM one bit at a time.
3427      * In order to do this, "data" must be broken down into bits.
3428      */
3429     mask = 0x01 << (count - 1);
3430     eecd = er32(EECD);
3431     if (eeprom->type == e1000_eeprom_microwire) {
3432         eecd &= ~E1000_EECD_DO;
3433     } else if (eeprom->type == e1000_eeprom_spi) {
3434         eecd |= E1000_EECD_DO;
3435     }
3436     do {
3437         /* A "1" is shifted out to the EEPROM by setting bit "DI" to a "1",
3438          * and then raising and then lowering the clock (the SK bit controls
3439          * the clock input to the EEPROM).  A "0" is shifted out to the EEPROM
3440          * by setting "DI" to "0" and then raising and then lowering the clock.
3441          */
3442         eecd &= ~E1000_EECD_DI;
3443
3444         if (data & mask)
3445             eecd |= E1000_EECD_DI;
3446
3447         ew32(EECD, eecd);
3448         E1000_WRITE_FLUSH();
3449
3450         udelay(eeprom->delay_usec);
3451
3452         e1000_raise_ee_clk(hw, &eecd);
3453         e1000_lower_ee_clk(hw, &eecd);
3454
3455         mask = mask >> 1;
3456
3457     } while (mask);
3458
3459     /* We leave the "DI" bit set to "0" when we leave this routine. */
3460     eecd &= ~E1000_EECD_DI;
3461     ew32(EECD, eecd);
3462 }
3463
3464 /******************************************************************************
3465  * Shift data bits in from the EEPROM
3466  *
3467  * hw - Struct containing variables accessed by shared code
3468  *****************************************************************************/
3469 static u16 e1000_shift_in_ee_bits(struct e1000_hw *hw, u16 count)
3470 {
3471     u32 eecd;
3472     u32 i;
3473     u16 data;
3474
3475     /* In order to read a register from the EEPROM, we need to shift 'count'
3476      * bits in from the EEPROM. Bits are "shifted in" by raising the clock
3477      * input to the EEPROM (setting the SK bit), and then reading the value of
3478      * the "DO" bit.  During this "shifting in" process the "DI" bit should
3479      * always be clear.
3480      */
3481
3482     eecd = er32(EECD);
3483
3484     eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
3485     data = 0;
3486
3487     for (i = 0; i < count; i++) {
3488         data = data << 1;
3489         e1000_raise_ee_clk(hw, &eecd);
3490
3491         eecd = er32(EECD);
3492
3493         eecd &= ~(E1000_EECD_DI);
3494         if (eecd & E1000_EECD_DO)
3495             data |= 1;
3496
3497         e1000_lower_ee_clk(hw, &eecd);
3498     }
3499
3500     return data;
3501 }
3502
3503 /******************************************************************************
3504  * Prepares EEPROM for access
3505  *
3506  * hw - Struct containing variables accessed by shared code
3507  *
3508  * Lowers EEPROM clock. Clears input pin. Sets the chip select pin. This
3509  * function should be called before issuing a command to the EEPROM.
3510  *****************************************************************************/
3511 static s32 e1000_acquire_eeprom(struct e1000_hw *hw)
3512 {
3513     struct e1000_eeprom_info *eeprom = &hw->eeprom;
3514     u32 eecd, i=0;
3515
3516     DEBUGFUNC("e1000_acquire_eeprom");
3517
3518     if (e1000_swfw_sync_acquire(hw, E1000_SWFW_EEP_SM))
3519         return -E1000_ERR_SWFW_SYNC;
3520     eecd = er32(EECD);
3521
3522     /* Request EEPROM Access */
3523     if (hw->mac_type > e1000_82544) {
3524         eecd |= E1000_EECD_REQ;
3525         ew32(EECD, eecd);
3526         eecd = er32(EECD);
3527         while ((!(eecd & E1000_EECD_GNT)) &&
3528                (i < E1000_EEPROM_GRANT_ATTEMPTS)) {
3529             i++;
3530             udelay(5);
3531             eecd = er32(EECD);
3532         }
3533         if (!(eecd & E1000_EECD_GNT)) {
3534             eecd &= ~E1000_EECD_REQ;
3535             ew32(EECD, eecd);
3536             DEBUGOUT("Could not acquire EEPROM grant\n");
3537             e1000_swfw_sync_release(hw, E1000_SWFW_EEP_SM);
3538             return -E1000_ERR_EEPROM;
3539         }
3540     }
3541
3542     /* Setup EEPROM for Read/Write */
3543
3544     if (eeprom->type == e1000_eeprom_microwire) {
3545         /* Clear SK and DI */
3546         eecd &= ~(E1000_EECD_DI | E1000_EECD_SK);
3547         ew32(EECD, eecd);
3548
3549         /* Set CS */
3550         eecd |= E1000_EECD_CS;
3551         ew32(EECD, eecd);
3552     } else if (eeprom->type == e1000_eeprom_spi) {
3553         /* Clear SK and CS */
3554         eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
3555         ew32(EECD, eecd);
3556         udelay(1);
3557     }
3558
3559     return E1000_SUCCESS;
3560 }
3561
3562 /******************************************************************************
3563  * Returns EEPROM to a "standby" state
3564  *
3565  * hw - Struct containing variables accessed by shared code
3566  *****************************************************************************/
3567 static void e1000_standby_eeprom(struct e1000_hw *hw)
3568 {
3569     struct e1000_eeprom_info *eeprom = &hw->eeprom;
3570     u32 eecd;
3571
3572     eecd = er32(EECD);
3573
3574     if (eeprom->type == e1000_eeprom_microwire) {
3575         eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
3576         ew32(EECD, eecd);
3577         E1000_WRITE_FLUSH();
3578         udelay(eeprom->delay_usec);
3579
3580         /* Clock high */
3581         eecd |= E1000_EECD_SK;
3582         ew32(EECD, eecd);
3583         E1000_WRITE_FLUSH();
3584         udelay(eeprom->delay_usec);
3585
3586         /* Select EEPROM */
3587         eecd |= E1000_EECD_CS;
3588         ew32(EECD, eecd);
3589         E1000_WRITE_FLUSH();
3590         udelay(eeprom->delay_usec);
3591
3592         /* Clock low */
3593         eecd &= ~E1000_EECD_SK;
3594         ew32(EECD, eecd);
3595         E1000_WRITE_FLUSH();
3596         udelay(eeprom->delay_usec);
3597     } else if (eeprom->type == e1000_eeprom_spi) {
3598         /* Toggle CS to flush commands */
3599         eecd |= E1000_EECD_CS;
3600         ew32(EECD, eecd);
3601         E1000_WRITE_FLUSH();
3602         udelay(eeprom->delay_usec);
3603         eecd &= ~E1000_EECD_CS;
3604         ew32(EECD, eecd);
3605         E1000_WRITE_FLUSH();
3606         udelay(eeprom->delay_usec);
3607     }
3608 }
3609
3610 /******************************************************************************
3611  * Terminates a command by inverting the EEPROM's chip select pin
3612  *
3613  * hw - Struct containing variables accessed by shared code
3614  *****************************************************************************/
3615 static void e1000_release_eeprom(struct e1000_hw *hw)
3616 {
3617     u32 eecd;
3618
3619     DEBUGFUNC("e1000_release_eeprom");
3620
3621     eecd = er32(EECD);
3622
3623     if (hw->eeprom.type == e1000_eeprom_spi) {
3624         eecd |= E1000_EECD_CS;  /* Pull CS high */
3625         eecd &= ~E1000_EECD_SK; /* Lower SCK */
3626
3627         ew32(EECD, eecd);
3628
3629         udelay(hw->eeprom.delay_usec);
3630     } else if (hw->eeprom.type == e1000_eeprom_microwire) {
3631         /* cleanup eeprom */
3632
3633         /* CS on Microwire is active-high */
3634         eecd &= ~(E1000_EECD_CS | E1000_EECD_DI);
3635
3636         ew32(EECD, eecd);
3637
3638         /* Rising edge of clock */
3639         eecd |= E1000_EECD_SK;
3640         ew32(EECD, eecd);
3641         E1000_WRITE_FLUSH();
3642         udelay(hw->eeprom.delay_usec);
3643
3644         /* Falling edge of clock */
3645         eecd &= ~E1000_EECD_SK;
3646         ew32(EECD, eecd);
3647         E1000_WRITE_FLUSH();
3648         udelay(hw->eeprom.delay_usec);
3649     }
3650
3651     /* Stop requesting EEPROM access */
3652     if (hw->mac_type > e1000_82544) {
3653         eecd &= ~E1000_EECD_REQ;
3654         ew32(EECD, eecd);
3655     }
3656
3657     e1000_swfw_sync_release(hw, E1000_SWFW_EEP_SM);
3658 }
3659
3660 /******************************************************************************
3661  * Reads a 16 bit word from the EEPROM.
3662  *
3663  * hw - Struct containing variables accessed by shared code
3664  *****************************************************************************/
3665 static s32 e1000_spi_eeprom_ready(struct e1000_hw *hw)
3666 {
3667     u16 retry_count = 0;
3668     u8 spi_stat_reg;
3669
3670     DEBUGFUNC("e1000_spi_eeprom_ready");
3671
3672     /* Read "Status Register" repeatedly until the LSB is cleared.  The
3673      * EEPROM will signal that the command has been completed by clearing
3674      * bit 0 of the internal status register.  If it's not cleared within
3675      * 5 milliseconds, then error out.
3676      */
3677     retry_count = 0;
3678     do {
3679         e1000_shift_out_ee_bits(hw, EEPROM_RDSR_OPCODE_SPI,
3680                                 hw->eeprom.opcode_bits);
3681         spi_stat_reg = (u8)e1000_shift_in_ee_bits(hw, 8);
3682         if (!(spi_stat_reg & EEPROM_STATUS_RDY_SPI))
3683             break;
3684
3685         udelay(5);
3686         retry_count += 5;
3687
3688         e1000_standby_eeprom(hw);
3689     } while (retry_count < EEPROM_MAX_RETRY_SPI);
3690
3691     /* ATMEL SPI write time could vary from 0-20mSec on 3.3V devices (and
3692      * only 0-5mSec on 5V devices)
3693      */
3694     if (retry_count >= EEPROM_MAX_RETRY_SPI) {
3695         DEBUGOUT("SPI EEPROM Status error\n");
3696         return -E1000_ERR_EEPROM;
3697     }
3698
3699     return E1000_SUCCESS;
3700 }
3701
3702 /******************************************************************************
3703  * Reads a 16 bit word from the EEPROM.
3704  *
3705  * hw - Struct containing variables accessed by shared code
3706  * offset - offset of  word in the EEPROM to read
3707  * data - word read from the EEPROM
3708  * words - number of words to read
3709  *****************************************************************************/
3710 s32 e1000_read_eeprom(struct e1000_hw *hw, u16 offset, u16 words, u16 *data)
3711 {
3712     s32 ret;
3713     spin_lock(&e1000_eeprom_lock);
3714     ret = e1000_do_read_eeprom(hw, offset, words, data);
3715     spin_unlock(&e1000_eeprom_lock);
3716     return ret;
3717 }
3718
3719 static s32 e1000_do_read_eeprom(struct e1000_hw *hw, u16 offset, u16 words, u16 *data)
3720 {
3721     struct e1000_eeprom_info *eeprom = &hw->eeprom;
3722     u32 i = 0;
3723
3724     DEBUGFUNC("e1000_read_eeprom");
3725
3726     /* If eeprom is not yet detected, do so now */
3727     if (eeprom->word_size == 0)
3728         e1000_init_eeprom_params(hw);
3729
3730     /* A check for invalid values:  offset too large, too many words, and not
3731      * enough words.
3732      */
3733     if ((offset >= eeprom->word_size) || (words > eeprom->word_size - offset) ||
3734        (words == 0)) {
3735         DEBUGOUT2("\"words\" parameter out of bounds. Words = %d, size = %d\n", offset, eeprom->word_size);
3736         return -E1000_ERR_EEPROM;
3737     }
3738
3739     /* EEPROM's that don't use EERD to read require us to bit-bang the SPI
3740      * directly. In this case, we need to acquire the EEPROM so that
3741      * FW or other port software does not interrupt.
3742      */
3743     if (!hw->eeprom.use_eerd) {
3744         /* Prepare the EEPROM for bit-bang reading */
3745         if (e1000_acquire_eeprom(hw) != E1000_SUCCESS)
3746             return -E1000_ERR_EEPROM;
3747     }
3748
3749     /* Eerd register EEPROM access requires no eeprom aquire/release */
3750     if (eeprom->use_eerd)
3751         return e1000_read_eeprom_eerd(hw, offset, words, data);
3752
3753     /* Set up the SPI or Microwire EEPROM for bit-bang reading.  We have
3754      * acquired the EEPROM at this point, so any returns should relase it */
3755     if (eeprom->type == e1000_eeprom_spi) {
3756         u16 word_in;
3757         u8 read_opcode = EEPROM_READ_OPCODE_SPI;
3758
3759         if (e1000_spi_eeprom_ready(hw)) {
3760             e1000_release_eeprom(hw);
3761             return -E1000_ERR_EEPROM;
3762         }
3763
3764         e1000_standby_eeprom(hw);
3765
3766         /* Some SPI eeproms use the 8th address bit embedded in the opcode */
3767         if ((eeprom->address_bits == 8) && (offset >= 128))
3768             read_opcode |= EEPROM_A8_OPCODE_SPI;
3769
3770         /* Send the READ command (opcode + addr)  */
3771         e1000_shift_out_ee_bits(hw, read_opcode, eeprom->opcode_bits);
3772         e1000_shift_out_ee_bits(hw, (u16)(offset*2), eeprom->address_bits);
3773
3774         /* Read the data.  The address of the eeprom internally increments with
3775          * each byte (spi) being read, saving on the overhead of eeprom setup
3776          * and tear-down.  The address counter will roll over if reading beyond
3777          * the size of the eeprom, thus allowing the entire memory to be read
3778          * starting from any offset. */
3779         for (i = 0; i < words; i++) {
3780             word_in = e1000_shift_in_ee_bits(hw, 16);
3781             data[i] = (word_in >> 8) | (word_in << 8);
3782         }
3783     } else if (eeprom->type == e1000_eeprom_microwire) {
3784         for (i = 0; i < words; i++) {
3785             /* Send the READ command (opcode + addr)  */
3786             e1000_shift_out_ee_bits(hw, EEPROM_READ_OPCODE_MICROWIRE,
3787                                     eeprom->opcode_bits);
3788             e1000_shift_out_ee_bits(hw, (u16)(offset + i),
3789                                     eeprom->address_bits);
3790
3791             /* Read the data.  For microwire, each word requires the overhead
3792              * of eeprom setup and tear-down. */
3793             data[i] = e1000_shift_in_ee_bits(hw, 16);
3794             e1000_standby_eeprom(hw);
3795         }
3796     }
3797
3798     /* End this read operation */
3799     e1000_release_eeprom(hw);
3800
3801     return E1000_SUCCESS;
3802 }
3803
3804 /******************************************************************************
3805  * Reads a 16 bit word from the EEPROM using the EERD register.
3806  *
3807  * hw - Struct containing variables accessed by shared code
3808  * offset - offset of  word in the EEPROM to read
3809  * data - word read from the EEPROM
3810  * words - number of words to read
3811  *****************************************************************************/
3812 static s32 e1000_read_eeprom_eerd(struct e1000_hw *hw, u16 offset, u16 words,
3813                                   u16 *data)
3814 {
3815     u32 i, eerd = 0;
3816     s32 error = 0;
3817
3818     for (i = 0; i < words; i++) {
3819         eerd = ((offset+i) << E1000_EEPROM_RW_ADDR_SHIFT) +
3820                          E1000_EEPROM_RW_REG_START;
3821
3822         ew32(EERD, eerd);
3823         error = e1000_poll_eerd_eewr_done(hw, E1000_EEPROM_POLL_READ);
3824
3825         if (error) {
3826             break;
3827         }
3828         data[i] = (er32(EERD) >> E1000_EEPROM_RW_REG_DATA);
3829
3830     }
3831
3832     return error;
3833 }
3834
3835 /******************************************************************************
3836  * Writes a 16 bit word from the EEPROM using the EEWR register.
3837  *
3838  * hw - Struct containing variables accessed by shared code
3839  * offset - offset of  word in the EEPROM to read
3840  * data - word read from the EEPROM
3841  * words - number of words to read
3842  *****************************************************************************/
3843 static s32 e1000_write_eeprom_eewr(struct e1000_hw *hw, u16 offset, u16 words,
3844                                    u16 *data)
3845 {
3846     u32    register_value = 0;
3847     u32    i              = 0;
3848     s32     error          = 0;
3849
3850     if (e1000_swfw_sync_acquire(hw, E1000_SWFW_EEP_SM))
3851         return -E1000_ERR_SWFW_SYNC;
3852
3853     for (i = 0; i < words; i++) {
3854         register_value = (data[i] << E1000_EEPROM_RW_REG_DATA) |
3855                          ((offset+i) << E1000_EEPROM_RW_ADDR_SHIFT) |
3856                          E1000_EEPROM_RW_REG_START;
3857
3858         error = e1000_poll_eerd_eewr_done(hw, E1000_EEPROM_POLL_WRITE);
3859         if (error) {
3860             break;
3861         }
3862
3863         ew32(EEWR, register_value);
3864
3865         error = e1000_poll_eerd_eewr_done(hw, E1000_EEPROM_POLL_WRITE);
3866
3867         if (error) {
3868             break;
3869         }
3870     }
3871
3872     e1000_swfw_sync_release(hw, E1000_SWFW_EEP_SM);
3873     return error;
3874 }
3875
3876 /******************************************************************************
3877  * Polls the status bit (bit 1) of the EERD to determine when the read is done.
3878  *
3879  * hw - Struct containing variables accessed by shared code
3880  *****************************************************************************/
3881 static s32 e1000_poll_eerd_eewr_done(struct e1000_hw *hw, int eerd)
3882 {
3883     u32 attempts = 100000;
3884     u32 i, reg = 0;
3885     s32 done = E1000_ERR_EEPROM;
3886
3887     for (i = 0; i < attempts; i++) {
3888         if (eerd == E1000_EEPROM_POLL_READ)
3889             reg = er32(EERD);
3890         else
3891             reg = er32(EEWR);
3892
3893         if (reg & E1000_EEPROM_RW_REG_DONE) {
3894             done = E1000_SUCCESS;
3895             break;
3896         }
3897         udelay(5);
3898     }
3899
3900     return done;
3901 }
3902
3903 /******************************************************************************
3904  * Verifies that the EEPROM has a valid checksum
3905  *
3906  * hw - Struct containing variables accessed by shared code
3907  *
3908  * Reads the first 64 16 bit words of the EEPROM and sums the values read.
3909  * If the the sum of the 64 16 bit words is 0xBABA, the EEPROM's checksum is
3910  * valid.
3911  *****************************************************************************/
3912 s32 e1000_validate_eeprom_checksum(struct e1000_hw *hw)
3913 {
3914     u16 checksum = 0;
3915     u16 i, eeprom_data;
3916
3917     DEBUGFUNC("e1000_validate_eeprom_checksum");
3918
3919     for (i = 0; i < (EEPROM_CHECKSUM_REG + 1); i++) {
3920         if (e1000_read_eeprom(hw, i, 1, &eeprom_data) < 0) {
3921             DEBUGOUT("EEPROM Read Error\n");
3922             return -E1000_ERR_EEPROM;
3923         }
3924         checksum += eeprom_data;
3925     }
3926
3927     if (checksum == (u16)EEPROM_SUM)
3928         return E1000_SUCCESS;
3929     else {
3930         DEBUGOUT("EEPROM Checksum Invalid\n");
3931         return -E1000_ERR_EEPROM;
3932     }
3933 }
3934
3935 /******************************************************************************
3936  * Calculates the EEPROM checksum and writes it to the EEPROM
3937  *
3938  * hw - Struct containing variables accessed by shared code
3939  *
3940  * Sums the first 63 16 bit words of the EEPROM. Subtracts the sum from 0xBABA.
3941  * Writes the difference to word offset 63 of the EEPROM.
3942  *****************************************************************************/
3943 s32 e1000_update_eeprom_checksum(struct e1000_hw *hw)
3944 {
3945     u16 checksum = 0;
3946     u16 i, eeprom_data;
3947
3948     DEBUGFUNC("e1000_update_eeprom_checksum");
3949
3950     for (i = 0; i < EEPROM_CHECKSUM_REG; i++) {
3951         if (e1000_read_eeprom(hw, i, 1, &eeprom_data) < 0) {
3952             DEBUGOUT("EEPROM Read Error\n");
3953             return -E1000_ERR_EEPROM;
3954         }
3955         checksum += eeprom_data;
3956     }
3957     checksum = (u16)EEPROM_SUM - checksum;
3958     if (e1000_write_eeprom(hw, EEPROM_CHECKSUM_REG, 1, &checksum) < 0) {
3959         DEBUGOUT("EEPROM Write Error\n");
3960         return -E1000_ERR_EEPROM;
3961     }
3962     return E1000_SUCCESS;
3963 }
3964
3965 /******************************************************************************
3966  * Parent function for writing words to the different EEPROM types.
3967  *
3968  * hw - Struct containing variables accessed by shared code
3969  * offset - offset within the EEPROM to be written to
3970  * words - number of words to write
3971  * data - 16 bit word to be written to the EEPROM
3972  *
3973  * If e1000_update_eeprom_checksum is not called after this function, the
3974  * EEPROM will most likely contain an invalid checksum.
3975  *****************************************************************************/
3976 s32 e1000_write_eeprom(struct e1000_hw *hw, u16 offset, u16 words, u16 *data)
3977 {
3978     s32 ret;
3979     spin_lock(&e1000_eeprom_lock);
3980     ret = e1000_do_write_eeprom(hw, offset, words, data);
3981     spin_unlock(&e1000_eeprom_lock);
3982     return ret;
3983 }
3984
3985
3986 static s32 e1000_do_write_eeprom(struct e1000_hw *hw, u16 offset, u16 words, u16 *data)
3987 {
3988     struct e1000_eeprom_info *eeprom = &hw->eeprom;
3989     s32 status = 0;
3990
3991     DEBUGFUNC("e1000_write_eeprom");
3992
3993     /* If eeprom is not yet detected, do so now */
3994     if (eeprom->word_size == 0)
3995         e1000_init_eeprom_params(hw);
3996
3997     /* A check for invalid values:  offset too large, too many words, and not
3998      * enough words.
3999      */
4000     if ((offset >= eeprom->word_size) || (words > eeprom->word_size - offset) ||
4001        (words == 0)) {
4002         DEBUGOUT("\"words\" parameter out of bounds\n");
4003         return -E1000_ERR_EEPROM;
4004     }
4005
4006     if (eeprom->use_eewr)
4007         return e1000_write_eeprom_eewr(hw, offset, words, data);
4008
4009     /* Prepare the EEPROM for writing  */
4010     if (e1000_acquire_eeprom(hw) != E1000_SUCCESS)
4011         return -E1000_ERR_EEPROM;
4012
4013     if (eeprom->type == e1000_eeprom_microwire) {
4014         status = e1000_write_eeprom_microwire(hw, offset, words, data);
4015     } else {
4016         status = e1000_write_eeprom_spi(hw, offset, words, data);
4017         msleep(10);
4018     }
4019
4020     /* Done with writing */
4021     e1000_release_eeprom(hw);
4022
4023     return status;
4024 }
4025
4026 /******************************************************************************
4027  * Writes a 16 bit word to a given offset in an SPI EEPROM.
4028  *
4029  * hw - Struct containing variables accessed by shared code
4030  * offset - offset within the EEPROM to be written to
4031  * words - number of words to write
4032  * data - pointer to array of 8 bit words to be written to the EEPROM
4033  *
4034  *****************************************************************************/
4035 static s32 e1000_write_eeprom_spi(struct e1000_hw *hw, u16 offset, u16 words,
4036                                   u16 *data)
4037 {
4038     struct e1000_eeprom_info *eeprom = &hw->eeprom;
4039     u16 widx = 0;
4040
4041     DEBUGFUNC("e1000_write_eeprom_spi");
4042
4043     while (widx < words) {
4044         u8 write_opcode = EEPROM_WRITE_OPCODE_SPI;
4045
4046         if (e1000_spi_eeprom_ready(hw)) return -E1000_ERR_EEPROM;
4047
4048         e1000_standby_eeprom(hw);
4049
4050         /*  Send the WRITE ENABLE command (8 bit opcode )  */
4051         e1000_shift_out_ee_bits(hw, EEPROM_WREN_OPCODE_SPI,
4052                                     eeprom->opcode_bits);
4053
4054         e1000_standby_eeprom(hw);
4055
4056         /* Some SPI eeproms use the 8th address bit embedded in the opcode */
4057         if ((eeprom->address_bits == 8) && (offset >= 128))
4058             write_opcode |= EEPROM_A8_OPCODE_SPI;
4059
4060         /* Send the Write command (8-bit opcode + addr) */
4061         e1000_shift_out_ee_bits(hw, write_opcode, eeprom->opcode_bits);
4062
4063         e1000_shift_out_ee_bits(hw, (u16)((offset + widx)*2),
4064                                 eeprom->address_bits);
4065
4066         /* Send the data */
4067
4068         /* Loop to allow for up to whole page write (32 bytes) of eeprom */
4069         while (widx < words) {
4070             u16 word_out = data[widx];
4071             word_out = (word_out >> 8) | (word_out << 8);
4072             e1000_shift_out_ee_bits(hw, word_out, 16);
4073             widx++;
4074
4075             /* Some larger eeprom sizes are capable of a 32-byte PAGE WRITE
4076              * operation, while the smaller eeproms are capable of an 8-byte
4077              * PAGE WRITE operation.  Break the inner loop to pass new address
4078              */
4079             if ((((offset + widx)*2) % eeprom->page_size) == 0) {
4080                 e1000_standby_eeprom(hw);
4081                 break;
4082             }
4083         }
4084     }
4085
4086     return E1000_SUCCESS;
4087 }
4088
4089 /******************************************************************************
4090  * Writes a 16 bit word to a given offset in a Microwire EEPROM.
4091  *
4092  * hw - Struct containing variables accessed by shared code
4093  * offset - offset within the EEPROM to be written to
4094  * words - number of words to write
4095  * data - pointer to array of 16 bit words to be written to the EEPROM
4096  *
4097  *****************************************************************************/
4098 static s32 e1000_write_eeprom_microwire(struct e1000_hw *hw, u16 offset,
4099                                         u16 words, u16 *data)
4100 {
4101     struct e1000_eeprom_info *eeprom = &hw->eeprom;
4102     u32 eecd;
4103     u16 words_written = 0;
4104     u16 i = 0;
4105
4106     DEBUGFUNC("e1000_write_eeprom_microwire");
4107
4108     /* Send the write enable command to the EEPROM (3-bit opcode plus
4109      * 6/8-bit dummy address beginning with 11).  It's less work to include
4110      * the 11 of the dummy address as part of the opcode than it is to shift
4111      * it over the correct number of bits for the address.  This puts the
4112      * EEPROM into write/erase mode.
4113      */
4114     e1000_shift_out_ee_bits(hw, EEPROM_EWEN_OPCODE_MICROWIRE,
4115                             (u16)(eeprom->opcode_bits + 2));
4116
4117     e1000_shift_out_ee_bits(hw, 0, (u16)(eeprom->address_bits - 2));
4118
4119     /* Prepare the EEPROM */
4120     e1000_standby_eeprom(hw);
4121
4122     while (words_written < words) {
4123         /* Send the Write command (3-bit opcode + addr) */
4124         e1000_shift_out_ee_bits(hw, EEPROM_WRITE_OPCODE_MICROWIRE,
4125                                 eeprom->opcode_bits);
4126
4127         e1000_shift_out_ee_bits(hw, (u16)(offset + words_written),
4128                                 eeprom->address_bits);
4129
4130         /* Send the data */
4131         e1000_shift_out_ee_bits(hw, data[words_written], 16);
4132
4133         /* Toggle the CS line.  This in effect tells the EEPROM to execute
4134          * the previous command.
4135          */
4136         e1000_standby_eeprom(hw);
4137
4138         /* Read DO repeatedly until it is high (equal to '1').  The EEPROM will
4139          * signal that the command has been completed by raising the DO signal.
4140          * If DO does not go high in 10 milliseconds, then error out.
4141          */
4142         for (i = 0; i < 200; i++) {
4143             eecd = er32(EECD);
4144             if (eecd & E1000_EECD_DO) break;
4145             udelay(50);
4146         }
4147         if (i == 200) {
4148             DEBUGOUT("EEPROM Write did not complete\n");
4149             return -E1000_ERR_EEPROM;
4150         }
4151
4152         /* Recover from write */
4153         e1000_standby_eeprom(hw);
4154
4155         words_written++;
4156     }
4157
4158     /* Send the write disable command to the EEPROM (3-bit opcode plus
4159      * 6/8-bit dummy address beginning with 10).  It's less work to include
4160      * the 10 of the dummy address as part of the opcode than it is to shift
4161      * it over the correct number of bits for the address.  This takes the
4162      * EEPROM out of write/erase mode.
4163      */
4164     e1000_shift_out_ee_bits(hw, EEPROM_EWDS_OPCODE_MICROWIRE,
4165                             (u16)(eeprom->opcode_bits + 2));
4166
4167     e1000_shift_out_ee_bits(hw, 0, (u16)(eeprom->address_bits - 2));
4168
4169     return E1000_SUCCESS;
4170 }
4171
4172 /******************************************************************************
4173  * Reads the adapter's MAC address from the EEPROM and inverts the LSB for the
4174  * second function of dual function devices
4175  *
4176  * hw - Struct containing variables accessed by shared code
4177  *****************************************************************************/
4178 s32 e1000_read_mac_addr(struct e1000_hw *hw)
4179 {
4180     u16 offset;
4181     u16 eeprom_data, i;
4182
4183     DEBUGFUNC("e1000_read_mac_addr");
4184
4185     for (i = 0; i < NODE_ADDRESS_SIZE; i += 2) {
4186         offset = i >> 1;
4187         if (e1000_read_eeprom(hw, offset, 1, &eeprom_data) < 0) {
4188             DEBUGOUT("EEPROM Read Error\n");
4189             return -E1000_ERR_EEPROM;
4190         }
4191         hw->perm_mac_addr[i] = (u8)(eeprom_data & 0x00FF);
4192         hw->perm_mac_addr[i+1] = (u8)(eeprom_data >> 8);
4193     }
4194
4195     switch (hw->mac_type) {
4196     default:
4197         break;
4198     case e1000_82546:
4199     case e1000_82546_rev_3:
4200         if (er32(STATUS) & E1000_STATUS_FUNC_1)
4201             hw->perm_mac_addr[5] ^= 0x01;
4202         break;
4203     }
4204
4205     for (i = 0; i < NODE_ADDRESS_SIZE; i++)
4206         hw->mac_addr[i] = hw->perm_mac_addr[i];
4207     return E1000_SUCCESS;
4208 }
4209
4210 /******************************************************************************
4211  * Initializes receive address filters.
4212  *
4213  * hw - Struct containing variables accessed by shared code
4214  *
4215  * Places the MAC address in receive address register 0 and clears the rest
4216  * of the receive addresss registers. Clears the multicast table. Assumes
4217  * the receiver is in reset when the routine is called.
4218  *****************************************************************************/
4219 static void e1000_init_rx_addrs(struct e1000_hw *hw)
4220 {
4221     u32 i;
4222     u32 rar_num;
4223
4224     DEBUGFUNC("e1000_init_rx_addrs");
4225
4226     /* Setup the receive address. */
4227     DEBUGOUT("Programming MAC Address into RAR[0]\n");
4228
4229     e1000_rar_set(hw, hw->mac_addr, 0);
4230
4231     rar_num = E1000_RAR_ENTRIES;
4232
4233     /* Zero out the other 15 receive addresses. */
4234     DEBUGOUT("Clearing RAR[1-15]\n");
4235     for (i = 1; i < rar_num; i++) {
4236         E1000_WRITE_REG_ARRAY(hw, RA, (i << 1), 0);
4237         E1000_WRITE_FLUSH();
4238         E1000_WRITE_REG_ARRAY(hw, RA, ((i << 1) + 1), 0);
4239         E1000_WRITE_FLUSH();
4240     }
4241 }
4242
4243 /******************************************************************************
4244  * Hashes an address to determine its location in the multicast table
4245  *
4246  * hw - Struct containing variables accessed by shared code
4247  * mc_addr - the multicast address to hash
4248  *****************************************************************************/
4249 u32 e1000_hash_mc_addr(struct e1000_hw *hw, u8 *mc_addr)
4250 {
4251     u32 hash_value = 0;
4252
4253     /* The portion of the address that is used for the hash table is
4254      * determined by the mc_filter_type setting.
4255      */
4256     switch (hw->mc_filter_type) {
4257     /* [0] [1] [2] [3] [4] [5]
4258      * 01  AA  00  12  34  56
4259      * LSB                 MSB
4260      */
4261     case 0:
4262         /* [47:36] i.e. 0x563 for above example address */
4263         hash_value = ((mc_addr[4] >> 4) | (((u16)mc_addr[5]) << 4));
4264         break;
4265     case 1:
4266         /* [46:35] i.e. 0xAC6 for above example address */
4267         hash_value = ((mc_addr[4] >> 3) | (((u16)mc_addr[5]) << 5));
4268         break;
4269     case 2:
4270         /* [45:34] i.e. 0x5D8 for above example address */
4271         hash_value = ((mc_addr[4] >> 2) | (((u16)mc_addr[5]) << 6));
4272         break;
4273     case 3:
4274         /* [43:32] i.e. 0x634 for above example address */
4275         hash_value = ((mc_addr[4]) | (((u16)mc_addr[5]) << 8));
4276         break;
4277     }
4278
4279     hash_value &= 0xFFF;
4280     return hash_value;
4281 }
4282
4283 /******************************************************************************
4284  * Puts an ethernet address into a receive address register.
4285  *
4286  * hw - Struct containing variables accessed by shared code
4287  * addr - Address to put into receive address register
4288  * index - Receive address register to write
4289  *****************************************************************************/
4290 void e1000_rar_set(struct e1000_hw *hw, u8 *addr, u32 index)
4291 {
4292     u32 rar_low, rar_high;
4293
4294     /* HW expects these in little endian so we reverse the byte order
4295      * from network order (big endian) to little endian
4296      */
4297     rar_low = ((u32)addr[0] | ((u32)addr[1] << 8) |
4298                ((u32)addr[2] << 16) | ((u32)addr[3] << 24));
4299     rar_high = ((u32)addr[4] | ((u32)addr[5] << 8));
4300
4301     /* Disable Rx and flush all Rx frames before enabling RSS to avoid Rx
4302      * unit hang.
4303      *
4304      * Description:
4305      * If there are any Rx frames queued up or otherwise present in the HW
4306      * before RSS is enabled, and then we enable RSS, the HW Rx unit will
4307      * hang.  To work around this issue, we have to disable receives and
4308      * flush out all Rx frames before we enable RSS. To do so, we modify we
4309      * redirect all Rx traffic to manageability and then reset the HW.
4310      * This flushes away Rx frames, and (since the redirections to
4311      * manageability persists across resets) keeps new ones from coming in
4312      * while we work.  Then, we clear the Address Valid AV bit for all MAC
4313      * addresses and undo the re-direction to manageability.
4314      * Now, frames are coming in again, but the MAC won't accept them, so
4315      * far so good.  We now proceed to initialize RSS (if necessary) and
4316      * configure the Rx unit.  Last, we re-enable the AV bits and continue
4317      * on our merry way.
4318      */
4319     switch (hw->mac_type) {
4320     default:
4321         /* Indicate to hardware the Address is Valid. */
4322         rar_high |= E1000_RAH_AV;
4323         break;
4324     }
4325
4326     E1000_WRITE_REG_ARRAY(hw, RA, (index << 1), rar_low);
4327     E1000_WRITE_FLUSH();
4328     E1000_WRITE_REG_ARRAY(hw, RA, ((index << 1) + 1), rar_high);
4329     E1000_WRITE_FLUSH();
4330 }
4331
4332 /******************************************************************************
4333  * Writes a value to the specified offset in the VLAN filter table.
4334  *
4335  * hw - Struct containing variables accessed by shared code
4336  * offset - Offset in VLAN filer table to write
4337  * value - Value to write into VLAN filter table
4338  *****************************************************************************/
4339 void e1000_write_vfta(struct e1000_hw *hw, u32 offset, u32 value)
4340 {
4341     u32 temp;
4342
4343     if ((hw->mac_type == e1000_82544) && ((offset & 0x1) == 1)) {
4344         temp = E1000_READ_REG_ARRAY(hw, VFTA, (offset - 1));
4345         E1000_WRITE_REG_ARRAY(hw, VFTA, offset, value);
4346         E1000_WRITE_FLUSH();
4347         E1000_WRITE_REG_ARRAY(hw, VFTA, (offset - 1), temp);
4348         E1000_WRITE_FLUSH();
4349     } else {
4350         E1000_WRITE_REG_ARRAY(hw, VFTA, offset, value);
4351         E1000_WRITE_FLUSH();
4352     }
4353 }
4354
4355 /******************************************************************************
4356  * Clears the VLAN filer table
4357  *
4358  * hw - Struct containing variables accessed by shared code
4359  *****************************************************************************/
4360 static void e1000_clear_vfta(struct e1000_hw *hw)
4361 {
4362     u32 offset;
4363     u32 vfta_value = 0;
4364     u32 vfta_offset = 0;
4365     u32 vfta_bit_in_reg = 0;
4366
4367     for (offset = 0; offset < E1000_VLAN_FILTER_TBL_SIZE; offset++) {
4368         /* If the offset we want to clear is the same offset of the
4369          * manageability VLAN ID, then clear all bits except that of the
4370          * manageability unit */
4371         vfta_value = (offset == vfta_offset) ? vfta_bit_in_reg : 0;
4372         E1000_WRITE_REG_ARRAY(hw, VFTA, offset, vfta_value);
4373         E1000_WRITE_FLUSH();
4374     }
4375 }
4376
4377 static s32 e1000_id_led_init(struct e1000_hw *hw)
4378 {
4379     u32 ledctl;
4380     const u32 ledctl_mask = 0x000000FF;
4381     const u32 ledctl_on = E1000_LEDCTL_MODE_LED_ON;
4382     const u32 ledctl_off = E1000_LEDCTL_MODE_LED_OFF;
4383     u16 eeprom_data, i, temp;
4384     const u16 led_mask = 0x0F;
4385
4386     DEBUGFUNC("e1000_id_led_init");
4387
4388     if (hw->mac_type < e1000_82540) {
4389         /* Nothing to do */
4390         return E1000_SUCCESS;
4391     }
4392
4393     ledctl = er32(LEDCTL);
4394     hw->ledctl_default = ledctl;
4395     hw->ledctl_mode1 = hw->ledctl_default;
4396     hw->ledctl_mode2 = hw->ledctl_default;
4397
4398     if (e1000_read_eeprom(hw, EEPROM_ID_LED_SETTINGS, 1, &eeprom_data) < 0) {
4399         DEBUGOUT("EEPROM Read Error\n");
4400         return -E1000_ERR_EEPROM;
4401     }
4402
4403     if ((eeprom_data == ID_LED_RESERVED_0000) ||
4404             (eeprom_data == ID_LED_RESERVED_FFFF)) {
4405             eeprom_data = ID_LED_DEFAULT;
4406     }
4407
4408     for (i = 0; i < 4; i++) {
4409         temp = (eeprom_data >> (i << 2)) & led_mask;
4410         switch (temp) {
4411         case ID_LED_ON1_DEF2:
4412         case ID_LED_ON1_ON2:
4413         case ID_LED_ON1_OFF2:
4414             hw->ledctl_mode1 &= ~(ledctl_mask << (i << 3));
4415             hw->ledctl_mode1 |= ledctl_on << (i << 3);
4416             break;
4417         case ID_LED_OFF1_DEF2:
4418         case ID_LED_OFF1_ON2:
4419         case ID_LED_OFF1_OFF2:
4420             hw->ledctl_mode1 &= ~(ledctl_mask << (i << 3));
4421             hw->ledctl_mode1 |= ledctl_off << (i << 3);
4422             break;
4423         default:
4424             /* Do nothing */
4425             break;
4426         }
4427         switch (temp) {
4428         case ID_LED_DEF1_ON2:
4429         case ID_LED_ON1_ON2:
4430         case ID_LED_OFF1_ON2:
4431             hw->ledctl_mode2 &= ~(ledctl_mask << (i << 3));
4432             hw->ledctl_mode2 |= ledctl_on << (i << 3);
4433             break;
4434         case ID_LED_DEF1_OFF2:
4435         case ID_LED_ON1_OFF2:
4436         case ID_LED_OFF1_OFF2:
4437             hw->ledctl_mode2 &= ~(ledctl_mask << (i << 3));
4438             hw->ledctl_mode2 |= ledctl_off << (i << 3);
4439             break;
4440         default:
4441             /* Do nothing */
4442             break;
4443         }
4444     }
4445     return E1000_SUCCESS;
4446 }
4447
4448 /******************************************************************************
4449  * Prepares SW controlable LED for use and saves the current state of the LED.
4450  *
4451  * hw - Struct containing variables accessed by shared code
4452  *****************************************************************************/
4453 s32 e1000_setup_led(struct e1000_hw *hw)
4454 {
4455     u32 ledctl;
4456     s32 ret_val = E1000_SUCCESS;
4457
4458     DEBUGFUNC("e1000_setup_led");
4459
4460     switch (hw->mac_type) {
4461     case e1000_82542_rev2_0:
4462     case e1000_82542_rev2_1:
4463     case e1000_82543:
4464     case e1000_82544:
4465         /* No setup necessary */
4466         break;
4467     case e1000_82541:
4468     case e1000_82547:
4469     case e1000_82541_rev_2:
4470     case e1000_82547_rev_2:
4471         /* Turn off PHY Smart Power Down (if enabled) */
4472         ret_val = e1000_read_phy_reg(hw, IGP01E1000_GMII_FIFO,
4473                                      &hw->phy_spd_default);
4474         if (ret_val)
4475             return ret_val;
4476         ret_val = e1000_write_phy_reg(hw, IGP01E1000_GMII_FIFO,
4477                                       (u16)(hw->phy_spd_default &
4478                                       ~IGP01E1000_GMII_SPD));
4479         if (ret_val)
4480             return ret_val;
4481         /* Fall Through */
4482     default:
4483         if (hw->media_type == e1000_media_type_fiber) {
4484             ledctl = er32(LEDCTL);
4485             /* Save current LEDCTL settings */
4486             hw->ledctl_default = ledctl;
4487             /* Turn off LED0 */
4488             ledctl &= ~(E1000_LEDCTL_LED0_IVRT |
4489                         E1000_LEDCTL_LED0_BLINK |
4490                         E1000_LEDCTL_LED0_MODE_MASK);
4491             ledctl |= (E1000_LEDCTL_MODE_LED_OFF <<
4492                        E1000_LEDCTL_LED0_MODE_SHIFT);
4493             ew32(LEDCTL, ledctl);
4494         } else if (hw->media_type == e1000_media_type_copper)
4495             ew32(LEDCTL, hw->ledctl_mode1);
4496         break;
4497     }
4498
4499     return E1000_SUCCESS;
4500 }
4501
4502 /******************************************************************************
4503  * Restores the saved state of the SW controlable LED.
4504  *
4505  * hw - Struct containing variables accessed by shared code
4506  *****************************************************************************/
4507 s32 e1000_cleanup_led(struct e1000_hw *hw)
4508 {
4509     s32 ret_val = E1000_SUCCESS;
4510
4511     DEBUGFUNC("e1000_cleanup_led");
4512
4513     switch (hw->mac_type) {
4514     case e1000_82542_rev2_0:
4515     case e1000_82542_rev2_1:
4516     case e1000_82543:
4517     case e1000_82544:
4518         /* No cleanup necessary */
4519         break;
4520     case e1000_82541:
4521     case e1000_82547:
4522     case e1000_82541_rev_2:
4523     case e1000_82547_rev_2:
4524         /* Turn on PHY Smart Power Down (if previously enabled) */
4525         ret_val = e1000_write_phy_reg(hw, IGP01E1000_GMII_FIFO,
4526                                       hw->phy_spd_default);
4527         if (ret_val)
4528             return ret_val;
4529         /* Fall Through */
4530     default:
4531         /* Restore LEDCTL settings */
4532         ew32(LEDCTL, hw->ledctl_default);
4533         break;
4534     }
4535
4536     return E1000_SUCCESS;
4537 }
4538
4539 /******************************************************************************
4540  * Turns on the software controllable LED
4541  *
4542  * hw - Struct containing variables accessed by shared code
4543  *****************************************************************************/
4544 s32 e1000_led_on(struct e1000_hw *hw)
4545 {
4546     u32 ctrl = er32(CTRL);
4547
4548     DEBUGFUNC("e1000_led_on");
4549
4550     switch (hw->mac_type) {
4551     case e1000_82542_rev2_0:
4552     case e1000_82542_rev2_1:
4553     case e1000_82543:
4554         /* Set SW Defineable Pin 0 to turn on the LED */
4555         ctrl |= E1000_CTRL_SWDPIN0;
4556         ctrl |= E1000_CTRL_SWDPIO0;
4557         break;
4558     case e1000_82544:
4559         if (hw->media_type == e1000_media_type_fiber) {
4560             /* Set SW Defineable Pin 0 to turn on the LED */
4561             ctrl |= E1000_CTRL_SWDPIN0;
4562             ctrl |= E1000_CTRL_SWDPIO0;
4563         } else {
4564             /* Clear SW Defineable Pin 0 to turn on the LED */
4565             ctrl &= ~E1000_CTRL_SWDPIN0;
4566             ctrl |= E1000_CTRL_SWDPIO0;
4567         }
4568         break;
4569     default:
4570         if (hw->media_type == e1000_media_type_fiber) {
4571             /* Clear SW Defineable Pin 0 to turn on the LED */
4572             ctrl &= ~E1000_CTRL_SWDPIN0;
4573             ctrl |= E1000_CTRL_SWDPIO0;
4574         } else if (hw->media_type == e1000_media_type_copper) {
4575             ew32(LEDCTL, hw->ledctl_mode2);
4576             return E1000_SUCCESS;
4577         }
4578         break;
4579     }
4580
4581     ew32(CTRL, ctrl);
4582
4583     return E1000_SUCCESS;
4584 }
4585
4586 /******************************************************************************
4587  * Turns off the software controllable LED
4588  *
4589  * hw - Struct containing variables accessed by shared code
4590  *****************************************************************************/
4591 s32 e1000_led_off(struct e1000_hw *hw)
4592 {
4593     u32 ctrl = er32(CTRL);
4594
4595     DEBUGFUNC("e1000_led_off");
4596
4597     switch (hw->mac_type) {
4598     case e1000_82542_rev2_0:
4599     case e1000_82542_rev2_1:
4600     case e1000_82543:
4601         /* Clear SW Defineable Pin 0 to turn off the LED */
4602         ctrl &= ~E1000_CTRL_SWDPIN0;
4603         ctrl |= E1000_CTRL_SWDPIO0;
4604         break;
4605     case e1000_82544:
4606         if (hw->media_type == e1000_media_type_fiber) {
4607             /* Clear SW Defineable Pin 0 to turn off the LED */
4608             ctrl &= ~E1000_CTRL_SWDPIN0;
4609             ctrl |= E1000_CTRL_SWDPIO0;
4610         } else {
4611             /* Set SW Defineable Pin 0 to turn off the LED */
4612             ctrl |= E1000_CTRL_SWDPIN0;
4613             ctrl |= E1000_CTRL_SWDPIO0;
4614         }
4615         break;
4616     default:
4617         if (hw->media_type == e1000_media_type_fiber) {
4618             /* Set SW Defineable Pin 0 to turn off the LED */
4619             ctrl |= E1000_CTRL_SWDPIN0;
4620             ctrl |= E1000_CTRL_SWDPIO0;
4621         } else if (hw->media_type == e1000_media_type_copper) {
4622             ew32(LEDCTL, hw->ledctl_mode1);
4623             return E1000_SUCCESS;
4624         }
4625         break;
4626     }
4627
4628     ew32(CTRL, ctrl);
4629
4630     return E1000_SUCCESS;
4631 }
4632
4633 /******************************************************************************
4634  * Clears all hardware statistics counters.
4635  *
4636  * hw - Struct containing variables accessed by shared code
4637  *****************************************************************************/
4638 static void e1000_clear_hw_cntrs(struct e1000_hw *hw)
4639 {
4640     volatile u32 temp;
4641
4642     temp = er32(CRCERRS);
4643     temp = er32(SYMERRS);
4644     temp = er32(MPC);
4645     temp = er32(SCC);
4646     temp = er32(ECOL);
4647     temp = er32(MCC);
4648     temp = er32(LATECOL);
4649     temp = er32(COLC);
4650     temp = er32(DC);
4651     temp = er32(SEC);
4652     temp = er32(RLEC);
4653     temp = er32(XONRXC);
4654     temp = er32(XONTXC);
4655     temp = er32(XOFFRXC);
4656     temp = er32(XOFFTXC);
4657     temp = er32(FCRUC);
4658
4659     temp = er32(PRC64);
4660     temp = er32(PRC127);
4661     temp = er32(PRC255);
4662     temp = er32(PRC511);
4663     temp = er32(PRC1023);
4664     temp = er32(PRC1522);
4665
4666     temp = er32(GPRC);
4667     temp = er32(BPRC);
4668     temp = er32(MPRC);
4669     temp = er32(GPTC);
4670     temp = er32(GORCL);
4671     temp = er32(GORCH);
4672     temp = er32(GOTCL);
4673     temp = er32(GOTCH);
4674     temp = er32(RNBC);
4675     temp = er32(RUC);
4676     temp = er32(RFC);
4677     temp = er32(ROC);
4678     temp = er32(RJC);
4679     temp = er32(TORL);
4680     temp = er32(TORH);
4681     temp = er32(TOTL);
4682     temp = er32(TOTH);
4683     temp = er32(TPR);
4684     temp = er32(TPT);
4685
4686     temp = er32(PTC64);
4687     temp = er32(PTC127);
4688     temp = er32(PTC255);
4689     temp = er32(PTC511);
4690     temp = er32(PTC1023);
4691     temp = er32(PTC1522);
4692
4693     temp = er32(MPTC);
4694     temp = er32(BPTC);
4695
4696     if (hw->mac_type < e1000_82543) return;
4697
4698     temp = er32(ALGNERRC);
4699     temp = er32(RXERRC);
4700     temp = er32(TNCRS);
4701     temp = er32(CEXTERR);
4702     temp = er32(TSCTC);
4703     temp = er32(TSCTFC);
4704
4705     if (hw->mac_type <= e1000_82544) return;
4706
4707     temp = er32(MGTPRC);
4708     temp = er32(MGTPDC);
4709     temp = er32(MGTPTC);
4710 }
4711
4712 /******************************************************************************
4713  * Resets Adaptive IFS to its default state.
4714  *
4715  * hw - Struct containing variables accessed by shared code
4716  *
4717  * Call this after e1000_init_hw. You may override the IFS defaults by setting
4718  * hw->ifs_params_forced to true. However, you must initialize hw->
4719  * current_ifs_val, ifs_min_val, ifs_max_val, ifs_step_size, and ifs_ratio
4720  * before calling this function.
4721  *****************************************************************************/
4722 void e1000_reset_adaptive(struct e1000_hw *hw)
4723 {
4724     DEBUGFUNC("e1000_reset_adaptive");
4725
4726     if (hw->adaptive_ifs) {
4727         if (!hw->ifs_params_forced) {
4728             hw->current_ifs_val = 0;
4729             hw->ifs_min_val = IFS_MIN;
4730             hw->ifs_max_val = IFS_MAX;
4731             hw->ifs_step_size = IFS_STEP;
4732             hw->ifs_ratio = IFS_RATIO;
4733         }
4734         hw->in_ifs_mode = false;
4735         ew32(AIT, 0);
4736     } else {
4737         DEBUGOUT("Not in Adaptive IFS mode!\n");
4738     }
4739 }
4740
4741 /******************************************************************************
4742  * Called during the callback/watchdog routine to update IFS value based on
4743  * the ratio of transmits to collisions.
4744  *
4745  * hw - Struct containing variables accessed by shared code
4746  * tx_packets - Number of transmits since last callback
4747  * total_collisions - Number of collisions since last callback
4748  *****************************************************************************/
4749 void e1000_update_adaptive(struct e1000_hw *hw)
4750 {
4751     DEBUGFUNC("e1000_update_adaptive");
4752
4753     if (hw->adaptive_ifs) {
4754         if ((hw->collision_delta * hw->ifs_ratio) > hw->tx_packet_delta) {
4755             if (hw->tx_packet_delta > MIN_NUM_XMITS) {
4756                 hw->in_ifs_mode = true;
4757                 if (hw->current_ifs_val < hw->ifs_max_val) {
4758                     if (hw->current_ifs_val == 0)
4759                         hw->current_ifs_val = hw->ifs_min_val;
4760                     else
4761                         hw->current_ifs_val += hw->ifs_step_size;
4762                     ew32(AIT, hw->current_ifs_val);
4763                 }
4764             }
4765         } else {
4766             if (hw->in_ifs_mode && (hw->tx_packet_delta <= MIN_NUM_XMITS)) {
4767                 hw->current_ifs_val = 0;
4768                 hw->in_ifs_mode = false;
4769                 ew32(AIT, 0);
4770             }
4771         }
4772     } else {
4773         DEBUGOUT("Not in Adaptive IFS mode!\n");
4774     }
4775 }
4776
4777 /******************************************************************************
4778  * Adjusts the statistic counters when a frame is accepted by TBI_ACCEPT
4779  *
4780  * hw - Struct containing variables accessed by shared code
4781  * frame_len - The length of the frame in question
4782  * mac_addr - The Ethernet destination address of the frame in question
4783  *****************************************************************************/
4784 void e1000_tbi_adjust_stats(struct e1000_hw *hw, struct e1000_hw_stats *stats,
4785                             u32 frame_len, u8 *mac_addr)
4786 {
4787     u64 carry_bit;
4788
4789     /* First adjust the frame length. */
4790     frame_len--;
4791     /* We need to adjust the statistics counters, since the hardware
4792      * counters overcount this packet as a CRC error and undercount
4793      * the packet as a good packet
4794      */
4795     /* This packet should not be counted as a CRC error.    */
4796     stats->crcerrs--;
4797     /* This packet does count as a Good Packet Received.    */
4798     stats->gprc++;
4799
4800     /* Adjust the Good Octets received counters             */
4801     carry_bit = 0x80000000 & stats->gorcl;
4802     stats->gorcl += frame_len;
4803     /* If the high bit of Gorcl (the low 32 bits of the Good Octets
4804      * Received Count) was one before the addition,
4805      * AND it is zero after, then we lost the carry out,
4806      * need to add one to Gorch (Good Octets Received Count High).
4807      * This could be simplified if all environments supported
4808      * 64-bit integers.
4809      */
4810     if (carry_bit && ((stats->gorcl & 0x80000000) == 0))
4811         stats->gorch++;
4812     /* Is this a broadcast or multicast?  Check broadcast first,
4813      * since the test for a multicast frame will test positive on
4814      * a broadcast frame.
4815      */
4816     if ((mac_addr[0] == (u8)0xff) && (mac_addr[1] == (u8)0xff))
4817         /* Broadcast packet */
4818         stats->bprc++;
4819     else if (*mac_addr & 0x01)
4820         /* Multicast packet */
4821         stats->mprc++;
4822
4823     if (frame_len == hw->max_frame_size) {
4824         /* In this case, the hardware has overcounted the number of
4825          * oversize frames.
4826          */
4827         if (stats->roc > 0)
4828             stats->roc--;
4829     }
4830
4831     /* Adjust the bin counters when the extra byte put the frame in the
4832      * wrong bin. Remember that the frame_len was adjusted above.
4833      */
4834     if (frame_len == 64) {
4835         stats->prc64++;
4836         stats->prc127--;
4837     } else if (frame_len == 127) {
4838         stats->prc127++;
4839         stats->prc255--;
4840     } else if (frame_len == 255) {
4841         stats->prc255++;
4842         stats->prc511--;
4843     } else if (frame_len == 511) {
4844         stats->prc511++;
4845         stats->prc1023--;
4846     } else if (frame_len == 1023) {
4847         stats->prc1023++;
4848         stats->prc1522--;
4849     } else if (frame_len == 1522) {
4850         stats->prc1522++;
4851     }
4852 }
4853
4854 /******************************************************************************
4855  * Gets the current PCI bus type, speed, and width of the hardware
4856  *
4857  * hw - Struct containing variables accessed by shared code
4858  *****************************************************************************/
4859 void e1000_get_bus_info(struct e1000_hw *hw)
4860 {
4861     u32 status;
4862
4863     switch (hw->mac_type) {
4864     case e1000_82542_rev2_0:
4865     case e1000_82542_rev2_1:
4866         hw->bus_type = e1000_bus_type_pci;
4867         hw->bus_speed = e1000_bus_speed_unknown;
4868         hw->bus_width = e1000_bus_width_unknown;
4869         break;
4870     default:
4871         status = er32(STATUS);
4872         hw->bus_type = (status & E1000_STATUS_PCIX_MODE) ?
4873                        e1000_bus_type_pcix : e1000_bus_type_pci;
4874
4875         if (hw->device_id == E1000_DEV_ID_82546EB_QUAD_COPPER) {
4876             hw->bus_speed = (hw->bus_type == e1000_bus_type_pci) ?
4877                             e1000_bus_speed_66 : e1000_bus_speed_120;
4878         } else if (hw->bus_type == e1000_bus_type_pci) {
4879             hw->bus_speed = (status & E1000_STATUS_PCI66) ?
4880                             e1000_bus_speed_66 : e1000_bus_speed_33;
4881         } else {
4882             switch (status & E1000_STATUS_PCIX_SPEED) {
4883             case E1000_STATUS_PCIX_SPEED_66:
4884                 hw->bus_speed = e1000_bus_speed_66;
4885                 break;
4886             case E1000_STATUS_PCIX_SPEED_100:
4887                 hw->bus_speed = e1000_bus_speed_100;
4888                 break;
4889             case E1000_STATUS_PCIX_SPEED_133:
4890                 hw->bus_speed = e1000_bus_speed_133;
4891                 break;
4892             default:
4893                 hw->bus_speed = e1000_bus_speed_reserved;
4894                 break;
4895             }
4896         }
4897         hw->bus_width = (status & E1000_STATUS_BUS64) ?
4898                         e1000_bus_width_64 : e1000_bus_width_32;
4899         break;
4900     }
4901 }
4902
4903 /******************************************************************************
4904  * Writes a value to one of the devices registers using port I/O (as opposed to
4905  * memory mapped I/O). Only 82544 and newer devices support port I/O.
4906  *
4907  * hw - Struct containing variables accessed by shared code
4908  * offset - offset to write to
4909  * value - value to write
4910  *****************************************************************************/
4911 static void e1000_write_reg_io(struct e1000_hw *hw, u32 offset, u32 value)
4912 {
4913     unsigned long io_addr = hw->io_base;
4914     unsigned long io_data = hw->io_base + 4;
4915
4916     e1000_io_write(hw, io_addr, offset);
4917     e1000_io_write(hw, io_data, value);
4918 }
4919
4920 /******************************************************************************
4921  * Estimates the cable length.
4922  *
4923  * hw - Struct containing variables accessed by shared code
4924  * min_length - The estimated minimum length
4925  * max_length - The estimated maximum length
4926  *
4927  * returns: - E1000_ERR_XXX
4928  *            E1000_SUCCESS
4929  *
4930  * This function always returns a ranged length (minimum & maximum).
4931  * So for M88 phy's, this function interprets the one value returned from the
4932  * register to the minimum and maximum range.
4933  * For IGP phy's, the function calculates the range by the AGC registers.
4934  *****************************************************************************/
4935 static s32 e1000_get_cable_length(struct e1000_hw *hw, u16 *min_length,
4936                                   u16 *max_length)
4937 {
4938     s32 ret_val;
4939     u16 agc_value = 0;
4940     u16 i, phy_data;
4941     u16 cable_length;
4942
4943     DEBUGFUNC("e1000_get_cable_length");
4944
4945     *min_length = *max_length = 0;
4946
4947     /* Use old method for Phy older than IGP */
4948     if (hw->phy_type == e1000_phy_m88) {
4949
4950         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS,
4951                                      &phy_data);
4952         if (ret_val)
4953             return ret_val;
4954         cable_length = (phy_data & M88E1000_PSSR_CABLE_LENGTH) >>
4955                        M88E1000_PSSR_CABLE_LENGTH_SHIFT;
4956
4957         /* Convert the enum value to ranged values */
4958         switch (cable_length) {
4959         case e1000_cable_length_50:
4960             *min_length = 0;
4961             *max_length = e1000_igp_cable_length_50;
4962             break;
4963         case e1000_cable_length_50_80:
4964             *min_length = e1000_igp_cable_length_50;
4965             *max_length = e1000_igp_cable_length_80;
4966             break;
4967         case e1000_cable_length_80_110:
4968             *min_length = e1000_igp_cable_length_80;
4969             *max_length = e1000_igp_cable_length_110;
4970             break;
4971         case e1000_cable_length_110_140:
4972             *min_length = e1000_igp_cable_length_110;
4973             *max_length = e1000_igp_cable_length_140;
4974             break;
4975         case e1000_cable_length_140:
4976             *min_length = e1000_igp_cable_length_140;
4977             *max_length = e1000_igp_cable_length_170;
4978             break;
4979         default:
4980             return -E1000_ERR_PHY;
4981             break;
4982         }
4983     } else if (hw->phy_type == e1000_phy_igp) { /* For IGP PHY */
4984         u16 cur_agc_value;
4985         u16 min_agc_value = IGP01E1000_AGC_LENGTH_TABLE_SIZE;
4986         u16 agc_reg_array[IGP01E1000_PHY_CHANNEL_NUM] =
4987                                                          {IGP01E1000_PHY_AGC_A,
4988                                                           IGP01E1000_PHY_AGC_B,
4989                                                           IGP01E1000_PHY_AGC_C,
4990                                                           IGP01E1000_PHY_AGC_D};
4991         /* Read the AGC registers for all channels */
4992         for (i = 0; i < IGP01E1000_PHY_CHANNEL_NUM; i++) {
4993
4994             ret_val = e1000_read_phy_reg(hw, agc_reg_array[i], &phy_data);
4995             if (ret_val)
4996                 return ret_val;
4997
4998             cur_agc_value = phy_data >> IGP01E1000_AGC_LENGTH_SHIFT;
4999
5000             /* Value bound check. */
5001             if ((cur_agc_value >= IGP01E1000_AGC_LENGTH_TABLE_SIZE - 1) ||
5002                 (cur_agc_value == 0))
5003                 return -E1000_ERR_PHY;
5004
5005             agc_value += cur_agc_value;
5006
5007             /* Update minimal AGC value. */
5008             if (min_agc_value > cur_agc_value)
5009                 min_agc_value = cur_agc_value;
5010         }
5011
5012         /* Remove the minimal AGC result for length < 50m */
5013         if (agc_value < IGP01E1000_PHY_CHANNEL_NUM * e1000_igp_cable_length_50) {
5014             agc_value -= min_agc_value;
5015
5016             /* Get the average length of the remaining 3 channels */
5017             agc_value /= (IGP01E1000_PHY_CHANNEL_NUM - 1);
5018         } else {
5019             /* Get the average length of all the 4 channels. */
5020             agc_value /= IGP01E1000_PHY_CHANNEL_NUM;
5021         }
5022
5023         /* Set the range of the calculated length. */
5024         *min_length = ((e1000_igp_cable_length_table[agc_value] -
5025                        IGP01E1000_AGC_RANGE) > 0) ?
5026                        (e1000_igp_cable_length_table[agc_value] -
5027                        IGP01E1000_AGC_RANGE) : 0;
5028         *max_length = e1000_igp_cable_length_table[agc_value] +
5029                       IGP01E1000_AGC_RANGE;
5030     }
5031
5032     return E1000_SUCCESS;
5033 }
5034
5035 /******************************************************************************
5036  * Check the cable polarity
5037  *
5038  * hw - Struct containing variables accessed by shared code
5039  * polarity - output parameter : 0 - Polarity is not reversed
5040  *                               1 - Polarity is reversed.
5041  *
5042  * returns: - E1000_ERR_XXX
5043  *            E1000_SUCCESS
5044  *
5045  * For phy's older than IGP, this function simply reads the polarity bit in the
5046  * Phy Status register.  For IGP phy's, this bit is valid only if link speed is
5047  * 10 Mbps.  If the link speed is 100 Mbps there is no polarity so this bit will
5048  * return 0.  If the link speed is 1000 Mbps the polarity status is in the
5049  * IGP01E1000_PHY_PCS_INIT_REG.
5050  *****************************************************************************/
5051 static s32 e1000_check_polarity(struct e1000_hw *hw,
5052                                 e1000_rev_polarity *polarity)
5053 {
5054     s32 ret_val;
5055     u16 phy_data;
5056
5057     DEBUGFUNC("e1000_check_polarity");
5058
5059     if (hw->phy_type == e1000_phy_m88) {
5060         /* return the Polarity bit in the Status register. */
5061         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS,
5062                                      &phy_data);
5063         if (ret_val)
5064             return ret_val;
5065         *polarity = ((phy_data & M88E1000_PSSR_REV_POLARITY) >>
5066                      M88E1000_PSSR_REV_POLARITY_SHIFT) ?
5067                      e1000_rev_polarity_reversed : e1000_rev_polarity_normal;
5068
5069     } else if (hw->phy_type == e1000_phy_igp) {
5070         /* Read the Status register to check the speed */
5071         ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_STATUS,
5072                                      &phy_data);
5073         if (ret_val)
5074             return ret_val;
5075
5076         /* If speed is 1000 Mbps, must read the IGP01E1000_PHY_PCS_INIT_REG to
5077          * find the polarity status */
5078         if ((phy_data & IGP01E1000_PSSR_SPEED_MASK) ==
5079            IGP01E1000_PSSR_SPEED_1000MBPS) {
5080
5081             /* Read the GIG initialization PCS register (0x00B4) */
5082             ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PCS_INIT_REG,
5083                                          &phy_data);
5084             if (ret_val)
5085                 return ret_val;
5086
5087             /* Check the polarity bits */
5088             *polarity = (phy_data & IGP01E1000_PHY_POLARITY_MASK) ?
5089                          e1000_rev_polarity_reversed : e1000_rev_polarity_normal;
5090         } else {
5091             /* For 10 Mbps, read the polarity bit in the status register. (for
5092              * 100 Mbps this bit is always 0) */
5093             *polarity = (phy_data & IGP01E1000_PSSR_POLARITY_REVERSED) ?
5094                          e1000_rev_polarity_reversed : e1000_rev_polarity_normal;
5095         }
5096     }
5097     return E1000_SUCCESS;
5098 }
5099
5100 /******************************************************************************
5101  * Check if Downshift occured
5102  *
5103  * hw - Struct containing variables accessed by shared code
5104  * downshift - output parameter : 0 - No Downshift ocured.
5105  *                                1 - Downshift ocured.
5106  *
5107  * returns: - E1000_ERR_XXX
5108  *            E1000_SUCCESS
5109  *
5110  * For phy's older than IGP, this function reads the Downshift bit in the Phy
5111  * Specific Status register.  For IGP phy's, it reads the Downgrade bit in the
5112  * Link Health register.  In IGP this bit is latched high, so the driver must
5113  * read it immediately after link is established.
5114  *****************************************************************************/
5115 static s32 e1000_check_downshift(struct e1000_hw *hw)
5116 {
5117     s32 ret_val;
5118     u16 phy_data;
5119
5120     DEBUGFUNC("e1000_check_downshift");
5121
5122     if (hw->phy_type == e1000_phy_igp) {
5123         ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_LINK_HEALTH,
5124                                      &phy_data);
5125         if (ret_val)
5126             return ret_val;
5127
5128         hw->speed_downgraded = (phy_data & IGP01E1000_PLHR_SS_DOWNGRADE) ? 1 : 0;
5129     } else if (hw->phy_type == e1000_phy_m88) {
5130         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS,
5131                                      &phy_data);
5132         if (ret_val)
5133             return ret_val;
5134
5135         hw->speed_downgraded = (phy_data & M88E1000_PSSR_DOWNSHIFT) >>
5136                                M88E1000_PSSR_DOWNSHIFT_SHIFT;
5137     }
5138
5139     return E1000_SUCCESS;
5140 }
5141
5142 /*****************************************************************************
5143  *
5144  * 82541_rev_2 & 82547_rev_2 have the capability to configure the DSP when a
5145  * gigabit link is achieved to improve link quality.
5146  *
5147  * hw: Struct containing variables accessed by shared code
5148  *
5149  * returns: - E1000_ERR_PHY if fail to read/write the PHY
5150  *            E1000_SUCCESS at any other case.
5151  *
5152  ****************************************************************************/
5153
5154 static s32 e1000_config_dsp_after_link_change(struct e1000_hw *hw, bool link_up)
5155 {
5156     s32 ret_val;
5157     u16 phy_data, phy_saved_data, speed, duplex, i;
5158     u16 dsp_reg_array[IGP01E1000_PHY_CHANNEL_NUM] =
5159                                         {IGP01E1000_PHY_AGC_PARAM_A,
5160                                         IGP01E1000_PHY_AGC_PARAM_B,
5161                                         IGP01E1000_PHY_AGC_PARAM_C,
5162                                         IGP01E1000_PHY_AGC_PARAM_D};
5163     u16 min_length, max_length;
5164
5165     DEBUGFUNC("e1000_config_dsp_after_link_change");
5166
5167     if (hw->phy_type != e1000_phy_igp)
5168         return E1000_SUCCESS;
5169
5170     if (link_up) {
5171         ret_val = e1000_get_speed_and_duplex(hw, &speed, &duplex);
5172         if (ret_val) {
5173             DEBUGOUT("Error getting link speed and duplex\n");
5174             return ret_val;
5175         }
5176
5177         if (speed == SPEED_1000) {
5178
5179             ret_val = e1000_get_cable_length(hw, &min_length, &max_length);
5180             if (ret_val)
5181                 return ret_val;
5182
5183             if ((hw->dsp_config_state == e1000_dsp_config_enabled) &&
5184                 min_length >= e1000_igp_cable_length_50) {
5185
5186                 for (i = 0; i < IGP01E1000_PHY_CHANNEL_NUM; i++) {
5187                     ret_val = e1000_read_phy_reg(hw, dsp_reg_array[i],
5188                                                  &phy_data);
5189                     if (ret_val)
5190                         return ret_val;
5191
5192                     phy_data &= ~IGP01E1000_PHY_EDAC_MU_INDEX;
5193
5194                     ret_val = e1000_write_phy_reg(hw, dsp_reg_array[i],
5195                                                   phy_data);
5196                     if (ret_val)
5197                         return ret_val;
5198                 }
5199                 hw->dsp_config_state = e1000_dsp_config_activated;
5200             }
5201
5202             if ((hw->ffe_config_state == e1000_ffe_config_enabled) &&
5203                (min_length < e1000_igp_cable_length_50)) {
5204
5205                 u16 ffe_idle_err_timeout = FFE_IDLE_ERR_COUNT_TIMEOUT_20;
5206                 u32 idle_errs = 0;
5207
5208                 /* clear previous idle error counts */
5209                 ret_val = e1000_read_phy_reg(hw, PHY_1000T_STATUS,
5210                                              &phy_data);
5211                 if (ret_val)
5212                     return ret_val;
5213
5214                 for (i = 0; i < ffe_idle_err_timeout; i++) {
5215                     udelay(1000);
5216                     ret_val = e1000_read_phy_reg(hw, PHY_1000T_STATUS,
5217                                                  &phy_data);
5218                     if (ret_val)
5219                         return ret_val;
5220
5221                     idle_errs += (phy_data & SR_1000T_IDLE_ERROR_CNT);
5222                     if (idle_errs > SR_1000T_PHY_EXCESSIVE_IDLE_ERR_COUNT) {
5223                         hw->ffe_config_state = e1000_ffe_config_active;
5224
5225                         ret_val = e1000_write_phy_reg(hw,
5226                                     IGP01E1000_PHY_DSP_FFE,
5227                                     IGP01E1000_PHY_DSP_FFE_CM_CP);
5228                         if (ret_val)
5229                             return ret_val;
5230                         break;
5231                     }
5232
5233                     if (idle_errs)
5234                         ffe_idle_err_timeout = FFE_IDLE_ERR_COUNT_TIMEOUT_100;
5235                 }
5236             }
5237         }
5238     } else {
5239         if (hw->dsp_config_state == e1000_dsp_config_activated) {
5240             /* Save off the current value of register 0x2F5B to be restored at
5241              * the end of the routines. */
5242             ret_val = e1000_read_phy_reg(hw, 0x2F5B, &phy_saved_data);
5243
5244             if (ret_val)
5245                 return ret_val;
5246
5247             /* Disable the PHY transmitter */
5248             ret_val = e1000_write_phy_reg(hw, 0x2F5B, 0x0003);
5249
5250             if (ret_val)
5251                 return ret_val;
5252
5253             mdelay(20);
5254
5255             ret_val = e1000_write_phy_reg(hw, 0x0000,
5256                                           IGP01E1000_IEEE_FORCE_GIGA);
5257             if (ret_val)
5258                 return ret_val;
5259             for (i = 0; i < IGP01E1000_PHY_CHANNEL_NUM; i++) {
5260                 ret_val = e1000_read_phy_reg(hw, dsp_reg_array[i], &phy_data);
5261                 if (ret_val)
5262                     return ret_val;
5263
5264                 phy_data &= ~IGP01E1000_PHY_EDAC_MU_INDEX;
5265                 phy_data |=  IGP01E1000_PHY_EDAC_SIGN_EXT_9_BITS;
5266
5267                 ret_val = e1000_write_phy_reg(hw,dsp_reg_array[i], phy_data);
5268                 if (ret_val)
5269                     return ret_val;
5270             }
5271
5272             ret_val = e1000_write_phy_reg(hw, 0x0000,
5273                                           IGP01E1000_IEEE_RESTART_AUTONEG);
5274             if (ret_val)
5275                 return ret_val;
5276
5277             mdelay(20);
5278
5279             /* Now enable the transmitter */
5280             ret_val = e1000_write_phy_reg(hw, 0x2F5B, phy_saved_data);
5281
5282             if (ret_val)
5283                 return ret_val;
5284
5285             hw->dsp_config_state = e1000_dsp_config_enabled;
5286         }
5287
5288         if (hw->ffe_config_state == e1000_ffe_config_active) {
5289             /* Save off the current value of register 0x2F5B to be restored at
5290              * the end of the routines. */
5291             ret_val = e1000_read_phy_reg(hw, 0x2F5B, &phy_saved_data);
5292
5293             if (ret_val)
5294                 return ret_val;
5295
5296             /* Disable the PHY transmitter */
5297             ret_val = e1000_write_phy_reg(hw, 0x2F5B, 0x0003);
5298
5299             if (ret_val)
5300                 return ret_val;
5301
5302             mdelay(20);
5303
5304             ret_val = e1000_write_phy_reg(hw, 0x0000,
5305                                           IGP01E1000_IEEE_FORCE_GIGA);
5306             if (ret_val)
5307                 return ret_val;
5308             ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_DSP_FFE,
5309                                           IGP01E1000_PHY_DSP_FFE_DEFAULT);
5310             if (ret_val)
5311                 return ret_val;
5312
5313             ret_val = e1000_write_phy_reg(hw, 0x0000,
5314                                           IGP01E1000_IEEE_RESTART_AUTONEG);
5315             if (ret_val)
5316                 return ret_val;
5317
5318             mdelay(20);
5319
5320             /* Now enable the transmitter */
5321             ret_val = e1000_write_phy_reg(hw, 0x2F5B, phy_saved_data);
5322
5323             if (ret_val)
5324                 return ret_val;
5325
5326             hw->ffe_config_state = e1000_ffe_config_enabled;
5327         }
5328     }
5329     return E1000_SUCCESS;
5330 }
5331
5332 /*****************************************************************************
5333  * Set PHY to class A mode
5334  * Assumes the following operations will follow to enable the new class mode.
5335  *  1. Do a PHY soft reset
5336  *  2. Restart auto-negotiation or force link.
5337  *
5338  * hw - Struct containing variables accessed by shared code
5339  ****************************************************************************/
5340 static s32 e1000_set_phy_mode(struct e1000_hw *hw)
5341 {
5342     s32 ret_val;
5343     u16 eeprom_data;
5344
5345     DEBUGFUNC("e1000_set_phy_mode");
5346
5347     if ((hw->mac_type == e1000_82545_rev_3) &&
5348         (hw->media_type == e1000_media_type_copper)) {
5349         ret_val = e1000_read_eeprom(hw, EEPROM_PHY_CLASS_WORD, 1, &eeprom_data);
5350         if (ret_val) {
5351             return ret_val;
5352         }
5353
5354         if ((eeprom_data != EEPROM_RESERVED_WORD) &&
5355             (eeprom_data & EEPROM_PHY_CLASS_A)) {
5356             ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x000B);
5357             if (ret_val)
5358                 return ret_val;
5359             ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, 0x8104);
5360             if (ret_val)
5361                 return ret_val;
5362
5363             hw->phy_reset_disable = false;
5364         }
5365     }
5366
5367     return E1000_SUCCESS;
5368 }
5369
5370 /*****************************************************************************
5371  *
5372  * This function sets the lplu state according to the active flag.  When
5373  * activating lplu this function also disables smart speed and vise versa.
5374  * lplu will not be activated unless the device autonegotiation advertisment
5375  * meets standards of either 10 or 10/100 or 10/100/1000 at all duplexes.
5376  * hw: Struct containing variables accessed by shared code
5377  * active - true to enable lplu false to disable lplu.
5378  *
5379  * returns: - E1000_ERR_PHY if fail to read/write the PHY
5380  *            E1000_SUCCESS at any other case.
5381  *
5382  ****************************************************************************/
5383
5384 static s32 e1000_set_d3_lplu_state(struct e1000_hw *hw, bool active)
5385 {
5386     s32 ret_val;
5387     u16 phy_data;
5388     DEBUGFUNC("e1000_set_d3_lplu_state");
5389
5390     if (hw->phy_type != e1000_phy_igp)
5391         return E1000_SUCCESS;
5392
5393     /* During driver activity LPLU should not be used or it will attain link
5394      * from the lowest speeds starting from 10Mbps. The capability is used for
5395      * Dx transitions and states */
5396     if (hw->mac_type == e1000_82541_rev_2 || hw->mac_type == e1000_82547_rev_2) {
5397         ret_val = e1000_read_phy_reg(hw, IGP01E1000_GMII_FIFO, &phy_data);
5398         if (ret_val)
5399             return ret_val;
5400     } else {
5401         ret_val = e1000_read_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT, &phy_data);
5402         if (ret_val)
5403             return ret_val;
5404     }
5405
5406     if (!active) {
5407         if (hw->mac_type == e1000_82541_rev_2 ||
5408             hw->mac_type == e1000_82547_rev_2) {
5409             phy_data &= ~IGP01E1000_GMII_FLEX_SPD;
5410             ret_val = e1000_write_phy_reg(hw, IGP01E1000_GMII_FIFO, phy_data);
5411             if (ret_val)
5412                 return ret_val;
5413         } else {
5414             phy_data &= ~IGP02E1000_PM_D3_LPLU;
5415             ret_val = e1000_write_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT,
5416                                           phy_data);
5417             if (ret_val)
5418                 return ret_val;
5419         }
5420
5421         /* LPLU and SmartSpeed are mutually exclusive.  LPLU is used during
5422          * Dx states where the power conservation is most important.  During
5423          * driver activity we should enable SmartSpeed, so performance is
5424          * maintained. */
5425         if (hw->smart_speed == e1000_smart_speed_on) {
5426             ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
5427                                          &phy_data);
5428             if (ret_val)
5429                 return ret_val;
5430
5431             phy_data |= IGP01E1000_PSCFR_SMART_SPEED;
5432             ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
5433                                           phy_data);
5434             if (ret_val)
5435                 return ret_val;
5436         } else if (hw->smart_speed == e1000_smart_speed_off) {
5437             ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
5438                                          &phy_data);
5439             if (ret_val)
5440                 return ret_val;
5441
5442             phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
5443             ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
5444                                           phy_data);
5445             if (ret_val)
5446                 return ret_val;
5447         }
5448
5449     } else if ((hw->autoneg_advertised == AUTONEG_ADVERTISE_SPEED_DEFAULT) ||
5450                (hw->autoneg_advertised == AUTONEG_ADVERTISE_10_ALL ) ||
5451                (hw->autoneg_advertised == AUTONEG_ADVERTISE_10_100_ALL)) {
5452
5453         if (hw->mac_type == e1000_82541_rev_2 ||
5454             hw->mac_type == e1000_82547_rev_2) {
5455             phy_data |= IGP01E1000_GMII_FLEX_SPD;
5456             ret_val = e1000_write_phy_reg(hw, IGP01E1000_GMII_FIFO, phy_data);
5457             if (ret_val)
5458                 return ret_val;
5459         } else {
5460             phy_data |= IGP02E1000_PM_D3_LPLU;
5461             ret_val = e1000_write_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT,
5462                                           phy_data);
5463             if (ret_val)
5464                 return ret_val;
5465         }
5466
5467         /* When LPLU is enabled we should disable SmartSpeed */
5468         ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG, &phy_data);
5469         if (ret_val)
5470             return ret_val;
5471
5472         phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
5473         ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG, phy_data);
5474         if (ret_val)
5475             return ret_val;
5476
5477     }
5478     return E1000_SUCCESS;
5479 }
5480
5481 /******************************************************************************
5482  * Change VCO speed register to improve Bit Error Rate performance of SERDES.
5483  *
5484  * hw - Struct containing variables accessed by shared code
5485  *****************************************************************************/
5486 static s32 e1000_set_vco_speed(struct e1000_hw *hw)
5487 {
5488     s32  ret_val;
5489     u16 default_page = 0;
5490     u16 phy_data;
5491
5492     DEBUGFUNC("e1000_set_vco_speed");
5493
5494     switch (hw->mac_type) {
5495     case e1000_82545_rev_3:
5496     case e1000_82546_rev_3:
5497        break;
5498     default:
5499         return E1000_SUCCESS;
5500     }
5501
5502     /* Set PHY register 30, page 5, bit 8 to 0 */
5503
5504     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, &default_page);
5505     if (ret_val)
5506         return ret_val;
5507
5508     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x0005);
5509     if (ret_val)
5510         return ret_val;
5511
5512     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, &phy_data);
5513     if (ret_val)
5514         return ret_val;
5515
5516     phy_data &= ~M88E1000_PHY_VCO_REG_BIT8;
5517     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, phy_data);
5518     if (ret_val)
5519         return ret_val;
5520
5521     /* Set PHY register 30, page 4, bit 11 to 1 */
5522
5523     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x0004);
5524     if (ret_val)
5525         return ret_val;
5526
5527     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, &phy_data);
5528     if (ret_val)
5529         return ret_val;
5530
5531     phy_data |= M88E1000_PHY_VCO_REG_BIT11;
5532     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, phy_data);
5533     if (ret_val)
5534         return ret_val;
5535
5536     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, default_page);
5537     if (ret_val)
5538         return ret_val;
5539
5540     return E1000_SUCCESS;
5541 }
5542
5543
5544 /******************************************************************************
5545  * Verifies the hardware needs to allow ARPs to be processed by the host
5546  *
5547  * hw - Struct containing variables accessed by shared code
5548  *
5549  * returns: - true/false
5550  *
5551  *****************************************************************************/
5552 u32 e1000_enable_mng_pass_thru(struct e1000_hw *hw)
5553 {
5554     u32 manc;
5555
5556     if (hw->asf_firmware_present) {
5557         manc = er32(MANC);
5558
5559         if (!(manc & E1000_MANC_RCV_TCO_EN) ||
5560             !(manc & E1000_MANC_EN_MAC_ADDR_FILTER))
5561             return false;
5562         if ((manc & E1000_MANC_SMBUS_EN) && !(manc & E1000_MANC_ASF_EN))
5563             return true;
5564     }
5565     return false;
5566 }
5567
5568 static s32 e1000_polarity_reversal_workaround(struct e1000_hw *hw)
5569 {
5570     s32 ret_val;
5571     u16 mii_status_reg;
5572     u16 i;
5573
5574     /* Polarity reversal workaround for forced 10F/10H links. */
5575
5576     /* Disable the transmitter on the PHY */
5577
5578     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x0019);
5579     if (ret_val)
5580         return ret_val;
5581     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, 0xFFFF);
5582     if (ret_val)
5583         return ret_val;
5584
5585     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x0000);
5586     if (ret_val)
5587         return ret_val;
5588
5589     /* This loop will early-out if the NO link condition has been met. */
5590     for (i = PHY_FORCE_TIME; i > 0; i--) {
5591         /* Read the MII Status Register and wait for Link Status bit
5592          * to be clear.
5593          */
5594
5595         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
5596         if (ret_val)
5597             return ret_val;
5598
5599         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
5600         if (ret_val)
5601             return ret_val;
5602
5603         if ((mii_status_reg & ~MII_SR_LINK_STATUS) == 0) break;
5604         mdelay(100);
5605     }
5606
5607     /* Recommended delay time after link has been lost */
5608     mdelay(1000);
5609
5610     /* Now we will re-enable th transmitter on the PHY */
5611
5612     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x0019);
5613     if (ret_val)
5614         return ret_val;
5615     mdelay(50);
5616     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, 0xFFF0);
5617     if (ret_val)
5618         return ret_val;
5619     mdelay(50);
5620     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, 0xFF00);
5621     if (ret_val)
5622         return ret_val;
5623     mdelay(50);
5624     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, 0x0000);
5625     if (ret_val)
5626         return ret_val;
5627
5628     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x0000);
5629     if (ret_val)
5630         return ret_val;
5631
5632     /* This loop will early-out if the link condition has been met. */
5633     for (i = PHY_FORCE_TIME; i > 0; i--) {
5634         /* Read the MII Status Register and wait for Link Status bit
5635          * to be set.
5636          */
5637
5638         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
5639         if (ret_val)
5640             return ret_val;
5641
5642         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
5643         if (ret_val)
5644             return ret_val;
5645
5646         if (mii_status_reg & MII_SR_LINK_STATUS) break;
5647         mdelay(100);
5648     }
5649     return E1000_SUCCESS;
5650 }
5651
5652 /*******************************************************************************
5653  *
5654  * Check for EEPROM Auto Read bit done.
5655  *
5656  * hw: Struct containing variables accessed by shared code
5657  *
5658  * returns: - E1000_ERR_RESET if fail to reset MAC
5659  *            E1000_SUCCESS at any other case.
5660  *
5661  ******************************************************************************/
5662 static s32 e1000_get_auto_rd_done(struct e1000_hw *hw)
5663 {
5664     DEBUGFUNC("e1000_get_auto_rd_done");
5665     msleep(5);
5666     return E1000_SUCCESS;
5667 }
5668
5669 /***************************************************************************
5670  * Checks if the PHY configuration is done
5671  *
5672  * hw: Struct containing variables accessed by shared code
5673  *
5674  * returns: - E1000_ERR_RESET if fail to reset MAC
5675  *            E1000_SUCCESS at any other case.
5676  *
5677  ***************************************************************************/
5678 static s32 e1000_get_phy_cfg_done(struct e1000_hw *hw)
5679 {
5680     DEBUGFUNC("e1000_get_phy_cfg_done");
5681     mdelay(10);
5682     return E1000_SUCCESS;
5683 }
5684
5685 /***************************************************************************
5686  *
5687  * Using the combination of SMBI and SWESMBI semaphore bits when resetting
5688  * adapter or Eeprom access.
5689  *
5690  * hw: Struct containing variables accessed by shared code
5691  *
5692  * returns: - E1000_ERR_EEPROM if fail to access EEPROM.
5693  *            E1000_SUCCESS at any other case.
5694  *
5695  ***************************************************************************/
5696 static s32 e1000_get_hw_eeprom_semaphore(struct e1000_hw *hw)
5697 {
5698     s32 timeout;
5699     u32 swsm;
5700
5701     DEBUGFUNC("e1000_get_hw_eeprom_semaphore");
5702
5703     if (!hw->eeprom_semaphore_present)
5704         return E1000_SUCCESS;
5705
5706     /* Get the FW semaphore. */
5707     timeout = hw->eeprom.word_size + 1;
5708     while (timeout) {
5709         swsm = er32(SWSM);
5710         swsm |= E1000_SWSM_SWESMBI;
5711         ew32(SWSM, swsm);
5712         /* if we managed to set the bit we got the semaphore. */
5713         swsm = er32(SWSM);
5714         if (swsm & E1000_SWSM_SWESMBI)
5715             break;
5716
5717         udelay(50);
5718         timeout--;
5719     }
5720
5721     if (!timeout) {
5722         /* Release semaphores */
5723         e1000_put_hw_eeprom_semaphore(hw);
5724         DEBUGOUT("Driver can't access the Eeprom - SWESMBI bit is set.\n");
5725         return -E1000_ERR_EEPROM;
5726     }
5727
5728     return E1000_SUCCESS;
5729 }
5730
5731 /***************************************************************************
5732  * This function clears HW semaphore bits.
5733  *
5734  * hw: Struct containing variables accessed by shared code
5735  *
5736  * returns: - None.
5737  *
5738  ***************************************************************************/
5739 static void e1000_put_hw_eeprom_semaphore(struct e1000_hw *hw)
5740 {
5741     u32 swsm;
5742
5743     DEBUGFUNC("e1000_put_hw_eeprom_semaphore");
5744
5745     if (!hw->eeprom_semaphore_present)
5746         return;
5747
5748     swsm = er32(SWSM);
5749     swsm &= ~(E1000_SWSM_SWESMBI);
5750     ew32(SWSM, swsm);
5751 }