⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 phy.c

📁 linux 内核源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * drivers/net/phy/phy.c * * Framework for configuring and reading PHY devices * Based on code in sungem_phy.c and gianfar_phy.c * * Author: Andy Fleming * * Copyright (c) 2004 Freescale Semiconductor, Inc. * Copyright (c) 2006, 2007  Maciej W. Rozycki * * This program is free software; you can redistribute  it and/or modify it * under  the terms of  the GNU General  Public License as published by the * Free Software Foundation;  either version 2 of the  License, or (at your * option) any later version. * */#include <linux/kernel.h>#include <linux/string.h>#include <linux/errno.h>#include <linux/unistd.h>#include <linux/slab.h>#include <linux/interrupt.h>#include <linux/init.h>#include <linux/delay.h>#include <linux/netdevice.h>#include <linux/etherdevice.h>#include <linux/skbuff.h>#include <linux/spinlock.h>#include <linux/mm.h>#include <linux/module.h>#include <linux/mii.h>#include <linux/ethtool.h>#include <linux/phy.h>#include <linux/timer.h>#include <linux/workqueue.h>#include <asm/atomic.h>#include <asm/io.h>#include <asm/irq.h>#include <asm/uaccess.h>/** * phy_print_status - Convenience function to print out the current phy status * @phydev: the phy_device struct */void phy_print_status(struct phy_device *phydev){	pr_info("PHY: %s - Link is %s", phydev->dev.bus_id,			phydev->link ? "Up" : "Down");	if (phydev->link)		printk(" - %d/%s", phydev->speed,				DUPLEX_FULL == phydev->duplex ?				"Full" : "Half");	printk("\n");}EXPORT_SYMBOL(phy_print_status);/** * phy_read - Convenience function for reading a given PHY register * @phydev: the phy_device struct * @regnum: register number to read * * NOTE: MUST NOT be called from interrupt context, * because the bus read/write functions may wait for an interrupt * to conclude the operation. */int phy_read(struct phy_device *phydev, u16 regnum){	int retval;	struct mii_bus *bus = phydev->bus;	spin_lock_bh(&bus->mdio_lock);	retval = bus->read(bus, phydev->addr, regnum);	spin_unlock_bh(&bus->mdio_lock);	return retval;}EXPORT_SYMBOL(phy_read);/** * phy_write - Convenience function for writing a given PHY register * @phydev: the phy_device struct * @regnum: register number to write * @val: value to write to @regnum * * NOTE: MUST NOT be called from interrupt context, * because the bus read/write functions may wait for an interrupt * to conclude the operation. */int phy_write(struct phy_device *phydev, u16 regnum, u16 val){	int err;	struct mii_bus *bus = phydev->bus;	spin_lock_bh(&bus->mdio_lock);	err = bus->write(bus, phydev->addr, regnum, val);	spin_unlock_bh(&bus->mdio_lock);	return err;}EXPORT_SYMBOL(phy_write);/** * phy_clear_interrupt - Ack the phy device's interrupt * @phydev: the phy_device struct * * If the @phydev driver has an ack_interrupt function, call it to * ack and clear the phy device's interrupt. * * Returns 0 on success on < 0 on error. */int phy_clear_interrupt(struct phy_device *phydev){	int err = 0;	if (phydev->drv->ack_interrupt)		err = phydev->drv->ack_interrupt(phydev);	return err;}/** * phy_config_interrupt - configure the PHY device for the requested interrupts * @phydev: the phy_device struct * @interrupts: interrupt flags to configure for this @phydev * * Returns 0 on success on < 0 on error. */int phy_config_interrupt(struct phy_device *phydev, u32 interrupts){	int err = 0;	phydev->interrupts = interrupts;	if (phydev->drv->config_intr)		err = phydev->drv->config_intr(phydev);	return err;}/** * phy_aneg_done - return auto-negotiation status * @phydev: target phy_device struct * * Description: Reads the status register and returns 0 either if *   auto-negotiation is incomplete, or if there was an error. *   Returns BMSR_ANEGCOMPLETE if auto-negotiation is done. */static inline int phy_aneg_done(struct phy_device *phydev){	int retval;	retval = phy_read(phydev, MII_BMSR);	return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);}/* A structure for mapping a particular speed and duplex * combination to a particular SUPPORTED and ADVERTISED value */struct phy_setting {	int speed;	int duplex;	u32 setting;};/* A mapping of all SUPPORTED settings to speed/duplex */static const struct phy_setting settings[] = {	{		.speed = 10000,		.duplex = DUPLEX_FULL,		.setting = SUPPORTED_10000baseT_Full,	},	{		.speed = SPEED_1000,		.duplex = DUPLEX_FULL,		.setting = SUPPORTED_1000baseT_Full,	},	{		.speed = SPEED_1000,		.duplex = DUPLEX_HALF,		.setting = SUPPORTED_1000baseT_Half,	},	{		.speed = SPEED_100,		.duplex = DUPLEX_FULL,		.setting = SUPPORTED_100baseT_Full,	},	{		.speed = SPEED_100,		.duplex = DUPLEX_HALF,		.setting = SUPPORTED_100baseT_Half,	},	{		.speed = SPEED_10,		.duplex = DUPLEX_FULL,		.setting = SUPPORTED_10baseT_Full,	},	{		.speed = SPEED_10,		.duplex = DUPLEX_HALF,		.setting = SUPPORTED_10baseT_Half,	},};#define MAX_NUM_SETTINGS ARRAY_SIZE(settings)/** * phy_find_setting - find a PHY settings array entry that matches speed & duplex * @speed: speed to match * @duplex: duplex to match * * Description: Searches the settings array for the setting which *   matches the desired speed and duplex, and returns the index *   of that setting.  Returns the index of the last setting if *   none of the others match. */static inline int phy_find_setting(int speed, int duplex){	int idx = 0;	while (idx < ARRAY_SIZE(settings) &&			(settings[idx].speed != speed ||			settings[idx].duplex != duplex))		idx++;	return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;}/** * phy_find_valid - find a PHY setting that matches the requested features mask * @idx: The first index in settings[] to search * @features: A mask of the valid settings * * Description: Returns the index of the first valid setting less *   than or equal to the one pointed to by idx, as determined by *   the mask in features.  Returns the index of the last setting *   if nothing else matches. */static inline int phy_find_valid(int idx, u32 features){	while (idx < MAX_NUM_SETTINGS && !(settings[idx].setting & features))		idx++;	return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;}/** * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex * @phydev: the target phy_device struct * * Description: Make sure the PHY is set to supported speeds and *   duplexes.  Drop down by one in this order:  1000/FULL, *   1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF. */void phy_sanitize_settings(struct phy_device *phydev){	u32 features = phydev->supported;	int idx;	/* Sanitize settings based on PHY capabilities */	if ((features & SUPPORTED_Autoneg) == 0)		phydev->autoneg = AUTONEG_DISABLE;	idx = phy_find_valid(phy_find_setting(phydev->speed, phydev->duplex),			features);	phydev->speed = settings[idx].speed;	phydev->duplex = settings[idx].duplex;}EXPORT_SYMBOL(phy_sanitize_settings);/** * phy_ethtool_sset - generic ethtool sset function, handles all the details * @phydev: target phy_device struct * @cmd: ethtool_cmd * * A few notes about parameter checking: * - We don't set port or transceiver, so we don't care what they *   were set to. * - phy_start_aneg() will make sure forced settings are sane, and *   choose the next best ones from the ones selected, so we don't *   care if ethtool tries to give us bad values. */int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd){	if (cmd->phy_address != phydev->addr)		return -EINVAL;	/* We make sure that we don't pass unsupported	 * values in to the PHY */	cmd->advertising &= phydev->supported;	/* Verify the settings we care about. */	if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)		return -EINVAL;	if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)		return -EINVAL;	if (cmd->autoneg == AUTONEG_DISABLE			&& ((cmd->speed != SPEED_1000					&& cmd->speed != SPEED_100					&& cmd->speed != SPEED_10)				|| (cmd->duplex != DUPLEX_HALF					&& cmd->duplex != DUPLEX_FULL)))		return -EINVAL;	phydev->autoneg = cmd->autoneg;	phydev->speed = cmd->speed;	phydev->advertising = cmd->advertising;	if (AUTONEG_ENABLE == cmd->autoneg)		phydev->advertising |= ADVERTISED_Autoneg;	else		phydev->advertising &= ~ADVERTISED_Autoneg;	phydev->duplex = cmd->duplex;	/* Restart the PHY */	phy_start_aneg(phydev);	return 0;}EXPORT_SYMBOL(phy_ethtool_sset);int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd){	cmd->supported = phydev->supported;	cmd->advertising = phydev->advertising;	cmd->speed = phydev->speed;	cmd->duplex = phydev->duplex;	cmd->port = PORT_MII;	cmd->phy_address = phydev->addr;	cmd->transceiver = XCVR_EXTERNAL;	cmd->autoneg = phydev->autoneg;	return 0;}EXPORT_SYMBOL(phy_ethtool_gset);/** * phy_mii_ioctl - generic PHY MII ioctl interface * @phydev: the phy_device struct * @mii_data: MII ioctl data * @cmd: ioctl cmd to execute * * Note that this function is currently incompatible with the * PHYCONTROL layer.  It changes registers without regard to * current state.  Use at own risk. */int phy_mii_ioctl(struct phy_device *phydev,		struct mii_ioctl_data *mii_data, int cmd){	u16 val = mii_data->val_in;	switch (cmd) {	case SIOCGMIIPHY:		mii_data->phy_id = phydev->addr;		break;	case SIOCGMIIREG:		mii_data->val_out = phy_read(phydev, mii_data->reg_num);		break;	case SIOCSMIIREG:		if (!capable(CAP_NET_ADMIN))			return -EPERM;		if (mii_data->phy_id == phydev->addr) {			switch(mii_data->reg_num) {			case MII_BMCR:				if ((val & (BMCR_RESET|BMCR_ANENABLE)) == 0)					phydev->autoneg = AUTONEG_DISABLE;				else					phydev->autoneg = AUTONEG_ENABLE;				if ((!phydev->autoneg) && (val & BMCR_FULLDPLX))					phydev->duplex = DUPLEX_FULL;				else					phydev->duplex = DUPLEX_HALF;				if ((!phydev->autoneg) &&						(val & BMCR_SPEED1000))					phydev->speed = SPEED_1000;				else if ((!phydev->autoneg) &&						(val & BMCR_SPEED100))					phydev->speed = SPEED_100;				break;			case MII_ADVERTISE:				phydev->advertising = val;				break;			default:				/* do nothing */				break;			}		}		phy_write(phydev, mii_data->reg_num, val);				if (mii_data->reg_num == MII_BMCR 				&& val & BMCR_RESET				&& phydev->drv->config_init)			phydev->drv->config_init(phydev);		break;	default:		return -ENOTTY;	}	return 0;}EXPORT_SYMBOL(phy_mii_ioctl);/** * phy_start_aneg - start auto-negotiation for this PHY device * @phydev: the phy_device struct * * Description: Sanitizes the settings (if we're not autonegotiating *   them), and then calls the driver's config_aneg function. *   If the PHYCONTROL Layer is operating, we change the state to *   reflect the beginning of Auto-negotiation or forcing. */int phy_start_aneg(struct phy_device *phydev){	int err;	spin_lock_bh(&phydev->lock);	if (AUTONEG_DISABLE == phydev->autoneg)		phy_sanitize_settings(phydev);	err = phydev->drv->config_aneg(phydev);	if (err < 0)		goto out_unlock;	if (phydev->state != PHY_HALTED) {		if (AUTONEG_ENABLE == phydev->autoneg) {			phydev->state = PHY_AN;			phydev->link_timeout = PHY_AN_TIMEOUT;		} else {			phydev->state = PHY_FORCING;			phydev->link_timeout = PHY_FORCE_TIMEOUT;		}	}out_unlock:	spin_unlock_bh(&phydev->lock);	return err;}EXPORT_SYMBOL(phy_start_aneg);static void phy_change(struct work_struct *work);static void phy_timer(unsigned long data);/** * phy_start_machine - start PHY state machine tracking * @phydev: the phy_device struct * @handler: callback function for state change notifications * * Description: The PHY infrastructure can run a state machine *   which tracks whether the PHY is starting up, negotiating, *   etc.  This function starts the timer which tracks the state *   of the PHY.  If you want to be notified when the state changes, *   pass in the callback @handler, otherwise, pass NULL.  If you *   want to maintain your own state machine, do not call this *   function. */void phy_start_machine(struct phy_device *phydev,		void (*handler)(struct net_device *)){	phydev->adjust_state = handler;	init_timer(&phydev->phy_timer);	phydev->phy_timer.function = &phy_timer;	phydev->phy_timer.data = (unsigned long) phydev;	mod_timer(&phydev->phy_timer, jiffies + HZ);}/** * phy_stop_machine - stop the PHY state machine tracking * @phydev: target phy_device struct * * Description: Stops the state machine timer, sets the state to UP *   (unless it wasn't up yet). This function must be called BEFORE

⌨️ 快捷键说明

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