phy.c

来自「linux 内核源代码」· C语言 代码 · 共 1,774 行 · 第 1/4 页

C
1,774
字号
	e1000e_phy_force_speed_duplex_setup(hw, &phy_data);	ret_val = e1e_wphy(hw, PHY_CONTROL, phy_data);	if (ret_val)		return ret_val;	/* Clear Auto-Crossover to force MDI manually.  IGP requires MDI	 * forced whenever speed and duplex are forced.	 */	ret_val = e1e_rphy(hw, IGP01E1000_PHY_PORT_CTRL, &phy_data);	if (ret_val)		return ret_val;	phy_data &= ~IGP01E1000_PSCR_AUTO_MDIX;	phy_data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;	ret_val = e1e_wphy(hw, IGP01E1000_PHY_PORT_CTRL, phy_data);	if (ret_val)		return ret_val;	hw_dbg(hw, "IGP PSCR: %X\n", phy_data);	udelay(1);	if (phy->wait_for_link) {		hw_dbg(hw, "Waiting for forced speed/duplex link on IGP phy.\n");		ret_val = e1000e_phy_has_link_generic(hw,						     PHY_FORCE_LIMIT,						     100000,						     &link);		if (ret_val)			return ret_val;		if (!link)			hw_dbg(hw, "Link taking longer than expected.\n");		/* Try once more */		ret_val = e1000e_phy_has_link_generic(hw,						     PHY_FORCE_LIMIT,						     100000,						     &link);		if (ret_val)			return ret_val;	}	return ret_val;}/** *  e1000e_phy_force_speed_duplex_m88 - Force speed/duplex for m88 PHY *  @hw: pointer to the HW structure * *  Calls the PHY setup function to force speed and duplex.  Clears the *  auto-crossover to force MDI manually.  Resets the PHY to commit the *  changes.  If time expires while waiting for link up, we reset the DSP. *  After reset, TX_CLK and CRS on TX must be set.  Return successful upon *  successful completion, else return corresponding error code. **/s32 e1000e_phy_force_speed_duplex_m88(struct e1000_hw *hw){	struct e1000_phy_info *phy = &hw->phy;	s32 ret_val;	u16 phy_data;	bool link;	/* Clear Auto-Crossover to force MDI manually.  M88E1000 requires MDI	 * forced whenever speed and duplex are forced.	 */	ret_val = e1e_rphy(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);	if (ret_val)		return ret_val;	phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;	ret_val = e1e_wphy(hw, M88E1000_PHY_SPEC_CTRL, phy_data);	if (ret_val)		return ret_val;	hw_dbg(hw, "M88E1000 PSCR: %X\n", phy_data);	ret_val = e1e_rphy(hw, PHY_CONTROL, &phy_data);	if (ret_val)		return ret_val;	e1000e_phy_force_speed_duplex_setup(hw, &phy_data);	/* Reset the phy to commit changes. */	phy_data |= MII_CR_RESET;	ret_val = e1e_wphy(hw, PHY_CONTROL, phy_data);	if (ret_val)		return ret_val;	udelay(1);	if (phy->wait_for_link) {		hw_dbg(hw, "Waiting for forced speed/duplex link on M88 phy.\n");		ret_val = e1000e_phy_has_link_generic(hw, PHY_FORCE_LIMIT,						     100000, &link);		if (ret_val)			return ret_val;		if (!link) {			/* We didn't get link.			 * Reset the DSP and cross our fingers.			 */			ret_val = e1e_wphy(hw, M88E1000_PHY_PAGE_SELECT, 0x001d);			if (ret_val)				return ret_val;			ret_val = e1000e_phy_reset_dsp(hw);			if (ret_val)				return ret_val;		}		/* Try once more */		ret_val = e1000e_phy_has_link_generic(hw, PHY_FORCE_LIMIT,						     100000, &link);		if (ret_val)			return ret_val;	}	ret_val = e1e_rphy(hw, M88E1000_EXT_PHY_SPEC_CTRL, &phy_data);	if (ret_val)		return ret_val;	/* Resetting the phy means we need to re-force TX_CLK in the	 * Extended PHY Specific Control Register to 25MHz clock from	 * the reset value of 2.5MHz.	 */	phy_data |= M88E1000_EPSCR_TX_CLK_25;	ret_val = e1e_wphy(hw, M88E1000_EXT_PHY_SPEC_CTRL, phy_data);	if (ret_val)		return ret_val;	/* In addition, we must re-enable CRS on Tx for both half and full	 * duplex.	 */	ret_val = e1e_rphy(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);	if (ret_val)		return ret_val;	phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;	ret_val = e1e_wphy(hw, M88E1000_PHY_SPEC_CTRL, phy_data);	return ret_val;}/** *  e1000e_phy_force_speed_duplex_setup - Configure forced PHY speed/duplex *  @hw: pointer to the HW structure *  @phy_ctrl: pointer to current value of PHY_CONTROL * *  Forces speed and duplex on the PHY by doing the following: disable flow *  control, force speed/duplex on the MAC, disable auto speed detection, *  disable auto-negotiation, configure duplex, configure speed, configure *  the collision distance, write configuration to CTRL register.  The *  caller must write to the PHY_CONTROL register for these settings to *  take affect. **/void e1000e_phy_force_speed_duplex_setup(struct e1000_hw *hw, u16 *phy_ctrl){	struct e1000_mac_info *mac = &hw->mac;	u32 ctrl;	/* Turn off flow control when forcing speed/duplex */	mac->fc = e1000_fc_none;	/* Force speed/duplex on the mac */	ctrl = er32(CTRL);	ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Disable Auto Speed Detection */	ctrl &= ~E1000_CTRL_ASDE;	/* Disable autoneg on the phy */	*phy_ctrl &= ~MII_CR_AUTO_NEG_EN;	/* Forcing Full or Half Duplex? */	if (mac->forced_speed_duplex & E1000_ALL_HALF_DUPLEX) {		ctrl &= ~E1000_CTRL_FD;		*phy_ctrl &= ~MII_CR_FULL_DUPLEX;		hw_dbg(hw, "Half Duplex\n");	} else {		ctrl |= E1000_CTRL_FD;		*phy_ctrl |= MII_CR_FULL_DUPLEX;		hw_dbg(hw, "Full Duplex\n");	}	/* Forcing 10mb or 100mb? */	if (mac->forced_speed_duplex & E1000_ALL_100_SPEED) {		ctrl |= E1000_CTRL_SPD_100;		*phy_ctrl |= MII_CR_SPEED_100;		*phy_ctrl &= ~(MII_CR_SPEED_1000 | MII_CR_SPEED_10);		hw_dbg(hw, "Forcing 100mb\n");	} else {		ctrl &= ~(E1000_CTRL_SPD_1000 | E1000_CTRL_SPD_100);		*phy_ctrl |= MII_CR_SPEED_10;		*phy_ctrl &= ~(MII_CR_SPEED_1000 | MII_CR_SPEED_100);		hw_dbg(hw, "Forcing 10mb\n");	}	e1000e_config_collision_dist(hw);	ew32(CTRL, ctrl);}/** *  e1000e_set_d3_lplu_state - Sets low power link up state for D3 *  @hw: pointer to the HW structure *  @active: boolean used to enable/disable lplu * *  Success returns 0, Failure returns 1 * *  The low power link up (lplu) state is set to the power management level D3 *  and SmartSpeed is disabled when active is true, else clear lplu for D3 *  and enable Smartspeed.  LPLU and Smartspeed are mutually exclusive.  LPLU *  is used during Dx states where the power conservation is most important. *  During driver activity, SmartSpeed should be enabled so performance is *  maintained. **/s32 e1000e_set_d3_lplu_state(struct e1000_hw *hw, bool active){	struct e1000_phy_info *phy = &hw->phy;	s32 ret_val;	u16 data;	ret_val = e1e_rphy(hw, IGP02E1000_PHY_POWER_MGMT, &data);	if (ret_val)		return ret_val;	if (!active) {		data &= ~IGP02E1000_PM_D3_LPLU;		ret_val = e1e_wphy(hw,					     IGP02E1000_PHY_POWER_MGMT,					     data);		if (ret_val)			return ret_val;		/* LPLU and SmartSpeed are mutually exclusive.  LPLU is used		 * during Dx states where the power conservation is most		 * important.  During driver activity we should enable		 * SmartSpeed, so performance is maintained. */		if (phy->smart_speed == e1000_smart_speed_on) {			ret_val = e1e_rphy(hw, IGP01E1000_PHY_PORT_CONFIG,						    &data);			if (ret_val)				return ret_val;			data |= IGP01E1000_PSCFR_SMART_SPEED;			ret_val = e1e_wphy(hw, IGP01E1000_PHY_PORT_CONFIG,						     data);			if (ret_val)				return ret_val;		} else if (phy->smart_speed == e1000_smart_speed_off) {			ret_val = e1e_rphy(hw, IGP01E1000_PHY_PORT_CONFIG,						     &data);			if (ret_val)				return ret_val;			data &= ~IGP01E1000_PSCFR_SMART_SPEED;			ret_val = e1e_wphy(hw, IGP01E1000_PHY_PORT_CONFIG,						     data);			if (ret_val)				return ret_val;		}	} else if ((phy->autoneg_advertised == E1000_ALL_SPEED_DUPLEX) ||		   (phy->autoneg_advertised == E1000_ALL_NOT_GIG) ||		   (phy->autoneg_advertised == E1000_ALL_10_SPEED)) {		data |= IGP02E1000_PM_D3_LPLU;		ret_val = e1e_wphy(hw, IGP02E1000_PHY_POWER_MGMT, data);		if (ret_val)			return ret_val;		/* When LPLU is enabled, we should disable SmartSpeed */		ret_val = e1e_rphy(hw, IGP01E1000_PHY_PORT_CONFIG, &data);		if (ret_val)			return ret_val;		data &= ~IGP01E1000_PSCFR_SMART_SPEED;		ret_val = e1e_wphy(hw, IGP01E1000_PHY_PORT_CONFIG, data);	}	return ret_val;}/** *  e1000e_check_downshift - Checks whether a downshift in speed occured *  @hw: pointer to the HW structure * *  Success returns 0, Failure returns 1 * *  A downshift is detected by querying the PHY link health. **/s32 e1000e_check_downshift(struct e1000_hw *hw){	struct e1000_phy_info *phy = &hw->phy;	s32 ret_val;	u16 phy_data, offset, mask;	switch (phy->type) {	case e1000_phy_m88:	case e1000_phy_gg82563:		offset	= M88E1000_PHY_SPEC_STATUS;		mask	= M88E1000_PSSR_DOWNSHIFT;		break;	case e1000_phy_igp_2:	case e1000_phy_igp_3:		offset	= IGP01E1000_PHY_LINK_HEALTH;		mask	= IGP01E1000_PLHR_SS_DOWNGRADE;		break;	default:		/* speed downshift not supported */		phy->speed_downgraded = 0;		return 0;	}	ret_val = e1e_rphy(hw, offset, &phy_data);	if (!ret_val)		phy->speed_downgraded = (phy_data & mask);	return ret_val;}/** *  e1000_check_polarity_m88 - Checks the polarity. *  @hw: pointer to the HW structure * *  Success returns 0, Failure returns -E1000_ERR_PHY (-2) * *  Polarity is determined based on the PHY specific status register. **/static s32 e1000_check_polarity_m88(struct e1000_hw *hw){	struct e1000_phy_info *phy = &hw->phy;	s32 ret_val;	u16 data;	ret_val = e1e_rphy(hw, M88E1000_PHY_SPEC_STATUS, &data);	if (!ret_val)		phy->cable_polarity = (data & M88E1000_PSSR_REV_POLARITY)				      ? e1000_rev_polarity_reversed				      : e1000_rev_polarity_normal;	return ret_val;}/** *  e1000_check_polarity_igp - Checks the polarity. *  @hw: pointer to the HW structure * *  Success returns 0, Failure returns -E1000_ERR_PHY (-2) * *  Polarity is determined based on the PHY port status register, and the *  current speed (since there is no polarity at 100Mbps). **/static s32 e1000_check_polarity_igp(struct e1000_hw *hw){	struct e1000_phy_info *phy = &hw->phy;	s32 ret_val;	u16 data, offset, mask;	/* Polarity is determined based on the speed of	 * our connection. */	ret_val = e1e_rphy(hw, IGP01E1000_PHY_PORT_STATUS, &data);	if (ret_val)		return ret_val;	if ((data & IGP01E1000_PSSR_SPEED_MASK) ==	    IGP01E1000_PSSR_SPEED_1000MBPS) {		offset	= IGP01E1000_PHY_PCS_INIT_REG;		mask	= IGP01E1000_PHY_POLARITY_MASK;	} else {		/* This really only applies to 10Mbps since		 * there is no polarity for 100Mbps (always 0).		 */		offset	= IGP01E1000_PHY_PORT_STATUS;		mask	= IGP01E1000_PSSR_POLARITY_REVERSED;	}	ret_val = e1e_rphy(hw, offset, &data);	if (!ret_val)		phy->cable_polarity = (data & mask)				      ? e1000_rev_polarity_reversed				      : e1000_rev_polarity_normal;	return ret_val;}/** *  e1000_wait_autoneg - Wait for auto-neg compeletion *  @hw: pointer to the HW structure * *  Waits for auto-negotiation to complete or for the auto-negotiation time *  limit to expire, which ever happens first. **/static s32 e1000_wait_autoneg(struct e1000_hw *hw){	s32 ret_val = 0;	u16 i, phy_status;	/* Break after autoneg completes or PHY_AUTO_NEG_LIMIT expires. */	for (i = PHY_AUTO_NEG_LIMIT; i > 0; i--) {		ret_val = e1e_rphy(hw, PHY_STATUS, &phy_status);		if (ret_val)			break;		ret_val = e1e_rphy(hw, PHY_STATUS, &phy_status);		if (ret_val)			break;		if (phy_status & MII_SR_AUTONEG_COMPLETE)			break;		msleep(100);	}	/* PHY_AUTO_NEG_TIME expiration doesn't guarantee auto-negotiation	 * has completed.	 */	return ret_val;}/** *  e1000e_phy_has_link_generic - Polls PHY for link *  @hw: pointer to the HW structure *  @iterations: number of times to poll for link *  @usec_interval: delay between polling attempts *  @success: pointer to whether polling was successful or not * *  Polls the PHY status register for link, 'iterations' number of times. **/s32 e1000e_phy_has_link_generic(struct e1000_hw *hw, u32 iterations,			       u32 usec_interval, bool *success){	s32 ret_val = 0;	u16 i, phy_status;	for (i = 0; i < iterations; i++) {		/* Some PHYs require the PHY_STATUS register to be read		 * twice due to the link bit being sticky.  No harm doing		 * it across the board.		 */		ret_val = e1e_rphy(hw, PHY_STATUS, &phy_status);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?