2017-03-21 16:36:37 +00:00
|
|
|
/*
|
|
|
|
* Core PHY library, taken from phy.c
|
|
|
|
*
|
|
|
|
* 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/export.h>
|
|
|
|
#include <linux/phy.h>
|
|
|
|
|
2017-03-21 16:37:08 +00:00
|
|
|
static void mmd_phy_indirect(struct mii_bus *bus, int phy_addr, int devad,
|
|
|
|
u16 regnum)
|
2017-03-21 16:36:37 +00:00
|
|
|
{
|
|
|
|
/* Write the desired MMD Devad */
|
2017-03-21 16:37:08 +00:00
|
|
|
bus->write(bus, phy_addr, MII_MMD_CTRL, devad);
|
2017-03-21 16:36:37 +00:00
|
|
|
|
|
|
|
/* Write the desired MMD register address */
|
2017-03-21 16:37:08 +00:00
|
|
|
bus->write(bus, phy_addr, MII_MMD_DATA, regnum);
|
2017-03-21 16:36:37 +00:00
|
|
|
|
|
|
|
/* Select the Function : DATA with no post increment */
|
2017-03-21 16:37:08 +00:00
|
|
|
bus->write(bus, phy_addr, MII_MMD_CTRL, devad | MII_MMD_CTRL_NOINCR);
|
2017-03-21 16:36:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* phy_read_mmd - Convenience function for reading a register
|
|
|
|
* from an MMD on a given PHY.
|
|
|
|
* @phydev: The phy_device struct
|
2017-03-21 16:37:03 +00:00
|
|
|
* @devad: The MMD to read from (0..31)
|
|
|
|
* @regnum: The register on the MMD to read (0..65535)
|
2017-03-21 16:36:37 +00:00
|
|
|
*
|
|
|
|
* Same rules as for phy_read();
|
|
|
|
*/
|
|
|
|
int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
|
|
|
|
{
|
2017-03-21 16:37:03 +00:00
|
|
|
int val;
|
|
|
|
|
2017-03-21 16:36:43 +00:00
|
|
|
if (regnum > (u16)~0 || devad > 32)
|
|
|
|
return -EINVAL;
|
2017-03-21 16:36:37 +00:00
|
|
|
|
2017-03-21 16:37:03 +00:00
|
|
|
if (phydev->drv->read_mmd) {
|
|
|
|
val = phydev->drv->read_mmd(phydev, devad, regnum);
|
|
|
|
} else if (phydev->is_c45) {
|
2017-03-21 16:36:43 +00:00
|
|
|
u32 addr = MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff);
|
|
|
|
|
2017-03-21 16:37:03 +00:00
|
|
|
val = mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, addr);
|
|
|
|
} else {
|
2017-03-21 16:36:37 +00:00
|
|
|
struct mii_bus *bus = phydev->mdio.bus;
|
2017-03-21 16:37:03 +00:00
|
|
|
int phy_addr = phydev->mdio.addr;
|
2017-03-21 16:36:37 +00:00
|
|
|
|
|
|
|
mutex_lock(&bus->mdio_lock);
|
2017-03-21 16:37:08 +00:00
|
|
|
mmd_phy_indirect(bus, phy_addr, devad, regnum);
|
2017-03-21 16:36:37 +00:00
|
|
|
|
2017-03-21 16:37:03 +00:00
|
|
|
/* Read the content of the MMD's selected register */
|
|
|
|
val = bus->read(bus, phy_addr, MII_MMD_DATA);
|
2017-03-21 16:36:37 +00:00
|
|
|
mutex_unlock(&bus->mdio_lock);
|
|
|
|
}
|
2017-03-21 16:37:03 +00:00
|
|
|
return val;
|
2017-03-21 16:36:37 +00:00
|
|
|
}
|
2017-03-21 16:37:03 +00:00
|
|
|
EXPORT_SYMBOL(phy_read_mmd);
|
2017-03-21 16:36:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* phy_write_mmd - Convenience function for writing a register
|
|
|
|
* on an MMD on a given PHY.
|
|
|
|
* @phydev: The phy_device struct
|
|
|
|
* @devad: The MMD to read from
|
|
|
|
* @regnum: The register on the MMD to read
|
|
|
|
* @val: value to write to @regnum
|
|
|
|
*
|
|
|
|
* Same rules as for phy_write();
|
|
|
|
*/
|
|
|
|
int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
|
|
|
|
{
|
2017-03-21 16:37:03 +00:00
|
|
|
int ret;
|
|
|
|
|
2017-03-21 16:36:43 +00:00
|
|
|
if (regnum > (u16)~0 || devad > 32)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2017-03-21 16:37:03 +00:00
|
|
|
if (phydev->drv->read_mmd) {
|
|
|
|
ret = phydev->drv->write_mmd(phydev, devad, regnum, val);
|
|
|
|
} else if (phydev->is_c45) {
|
2017-03-21 16:36:43 +00:00
|
|
|
u32 addr = MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff);
|
|
|
|
|
2017-03-21 16:37:03 +00:00
|
|
|
ret = mdiobus_write(phydev->mdio.bus, phydev->mdio.addr,
|
|
|
|
addr, val);
|
|
|
|
} else {
|
|
|
|
struct mii_bus *bus = phydev->mdio.bus;
|
|
|
|
int phy_addr = phydev->mdio.addr;
|
2017-03-21 16:36:37 +00:00
|
|
|
|
2017-03-21 16:37:03 +00:00
|
|
|
mutex_lock(&bus->mdio_lock);
|
2017-03-21 16:37:08 +00:00
|
|
|
mmd_phy_indirect(bus, phy_addr, devad, regnum);
|
2017-03-21 16:36:37 +00:00
|
|
|
|
2017-03-21 16:37:03 +00:00
|
|
|
/* Write the data into MMD's selected register */
|
|
|
|
bus->write(bus, phy_addr, MII_MMD_DATA, val);
|
|
|
|
mutex_unlock(&bus->mdio_lock);
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
return ret;
|
2017-03-21 16:36:37 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(phy_write_mmd);
|