Sergei Shtylyovphy: IRQ cannot be shared33c133c12 years agoHistory
1163 lines · 28.4 KB · C
RawDownload
/* * 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. * */ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt #include <linux/kernel.h>#include <linux/string.h>#include <linux/errno.h>#include <linux/unistd.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/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 <linux/mdio.h> #include <linux/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){	if (phydev->link)		pr_info("%s - Link is Up - %d/%s\n",			dev_name(&phydev->dev),			phydev->speed,			DUPLEX_FULL == phydev->duplex ? "Full" : "Half");	else		pr_info("%s - Link is Down\n", dev_name(&phydev->dev));}EXPORT_SYMBOL(phy_print_status); /** * 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. */static 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. */static 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. */static 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;} /** * 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){	u32 speed = ethtool_cmd_speed(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 &&	    ((speed != SPEED_1000 &&	      speed != SPEED_100 &&	      speed != SPEED_10) ||	     (cmd->duplex != DUPLEX_HALF &&	      cmd->duplex != DUPLEX_FULL)))		return -EINVAL; 	phydev->autoneg = cmd->autoneg; 	phydev->speed = 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; 	ethtool_cmd_speed_set(cmd, phydev->speed);	cmd->duplex = phydev->duplex;	cmd->port = PORT_MII;	cmd->phy_address = phydev->addr;	cmd->transceiver = phy_is_internal(phydev) ?		XCVR_INTERNAL : 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 * @ifr: &struct ifreq for socket ioctl's * @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 ifreq *ifr, int cmd){	struct mii_ioctl_data *mii_data = if_mii(ifr);	u16 val = mii_data->val_in; 	switch (cmd) {	case SIOCGMIIPHY:		mii_data->phy_id = phydev->addr;		/* fall through */ 	case SIOCGMIIREG:		mii_data->val_out = mdiobus_read(phydev->bus, mii_data->phy_id,						 mii_data->reg_num);		break; 	case SIOCSMIIREG:		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;			}		} 		mdiobus_write(phydev->bus, mii_data->phy_id,			      mii_data->reg_num, val); 		if (mii_data->reg_num == MII_BMCR &&		    val & BMCR_RESET &&		    phydev->drv->config_init) {			phy_scan_fixups(phydev);			phydev->drv->config_init(phydev);		}		break; 	case SIOCSHWTSTAMP:		if (phydev->drv->hwtstamp)			return phydev->drv->hwtstamp(phydev, ifr);		/* fall through */ 	default:		return -EOPNOTSUPP;	} 	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; 	mutex_lock(&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:	mutex_unlock(&phydev->lock);	return err;}EXPORT_SYMBOL(phy_start_aneg);  /** * 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; 	queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, 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 *   phy_detach. */void phy_stop_machine(struct phy_device *phydev){	cancel_delayed_work_sync(&phydev->state_queue); 	mutex_lock(&phydev->lock);	if (phydev->state > PHY_UP)		phydev->state = PHY_UP;	mutex_unlock(&phydev->lock); 	phydev->adjust_state = NULL;} /** * phy_error - enter HALTED state for this PHY device * @phydev: target phy_device struct * * Moves the PHY to the HALTED state in response to a read * or write error, and tells the controller the link is down. * Must not be called from interrupt context, or while the * phydev->lock is held. */static void phy_error(struct phy_device *phydev){	mutex_lock(&phydev->lock);	phydev->state = PHY_HALTED;	mutex_unlock(&phydev->lock);} /** * phy_interrupt - PHY interrupt handler * @irq: interrupt line * @phy_dat: phy_device pointer * * Description: When a PHY interrupt occurs, the handler disables * interrupts, and schedules a work task to clear the interrupt. */static irqreturn_t phy_interrupt(int irq, void *phy_dat){	struct phy_device *phydev = phy_dat; 	if (PHY_HALTED == phydev->state)		return IRQ_NONE;		/* It can't be ours.  */ 	/* The MDIO bus is not allowed to be written in interrupt	 * context, so we need to disable the irq here.  A work	 * queue will write the PHY to disable and clear the	 * interrupt, and then reenable the irq line. */	disable_irq_nosync(irq);	atomic_inc(&phydev->irq_disable);

Lines 1–500 of 1163.