1917 lines · 47.7 KB · C
RawDownload
/* Framework for finding and configuring PHYs. * Also contains generic PHY driver * * 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. * */ #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/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/mm.h>#include <linux/module.h>#include <linux/mii.h>#include <linux/ethtool.h>#include <linux/phy.h>#include <linux/phy_led_triggers.h>#include <linux/mdio.h>#include <linux/io.h>#include <linux/uaccess.h>#include <linux/of.h> #include <asm/irq.h> MODULE_DESCRIPTION("PHY library");MODULE_AUTHOR("Andy Fleming");MODULE_LICENSE("GPL"); void phy_device_free(struct phy_device *phydev){	put_device(&phydev->mdio.dev);}EXPORT_SYMBOL(phy_device_free); static void phy_mdio_device_free(struct mdio_device *mdiodev){	struct phy_device *phydev; 	phydev = container_of(mdiodev, struct phy_device, mdio);	phy_device_free(phydev);} static void phy_device_release(struct device *dev){	kfree(to_phy_device(dev));} static void phy_mdio_device_remove(struct mdio_device *mdiodev){	struct phy_device *phydev; 	phydev = container_of(mdiodev, struct phy_device, mdio);	phy_device_remove(phydev);} static struct phy_driver genphy_driver;extern struct phy_driver genphy_10g_driver; static LIST_HEAD(phy_fixup_list);static DEFINE_MUTEX(phy_fixup_lock); #ifdef CONFIG_PMstatic bool mdio_bus_phy_may_suspend(struct phy_device *phydev){	struct device_driver *drv = phydev->mdio.dev.driver;	struct phy_driver *phydrv = to_phy_driver(drv);	struct net_device *netdev = phydev->attached_dev; 	if (!drv || !phydrv->suspend)		return false; 	/* PHY not attached? May suspend if the PHY has not already been	 * suspended as part of a prior call to phy_disconnect() ->	 * phy_detach() -> phy_suspend() because the parent netdev might be the	 * MDIO bus driver and clock gated at this point.	 */	if (!netdev)		return !phydev->suspended; 	/* Don't suspend PHY if the attached netdev parent may wakeup.	 * The parent may point to a PCI device, as in tg3 driver.	 */	if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))		return false; 	/* Also don't suspend PHY if the netdev itself may wakeup. This	 * is the case for devices w/o underlaying pwr. mgmt. aware bus,	 * e.g. SoC devices.	 */	if (device_may_wakeup(&netdev->dev))		return false; 	return true;} static int mdio_bus_phy_suspend(struct device *dev){	struct phy_device *phydev = to_phy_device(dev); 	/* We must stop the state machine manually, otherwise it stops out of	 * control, possibly with the phydev->lock held. Upon resume, netdev	 * may call phy routines that try to grab the same lock, and that may	 * lead to a deadlock.	 */	if (phydev->attached_dev && phydev->adjust_link)		phy_stop_machine(phydev); 	if (!mdio_bus_phy_may_suspend(phydev))		return 0; 	return phy_suspend(phydev);} static int mdio_bus_phy_resume(struct device *dev){	struct phy_device *phydev = to_phy_device(dev);	int ret; 	if (!mdio_bus_phy_may_suspend(phydev))		goto no_resume; 	ret = phy_resume(phydev);	if (ret < 0)		return ret; no_resume:	if (phydev->attached_dev && phydev->adjust_link)		phy_start_machine(phydev); 	return 0;} static int mdio_bus_phy_restore(struct device *dev){	struct phy_device *phydev = to_phy_device(dev);	struct net_device *netdev = phydev->attached_dev;	int ret; 	if (!netdev)		return 0; 	ret = phy_init_hw(phydev);	if (ret < 0)		return ret; 	/* The PHY needs to renegotiate. */	phydev->link = 0;	phydev->state = PHY_UP; 	phy_start_machine(phydev); 	return 0;} static const struct dev_pm_ops mdio_bus_phy_pm_ops = {	.suspend = mdio_bus_phy_suspend,	.resume = mdio_bus_phy_resume,	.freeze = mdio_bus_phy_suspend,	.thaw = mdio_bus_phy_resume,	.restore = mdio_bus_phy_restore,}; #define MDIO_BUS_PHY_PM_OPS (&mdio_bus_phy_pm_ops) #else #define MDIO_BUS_PHY_PM_OPS NULL #endif /* CONFIG_PM */ /** * phy_register_fixup - creates a new phy_fixup and adds it to the list * @bus_id: A string which matches phydev->mdio.dev.bus_id (or PHY_ANY_ID) * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY) *	It can also be PHY_ANY_UID * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before *	comparison * @run: The actual code to be run when a matching PHY is found */int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,		       int (*run)(struct phy_device *)){	struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL); 	if (!fixup)		return -ENOMEM; 	strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));	fixup->phy_uid = phy_uid;	fixup->phy_uid_mask = phy_uid_mask;	fixup->run = run; 	mutex_lock(&phy_fixup_lock);	list_add_tail(&fixup->list, &phy_fixup_list);	mutex_unlock(&phy_fixup_lock); 	return 0;}EXPORT_SYMBOL(phy_register_fixup); /* Registers a fixup to be run on any PHY with the UID in phy_uid */int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,			       int (*run)(struct phy_device *)){	return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);}EXPORT_SYMBOL(phy_register_fixup_for_uid); /* Registers a fixup to be run on the PHY with id string bus_id */int phy_register_fixup_for_id(const char *bus_id,			      int (*run)(struct phy_device *)){	return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);}EXPORT_SYMBOL(phy_register_fixup_for_id); /** * phy_unregister_fixup - remove a phy_fixup from the list * @bus_id: A string matches fixup->bus_id (or PHY_ANY_ID) in phy_fixup_list * @phy_uid: A phy id matches fixup->phy_id (or PHY_ANY_UID) in phy_fixup_list * @phy_uid_mask: Applied to phy_uid and fixup->phy_uid before comparison */int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask){	struct list_head *pos, *n;	struct phy_fixup *fixup;	int ret; 	ret = -ENODEV; 	mutex_lock(&phy_fixup_lock);	list_for_each_safe(pos, n, &phy_fixup_list) {		fixup = list_entry(pos, struct phy_fixup, list); 		if ((!strcmp(fixup->bus_id, bus_id)) &&		    ((fixup->phy_uid & phy_uid_mask) ==		     (phy_uid & phy_uid_mask))) {			list_del(&fixup->list);			kfree(fixup);			ret = 0;			break;		}	}	mutex_unlock(&phy_fixup_lock); 	return ret;}EXPORT_SYMBOL(phy_unregister_fixup); /* Unregisters a fixup of any PHY with the UID in phy_uid */int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask){	return phy_unregister_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask);}EXPORT_SYMBOL(phy_unregister_fixup_for_uid); /* Unregisters a fixup of the PHY with id string bus_id */int phy_unregister_fixup_for_id(const char *bus_id){	return phy_unregister_fixup(bus_id, PHY_ANY_UID, 0xffffffff);}EXPORT_SYMBOL(phy_unregister_fixup_for_id); /* Returns 1 if fixup matches phydev in bus_id and phy_uid. * Fixups can be set to match any in one or more fields. */static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup){	if (strcmp(fixup->bus_id, phydev_name(phydev)) != 0)		if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)			return 0; 	if ((fixup->phy_uid & fixup->phy_uid_mask) !=	    (phydev->phy_id & fixup->phy_uid_mask))		if (fixup->phy_uid != PHY_ANY_UID)			return 0; 	return 1;} /* Runs any matching fixups for this phydev */static int phy_scan_fixups(struct phy_device *phydev){	struct phy_fixup *fixup; 	mutex_lock(&phy_fixup_lock);	list_for_each_entry(fixup, &phy_fixup_list, list) {		if (phy_needs_fixup(phydev, fixup)) {			int err = fixup->run(phydev); 			if (err < 0) {				mutex_unlock(&phy_fixup_lock);				return err;			}			phydev->has_fixups = true;		}	}	mutex_unlock(&phy_fixup_lock); 	return 0;} static int phy_bus_match(struct device *dev, struct device_driver *drv){	struct phy_device *phydev = to_phy_device(dev);	struct phy_driver *phydrv = to_phy_driver(drv);	const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids);	int i; 	if (!(phydrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY))		return 0; 	if (phydrv->match_phy_device)		return phydrv->match_phy_device(phydev); 	if (phydev->is_c45) {		for (i = 1; i < num_ids; i++) {			if (!(phydev->c45_ids.devices_in_package & (1 << i)))				continue; 			if ((phydrv->phy_id & phydrv->phy_id_mask) ==			    (phydev->c45_ids.device_ids[i] &			     phydrv->phy_id_mask))				return 1;		}		return 0;	} else {		return (phydrv->phy_id & phydrv->phy_id_mask) ==			(phydev->phy_id & phydrv->phy_id_mask);	}} struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id,				     bool is_c45,				     struct phy_c45_device_ids *c45_ids){	struct phy_device *dev;	struct mdio_device *mdiodev; 	/* We allocate the device, and initialize the default values */	dev = kzalloc(sizeof(*dev), GFP_KERNEL);	if (!dev)		return ERR_PTR(-ENOMEM); 	mdiodev = &dev->mdio;	mdiodev->dev.release = phy_device_release;	mdiodev->dev.parent = &bus->dev;	mdiodev->dev.bus = &mdio_bus_type;	mdiodev->bus = bus;	mdiodev->pm_ops = MDIO_BUS_PHY_PM_OPS;	mdiodev->bus_match = phy_bus_match;	mdiodev->addr = addr;	mdiodev->flags = MDIO_DEVICE_FLAG_PHY;	mdiodev->device_free = phy_mdio_device_free;	mdiodev->device_remove = phy_mdio_device_remove; 	dev->speed = 0;	dev->duplex = -1;	dev->pause = 0;	dev->asym_pause = 0;	dev->link = 1;	dev->interface = PHY_INTERFACE_MODE_GMII; 	dev->autoneg = AUTONEG_ENABLE; 	dev->is_c45 = is_c45;	dev->phy_id = phy_id;	if (c45_ids)		dev->c45_ids = *c45_ids;	dev->irq = bus->irq[addr];	dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr); 	dev->state = PHY_DOWN; 	mutex_init(&dev->lock);	INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);	INIT_WORK(&dev->phy_queue, phy_change_work); 	/* Request the appropriate module unconditionally; don't	 * bother trying to do so only if it isn't already loaded,	 * because that gets complicated. A hotplug event would have	 * done an unconditional modprobe anyway.	 * We don't do normal hotplug because it won't work for MDIO	 * -- because it relies on the device staying around for long	 * enough for the driver to get loaded. With MDIO, the NIC	 * driver will get bored and give up as soon as it finds that	 * there's no driver _already_ loaded.	 */	request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT, MDIO_ID_ARGS(phy_id)); 	device_initialize(&mdiodev->dev); 	return dev;}EXPORT_SYMBOL(phy_device_create); /* get_phy_c45_devs_in_pkg - reads a MMD's devices in package registers. * @bus: the target MII bus * @addr: PHY address on the MII bus * @dev_addr: MMD address in the PHY. * @devices_in_package: where to store the devices in package information. * * Description: reads devices in package registers of a MMD at @dev_addr * from PHY at @addr on @bus. * * Returns: 0 on success, -EIO on failure. */static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr,				   u32 *devices_in_package){	int phy_reg, reg_addr; 	reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS2;	phy_reg = mdiobus_read(bus, addr, reg_addr);	if (phy_reg < 0)		return -EIO;	*devices_in_package = (phy_reg & 0xffff) << 16; 	reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS1;	phy_reg = mdiobus_read(bus, addr, reg_addr);	if (phy_reg < 0)		return -EIO;	*devices_in_package |= (phy_reg & 0xffff); 	return 0;} /** * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs. * @bus: the target MII bus * @addr: PHY address on the MII bus * @phy_id: where to store the ID retrieved. * @c45_ids: where to store the c45 ID information. * *   If the PHY devices-in-package appears to be valid, it and the *   corresponding identifiers are stored in @c45_ids, zero is stored *   in @phy_id.  Otherwise 0xffffffff is stored in @phy_id.  Returns *   zero on success. * */static int get_phy_c45_ids(struct mii_bus *bus, int addr, u32 *phy_id,			   struct phy_c45_device_ids *c45_ids) {	int phy_reg;	int i, reg_addr;	const int num_ids = ARRAY_SIZE(c45_ids->device_ids);	u32 *devs = &c45_ids->devices_in_package; 	/* Find first non-zero Devices In package. Device zero is reserved	 * for 802.3 c45 complied PHYs, so don't probe it at first.	 */	for (i = 1; i < num_ids && *devs == 0; i++) {		phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, devs);		if (phy_reg < 0)			return -EIO; 		if ((*devs & 0x1fffffff) == 0x1fffffff) {			/*  If mostly Fs, there is no device there,			 *  then let's continue to probe more, as some			 *  10G PHYs have zero Devices In package,			 *  e.g. Cortina CS4315/CS4340 PHY.			 */			phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, devs);			if (phy_reg < 0)				return -EIO;			/* no device there, let's get out of here */			if ((*devs & 0x1fffffff) == 0x1fffffff) {				*phy_id = 0xffffffff;				return 0;			} else {				break;			}		}	} 	/* Now probe Device Identifiers for each device present. */	for (i = 1; i < num_ids; i++) {		if (!(c45_ids->devices_in_package & (1 << i)))			continue; 		reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID1;		phy_reg = mdiobus_read(bus, addr, reg_addr);		if (phy_reg < 0)			return -EIO;		c45_ids->device_ids[i] = (phy_reg & 0xffff) << 16;

Lines 1–500 of 1917.