phy-core.c 2.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * 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>

12 13
static void mmd_phy_indirect(struct mii_bus *bus, int phy_addr, int devad,
			     u16 regnum)
14 15
{
	/* Write the desired MMD Devad */
16
	bus->write(bus, phy_addr, MII_MMD_CTRL, devad);
17 18

	/* Write the desired MMD register address */
19
	bus->write(bus, phy_addr, MII_MMD_DATA, regnum);
20 21

	/* Select the Function : DATA with no post increment */
22
	bus->write(bus, phy_addr, MII_MMD_CTRL, devad | MII_MMD_CTRL_NOINCR);
23 24 25 26 27 28
}

/**
 * phy_read_mmd - Convenience function for reading a register
 * from an MMD on a given PHY.
 * @phydev: The phy_device struct
29 30
 * @devad: The MMD to read from (0..31)
 * @regnum: The register on the MMD to read (0..65535)
31 32 33 34 35
 *
 * Same rules as for phy_read();
 */
int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
{
36 37
	int val;

38 39
	if (regnum > (u16)~0 || devad > 32)
		return -EINVAL;
40

41 42 43
	if (phydev->drv->read_mmd) {
		val = phydev->drv->read_mmd(phydev, devad, regnum);
	} else if (phydev->is_c45) {
44 45
		u32 addr = MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff);

46 47
		val = mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, addr);
	} else {
48
		struct mii_bus *bus = phydev->mdio.bus;
49
		int phy_addr = phydev->mdio.addr;
50 51

		mutex_lock(&bus->mdio_lock);
52
		mmd_phy_indirect(bus, phy_addr, devad, regnum);
53

54 55
		/* Read the content of the MMD's selected register */
		val = bus->read(bus, phy_addr, MII_MMD_DATA);
56 57
		mutex_unlock(&bus->mdio_lock);
	}
58
	return val;
59
}
60
EXPORT_SYMBOL(phy_read_mmd);
61 62 63 64 65 66 67 68 69 70 71 72 73

/**
 * 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)
{
74 75
	int ret;

76 77 78
	if (regnum > (u16)~0 || devad > 32)
		return -EINVAL;

79
	if (phydev->drv->write_mmd) {
80 81
		ret = phydev->drv->write_mmd(phydev, devad, regnum, val);
	} else if (phydev->is_c45) {
82 83
		u32 addr = MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff);

84 85 86 87 88
		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;
89

90
		mutex_lock(&bus->mdio_lock);
91
		mmd_phy_indirect(bus, phy_addr, devad, regnum);
92

93 94 95 96 97 98 99
		/* 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;
100 101
}
EXPORT_SYMBOL(phy_write_mmd);