Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright(c) 2007 - 2018 Intel Corporation. */
0003 
0004 #include <linux/if_ether.h>
0005 #include <linux/delay.h>
0006 
0007 #include "e1000_mac.h"
0008 #include "e1000_phy.h"
0009 
0010 static s32  igb_phy_setup_autoneg(struct e1000_hw *hw);
0011 static void igb_phy_force_speed_duplex_setup(struct e1000_hw *hw,
0012                          u16 *phy_ctrl);
0013 static s32  igb_wait_autoneg(struct e1000_hw *hw);
0014 static s32  igb_set_master_slave_mode(struct e1000_hw *hw);
0015 
0016 /* Cable length tables */
0017 static const u16 e1000_m88_cable_length_table[] = {
0018     0, 50, 80, 110, 140, 140, E1000_CABLE_LENGTH_UNDEFINED };
0019 
0020 static const u16 e1000_igp_2_cable_length_table[] = {
0021     0, 0, 0, 0, 0, 0, 0, 0, 3, 5, 8, 11, 13, 16, 18, 21,
0022     0, 0, 0, 3, 6, 10, 13, 16, 19, 23, 26, 29, 32, 35, 38, 41,
0023     6, 10, 14, 18, 22, 26, 30, 33, 37, 41, 44, 48, 51, 54, 58, 61,
0024     21, 26, 31, 35, 40, 44, 49, 53, 57, 61, 65, 68, 72, 75, 79, 82,
0025     40, 45, 51, 56, 61, 66, 70, 75, 79, 83, 87, 91, 94, 98, 101, 104,
0026     60, 66, 72, 77, 82, 87, 92, 96, 100, 104, 108, 111, 114, 117, 119, 121,
0027     83, 89, 95, 100, 105, 109, 113, 116, 119, 122, 124,
0028     104, 109, 114, 118, 121, 124};
0029 
0030 /**
0031  *  igb_check_reset_block - Check if PHY reset is blocked
0032  *  @hw: pointer to the HW structure
0033  *
0034  *  Read the PHY management control register and check whether a PHY reset
0035  *  is blocked.  If a reset is not blocked return 0, otherwise
0036  *  return E1000_BLK_PHY_RESET (12).
0037  **/
0038 s32 igb_check_reset_block(struct e1000_hw *hw)
0039 {
0040     u32 manc;
0041 
0042     manc = rd32(E1000_MANC);
0043 
0044     return (manc & E1000_MANC_BLK_PHY_RST_ON_IDE) ? E1000_BLK_PHY_RESET : 0;
0045 }
0046 
0047 /**
0048  *  igb_get_phy_id - Retrieve the PHY ID and revision
0049  *  @hw: pointer to the HW structure
0050  *
0051  *  Reads the PHY registers and stores the PHY ID and possibly the PHY
0052  *  revision in the hardware structure.
0053  **/
0054 s32 igb_get_phy_id(struct e1000_hw *hw)
0055 {
0056     struct e1000_phy_info *phy = &hw->phy;
0057     s32 ret_val = 0;
0058     u16 phy_id;
0059 
0060     /* ensure PHY page selection to fix misconfigured i210 */
0061     if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211))
0062         phy->ops.write_reg(hw, I347AT4_PAGE_SELECT, 0);
0063 
0064     ret_val = phy->ops.read_reg(hw, PHY_ID1, &phy_id);
0065     if (ret_val)
0066         goto out;
0067 
0068     phy->id = (u32)(phy_id << 16);
0069     udelay(20);
0070     ret_val = phy->ops.read_reg(hw, PHY_ID2, &phy_id);
0071     if (ret_val)
0072         goto out;
0073 
0074     phy->id |= (u32)(phy_id & PHY_REVISION_MASK);
0075     phy->revision = (u32)(phy_id & ~PHY_REVISION_MASK);
0076 
0077 out:
0078     return ret_val;
0079 }
0080 
0081 /**
0082  *  igb_phy_reset_dsp - Reset PHY DSP
0083  *  @hw: pointer to the HW structure
0084  *
0085  *  Reset the digital signal processor.
0086  **/
0087 static s32 igb_phy_reset_dsp(struct e1000_hw *hw)
0088 {
0089     s32 ret_val = 0;
0090 
0091     if (!(hw->phy.ops.write_reg))
0092         goto out;
0093 
0094     ret_val = hw->phy.ops.write_reg(hw, M88E1000_PHY_GEN_CONTROL, 0xC1);
0095     if (ret_val)
0096         goto out;
0097 
0098     ret_val = hw->phy.ops.write_reg(hw, M88E1000_PHY_GEN_CONTROL, 0);
0099 
0100 out:
0101     return ret_val;
0102 }
0103 
0104 /**
0105  *  igb_read_phy_reg_mdic - Read MDI control register
0106  *  @hw: pointer to the HW structure
0107  *  @offset: register offset to be read
0108  *  @data: pointer to the read data
0109  *
0110  *  Reads the MDI control register in the PHY at offset and stores the
0111  *  information read to data.
0112  **/
0113 s32 igb_read_phy_reg_mdic(struct e1000_hw *hw, u32 offset, u16 *data)
0114 {
0115     struct e1000_phy_info *phy = &hw->phy;
0116     u32 i, mdic = 0;
0117     s32 ret_val = 0;
0118 
0119     if (offset > MAX_PHY_REG_ADDRESS) {
0120         hw_dbg("PHY Address %d is out of range\n", offset);
0121         ret_val = -E1000_ERR_PARAM;
0122         goto out;
0123     }
0124 
0125     /* Set up Op-code, Phy Address, and register offset in the MDI
0126      * Control register.  The MAC will take care of interfacing with the
0127      * PHY to retrieve the desired data.
0128      */
0129     mdic = ((offset << E1000_MDIC_REG_SHIFT) |
0130         (phy->addr << E1000_MDIC_PHY_SHIFT) |
0131         (E1000_MDIC_OP_READ));
0132 
0133     wr32(E1000_MDIC, mdic);
0134 
0135     /* Poll the ready bit to see if the MDI read completed
0136      * Increasing the time out as testing showed failures with
0137      * the lower time out
0138      */
0139     for (i = 0; i < (E1000_GEN_POLL_TIMEOUT * 3); i++) {
0140         udelay(50);
0141         mdic = rd32(E1000_MDIC);
0142         if (mdic & E1000_MDIC_READY)
0143             break;
0144     }
0145     if (!(mdic & E1000_MDIC_READY)) {
0146         hw_dbg("MDI Read did not complete\n");
0147         ret_val = -E1000_ERR_PHY;
0148         goto out;
0149     }
0150     if (mdic & E1000_MDIC_ERROR) {
0151         hw_dbg("MDI Error\n");
0152         ret_val = -E1000_ERR_PHY;
0153         goto out;
0154     }
0155     *data = (u16) mdic;
0156 
0157 out:
0158     return ret_val;
0159 }
0160 
0161 /**
0162  *  igb_write_phy_reg_mdic - Write MDI control register
0163  *  @hw: pointer to the HW structure
0164  *  @offset: register offset to write to
0165  *  @data: data to write to register at offset
0166  *
0167  *  Writes data to MDI control register in the PHY at offset.
0168  **/
0169 s32 igb_write_phy_reg_mdic(struct e1000_hw *hw, u32 offset, u16 data)
0170 {
0171     struct e1000_phy_info *phy = &hw->phy;
0172     u32 i, mdic = 0;
0173     s32 ret_val = 0;
0174 
0175     if (offset > MAX_PHY_REG_ADDRESS) {
0176         hw_dbg("PHY Address %d is out of range\n", offset);
0177         ret_val = -E1000_ERR_PARAM;
0178         goto out;
0179     }
0180 
0181     /* Set up Op-code, Phy Address, and register offset in the MDI
0182      * Control register.  The MAC will take care of interfacing with the
0183      * PHY to retrieve the desired data.
0184      */
0185     mdic = (((u32)data) |
0186         (offset << E1000_MDIC_REG_SHIFT) |
0187         (phy->addr << E1000_MDIC_PHY_SHIFT) |
0188         (E1000_MDIC_OP_WRITE));
0189 
0190     wr32(E1000_MDIC, mdic);
0191 
0192     /* Poll the ready bit to see if the MDI read completed
0193      * Increasing the time out as testing showed failures with
0194      * the lower time out
0195      */
0196     for (i = 0; i < (E1000_GEN_POLL_TIMEOUT * 3); i++) {
0197         udelay(50);
0198         mdic = rd32(E1000_MDIC);
0199         if (mdic & E1000_MDIC_READY)
0200             break;
0201     }
0202     if (!(mdic & E1000_MDIC_READY)) {
0203         hw_dbg("MDI Write did not complete\n");
0204         ret_val = -E1000_ERR_PHY;
0205         goto out;
0206     }
0207     if (mdic & E1000_MDIC_ERROR) {
0208         hw_dbg("MDI Error\n");
0209         ret_val = -E1000_ERR_PHY;
0210         goto out;
0211     }
0212 
0213 out:
0214     return ret_val;
0215 }
0216 
0217 /**
0218  *  igb_read_phy_reg_i2c - Read PHY register using i2c
0219  *  @hw: pointer to the HW structure
0220  *  @offset: register offset to be read
0221  *  @data: pointer to the read data
0222  *
0223  *  Reads the PHY register at offset using the i2c interface and stores the
0224  *  retrieved information in data.
0225  **/
0226 s32 igb_read_phy_reg_i2c(struct e1000_hw *hw, u32 offset, u16 *data)
0227 {
0228     struct e1000_phy_info *phy = &hw->phy;
0229     u32 i, i2ccmd = 0;
0230 
0231     /* Set up Op-code, Phy Address, and register address in the I2CCMD
0232      * register.  The MAC will take care of interfacing with the
0233      * PHY to retrieve the desired data.
0234      */
0235     i2ccmd = ((offset << E1000_I2CCMD_REG_ADDR_SHIFT) |
0236           (phy->addr << E1000_I2CCMD_PHY_ADDR_SHIFT) |
0237           (E1000_I2CCMD_OPCODE_READ));
0238 
0239     wr32(E1000_I2CCMD, i2ccmd);
0240 
0241     /* Poll the ready bit to see if the I2C read completed */
0242     for (i = 0; i < E1000_I2CCMD_PHY_TIMEOUT; i++) {
0243         udelay(50);
0244         i2ccmd = rd32(E1000_I2CCMD);
0245         if (i2ccmd & E1000_I2CCMD_READY)
0246             break;
0247     }
0248     if (!(i2ccmd & E1000_I2CCMD_READY)) {
0249         hw_dbg("I2CCMD Read did not complete\n");
0250         return -E1000_ERR_PHY;
0251     }
0252     if (i2ccmd & E1000_I2CCMD_ERROR) {
0253         hw_dbg("I2CCMD Error bit set\n");
0254         return -E1000_ERR_PHY;
0255     }
0256 
0257     /* Need to byte-swap the 16-bit value. */
0258     *data = ((i2ccmd >> 8) & 0x00FF) | ((i2ccmd << 8) & 0xFF00);
0259 
0260     return 0;
0261 }
0262 
0263 /**
0264  *  igb_write_phy_reg_i2c - Write PHY register using i2c
0265  *  @hw: pointer to the HW structure
0266  *  @offset: register offset to write to
0267  *  @data: data to write at register offset
0268  *
0269  *  Writes the data to PHY register at the offset using the i2c interface.
0270  **/
0271 s32 igb_write_phy_reg_i2c(struct e1000_hw *hw, u32 offset, u16 data)
0272 {
0273     struct e1000_phy_info *phy = &hw->phy;
0274     u32 i, i2ccmd = 0;
0275     u16 phy_data_swapped;
0276 
0277     /* Prevent overwriting SFP I2C EEPROM which is at A0 address.*/
0278     if ((hw->phy.addr == 0) || (hw->phy.addr > 7)) {
0279         hw_dbg("PHY I2C Address %d is out of range.\n",
0280               hw->phy.addr);
0281         return -E1000_ERR_CONFIG;
0282     }
0283 
0284     /* Swap the data bytes for the I2C interface */
0285     phy_data_swapped = ((data >> 8) & 0x00FF) | ((data << 8) & 0xFF00);
0286 
0287     /* Set up Op-code, Phy Address, and register address in the I2CCMD
0288      * register.  The MAC will take care of interfacing with the
0289      * PHY to retrieve the desired data.
0290      */
0291     i2ccmd = ((offset << E1000_I2CCMD_REG_ADDR_SHIFT) |
0292           (phy->addr << E1000_I2CCMD_PHY_ADDR_SHIFT) |
0293           E1000_I2CCMD_OPCODE_WRITE |
0294           phy_data_swapped);
0295 
0296     wr32(E1000_I2CCMD, i2ccmd);
0297 
0298     /* Poll the ready bit to see if the I2C read completed */
0299     for (i = 0; i < E1000_I2CCMD_PHY_TIMEOUT; i++) {
0300         udelay(50);
0301         i2ccmd = rd32(E1000_I2CCMD);
0302         if (i2ccmd & E1000_I2CCMD_READY)
0303             break;
0304     }
0305     if (!(i2ccmd & E1000_I2CCMD_READY)) {
0306         hw_dbg("I2CCMD Write did not complete\n");
0307         return -E1000_ERR_PHY;
0308     }
0309     if (i2ccmd & E1000_I2CCMD_ERROR) {
0310         hw_dbg("I2CCMD Error bit set\n");
0311         return -E1000_ERR_PHY;
0312     }
0313 
0314     return 0;
0315 }
0316 
0317 /**
0318  *  igb_read_sfp_data_byte - Reads SFP module data.
0319  *  @hw: pointer to the HW structure
0320  *  @offset: byte location offset to be read
0321  *  @data: read data buffer pointer
0322  *
0323  *  Reads one byte from SFP module data stored
0324  *  in SFP resided EEPROM memory or SFP diagnostic area.
0325  *  Function should be called with
0326  *  E1000_I2CCMD_SFP_DATA_ADDR(<byte offset>) for SFP module database access
0327  *  E1000_I2CCMD_SFP_DIAG_ADDR(<byte offset>) for SFP diagnostics parameters
0328  *  access
0329  **/
0330 s32 igb_read_sfp_data_byte(struct e1000_hw *hw, u16 offset, u8 *data)
0331 {
0332     u32 i = 0;
0333     u32 i2ccmd = 0;
0334     u32 data_local = 0;
0335 
0336     if (offset > E1000_I2CCMD_SFP_DIAG_ADDR(255)) {
0337         hw_dbg("I2CCMD command address exceeds upper limit\n");
0338         return -E1000_ERR_PHY;
0339     }
0340 
0341     /* Set up Op-code, EEPROM Address,in the I2CCMD
0342      * register. The MAC will take care of interfacing with the
0343      * EEPROM to retrieve the desired data.
0344      */
0345     i2ccmd = ((offset << E1000_I2CCMD_REG_ADDR_SHIFT) |
0346           E1000_I2CCMD_OPCODE_READ);
0347 
0348     wr32(E1000_I2CCMD, i2ccmd);
0349 
0350     /* Poll the ready bit to see if the I2C read completed */
0351     for (i = 0; i < E1000_I2CCMD_PHY_TIMEOUT; i++) {
0352         udelay(50);
0353         data_local = rd32(E1000_I2CCMD);
0354         if (data_local & E1000_I2CCMD_READY)
0355             break;
0356     }
0357     if (!(data_local & E1000_I2CCMD_READY)) {
0358         hw_dbg("I2CCMD Read did not complete\n");
0359         return -E1000_ERR_PHY;
0360     }
0361     if (data_local & E1000_I2CCMD_ERROR) {
0362         hw_dbg("I2CCMD Error bit set\n");
0363         return -E1000_ERR_PHY;
0364     }
0365     *data = (u8) data_local & 0xFF;
0366 
0367     return 0;
0368 }
0369 
0370 /**
0371  *  igb_read_phy_reg_igp - Read igp PHY register
0372  *  @hw: pointer to the HW structure
0373  *  @offset: register offset to be read
0374  *  @data: pointer to the read data
0375  *
0376  *  Acquires semaphore, if necessary, then reads the PHY register at offset
0377  *  and storing the retrieved information in data.  Release any acquired
0378  *  semaphores before exiting.
0379  **/
0380 s32 igb_read_phy_reg_igp(struct e1000_hw *hw, u32 offset, u16 *data)
0381 {
0382     s32 ret_val = 0;
0383 
0384     if (!(hw->phy.ops.acquire))
0385         goto out;
0386 
0387     ret_val = hw->phy.ops.acquire(hw);
0388     if (ret_val)
0389         goto out;
0390 
0391     if (offset > MAX_PHY_MULTI_PAGE_REG) {
0392         ret_val = igb_write_phy_reg_mdic(hw,
0393                          IGP01E1000_PHY_PAGE_SELECT,
0394                          (u16)offset);
0395         if (ret_val) {
0396             hw->phy.ops.release(hw);
0397             goto out;
0398         }
0399     }
0400 
0401     ret_val = igb_read_phy_reg_mdic(hw, MAX_PHY_REG_ADDRESS & offset,
0402                     data);
0403 
0404     hw->phy.ops.release(hw);
0405 
0406 out:
0407     return ret_val;
0408 }
0409 
0410 /**
0411  *  igb_write_phy_reg_igp - Write igp PHY register
0412  *  @hw: pointer to the HW structure
0413  *  @offset: register offset to write to
0414  *  @data: data to write at register offset
0415  *
0416  *  Acquires semaphore, if necessary, then writes the data to PHY register
0417  *  at the offset.  Release any acquired semaphores before exiting.
0418  **/
0419 s32 igb_write_phy_reg_igp(struct e1000_hw *hw, u32 offset, u16 data)
0420 {
0421     s32 ret_val = 0;
0422 
0423     if (!(hw->phy.ops.acquire))
0424         goto out;
0425 
0426     ret_val = hw->phy.ops.acquire(hw);
0427     if (ret_val)
0428         goto out;
0429 
0430     if (offset > MAX_PHY_MULTI_PAGE_REG) {
0431         ret_val = igb_write_phy_reg_mdic(hw,
0432                          IGP01E1000_PHY_PAGE_SELECT,
0433                          (u16)offset);
0434         if (ret_val) {
0435             hw->phy.ops.release(hw);
0436             goto out;
0437         }
0438     }
0439 
0440     ret_val = igb_write_phy_reg_mdic(hw, MAX_PHY_REG_ADDRESS & offset,
0441                      data);
0442 
0443     hw->phy.ops.release(hw);
0444 
0445 out:
0446     return ret_val;
0447 }
0448 
0449 /**
0450  *  igb_copper_link_setup_82580 - Setup 82580 PHY for copper link
0451  *  @hw: pointer to the HW structure
0452  *
0453  *  Sets up Carrier-sense on Transmit and downshift values.
0454  **/
0455 s32 igb_copper_link_setup_82580(struct e1000_hw *hw)
0456 {
0457     struct e1000_phy_info *phy = &hw->phy;
0458     s32 ret_val;
0459     u16 phy_data;
0460 
0461     if (phy->reset_disable) {
0462         ret_val = 0;
0463         goto out;
0464     }
0465 
0466     if (phy->type == e1000_phy_82580) {
0467         ret_val = hw->phy.ops.reset(hw);
0468         if (ret_val) {
0469             hw_dbg("Error resetting the PHY.\n");
0470             goto out;
0471         }
0472     }
0473 
0474     /* Enable CRS on TX. This must be set for half-duplex operation. */
0475     ret_val = phy->ops.read_reg(hw, I82580_CFG_REG, &phy_data);
0476     if (ret_val)
0477         goto out;
0478 
0479     phy_data |= I82580_CFG_ASSERT_CRS_ON_TX;
0480 
0481     /* Enable downshift */
0482     phy_data |= I82580_CFG_ENABLE_DOWNSHIFT;
0483 
0484     ret_val = phy->ops.write_reg(hw, I82580_CFG_REG, phy_data);
0485     if (ret_val)
0486         goto out;
0487 
0488     /* Set MDI/MDIX mode */
0489     ret_val = phy->ops.read_reg(hw, I82580_PHY_CTRL_2, &phy_data);
0490     if (ret_val)
0491         goto out;
0492     phy_data &= ~I82580_PHY_CTRL2_MDIX_CFG_MASK;
0493     /* Options:
0494      *   0 - Auto (default)
0495      *   1 - MDI mode
0496      *   2 - MDI-X mode
0497      */
0498     switch (hw->phy.mdix) {
0499     case 1:
0500         break;
0501     case 2:
0502         phy_data |= I82580_PHY_CTRL2_MANUAL_MDIX;
0503         break;
0504     case 0:
0505     default:
0506         phy_data |= I82580_PHY_CTRL2_AUTO_MDI_MDIX;
0507         break;
0508     }
0509     ret_val = hw->phy.ops.write_reg(hw, I82580_PHY_CTRL_2, phy_data);
0510 
0511 out:
0512     return ret_val;
0513 }
0514 
0515 /**
0516  *  igb_copper_link_setup_m88 - Setup m88 PHY's for copper link
0517  *  @hw: pointer to the HW structure
0518  *
0519  *  Sets up MDI/MDI-X and polarity for m88 PHY's.  If necessary, transmit clock
0520  *  and downshift values are set also.
0521  **/
0522 s32 igb_copper_link_setup_m88(struct e1000_hw *hw)
0523 {
0524     struct e1000_phy_info *phy = &hw->phy;
0525     s32 ret_val;
0526     u16 phy_data;
0527 
0528     if (phy->reset_disable) {
0529         ret_val = 0;
0530         goto out;
0531     }
0532 
0533     /* Enable CRS on TX. This must be set for half-duplex operation. */
0534     ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
0535     if (ret_val)
0536         goto out;
0537 
0538     phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
0539 
0540     /* Options:
0541      *   MDI/MDI-X = 0 (default)
0542      *   0 - Auto for all speeds
0543      *   1 - MDI mode
0544      *   2 - MDI-X mode
0545      *   3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
0546      */
0547     phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
0548 
0549     switch (phy->mdix) {
0550     case 1:
0551         phy_data |= M88E1000_PSCR_MDI_MANUAL_MODE;
0552         break;
0553     case 2:
0554         phy_data |= M88E1000_PSCR_MDIX_MANUAL_MODE;
0555         break;
0556     case 3:
0557         phy_data |= M88E1000_PSCR_AUTO_X_1000T;
0558         break;
0559     case 0:
0560     default:
0561         phy_data |= M88E1000_PSCR_AUTO_X_MODE;
0562         break;
0563     }
0564 
0565     /* Options:
0566      *   disable_polarity_correction = 0 (default)
0567      *       Automatic Correction for Reversed Cable Polarity
0568      *   0 - Disabled
0569      *   1 - Enabled
0570      */
0571     phy_data &= ~M88E1000_PSCR_POLARITY_REVERSAL;
0572     if (phy->disable_polarity_correction == 1)
0573         phy_data |= M88E1000_PSCR_POLARITY_REVERSAL;
0574 
0575     ret_val = phy->ops.write_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
0576     if (ret_val)
0577         goto out;
0578 
0579     if (phy->revision < E1000_REVISION_4) {
0580         /* Force TX_CLK in the Extended PHY Specific Control Register
0581          * to 25MHz clock.
0582          */
0583         ret_val = phy->ops.read_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL,
0584                         &phy_data);
0585         if (ret_val)
0586             goto out;
0587 
0588         phy_data |= M88E1000_EPSCR_TX_CLK_25;
0589 
0590         if ((phy->revision == E1000_REVISION_2) &&
0591             (phy->id == M88E1111_I_PHY_ID)) {
0592             /* 82573L PHY - set the downshift counter to 5x. */
0593             phy_data &= ~M88EC018_EPSCR_DOWNSHIFT_COUNTER_MASK;
0594             phy_data |= M88EC018_EPSCR_DOWNSHIFT_COUNTER_5X;
0595         } else {
0596             /* Configure Master and Slave downshift values */
0597             phy_data &= ~(M88E1000_EPSCR_MASTER_DOWNSHIFT_MASK |
0598                       M88E1000_EPSCR_SLAVE_DOWNSHIFT_MASK);
0599             phy_data |= (M88E1000_EPSCR_MASTER_DOWNSHIFT_1X |
0600                      M88E1000_EPSCR_SLAVE_DOWNSHIFT_1X);
0601         }
0602         ret_val = phy->ops.write_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL,
0603                          phy_data);
0604         if (ret_val)
0605             goto out;
0606     }
0607 
0608     /* Commit the changes. */
0609     ret_val = igb_phy_sw_reset(hw);
0610     if (ret_val) {
0611         hw_dbg("Error committing the PHY changes\n");
0612         goto out;
0613     }
0614 
0615 out:
0616     return ret_val;
0617 }
0618 
0619 /**
0620  *  igb_copper_link_setup_m88_gen2 - Setup m88 PHY's for copper link
0621  *  @hw: pointer to the HW structure
0622  *
0623  *  Sets up MDI/MDI-X and polarity for i347-AT4, m88e1322 and m88e1112 PHY's.
0624  *  Also enables and sets the downshift parameters.
0625  **/
0626 s32 igb_copper_link_setup_m88_gen2(struct e1000_hw *hw)
0627 {
0628     struct e1000_phy_info *phy = &hw->phy;
0629     s32 ret_val;
0630     u16 phy_data;
0631 
0632     if (phy->reset_disable)
0633         return 0;
0634 
0635     /* Enable CRS on Tx. This must be set for half-duplex operation. */
0636     ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
0637     if (ret_val)
0638         return ret_val;
0639 
0640     /* Options:
0641      *   MDI/MDI-X = 0 (default)
0642      *   0 - Auto for all speeds
0643      *   1 - MDI mode
0644      *   2 - MDI-X mode
0645      *   3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
0646      */
0647     phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
0648 
0649     switch (phy->mdix) {
0650     case 1:
0651         phy_data |= M88E1000_PSCR_MDI_MANUAL_MODE;
0652         break;
0653     case 2:
0654         phy_data |= M88E1000_PSCR_MDIX_MANUAL_MODE;
0655         break;
0656     case 3:
0657         /* M88E1112 does not support this mode) */
0658         if (phy->id != M88E1112_E_PHY_ID) {
0659             phy_data |= M88E1000_PSCR_AUTO_X_1000T;
0660             break;
0661         }
0662         fallthrough;
0663     case 0:
0664     default:
0665         phy_data |= M88E1000_PSCR_AUTO_X_MODE;
0666         break;
0667     }
0668 
0669     /* Options:
0670      *   disable_polarity_correction = 0 (default)
0671      *       Automatic Correction for Reversed Cable Polarity
0672      *   0 - Disabled
0673      *   1 - Enabled
0674      */
0675     phy_data &= ~M88E1000_PSCR_POLARITY_REVERSAL;
0676     if (phy->disable_polarity_correction == 1)
0677         phy_data |= M88E1000_PSCR_POLARITY_REVERSAL;
0678 
0679     /* Enable downshift and setting it to X6 */
0680     if (phy->id == M88E1543_E_PHY_ID) {
0681         phy_data &= ~I347AT4_PSCR_DOWNSHIFT_ENABLE;
0682         ret_val =
0683             phy->ops.write_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
0684         if (ret_val)
0685             return ret_val;
0686 
0687         ret_val = igb_phy_sw_reset(hw);
0688         if (ret_val) {
0689             hw_dbg("Error committing the PHY changes\n");
0690             return ret_val;
0691         }
0692     }
0693 
0694     phy_data &= ~I347AT4_PSCR_DOWNSHIFT_MASK;
0695     phy_data |= I347AT4_PSCR_DOWNSHIFT_6X;
0696     phy_data |= I347AT4_PSCR_DOWNSHIFT_ENABLE;
0697 
0698     ret_val = phy->ops.write_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
0699     if (ret_val)
0700         return ret_val;
0701 
0702     /* Commit the changes. */
0703     ret_val = igb_phy_sw_reset(hw);
0704     if (ret_val) {
0705         hw_dbg("Error committing the PHY changes\n");
0706         return ret_val;
0707     }
0708     ret_val = igb_set_master_slave_mode(hw);
0709     if (ret_val)
0710         return ret_val;
0711 
0712     return 0;
0713 }
0714 
0715 /**
0716  *  igb_copper_link_setup_igp - Setup igp PHY's for copper link
0717  *  @hw: pointer to the HW structure
0718  *
0719  *  Sets up LPLU, MDI/MDI-X, polarity, Smartspeed and Master/Slave config for
0720  *  igp PHY's.
0721  **/
0722 s32 igb_copper_link_setup_igp(struct e1000_hw *hw)
0723 {
0724     struct e1000_phy_info *phy = &hw->phy;
0725     s32 ret_val;
0726     u16 data;
0727 
0728     if (phy->reset_disable) {
0729         ret_val = 0;
0730         goto out;
0731     }
0732 
0733     ret_val = phy->ops.reset(hw);
0734     if (ret_val) {
0735         hw_dbg("Error resetting the PHY.\n");
0736         goto out;
0737     }
0738 
0739     /* Wait 100ms for MAC to configure PHY from NVM settings, to avoid
0740      * timeout issues when LFS is enabled.
0741      */
0742     msleep(100);
0743 
0744     /* The NVM settings will configure LPLU in D3 for
0745      * non-IGP1 PHYs.
0746      */
0747     if (phy->type == e1000_phy_igp) {
0748         /* disable lplu d3 during driver init */
0749         if (phy->ops.set_d3_lplu_state)
0750             ret_val = phy->ops.set_d3_lplu_state(hw, false);
0751         if (ret_val) {
0752             hw_dbg("Error Disabling LPLU D3\n");
0753             goto out;
0754         }
0755     }
0756 
0757     /* disable lplu d0 during driver init */
0758     ret_val = phy->ops.set_d0_lplu_state(hw, false);
0759     if (ret_val) {
0760         hw_dbg("Error Disabling LPLU D0\n");
0761         goto out;
0762     }
0763     /* Configure mdi-mdix settings */
0764     ret_val = phy->ops.read_reg(hw, IGP01E1000_PHY_PORT_CTRL, &data);
0765     if (ret_val)
0766         goto out;
0767 
0768     data &= ~IGP01E1000_PSCR_AUTO_MDIX;
0769 
0770     switch (phy->mdix) {
0771     case 1:
0772         data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
0773         break;
0774     case 2:
0775         data |= IGP01E1000_PSCR_FORCE_MDI_MDIX;
0776         break;
0777     case 0:
0778     default:
0779         data |= IGP01E1000_PSCR_AUTO_MDIX;
0780         break;
0781     }
0782     ret_val = phy->ops.write_reg(hw, IGP01E1000_PHY_PORT_CTRL, data);
0783     if (ret_val)
0784         goto out;
0785 
0786     /* set auto-master slave resolution settings */
0787     if (hw->mac.autoneg) {
0788         /* when autonegotiation advertisement is only 1000Mbps then we
0789          * should disable SmartSpeed and enable Auto MasterSlave
0790          * resolution as hardware default.
0791          */
0792         if (phy->autoneg_advertised == ADVERTISE_1000_FULL) {
0793             /* Disable SmartSpeed */
0794             ret_val = phy->ops.read_reg(hw,
0795                             IGP01E1000_PHY_PORT_CONFIG,
0796                             &data);
0797             if (ret_val)
0798                 goto out;
0799 
0800             data &= ~IGP01E1000_PSCFR_SMART_SPEED;
0801             ret_val = phy->ops.write_reg(hw,
0802                              IGP01E1000_PHY_PORT_CONFIG,
0803                              data);
0804             if (ret_val)
0805                 goto out;
0806 
0807             /* Set auto Master/Slave resolution process */
0808             ret_val = phy->ops.read_reg(hw, PHY_1000T_CTRL, &data);
0809             if (ret_val)
0810                 goto out;
0811 
0812             data &= ~CR_1000T_MS_ENABLE;
0813             ret_val = phy->ops.write_reg(hw, PHY_1000T_CTRL, data);
0814             if (ret_val)
0815                 goto out;
0816         }
0817 
0818         ret_val = phy->ops.read_reg(hw, PHY_1000T_CTRL, &data);
0819         if (ret_val)
0820             goto out;
0821 
0822         /* load defaults for future use */
0823         phy->original_ms_type = (data & CR_1000T_MS_ENABLE) ?
0824             ((data & CR_1000T_MS_VALUE) ?
0825             e1000_ms_force_master :
0826             e1000_ms_force_slave) :
0827             e1000_ms_auto;
0828 
0829         switch (phy->ms_type) {
0830         case e1000_ms_force_master:
0831             data |= (CR_1000T_MS_ENABLE | CR_1000T_MS_VALUE);
0832             break;
0833         case e1000_ms_force_slave:
0834             data |= CR_1000T_MS_ENABLE;
0835             data &= ~(CR_1000T_MS_VALUE);
0836             break;
0837         case e1000_ms_auto:
0838             data &= ~CR_1000T_MS_ENABLE;
0839             break;
0840         default:
0841             break;
0842         }
0843         ret_val = phy->ops.write_reg(hw, PHY_1000T_CTRL, data);
0844         if (ret_val)
0845             goto out;
0846     }
0847 
0848 out:
0849     return ret_val;
0850 }
0851 
0852 /**
0853  *  igb_copper_link_autoneg - Setup/Enable autoneg for copper link
0854  *  @hw: pointer to the HW structure
0855  *
0856  *  Performs initial bounds checking on autoneg advertisement parameter, then
0857  *  configure to advertise the full capability.  Setup the PHY to autoneg
0858  *  and restart the negotiation process between the link partner.  If
0859  *  autoneg_wait_to_complete, then wait for autoneg to complete before exiting.
0860  **/
0861 static s32 igb_copper_link_autoneg(struct e1000_hw *hw)
0862 {
0863     struct e1000_phy_info *phy = &hw->phy;
0864     s32 ret_val;
0865     u16 phy_ctrl;
0866 
0867     /* Perform some bounds checking on the autoneg advertisement
0868      * parameter.
0869      */
0870     phy->autoneg_advertised &= phy->autoneg_mask;
0871 
0872     /* If autoneg_advertised is zero, we assume it was not defaulted
0873      * by the calling code so we set to advertise full capability.
0874      */
0875     if (phy->autoneg_advertised == 0)
0876         phy->autoneg_advertised = phy->autoneg_mask;
0877 
0878     hw_dbg("Reconfiguring auto-neg advertisement params\n");
0879     ret_val = igb_phy_setup_autoneg(hw);
0880     if (ret_val) {
0881         hw_dbg("Error Setting up Auto-Negotiation\n");
0882         goto out;
0883     }
0884     hw_dbg("Restarting Auto-Neg\n");
0885 
0886     /* Restart auto-negotiation by setting the Auto Neg Enable bit and
0887      * the Auto Neg Restart bit in the PHY control register.
0888      */
0889     ret_val = phy->ops.read_reg(hw, PHY_CONTROL, &phy_ctrl);
0890     if (ret_val)
0891         goto out;
0892 
0893     phy_ctrl |= (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
0894     ret_val = phy->ops.write_reg(hw, PHY_CONTROL, phy_ctrl);
0895     if (ret_val)
0896         goto out;
0897 
0898     /* Does the user want to wait for Auto-Neg to complete here, or
0899      * check at a later time (for example, callback routine).
0900      */
0901     if (phy->autoneg_wait_to_complete) {
0902         ret_val = igb_wait_autoneg(hw);
0903         if (ret_val) {
0904             hw_dbg("Error while waiting for autoneg to complete\n");
0905             goto out;
0906         }
0907     }
0908 
0909     hw->mac.get_link_status = true;
0910 
0911 out:
0912     return ret_val;
0913 }
0914 
0915 /**
0916  *  igb_phy_setup_autoneg - Configure PHY for auto-negotiation
0917  *  @hw: pointer to the HW structure
0918  *
0919  *  Reads the MII auto-neg advertisement register and/or the 1000T control
0920  *  register and if the PHY is already setup for auto-negotiation, then
0921  *  return successful.  Otherwise, setup advertisement and flow control to
0922  *  the appropriate values for the wanted auto-negotiation.
0923  **/
0924 static s32 igb_phy_setup_autoneg(struct e1000_hw *hw)
0925 {
0926     struct e1000_phy_info *phy = &hw->phy;
0927     s32 ret_val;
0928     u16 mii_autoneg_adv_reg;
0929     u16 mii_1000t_ctrl_reg = 0;
0930 
0931     phy->autoneg_advertised &= phy->autoneg_mask;
0932 
0933     /* Read the MII Auto-Neg Advertisement Register (Address 4). */
0934     ret_val = phy->ops.read_reg(hw, PHY_AUTONEG_ADV, &mii_autoneg_adv_reg);
0935     if (ret_val)
0936         goto out;
0937 
0938     if (phy->autoneg_mask & ADVERTISE_1000_FULL) {
0939         /* Read the MII 1000Base-T Control Register (Address 9). */
0940         ret_val = phy->ops.read_reg(hw, PHY_1000T_CTRL,
0941                         &mii_1000t_ctrl_reg);
0942         if (ret_val)
0943             goto out;
0944     }
0945 
0946     /* Need to parse both autoneg_advertised and fc and set up
0947      * the appropriate PHY registers.  First we will parse for
0948      * autoneg_advertised software override.  Since we can advertise
0949      * a plethora of combinations, we need to check each bit
0950      * individually.
0951      */
0952 
0953     /* First we clear all the 10/100 mb speed bits in the Auto-Neg
0954      * Advertisement Register (Address 4) and the 1000 mb speed bits in
0955      * the  1000Base-T Control Register (Address 9).
0956      */
0957     mii_autoneg_adv_reg &= ~(NWAY_AR_100TX_FD_CAPS |
0958                  NWAY_AR_100TX_HD_CAPS |
0959                  NWAY_AR_10T_FD_CAPS   |
0960                  NWAY_AR_10T_HD_CAPS);
0961     mii_1000t_ctrl_reg &= ~(CR_1000T_HD_CAPS | CR_1000T_FD_CAPS);
0962 
0963     hw_dbg("autoneg_advertised %x\n", phy->autoneg_advertised);
0964 
0965     /* Do we want to advertise 10 Mb Half Duplex? */
0966     if (phy->autoneg_advertised & ADVERTISE_10_HALF) {
0967         hw_dbg("Advertise 10mb Half duplex\n");
0968         mii_autoneg_adv_reg |= NWAY_AR_10T_HD_CAPS;
0969     }
0970 
0971     /* Do we want to advertise 10 Mb Full Duplex? */
0972     if (phy->autoneg_advertised & ADVERTISE_10_FULL) {
0973         hw_dbg("Advertise 10mb Full duplex\n");
0974         mii_autoneg_adv_reg |= NWAY_AR_10T_FD_CAPS;
0975     }
0976 
0977     /* Do we want to advertise 100 Mb Half Duplex? */
0978     if (phy->autoneg_advertised & ADVERTISE_100_HALF) {
0979         hw_dbg("Advertise 100mb Half duplex\n");
0980         mii_autoneg_adv_reg |= NWAY_AR_100TX_HD_CAPS;
0981     }
0982 
0983     /* Do we want to advertise 100 Mb Full Duplex? */
0984     if (phy->autoneg_advertised & ADVERTISE_100_FULL) {
0985         hw_dbg("Advertise 100mb Full duplex\n");
0986         mii_autoneg_adv_reg |= NWAY_AR_100TX_FD_CAPS;
0987     }
0988 
0989     /* We do not allow the Phy to advertise 1000 Mb Half Duplex */
0990     if (phy->autoneg_advertised & ADVERTISE_1000_HALF)
0991         hw_dbg("Advertise 1000mb Half duplex request denied!\n");
0992 
0993     /* Do we want to advertise 1000 Mb Full Duplex? */
0994     if (phy->autoneg_advertised & ADVERTISE_1000_FULL) {
0995         hw_dbg("Advertise 1000mb Full duplex\n");
0996         mii_1000t_ctrl_reg |= CR_1000T_FD_CAPS;
0997     }
0998 
0999     /* Check for a software override of the flow control settings, and
1000      * setup the PHY advertisement registers accordingly.  If
1001      * auto-negotiation is enabled, then software will have to set the
1002      * "PAUSE" bits to the correct value in the Auto-Negotiation
1003      * Advertisement Register (PHY_AUTONEG_ADV) and re-start auto-
1004      * negotiation.
1005      *
1006      * The possible values of the "fc" parameter are:
1007      *      0:  Flow control is completely disabled
1008      *      1:  Rx flow control is enabled (we can receive pause frames
1009      *          but not send pause frames).
1010      *      2:  Tx flow control is enabled (we can send pause frames
1011      *          but we do not support receiving pause frames).
1012      *      3:  Both Rx and TX flow control (symmetric) are enabled.
1013      *  other:  No software override.  The flow control configuration
1014      *          in the EEPROM is used.
1015      */
1016     switch (hw->fc.current_mode) {
1017     case e1000_fc_none:
1018         /* Flow control (RX & TX) is completely disabled by a
1019          * software over-ride.
1020          */
1021         mii_autoneg_adv_reg &= ~(NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
1022         break;
1023     case e1000_fc_rx_pause:
1024         /* RX Flow control is enabled, and TX Flow control is
1025          * disabled, by a software over-ride.
1026          *
1027          * Since there really isn't a way to advertise that we are
1028          * capable of RX Pause ONLY, we will advertise that we
1029          * support both symmetric and asymmetric RX PAUSE.  Later
1030          * (in e1000_config_fc_after_link_up) we will disable the
1031          * hw's ability to send PAUSE frames.
1032          */
1033         mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
1034         break;
1035     case e1000_fc_tx_pause:
1036         /* TX Flow control is enabled, and RX Flow control is
1037          * disabled, by a software over-ride.
1038          */
1039         mii_autoneg_adv_reg |= NWAY_AR_ASM_DIR;
1040         mii_autoneg_adv_reg &= ~NWAY_AR_PAUSE;
1041         break;
1042     case e1000_fc_full:
1043         /* Flow control (both RX and TX) is enabled by a software
1044          * over-ride.
1045          */
1046         mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
1047         break;
1048     default:
1049         hw_dbg("Flow control param set incorrectly\n");
1050         ret_val = -E1000_ERR_CONFIG;
1051         goto out;
1052     }
1053 
1054     ret_val = phy->ops.write_reg(hw, PHY_AUTONEG_ADV, mii_autoneg_adv_reg);
1055     if (ret_val)
1056         goto out;
1057 
1058     hw_dbg("Auto-Neg Advertising %x\n", mii_autoneg_adv_reg);
1059 
1060     if (phy->autoneg_mask & ADVERTISE_1000_FULL) {
1061         ret_val = phy->ops.write_reg(hw,
1062                          PHY_1000T_CTRL,
1063                          mii_1000t_ctrl_reg);
1064         if (ret_val)
1065             goto out;
1066     }
1067 
1068 out:
1069     return ret_val;
1070 }
1071 
1072 /**
1073  *  igb_setup_copper_link - Configure copper link settings
1074  *  @hw: pointer to the HW structure
1075  *
1076  *  Calls the appropriate function to configure the link for auto-neg or forced
1077  *  speed and duplex.  Then we check for link, once link is established calls
1078  *  to configure collision distance and flow control are called.  If link is
1079  *  not established, we return -E1000_ERR_PHY (-2).
1080  **/
1081 s32 igb_setup_copper_link(struct e1000_hw *hw)
1082 {
1083     s32 ret_val;
1084     bool link;
1085 
1086     if (hw->mac.autoneg) {
1087         /* Setup autoneg and flow control advertisement and perform
1088          * autonegotiation.
1089          */
1090         ret_val = igb_copper_link_autoneg(hw);
1091         if (ret_val)
1092             goto out;
1093     } else {
1094         /* PHY will be set to 10H, 10F, 100H or 100F
1095          * depending on user settings.
1096          */
1097         hw_dbg("Forcing Speed and Duplex\n");
1098         ret_val = hw->phy.ops.force_speed_duplex(hw);
1099         if (ret_val) {
1100             hw_dbg("Error Forcing Speed and Duplex\n");
1101             goto out;
1102         }
1103     }
1104 
1105     /* Check link status. Wait up to 100 microseconds for link to become
1106      * valid.
1107      */
1108     ret_val = igb_phy_has_link(hw, COPPER_LINK_UP_LIMIT, 10, &link);
1109     if (ret_val)
1110         goto out;
1111 
1112     if (link) {
1113         hw_dbg("Valid link established!!!\n");
1114         igb_config_collision_dist(hw);
1115         ret_val = igb_config_fc_after_link_up(hw);
1116     } else {
1117         hw_dbg("Unable to establish link!!!\n");
1118     }
1119 
1120 out:
1121     return ret_val;
1122 }
1123 
1124 /**
1125  *  igb_phy_force_speed_duplex_igp - Force speed/duplex for igp PHY
1126  *  @hw: pointer to the HW structure
1127  *
1128  *  Calls the PHY setup function to force speed and duplex.  Clears the
1129  *  auto-crossover to force MDI manually.  Waits for link and returns
1130  *  successful if link up is successful, else -E1000_ERR_PHY (-2).
1131  **/
1132 s32 igb_phy_force_speed_duplex_igp(struct e1000_hw *hw)
1133 {
1134     struct e1000_phy_info *phy = &hw->phy;
1135     s32 ret_val;
1136     u16 phy_data;
1137     bool link;
1138 
1139     ret_val = phy->ops.read_reg(hw, PHY_CONTROL, &phy_data);
1140     if (ret_val)
1141         goto out;
1142 
1143     igb_phy_force_speed_duplex_setup(hw, &phy_data);
1144 
1145     ret_val = phy->ops.write_reg(hw, PHY_CONTROL, phy_data);
1146     if (ret_val)
1147         goto out;
1148 
1149     /* Clear Auto-Crossover to force MDI manually.  IGP requires MDI
1150      * forced whenever speed and duplex are forced.
1151      */
1152     ret_val = phy->ops.read_reg(hw, IGP01E1000_PHY_PORT_CTRL, &phy_data);
1153     if (ret_val)
1154         goto out;
1155 
1156     phy_data &= ~IGP01E1000_PSCR_AUTO_MDIX;
1157     phy_data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
1158 
1159     ret_val = phy->ops.write_reg(hw, IGP01E1000_PHY_PORT_CTRL, phy_data);
1160     if (ret_val)
1161         goto out;
1162 
1163     hw_dbg("IGP PSCR: %X\n", phy_data);
1164 
1165     udelay(1);
1166 
1167     if (phy->autoneg_wait_to_complete) {
1168         hw_dbg("Waiting for forced speed/duplex link on IGP phy.\n");
1169 
1170         ret_val = igb_phy_has_link(hw, PHY_FORCE_LIMIT, 10000, &link);
1171         if (ret_val)
1172             goto out;
1173 
1174         if (!link)
1175             hw_dbg("Link taking longer than expected.\n");
1176 
1177         /* Try once more */
1178         ret_val = igb_phy_has_link(hw, PHY_FORCE_LIMIT, 10000, &link);
1179         if (ret_val)
1180             goto out;
1181     }
1182 
1183 out:
1184     return ret_val;
1185 }
1186 
1187 /**
1188  *  igb_phy_force_speed_duplex_m88 - Force speed/duplex for m88 PHY
1189  *  @hw: pointer to the HW structure
1190  *
1191  *  Calls the PHY setup function to force speed and duplex.  Clears the
1192  *  auto-crossover to force MDI manually.  Resets the PHY to commit the
1193  *  changes.  If time expires while waiting for link up, we reset the DSP.
1194  *  After reset, TX_CLK and CRS on TX must be set.  Return successful upon
1195  *  successful completion, else return corresponding error code.
1196  **/
1197 s32 igb_phy_force_speed_duplex_m88(struct e1000_hw *hw)
1198 {
1199     struct e1000_phy_info *phy = &hw->phy;
1200     s32 ret_val;
1201     u16 phy_data;
1202     bool link;
1203 
1204     /* I210 and I211 devices support Auto-Crossover in forced operation. */
1205     if (phy->type != e1000_phy_i210) {
1206         /* Clear Auto-Crossover to force MDI manually.  M88E1000
1207          * requires MDI forced whenever speed and duplex are forced.
1208          */
1209         ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_CTRL,
1210                         &phy_data);
1211         if (ret_val)
1212             goto out;
1213 
1214         phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
1215         ret_val = phy->ops.write_reg(hw, M88E1000_PHY_SPEC_CTRL,
1216                          phy_data);
1217         if (ret_val)
1218             goto out;
1219 
1220         hw_dbg("M88E1000 PSCR: %X\n", phy_data);
1221     }
1222 
1223     ret_val = phy->ops.read_reg(hw, PHY_CONTROL, &phy_data);
1224     if (ret_val)
1225         goto out;
1226 
1227     igb_phy_force_speed_duplex_setup(hw, &phy_data);
1228 
1229     ret_val = phy->ops.write_reg(hw, PHY_CONTROL, phy_data);
1230     if (ret_val)
1231         goto out;
1232 
1233     /* Reset the phy to commit changes. */
1234     ret_val = igb_phy_sw_reset(hw);
1235     if (ret_val)
1236         goto out;
1237 
1238     if (phy->autoneg_wait_to_complete) {
1239         hw_dbg("Waiting for forced speed/duplex link on M88 phy.\n");
1240 
1241         ret_val = igb_phy_has_link(hw, PHY_FORCE_LIMIT, 100000, &link);
1242         if (ret_val)
1243             goto out;
1244 
1245         if (!link) {
1246             bool reset_dsp = true;
1247 
1248             switch (hw->phy.id) {
1249             case I347AT4_E_PHY_ID:
1250             case M88E1112_E_PHY_ID:
1251             case M88E1543_E_PHY_ID:
1252             case M88E1512_E_PHY_ID:
1253             case I210_I_PHY_ID:
1254                 reset_dsp = false;
1255                 break;
1256             default:
1257                 if (hw->phy.type != e1000_phy_m88)
1258                     reset_dsp = false;
1259                 break;
1260             }
1261             if (!reset_dsp) {
1262                 hw_dbg("Link taking longer than expected.\n");
1263             } else {
1264                 /* We didn't get link.
1265                  * Reset the DSP and cross our fingers.
1266                  */
1267                 ret_val = phy->ops.write_reg(hw,
1268                         M88E1000_PHY_PAGE_SELECT,
1269                         0x001d);
1270                 if (ret_val)
1271                     goto out;
1272                 ret_val = igb_phy_reset_dsp(hw);
1273                 if (ret_val)
1274                     goto out;
1275             }
1276         }
1277 
1278         /* Try once more */
1279         ret_val = igb_phy_has_link(hw, PHY_FORCE_LIMIT,
1280                        100000, &link);
1281         if (ret_val)
1282             goto out;
1283     }
1284 
1285     if (hw->phy.type != e1000_phy_m88 ||
1286         hw->phy.id == I347AT4_E_PHY_ID ||
1287         hw->phy.id == M88E1112_E_PHY_ID ||
1288         hw->phy.id == M88E1543_E_PHY_ID ||
1289         hw->phy.id == M88E1512_E_PHY_ID ||
1290         hw->phy.id == I210_I_PHY_ID)
1291         goto out;
1292 
1293     ret_val = phy->ops.read_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, &phy_data);
1294     if (ret_val)
1295         goto out;
1296 
1297     /* Resetting the phy means we need to re-force TX_CLK in the
1298      * Extended PHY Specific Control Register to 25MHz clock from
1299      * the reset value of 2.5MHz.
1300      */
1301     phy_data |= M88E1000_EPSCR_TX_CLK_25;
1302     ret_val = phy->ops.write_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
1303     if (ret_val)
1304         goto out;
1305 
1306     /* In addition, we must re-enable CRS on Tx for both half and full
1307      * duplex.
1308      */
1309     ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
1310     if (ret_val)
1311         goto out;
1312 
1313     phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
1314     ret_val = phy->ops.write_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
1315 
1316 out:
1317     return ret_val;
1318 }
1319 
1320 /**
1321  *  igb_phy_force_speed_duplex_setup - Configure forced PHY speed/duplex
1322  *  @hw: pointer to the HW structure
1323  *  @phy_ctrl: pointer to current value of PHY_CONTROL
1324  *
1325  *  Forces speed and duplex on the PHY by doing the following: disable flow
1326  *  control, force speed/duplex on the MAC, disable auto speed detection,
1327  *  disable auto-negotiation, configure duplex, configure speed, configure
1328  *  the collision distance, write configuration to CTRL register.  The
1329  *  caller must write to the PHY_CONTROL register for these settings to
1330  *  take affect.
1331  **/
1332 static void igb_phy_force_speed_duplex_setup(struct e1000_hw *hw,
1333                          u16 *phy_ctrl)
1334 {
1335     struct e1000_mac_info *mac = &hw->mac;
1336     u32 ctrl;
1337 
1338     /* Turn off flow control when forcing speed/duplex */
1339     hw->fc.current_mode = e1000_fc_none;
1340 
1341     /* Force speed/duplex on the mac */
1342     ctrl = rd32(E1000_CTRL);
1343     ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
1344     ctrl &= ~E1000_CTRL_SPD_SEL;
1345 
1346     /* Disable Auto Speed Detection */
1347     ctrl &= ~E1000_CTRL_ASDE;
1348 
1349     /* Disable autoneg on the phy */
1350     *phy_ctrl &= ~MII_CR_AUTO_NEG_EN;
1351 
1352     /* Forcing Full or Half Duplex? */
1353     if (mac->forced_speed_duplex & E1000_ALL_HALF_DUPLEX) {
1354         ctrl &= ~E1000_CTRL_FD;
1355         *phy_ctrl &= ~MII_CR_FULL_DUPLEX;
1356         hw_dbg("Half Duplex\n");
1357     } else {
1358         ctrl |= E1000_CTRL_FD;
1359         *phy_ctrl |= MII_CR_FULL_DUPLEX;
1360         hw_dbg("Full Duplex\n");
1361     }
1362 
1363     /* Forcing 10mb or 100mb? */
1364     if (mac->forced_speed_duplex & E1000_ALL_100_SPEED) {
1365         ctrl |= E1000_CTRL_SPD_100;
1366         *phy_ctrl |= MII_CR_SPEED_100;
1367         *phy_ctrl &= ~(MII_CR_SPEED_1000 | MII_CR_SPEED_10);
1368         hw_dbg("Forcing 100mb\n");
1369     } else {
1370         ctrl &= ~(E1000_CTRL_SPD_1000 | E1000_CTRL_SPD_100);
1371         *phy_ctrl |= MII_CR_SPEED_10;
1372         *phy_ctrl &= ~(MII_CR_SPEED_1000 | MII_CR_SPEED_100);
1373         hw_dbg("Forcing 10mb\n");
1374     }
1375 
1376     igb_config_collision_dist(hw);
1377 
1378     wr32(E1000_CTRL, ctrl);
1379 }
1380 
1381 /**
1382  *  igb_set_d3_lplu_state - Sets low power link up state for D3
1383  *  @hw: pointer to the HW structure
1384  *  @active: boolean used to enable/disable lplu
1385  *
1386  *  Success returns 0, Failure returns 1
1387  *
1388  *  The low power link up (lplu) state is set to the power management level D3
1389  *  and SmartSpeed is disabled when active is true, else clear lplu for D3
1390  *  and enable Smartspeed.  LPLU and Smartspeed are mutually exclusive.  LPLU
1391  *  is used during Dx states where the power conservation is most important.
1392  *  During driver activity, SmartSpeed should be enabled so performance is
1393  *  maintained.
1394  **/
1395 s32 igb_set_d3_lplu_state(struct e1000_hw *hw, bool active)
1396 {
1397     struct e1000_phy_info *phy = &hw->phy;
1398     s32 ret_val = 0;
1399     u16 data;
1400 
1401     if (!(hw->phy.ops.read_reg))
1402         goto out;
1403 
1404     ret_val = phy->ops.read_reg(hw, IGP02E1000_PHY_POWER_MGMT, &data);
1405     if (ret_val)
1406         goto out;
1407 
1408     if (!active) {
1409         data &= ~IGP02E1000_PM_D3_LPLU;
1410         ret_val = phy->ops.write_reg(hw, IGP02E1000_PHY_POWER_MGMT,
1411                          data);
1412         if (ret_val)
1413             goto out;
1414         /* LPLU and SmartSpeed are mutually exclusive.  LPLU is used
1415          * during Dx states where the power conservation is most
1416          * important.  During driver activity we should enable
1417          * SmartSpeed, so performance is maintained.
1418          */
1419         if (phy->smart_speed == e1000_smart_speed_on) {
1420             ret_val = phy->ops.read_reg(hw,
1421                             IGP01E1000_PHY_PORT_CONFIG,
1422                             &data);
1423             if (ret_val)
1424                 goto out;
1425 
1426             data |= IGP01E1000_PSCFR_SMART_SPEED;
1427             ret_val = phy->ops.write_reg(hw,
1428                              IGP01E1000_PHY_PORT_CONFIG,
1429                              data);
1430             if (ret_val)
1431                 goto out;
1432         } else if (phy->smart_speed == e1000_smart_speed_off) {
1433             ret_val = phy->ops.read_reg(hw,
1434                              IGP01E1000_PHY_PORT_CONFIG,
1435                              &data);
1436             if (ret_val)
1437                 goto out;
1438 
1439             data &= ~IGP01E1000_PSCFR_SMART_SPEED;
1440             ret_val = phy->ops.write_reg(hw,
1441                              IGP01E1000_PHY_PORT_CONFIG,
1442                              data);
1443             if (ret_val)
1444                 goto out;
1445         }
1446     } else if ((phy->autoneg_advertised == E1000_ALL_SPEED_DUPLEX) ||
1447            (phy->autoneg_advertised == E1000_ALL_NOT_GIG) ||
1448            (phy->autoneg_advertised == E1000_ALL_10_SPEED)) {
1449         data |= IGP02E1000_PM_D3_LPLU;
1450         ret_val = phy->ops.write_reg(hw, IGP02E1000_PHY_POWER_MGMT,
1451                           data);
1452         if (ret_val)
1453             goto out;
1454 
1455         /* When LPLU is enabled, we should disable SmartSpeed */
1456         ret_val = phy->ops.read_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
1457                         &data);
1458         if (ret_val)
1459             goto out;
1460 
1461         data &= ~IGP01E1000_PSCFR_SMART_SPEED;
1462         ret_val = phy->ops.write_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
1463                          data);
1464     }
1465 
1466 out:
1467     return ret_val;
1468 }
1469 
1470 /**
1471  *  igb_check_downshift - Checks whether a downshift in speed occurred
1472  *  @hw: pointer to the HW structure
1473  *
1474  *  Success returns 0, Failure returns 1
1475  *
1476  *  A downshift is detected by querying the PHY link health.
1477  **/
1478 s32 igb_check_downshift(struct e1000_hw *hw)
1479 {
1480     struct e1000_phy_info *phy = &hw->phy;
1481     s32 ret_val;
1482     u16 phy_data, offset, mask;
1483 
1484     switch (phy->type) {
1485     case e1000_phy_i210:
1486     case e1000_phy_m88:
1487     case e1000_phy_gg82563:
1488         offset  = M88E1000_PHY_SPEC_STATUS;
1489         mask    = M88E1000_PSSR_DOWNSHIFT;
1490         break;
1491     case e1000_phy_igp_2:
1492     case e1000_phy_igp:
1493     case e1000_phy_igp_3:
1494         offset  = IGP01E1000_PHY_LINK_HEALTH;
1495         mask    = IGP01E1000_PLHR_SS_DOWNGRADE;
1496         break;
1497     default:
1498         /* speed downshift not supported */
1499         phy->speed_downgraded = false;
1500         ret_val = 0;
1501         goto out;
1502     }
1503 
1504     ret_val = phy->ops.read_reg(hw, offset, &phy_data);
1505 
1506     if (!ret_val)
1507         phy->speed_downgraded = (phy_data & mask) ? true : false;
1508 
1509 out:
1510     return ret_val;
1511 }
1512 
1513 /**
1514  *  igb_check_polarity_m88 - Checks the polarity.
1515  *  @hw: pointer to the HW structure
1516  *
1517  *  Success returns 0, Failure returns -E1000_ERR_PHY (-2)
1518  *
1519  *  Polarity is determined based on the PHY specific status register.
1520  **/
1521 s32 igb_check_polarity_m88(struct e1000_hw *hw)
1522 {
1523     struct e1000_phy_info *phy = &hw->phy;
1524     s32 ret_val;
1525     u16 data;
1526 
1527     ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_STATUS, &data);
1528 
1529     if (!ret_val)
1530         phy->cable_polarity = (data & M88E1000_PSSR_REV_POLARITY)
1531                       ? e1000_rev_polarity_reversed
1532                       : e1000_rev_polarity_normal;
1533 
1534     return ret_val;
1535 }
1536 
1537 /**
1538  *  igb_check_polarity_igp - Checks the polarity.
1539  *  @hw: pointer to the HW structure
1540  *
1541  *  Success returns 0, Failure returns -E1000_ERR_PHY (-2)
1542  *
1543  *  Polarity is determined based on the PHY port status register, and the
1544  *  current speed (since there is no polarity at 100Mbps).
1545  **/
1546 static s32 igb_check_polarity_igp(struct e1000_hw *hw)
1547 {
1548     struct e1000_phy_info *phy = &hw->phy;
1549     s32 ret_val;
1550     u16 data, offset, mask;
1551 
1552     /* Polarity is determined based on the speed of
1553      * our connection.
1554      */
1555     ret_val = phy->ops.read_reg(hw, IGP01E1000_PHY_PORT_STATUS, &data);
1556     if (ret_val)
1557         goto out;
1558 
1559     if ((data & IGP01E1000_PSSR_SPEED_MASK) ==
1560         IGP01E1000_PSSR_SPEED_1000MBPS) {
1561         offset  = IGP01E1000_PHY_PCS_INIT_REG;
1562         mask    = IGP01E1000_PHY_POLARITY_MASK;
1563     } else {
1564         /* This really only applies to 10Mbps since
1565          * there is no polarity for 100Mbps (always 0).
1566          */
1567         offset  = IGP01E1000_PHY_PORT_STATUS;
1568         mask    = IGP01E1000_PSSR_POLARITY_REVERSED;
1569     }
1570 
1571     ret_val = phy->ops.read_reg(hw, offset, &data);
1572 
1573     if (!ret_val)
1574         phy->cable_polarity = (data & mask)
1575                       ? e1000_rev_polarity_reversed
1576                       : e1000_rev_polarity_normal;
1577 
1578 out:
1579     return ret_val;
1580 }
1581 
1582 /**
1583  *  igb_wait_autoneg - Wait for auto-neg completion
1584  *  @hw: pointer to the HW structure
1585  *
1586  *  Waits for auto-negotiation to complete or for the auto-negotiation time
1587  *  limit to expire, which ever happens first.
1588  **/
1589 static s32 igb_wait_autoneg(struct e1000_hw *hw)
1590 {
1591     s32 ret_val = 0;
1592     u16 i, phy_status;
1593 
1594     /* Break after autoneg completes or PHY_AUTO_NEG_LIMIT expires. */
1595     for (i = PHY_AUTO_NEG_LIMIT; i > 0; i--) {
1596         ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS, &phy_status);
1597         if (ret_val)
1598             break;
1599         ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS, &phy_status);
1600         if (ret_val)
1601             break;
1602         if (phy_status & MII_SR_AUTONEG_COMPLETE)
1603             break;
1604         msleep(100);
1605     }
1606 
1607     /* PHY_AUTO_NEG_TIME expiration doesn't guarantee auto-negotiation
1608      * has completed.
1609      */
1610     return ret_val;
1611 }
1612 
1613 /**
1614  *  igb_phy_has_link - Polls PHY for link
1615  *  @hw: pointer to the HW structure
1616  *  @iterations: number of times to poll for link
1617  *  @usec_interval: delay between polling attempts
1618  *  @success: pointer to whether polling was successful or not
1619  *
1620  *  Polls the PHY status register for link, 'iterations' number of times.
1621  **/
1622 s32 igb_phy_has_link(struct e1000_hw *hw, u32 iterations,
1623              u32 usec_interval, bool *success)
1624 {
1625     s32 ret_val = 0;
1626     u16 i, phy_status;
1627 
1628     for (i = 0; i < iterations; i++) {
1629         /* Some PHYs require the PHY_STATUS register to be read
1630          * twice due to the link bit being sticky.  No harm doing
1631          * it across the board.
1632          */
1633         ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS, &phy_status);
1634         if (ret_val && usec_interval > 0) {
1635             /* If the first read fails, another entity may have
1636              * ownership of the resources, wait and try again to
1637              * see if they have relinquished the resources yet.
1638              */
1639             if (usec_interval >= 1000)
1640                 mdelay(usec_interval/1000);
1641             else
1642                 udelay(usec_interval);
1643         }
1644         ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS, &phy_status);
1645         if (ret_val)
1646             break;
1647         if (phy_status & MII_SR_LINK_STATUS)
1648             break;
1649         if (usec_interval >= 1000)
1650             mdelay(usec_interval/1000);
1651         else
1652             udelay(usec_interval);
1653     }
1654 
1655     *success = (i < iterations) ? true : false;
1656 
1657     return ret_val;
1658 }
1659 
1660 /**
1661  *  igb_get_cable_length_m88 - Determine cable length for m88 PHY
1662  *  @hw: pointer to the HW structure
1663  *
1664  *  Reads the PHY specific status register to retrieve the cable length
1665  *  information.  The cable length is determined by averaging the minimum and
1666  *  maximum values to get the "average" cable length.  The m88 PHY has four
1667  *  possible cable length values, which are:
1668  *  Register Value      Cable Length
1669  *  0           < 50 meters
1670  *  1           50 - 80 meters
1671  *  2           80 - 110 meters
1672  *  3           110 - 140 meters
1673  *  4           > 140 meters
1674  **/
1675 s32 igb_get_cable_length_m88(struct e1000_hw *hw)
1676 {
1677     struct e1000_phy_info *phy = &hw->phy;
1678     s32 ret_val;
1679     u16 phy_data, index;
1680 
1681     ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data);
1682     if (ret_val)
1683         goto out;
1684 
1685     index = (phy_data & M88E1000_PSSR_CABLE_LENGTH) >>
1686         M88E1000_PSSR_CABLE_LENGTH_SHIFT;
1687     if (index >= ARRAY_SIZE(e1000_m88_cable_length_table) - 1) {
1688         ret_val = -E1000_ERR_PHY;
1689         goto out;
1690     }
1691 
1692     phy->min_cable_length = e1000_m88_cable_length_table[index];
1693     phy->max_cable_length = e1000_m88_cable_length_table[index + 1];
1694 
1695     phy->cable_length = (phy->min_cable_length + phy->max_cable_length) / 2;
1696 
1697 out:
1698     return ret_val;
1699 }
1700 
1701 s32 igb_get_cable_length_m88_gen2(struct e1000_hw *hw)
1702 {
1703     struct e1000_phy_info *phy = &hw->phy;
1704     s32 ret_val;
1705     u16 phy_data, phy_data2, index, default_page, is_cm;
1706     int len_tot = 0;
1707     u16 len_min;
1708     u16 len_max;
1709 
1710     switch (hw->phy.id) {
1711     case M88E1543_E_PHY_ID:
1712     case M88E1512_E_PHY_ID:
1713     case I347AT4_E_PHY_ID:
1714     case I210_I_PHY_ID:
1715         /* Remember the original page select and set it to 7 */
1716         ret_val = phy->ops.read_reg(hw, I347AT4_PAGE_SELECT,
1717                         &default_page);
1718         if (ret_val)
1719             goto out;
1720 
1721         ret_val = phy->ops.write_reg(hw, I347AT4_PAGE_SELECT, 0x07);
1722         if (ret_val)
1723             goto out;
1724 
1725         /* Check if the unit of cable length is meters or cm */
1726         ret_val = phy->ops.read_reg(hw, I347AT4_PCDC, &phy_data2);
1727         if (ret_val)
1728             goto out;
1729 
1730         is_cm = !(phy_data2 & I347AT4_PCDC_CABLE_LENGTH_UNIT);
1731 
1732         /* Get cable length from Pair 0 length Regs */
1733         ret_val = phy->ops.read_reg(hw, I347AT4_PCDL0, &phy_data);
1734         if (ret_val)
1735             goto out;
1736 
1737         phy->pair_length[0] = phy_data / (is_cm ? 100 : 1);
1738         len_tot = phy->pair_length[0];
1739         len_min = phy->pair_length[0];
1740         len_max = phy->pair_length[0];
1741 
1742         /* Get cable length from Pair 1 length Regs */
1743         ret_val = phy->ops.read_reg(hw, I347AT4_PCDL1, &phy_data);
1744         if (ret_val)
1745             goto out;
1746 
1747         phy->pair_length[1] = phy_data / (is_cm ? 100 : 1);
1748         len_tot += phy->pair_length[1];
1749         len_min = min(len_min, phy->pair_length[1]);
1750         len_max = max(len_max, phy->pair_length[1]);
1751 
1752         /* Get cable length from Pair 2 length Regs */
1753         ret_val = phy->ops.read_reg(hw, I347AT4_PCDL2, &phy_data);
1754         if (ret_val)
1755             goto out;
1756 
1757         phy->pair_length[2] = phy_data / (is_cm ? 100 : 1);
1758         len_tot += phy->pair_length[2];
1759         len_min = min(len_min, phy->pair_length[2]);
1760         len_max = max(len_max, phy->pair_length[2]);
1761 
1762         /* Get cable length from Pair 3 length Regs */
1763         ret_val = phy->ops.read_reg(hw, I347AT4_PCDL3, &phy_data);
1764         if (ret_val)
1765             goto out;
1766 
1767         phy->pair_length[3] = phy_data / (is_cm ? 100 : 1);
1768         len_tot += phy->pair_length[3];
1769         len_min = min(len_min, phy->pair_length[3]);
1770         len_max = max(len_max, phy->pair_length[3]);
1771 
1772         /* Populate the phy structure with cable length in meters */
1773         phy->min_cable_length = len_min;
1774         phy->max_cable_length = len_max;
1775         phy->cable_length = len_tot / 4;
1776 
1777         /* Reset the page selec to its original value */
1778         ret_val = phy->ops.write_reg(hw, I347AT4_PAGE_SELECT,
1779                          default_page);
1780         if (ret_val)
1781             goto out;
1782         break;
1783     case M88E1112_E_PHY_ID:
1784         /* Remember the original page select and set it to 5 */
1785         ret_val = phy->ops.read_reg(hw, I347AT4_PAGE_SELECT,
1786                         &default_page);
1787         if (ret_val)
1788             goto out;
1789 
1790         ret_val = phy->ops.write_reg(hw, I347AT4_PAGE_SELECT, 0x05);
1791         if (ret_val)
1792             goto out;
1793 
1794         ret_val = phy->ops.read_reg(hw, M88E1112_VCT_DSP_DISTANCE,
1795                         &phy_data);
1796         if (ret_val)
1797             goto out;
1798 
1799         index = (phy_data & M88E1000_PSSR_CABLE_LENGTH) >>
1800             M88E1000_PSSR_CABLE_LENGTH_SHIFT;
1801         if (index >= ARRAY_SIZE(e1000_m88_cable_length_table) - 1) {
1802             ret_val = -E1000_ERR_PHY;
1803             goto out;
1804         }
1805 
1806         phy->min_cable_length = e1000_m88_cable_length_table[index];
1807         phy->max_cable_length = e1000_m88_cable_length_table[index + 1];
1808 
1809         phy->cable_length = (phy->min_cable_length +
1810                      phy->max_cable_length) / 2;
1811 
1812         /* Reset the page select to its original value */
1813         ret_val = phy->ops.write_reg(hw, I347AT4_PAGE_SELECT,
1814                          default_page);
1815         if (ret_val)
1816             goto out;
1817 
1818         break;
1819     default:
1820         ret_val = -E1000_ERR_PHY;
1821         goto out;
1822     }
1823 
1824 out:
1825     return ret_val;
1826 }
1827 
1828 /**
1829  *  igb_get_cable_length_igp_2 - Determine cable length for igp2 PHY
1830  *  @hw: pointer to the HW structure
1831  *
1832  *  The automatic gain control (agc) normalizes the amplitude of the
1833  *  received signal, adjusting for the attenuation produced by the
1834  *  cable.  By reading the AGC registers, which represent the
1835  *  combination of coarse and fine gain value, the value can be put
1836  *  into a lookup table to obtain the approximate cable length
1837  *  for each channel.
1838  **/
1839 s32 igb_get_cable_length_igp_2(struct e1000_hw *hw)
1840 {
1841     struct e1000_phy_info *phy = &hw->phy;
1842     s32 ret_val = 0;
1843     u16 phy_data, i, agc_value = 0;
1844     u16 cur_agc_index, max_agc_index = 0;
1845     u16 min_agc_index = ARRAY_SIZE(e1000_igp_2_cable_length_table) - 1;
1846     static const u16 agc_reg_array[IGP02E1000_PHY_CHANNEL_NUM] = {
1847         IGP02E1000_PHY_AGC_A,
1848         IGP02E1000_PHY_AGC_B,
1849         IGP02E1000_PHY_AGC_C,
1850         IGP02E1000_PHY_AGC_D
1851     };
1852 
1853     /* Read the AGC registers for all channels */
1854     for (i = 0; i < IGP02E1000_PHY_CHANNEL_NUM; i++) {
1855         ret_val = phy->ops.read_reg(hw, agc_reg_array[i], &phy_data);
1856         if (ret_val)
1857             goto out;
1858 
1859         /* Getting bits 15:9, which represent the combination of
1860          * coarse and fine gain values.  The result is a number
1861          * that can be put into the lookup table to obtain the
1862          * approximate cable length.
1863          */
1864         cur_agc_index = (phy_data >> IGP02E1000_AGC_LENGTH_SHIFT) &
1865                 IGP02E1000_AGC_LENGTH_MASK;
1866 
1867         /* Array index bound check. */
1868         if ((cur_agc_index >= ARRAY_SIZE(e1000_igp_2_cable_length_table)) ||
1869             (cur_agc_index == 0)) {
1870             ret_val = -E1000_ERR_PHY;
1871             goto out;
1872         }
1873 
1874         /* Remove min & max AGC values from calculation. */
1875         if (e1000_igp_2_cable_length_table[min_agc_index] >
1876             e1000_igp_2_cable_length_table[cur_agc_index])
1877             min_agc_index = cur_agc_index;
1878         if (e1000_igp_2_cable_length_table[max_agc_index] <
1879             e1000_igp_2_cable_length_table[cur_agc_index])
1880             max_agc_index = cur_agc_index;
1881 
1882         agc_value += e1000_igp_2_cable_length_table[cur_agc_index];
1883     }
1884 
1885     agc_value -= (e1000_igp_2_cable_length_table[min_agc_index] +
1886               e1000_igp_2_cable_length_table[max_agc_index]);
1887     agc_value /= (IGP02E1000_PHY_CHANNEL_NUM - 2);
1888 
1889     /* Calculate cable length with the error range of +/- 10 meters. */
1890     phy->min_cable_length = ((agc_value - IGP02E1000_AGC_RANGE) > 0) ?
1891                  (agc_value - IGP02E1000_AGC_RANGE) : 0;
1892     phy->max_cable_length = agc_value + IGP02E1000_AGC_RANGE;
1893 
1894     phy->cable_length = (phy->min_cable_length + phy->max_cable_length) / 2;
1895 
1896 out:
1897     return ret_val;
1898 }
1899 
1900 /**
1901  *  igb_get_phy_info_m88 - Retrieve PHY information
1902  *  @hw: pointer to the HW structure
1903  *
1904  *  Valid for only copper links.  Read the PHY status register (sticky read)
1905  *  to verify that link is up.  Read the PHY special control register to
1906  *  determine the polarity and 10base-T extended distance.  Read the PHY
1907  *  special status register to determine MDI/MDIx and current speed.  If
1908  *  speed is 1000, then determine cable length, local and remote receiver.
1909  **/
1910 s32 igb_get_phy_info_m88(struct e1000_hw *hw)
1911 {
1912     struct e1000_phy_info *phy = &hw->phy;
1913     s32  ret_val;
1914     u16 phy_data;
1915     bool link;
1916 
1917     if (phy->media_type != e1000_media_type_copper) {
1918         hw_dbg("Phy info is only valid for copper media\n");
1919         ret_val = -E1000_ERR_CONFIG;
1920         goto out;
1921     }
1922 
1923     ret_val = igb_phy_has_link(hw, 1, 0, &link);
1924     if (ret_val)
1925         goto out;
1926 
1927     if (!link) {
1928         hw_dbg("Phy info is only valid if link is up\n");
1929         ret_val = -E1000_ERR_CONFIG;
1930         goto out;
1931     }
1932 
1933     ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
1934     if (ret_val)
1935         goto out;
1936 
1937     phy->polarity_correction = (phy_data & M88E1000_PSCR_POLARITY_REVERSAL)
1938                    ? true : false;
1939 
1940     ret_val = igb_check_polarity_m88(hw);
1941     if (ret_val)
1942         goto out;
1943 
1944     ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data);
1945     if (ret_val)
1946         goto out;
1947 
1948     phy->is_mdix = (phy_data & M88E1000_PSSR_MDIX) ? true : false;
1949 
1950     if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_1000MBS) {
1951         ret_val = phy->ops.get_cable_length(hw);
1952         if (ret_val)
1953             goto out;
1954 
1955         ret_val = phy->ops.read_reg(hw, PHY_1000T_STATUS, &phy_data);
1956         if (ret_val)
1957             goto out;
1958 
1959         phy->local_rx = (phy_data & SR_1000T_LOCAL_RX_STATUS)
1960                 ? e1000_1000t_rx_status_ok
1961                 : e1000_1000t_rx_status_not_ok;
1962 
1963         phy->remote_rx = (phy_data & SR_1000T_REMOTE_RX_STATUS)
1964                  ? e1000_1000t_rx_status_ok
1965                  : e1000_1000t_rx_status_not_ok;
1966     } else {
1967         /* Set values to "undefined" */
1968         phy->cable_length = E1000_CABLE_LENGTH_UNDEFINED;
1969         phy->local_rx = e1000_1000t_rx_status_undefined;
1970         phy->remote_rx = e1000_1000t_rx_status_undefined;
1971     }
1972 
1973 out:
1974     return ret_val;
1975 }
1976 
1977 /**
1978  *  igb_get_phy_info_igp - Retrieve igp PHY information
1979  *  @hw: pointer to the HW structure
1980  *
1981  *  Read PHY status to determine if link is up.  If link is up, then
1982  *  set/determine 10base-T extended distance and polarity correction.  Read
1983  *  PHY port status to determine MDI/MDIx and speed.  Based on the speed,
1984  *  determine on the cable length, local and remote receiver.
1985  **/
1986 s32 igb_get_phy_info_igp(struct e1000_hw *hw)
1987 {
1988     struct e1000_phy_info *phy = &hw->phy;
1989     s32 ret_val;
1990     u16 data;
1991     bool link;
1992 
1993     ret_val = igb_phy_has_link(hw, 1, 0, &link);
1994     if (ret_val)
1995         goto out;
1996 
1997     if (!link) {
1998         hw_dbg("Phy info is only valid if link is up\n");
1999         ret_val = -E1000_ERR_CONFIG;
2000         goto out;
2001     }
2002 
2003     phy->polarity_correction = true;
2004 
2005     ret_val = igb_check_polarity_igp(hw);
2006     if (ret_val)
2007         goto out;
2008 
2009     ret_val = phy->ops.read_reg(hw, IGP01E1000_PHY_PORT_STATUS, &data);
2010     if (ret_val)
2011         goto out;
2012 
2013     phy->is_mdix = (data & IGP01E1000_PSSR_MDIX) ? true : false;
2014 
2015     if ((data & IGP01E1000_PSSR_SPEED_MASK) ==
2016         IGP01E1000_PSSR_SPEED_1000MBPS) {
2017         ret_val = phy->ops.get_cable_length(hw);
2018         if (ret_val)
2019             goto out;
2020 
2021         ret_val = phy->ops.read_reg(hw, PHY_1000T_STATUS, &data);
2022         if (ret_val)
2023             goto out;
2024 
2025         phy->local_rx = (data & SR_1000T_LOCAL_RX_STATUS)
2026                 ? e1000_1000t_rx_status_ok
2027                 : e1000_1000t_rx_status_not_ok;
2028 
2029         phy->remote_rx = (data & SR_1000T_REMOTE_RX_STATUS)
2030                  ? e1000_1000t_rx_status_ok
2031                  : e1000_1000t_rx_status_not_ok;
2032     } else {
2033         phy->cable_length = E1000_CABLE_LENGTH_UNDEFINED;
2034         phy->local_rx = e1000_1000t_rx_status_undefined;
2035         phy->remote_rx = e1000_1000t_rx_status_undefined;
2036     }
2037 
2038 out:
2039     return ret_val;
2040 }
2041 
2042 /**
2043  *  igb_phy_sw_reset - PHY software reset
2044  *  @hw: pointer to the HW structure
2045  *
2046  *  Does a software reset of the PHY by reading the PHY control register and
2047  *  setting/write the control register reset bit to the PHY.
2048  **/
2049 s32 igb_phy_sw_reset(struct e1000_hw *hw)
2050 {
2051     s32 ret_val = 0;
2052     u16 phy_ctrl;
2053 
2054     if (!(hw->phy.ops.read_reg))
2055         goto out;
2056 
2057     ret_val = hw->phy.ops.read_reg(hw, PHY_CONTROL, &phy_ctrl);
2058     if (ret_val)
2059         goto out;
2060 
2061     phy_ctrl |= MII_CR_RESET;
2062     ret_val = hw->phy.ops.write_reg(hw, PHY_CONTROL, phy_ctrl);
2063     if (ret_val)
2064         goto out;
2065 
2066     udelay(1);
2067 
2068 out:
2069     return ret_val;
2070 }
2071 
2072 /**
2073  *  igb_phy_hw_reset - PHY hardware reset
2074  *  @hw: pointer to the HW structure
2075  *
2076  *  Verify the reset block is not blocking us from resetting.  Acquire
2077  *  semaphore (if necessary) and read/set/write the device control reset
2078  *  bit in the PHY.  Wait the appropriate delay time for the device to
2079  *  reset and release the semaphore (if necessary).
2080  **/
2081 s32 igb_phy_hw_reset(struct e1000_hw *hw)
2082 {
2083     struct e1000_phy_info *phy = &hw->phy;
2084     s32  ret_val;
2085     u32 ctrl;
2086 
2087     ret_val = igb_check_reset_block(hw);
2088     if (ret_val) {
2089         ret_val = 0;
2090         goto out;
2091     }
2092 
2093     ret_val = phy->ops.acquire(hw);
2094     if (ret_val)
2095         goto out;
2096 
2097     ctrl = rd32(E1000_CTRL);
2098     wr32(E1000_CTRL, ctrl | E1000_CTRL_PHY_RST);
2099     wrfl();
2100 
2101     udelay(phy->reset_delay_us);
2102 
2103     wr32(E1000_CTRL, ctrl);
2104     wrfl();
2105 
2106     udelay(150);
2107 
2108     phy->ops.release(hw);
2109 
2110     ret_val = phy->ops.get_cfg_done(hw);
2111 
2112 out:
2113     return ret_val;
2114 }
2115 
2116 /**
2117  *  igb_phy_init_script_igp3 - Inits the IGP3 PHY
2118  *  @hw: pointer to the HW structure
2119  *
2120  *  Initializes a Intel Gigabit PHY3 when an EEPROM is not present.
2121  **/
2122 s32 igb_phy_init_script_igp3(struct e1000_hw *hw)
2123 {
2124     hw_dbg("Running IGP 3 PHY init script\n");
2125 
2126     /* PHY init IGP 3 */
2127     /* Enable rise/fall, 10-mode work in class-A */
2128     hw->phy.ops.write_reg(hw, 0x2F5B, 0x9018);
2129     /* Remove all caps from Replica path filter */
2130     hw->phy.ops.write_reg(hw, 0x2F52, 0x0000);
2131     /* Bias trimming for ADC, AFE and Driver (Default) */
2132     hw->phy.ops.write_reg(hw, 0x2FB1, 0x8B24);
2133     /* Increase Hybrid poly bias */
2134     hw->phy.ops.write_reg(hw, 0x2FB2, 0xF8F0);
2135     /* Add 4% to TX amplitude in Giga mode */
2136     hw->phy.ops.write_reg(hw, 0x2010, 0x10B0);
2137     /* Disable trimming (TTT) */
2138     hw->phy.ops.write_reg(hw, 0x2011, 0x0000);
2139     /* Poly DC correction to 94.6% + 2% for all channels */
2140     hw->phy.ops.write_reg(hw, 0x20DD, 0x249A);
2141     /* ABS DC correction to 95.9% */
2142     hw->phy.ops.write_reg(hw, 0x20DE, 0x00D3);
2143     /* BG temp curve trim */
2144     hw->phy.ops.write_reg(hw, 0x28B4, 0x04CE);
2145     /* Increasing ADC OPAMP stage 1 currents to max */
2146     hw->phy.ops.write_reg(hw, 0x2F70, 0x29E4);
2147     /* Force 1000 ( required for enabling PHY regs configuration) */
2148     hw->phy.ops.write_reg(hw, 0x0000, 0x0140);
2149     /* Set upd_freq to 6 */
2150     hw->phy.ops.write_reg(hw, 0x1F30, 0x1606);
2151     /* Disable NPDFE */
2152     hw->phy.ops.write_reg(hw, 0x1F31, 0xB814);
2153     /* Disable adaptive fixed FFE (Default) */
2154     hw->phy.ops.write_reg(hw, 0x1F35, 0x002A);
2155     /* Enable FFE hysteresis */
2156     hw->phy.ops.write_reg(hw, 0x1F3E, 0x0067);
2157     /* Fixed FFE for short cable lengths */
2158     hw->phy.ops.write_reg(hw, 0x1F54, 0x0065);
2159     /* Fixed FFE for medium cable lengths */
2160     hw->phy.ops.write_reg(hw, 0x1F55, 0x002A);
2161     /* Fixed FFE for long cable lengths */
2162     hw->phy.ops.write_reg(hw, 0x1F56, 0x002A);
2163     /* Enable Adaptive Clip Threshold */
2164     hw->phy.ops.write_reg(hw, 0x1F72, 0x3FB0);
2165     /* AHT reset limit to 1 */
2166     hw->phy.ops.write_reg(hw, 0x1F76, 0xC0FF);
2167     /* Set AHT master delay to 127 msec */
2168     hw->phy.ops.write_reg(hw, 0x1F77, 0x1DEC);
2169     /* Set scan bits for AHT */
2170     hw->phy.ops.write_reg(hw, 0x1F78, 0xF9EF);
2171     /* Set AHT Preset bits */
2172     hw->phy.ops.write_reg(hw, 0x1F79, 0x0210);
2173     /* Change integ_factor of channel A to 3 */
2174     hw->phy.ops.write_reg(hw, 0x1895, 0x0003);
2175     /* Change prop_factor of channels BCD to 8 */
2176     hw->phy.ops.write_reg(hw, 0x1796, 0x0008);
2177     /* Change cg_icount + enable integbp for channels BCD */
2178     hw->phy.ops.write_reg(hw, 0x1798, 0xD008);
2179     /* Change cg_icount + enable integbp + change prop_factor_master
2180      * to 8 for channel A
2181      */
2182     hw->phy.ops.write_reg(hw, 0x1898, 0xD918);
2183     /* Disable AHT in Slave mode on channel A */
2184     hw->phy.ops.write_reg(hw, 0x187A, 0x0800);
2185     /* Enable LPLU and disable AN to 1000 in non-D0a states,
2186      * Enable SPD+B2B
2187      */
2188     hw->phy.ops.write_reg(hw, 0x0019, 0x008D);
2189     /* Enable restart AN on an1000_dis change */
2190     hw->phy.ops.write_reg(hw, 0x001B, 0x2080);
2191     /* Enable wh_fifo read clock in 10/100 modes */
2192     hw->phy.ops.write_reg(hw, 0x0014, 0x0045);
2193     /* Restart AN, Speed selection is 1000 */
2194     hw->phy.ops.write_reg(hw, 0x0000, 0x1340);
2195 
2196     return 0;
2197 }
2198 
2199 /**
2200  *  igb_initialize_M88E1512_phy - Initialize M88E1512 PHY
2201  *  @hw: pointer to the HW structure
2202  *
2203  *  Initialize Marvel 1512 to work correctly with Avoton.
2204  **/
2205 s32 igb_initialize_M88E1512_phy(struct e1000_hw *hw)
2206 {
2207     struct e1000_phy_info *phy = &hw->phy;
2208     s32 ret_val = 0;
2209 
2210     /* Switch to PHY page 0xFF. */
2211     ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0x00FF);
2212     if (ret_val)
2213         goto out;
2214 
2215     ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_2, 0x214B);
2216     if (ret_val)
2217         goto out;
2218 
2219     ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_1, 0x2144);
2220     if (ret_val)
2221         goto out;
2222 
2223     ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_2, 0x0C28);
2224     if (ret_val)
2225         goto out;
2226 
2227     ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_1, 0x2146);
2228     if (ret_val)
2229         goto out;
2230 
2231     ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_2, 0xB233);
2232     if (ret_val)
2233         goto out;
2234 
2235     ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_1, 0x214D);
2236     if (ret_val)
2237         goto out;
2238 
2239     ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_2, 0xCC0C);
2240     if (ret_val)
2241         goto out;
2242 
2243     ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_1, 0x2159);
2244     if (ret_val)
2245         goto out;
2246 
2247     /* Switch to PHY page 0xFB. */
2248     ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0x00FB);
2249     if (ret_val)
2250         goto out;
2251 
2252     ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_3, 0x000D);
2253     if (ret_val)
2254         goto out;
2255 
2256     /* Switch to PHY page 0x12. */
2257     ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0x12);
2258     if (ret_val)
2259         goto out;
2260 
2261     /* Change mode to SGMII-to-Copper */
2262     ret_val = phy->ops.write_reg(hw, E1000_M88E1512_MODE, 0x8001);
2263     if (ret_val)
2264         goto out;
2265 
2266     /* Return the PHY to page 0. */
2267     ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0);
2268     if (ret_val)
2269         goto out;
2270 
2271     ret_val = igb_phy_sw_reset(hw);
2272     if (ret_val) {
2273         hw_dbg("Error committing the PHY changes\n");
2274         return ret_val;
2275     }
2276 
2277     /* msec_delay(1000); */
2278     usleep_range(1000, 2000);
2279 out:
2280     return ret_val;
2281 }
2282 
2283 /**
2284  *  igb_initialize_M88E1543_phy - Initialize M88E1512 PHY
2285  *  @hw: pointer to the HW structure
2286  *
2287  *  Initialize Marvell 1543 to work correctly with Avoton.
2288  **/
2289 s32 igb_initialize_M88E1543_phy(struct e1000_hw *hw)
2290 {
2291     struct e1000_phy_info *phy = &hw->phy;
2292     s32 ret_val = 0;
2293 
2294     /* Switch to PHY page 0xFF. */
2295     ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0x00FF);
2296     if (ret_val)
2297         goto out;
2298 
2299     ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_2, 0x214B);
2300     if (ret_val)
2301         goto out;
2302 
2303     ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_1, 0x2144);
2304     if (ret_val)
2305         goto out;
2306 
2307     ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_2, 0x0C28);
2308     if (ret_val)
2309         goto out;
2310 
2311     ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_1, 0x2146);
2312     if (ret_val)
2313         goto out;
2314 
2315     ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_2, 0xB233);
2316     if (ret_val)
2317         goto out;
2318 
2319     ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_1, 0x214D);
2320     if (ret_val)
2321         goto out;
2322 
2323     ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_2, 0xDC0C);
2324     if (ret_val)
2325         goto out;
2326 
2327     ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_1, 0x2159);
2328     if (ret_val)
2329         goto out;
2330 
2331     /* Switch to PHY page 0xFB. */
2332     ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0x00FB);
2333     if (ret_val)
2334         goto out;
2335 
2336     ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_3, 0x0C0D);
2337     if (ret_val)
2338         goto out;
2339 
2340     /* Switch to PHY page 0x12. */
2341     ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0x12);
2342     if (ret_val)
2343         goto out;
2344 
2345     /* Change mode to SGMII-to-Copper */
2346     ret_val = phy->ops.write_reg(hw, E1000_M88E1512_MODE, 0x8001);
2347     if (ret_val)
2348         goto out;
2349 
2350     /* Switch to PHY page 1. */
2351     ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0x1);
2352     if (ret_val)
2353         goto out;
2354 
2355     /* Change mode to 1000BASE-X/SGMII and autoneg enable */
2356     ret_val = phy->ops.write_reg(hw, E1000_M88E1543_FIBER_CTRL, 0x9140);
2357     if (ret_val)
2358         goto out;
2359 
2360     /* Return the PHY to page 0. */
2361     ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0);
2362     if (ret_val)
2363         goto out;
2364 
2365     ret_val = igb_phy_sw_reset(hw);
2366     if (ret_val) {
2367         hw_dbg("Error committing the PHY changes\n");
2368         return ret_val;
2369     }
2370 
2371     /* msec_delay(1000); */
2372     usleep_range(1000, 2000);
2373 out:
2374     return ret_val;
2375 }
2376 
2377 /**
2378  * igb_power_up_phy_copper - Restore copper link in case of PHY power down
2379  * @hw: pointer to the HW structure
2380  *
2381  * In the case of a PHY power down to save power, or to turn off link during a
2382  * driver unload, restore the link to previous settings.
2383  **/
2384 void igb_power_up_phy_copper(struct e1000_hw *hw)
2385 {
2386     u16 mii_reg = 0;
2387 
2388     /* The PHY will retain its settings across a power down/up cycle */
2389     hw->phy.ops.read_reg(hw, PHY_CONTROL, &mii_reg);
2390     mii_reg &= ~MII_CR_POWER_DOWN;
2391     hw->phy.ops.write_reg(hw, PHY_CONTROL, mii_reg);
2392 }
2393 
2394 /**
2395  * igb_power_down_phy_copper - Power down copper PHY
2396  * @hw: pointer to the HW structure
2397  *
2398  * Power down PHY to save power when interface is down and wake on lan
2399  * is not enabled.
2400  **/
2401 void igb_power_down_phy_copper(struct e1000_hw *hw)
2402 {
2403     u16 mii_reg = 0;
2404 
2405     /* The PHY will retain its settings across a power down/up cycle */
2406     hw->phy.ops.read_reg(hw, PHY_CONTROL, &mii_reg);
2407     mii_reg |= MII_CR_POWER_DOWN;
2408     hw->phy.ops.write_reg(hw, PHY_CONTROL, mii_reg);
2409     usleep_range(1000, 2000);
2410 }
2411 
2412 /**
2413  *  igb_check_polarity_82580 - Checks the polarity.
2414  *  @hw: pointer to the HW structure
2415  *
2416  *  Success returns 0, Failure returns -E1000_ERR_PHY (-2)
2417  *
2418  *  Polarity is determined based on the PHY specific status register.
2419  **/
2420 static s32 igb_check_polarity_82580(struct e1000_hw *hw)
2421 {
2422     struct e1000_phy_info *phy = &hw->phy;
2423     s32 ret_val;
2424     u16 data;
2425 
2426 
2427     ret_val = phy->ops.read_reg(hw, I82580_PHY_STATUS_2, &data);
2428 
2429     if (!ret_val)
2430         phy->cable_polarity = (data & I82580_PHY_STATUS2_REV_POLARITY)
2431                       ? e1000_rev_polarity_reversed
2432                       : e1000_rev_polarity_normal;
2433 
2434     return ret_val;
2435 }
2436 
2437 /**
2438  *  igb_phy_force_speed_duplex_82580 - Force speed/duplex for I82580 PHY
2439  *  @hw: pointer to the HW structure
2440  *
2441  *  Calls the PHY setup function to force speed and duplex.  Clears the
2442  *  auto-crossover to force MDI manually.  Waits for link and returns
2443  *  successful if link up is successful, else -E1000_ERR_PHY (-2).
2444  **/
2445 s32 igb_phy_force_speed_duplex_82580(struct e1000_hw *hw)
2446 {
2447     struct e1000_phy_info *phy = &hw->phy;
2448     s32 ret_val;
2449     u16 phy_data;
2450     bool link;
2451 
2452     ret_val = phy->ops.read_reg(hw, PHY_CONTROL, &phy_data);
2453     if (ret_val)
2454         goto out;
2455 
2456     igb_phy_force_speed_duplex_setup(hw, &phy_data);
2457 
2458     ret_val = phy->ops.write_reg(hw, PHY_CONTROL, phy_data);
2459     if (ret_val)
2460         goto out;
2461 
2462     /* Clear Auto-Crossover to force MDI manually.  82580 requires MDI
2463      * forced whenever speed and duplex are forced.
2464      */
2465     ret_val = phy->ops.read_reg(hw, I82580_PHY_CTRL_2, &phy_data);
2466     if (ret_val)
2467         goto out;
2468 
2469     phy_data &= ~I82580_PHY_CTRL2_MDIX_CFG_MASK;
2470 
2471     ret_val = phy->ops.write_reg(hw, I82580_PHY_CTRL_2, phy_data);
2472     if (ret_val)
2473         goto out;
2474 
2475     hw_dbg("I82580_PHY_CTRL_2: %X\n", phy_data);
2476 
2477     udelay(1);
2478 
2479     if (phy->autoneg_wait_to_complete) {
2480         hw_dbg("Waiting for forced speed/duplex link on 82580 phy\n");
2481 
2482         ret_val = igb_phy_has_link(hw, PHY_FORCE_LIMIT, 100000, &link);
2483         if (ret_val)
2484             goto out;
2485 
2486         if (!link)
2487             hw_dbg("Link taking longer than expected.\n");
2488 
2489         /* Try once more */
2490         ret_val = igb_phy_has_link(hw, PHY_FORCE_LIMIT, 100000, &link);
2491         if (ret_val)
2492             goto out;
2493     }
2494 
2495 out:
2496     return ret_val;
2497 }
2498 
2499 /**
2500  *  igb_get_phy_info_82580 - Retrieve I82580 PHY information
2501  *  @hw: pointer to the HW structure
2502  *
2503  *  Read PHY status to determine if link is up.  If link is up, then
2504  *  set/determine 10base-T extended distance and polarity correction.  Read
2505  *  PHY port status to determine MDI/MDIx and speed.  Based on the speed,
2506  *  determine on the cable length, local and remote receiver.
2507  **/
2508 s32 igb_get_phy_info_82580(struct e1000_hw *hw)
2509 {
2510     struct e1000_phy_info *phy = &hw->phy;
2511     s32 ret_val;
2512     u16 data;
2513     bool link;
2514 
2515     ret_val = igb_phy_has_link(hw, 1, 0, &link);
2516     if (ret_val)
2517         goto out;
2518 
2519     if (!link) {
2520         hw_dbg("Phy info is only valid if link is up\n");
2521         ret_val = -E1000_ERR_CONFIG;
2522         goto out;
2523     }
2524 
2525     phy->polarity_correction = true;
2526 
2527     ret_val = igb_check_polarity_82580(hw);
2528     if (ret_val)
2529         goto out;
2530 
2531     ret_val = phy->ops.read_reg(hw, I82580_PHY_STATUS_2, &data);
2532     if (ret_val)
2533         goto out;
2534 
2535     phy->is_mdix = (data & I82580_PHY_STATUS2_MDIX) ? true : false;
2536 
2537     if ((data & I82580_PHY_STATUS2_SPEED_MASK) ==
2538         I82580_PHY_STATUS2_SPEED_1000MBPS) {
2539         ret_val = hw->phy.ops.get_cable_length(hw);
2540         if (ret_val)
2541             goto out;
2542 
2543         ret_val = phy->ops.read_reg(hw, PHY_1000T_STATUS, &data);
2544         if (ret_val)
2545             goto out;
2546 
2547         phy->local_rx = (data & SR_1000T_LOCAL_RX_STATUS)
2548                 ? e1000_1000t_rx_status_ok
2549                 : e1000_1000t_rx_status_not_ok;
2550 
2551         phy->remote_rx = (data & SR_1000T_REMOTE_RX_STATUS)
2552                  ? e1000_1000t_rx_status_ok
2553                  : e1000_1000t_rx_status_not_ok;
2554     } else {
2555         phy->cable_length = E1000_CABLE_LENGTH_UNDEFINED;
2556         phy->local_rx = e1000_1000t_rx_status_undefined;
2557         phy->remote_rx = e1000_1000t_rx_status_undefined;
2558     }
2559 
2560 out:
2561     return ret_val;
2562 }
2563 
2564 /**
2565  *  igb_get_cable_length_82580 - Determine cable length for 82580 PHY
2566  *  @hw: pointer to the HW structure
2567  *
2568  * Reads the diagnostic status register and verifies result is valid before
2569  * placing it in the phy_cable_length field.
2570  **/
2571 s32 igb_get_cable_length_82580(struct e1000_hw *hw)
2572 {
2573     struct e1000_phy_info *phy = &hw->phy;
2574     s32 ret_val;
2575     u16 phy_data, length;
2576 
2577     ret_val = phy->ops.read_reg(hw, I82580_PHY_DIAG_STATUS, &phy_data);
2578     if (ret_val)
2579         goto out;
2580 
2581     length = (phy_data & I82580_DSTATUS_CABLE_LENGTH) >>
2582          I82580_DSTATUS_CABLE_LENGTH_SHIFT;
2583 
2584     if (length == E1000_CABLE_LENGTH_UNDEFINED)
2585         ret_val = -E1000_ERR_PHY;
2586 
2587     phy->cable_length = length;
2588 
2589 out:
2590     return ret_val;
2591 }
2592 
2593 /**
2594  *  igb_set_master_slave_mode - Setup PHY for Master/slave mode
2595  *  @hw: pointer to the HW structure
2596  *
2597  *  Sets up Master/slave mode
2598  **/
2599 static s32 igb_set_master_slave_mode(struct e1000_hw *hw)
2600 {
2601     s32 ret_val;
2602     u16 phy_data;
2603 
2604     /* Resolve Master/Slave mode */
2605     ret_val = hw->phy.ops.read_reg(hw, PHY_1000T_CTRL, &phy_data);
2606     if (ret_val)
2607         return ret_val;
2608 
2609     /* load defaults for future use */
2610     hw->phy.original_ms_type = (phy_data & CR_1000T_MS_ENABLE) ?
2611                    ((phy_data & CR_1000T_MS_VALUE) ?
2612                     e1000_ms_force_master :
2613                     e1000_ms_force_slave) : e1000_ms_auto;
2614 
2615     switch (hw->phy.ms_type) {
2616     case e1000_ms_force_master:
2617         phy_data |= (CR_1000T_MS_ENABLE | CR_1000T_MS_VALUE);
2618         break;
2619     case e1000_ms_force_slave:
2620         phy_data |= CR_1000T_MS_ENABLE;
2621         phy_data &= ~(CR_1000T_MS_VALUE);
2622         break;
2623     case e1000_ms_auto:
2624         phy_data &= ~CR_1000T_MS_ENABLE;
2625         fallthrough;
2626     default:
2627         break;
2628     }
2629 
2630     return hw->phy.ops.write_reg(hw, PHY_1000T_CTRL, phy_data);
2631 }