📄 phy.c
字号:
/* * 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. * * 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/config.h>#include <linux/kernel.h>#include <linux/sched.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/version.h>#include <linux/mii.h>#include <linux/ethtool.h>#include <linux/phy.h>#include <asm/io.h>#include <asm/irq.h>#include <asm/uaccess.h>/* Convenience function to print out the current phy status */void phy_print_status(struct phy_device *phydev){ pr_info("%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);/* Convenience functions for reading/writing a given PHY * register. They 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);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);int phy_clear_interrupt(struct phy_device *phydev){ int err = 0; if (phydev->drv->ack_interrupt) err = phydev->drv->ack_interrupt(phydev); return err;}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 * * 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 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 (sizeof(settings)/sizeof(struct phy_setting))/* phy_find_setting * * 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 * 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 * * 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 = 0; 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: * A generic ethtool sset function. Handles all the details * * 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 * * A note about the PHYCONTROL Layer. If you turn off * CONFIG_PHYCONTROL, you will need to read the PHY status * registers after this function completes, and update your * controller manually. */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;}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;}/* 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)) phydev->autoneg = AUTONEG_DISABLE; else phydev->autoneg = AUTONEG_ENABLE; if ((!phydev->autoneg) && (val & BMCR_FULLDPLX)) phydev->duplex = DUPLEX_FULL; else phydev->duplex = DUPLEX_HALF; 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; } return 0;}/* phy_start_aneg * * 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(&phydev->lock); if (AUTONEG_DISABLE == phydev->autoneg) phy_sanitize_settings(phydev); err = phydev->drv->config_aneg(phydev);#ifdef CONFIG_PHYCONTROL 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:#endif spin_unlock(&phydev->lock); return err;}EXPORT_SYMBOL(phy_start_aneg);#ifdef CONFIG_PHYCONTROLstatic void phy_change(void *data);static void phy_timer(unsigned long data);/* phy_start_machine: * * 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, 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 * * description: Stops the state machine timer, sets the state to * UP (unless it wasn't up yet), and then frees the interrupt, * if it is in use. This function must be called BEFORE * phy_detach. */void phy_stop_machine(struct phy_device *phydev)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -