blob: b8d8276a309900d46cf8c15b7090946150b1f987 [file] [log] [blame]
Russell King98601182017-03-21 16:36:37 +00001/*
2 * Core PHY library, taken from phy.c
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 */
9#include <linux/export.h>
10#include <linux/phy.h>
11
12static inline void mmd_phy_indirect(struct mii_bus *bus, int prtad, int devad,
13 int addr)
14{
15 /* Write the desired MMD Devad */
16 bus->write(bus, addr, MII_MMD_CTRL, devad);
17
18 /* Write the desired MMD register address */
19 bus->write(bus, addr, MII_MMD_DATA, prtad);
20
21 /* Select the Function : DATA with no post increment */
22 bus->write(bus, addr, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
23}
24
25/**
26 * phy_read_mmd_indirect - reads data from the MMD registers
27 * @phydev: The PHY device bus
28 * @prtad: MMD Address
29 * @devad: MMD DEVAD
30 *
31 * Description: it reads data from the MMD registers (clause 22 to access to
32 * clause 45) of the specified phy address.
33 * To read these register we have:
34 * 1) Write reg 13 // DEVAD
35 * 2) Write reg 14 // MMD Address
36 * 3) Write reg 13 // MMD Data Command for MMD DEVAD
37 * 3) Read reg 14 // Read MMD data
38 */
39int phy_read_mmd_indirect(struct phy_device *phydev, int prtad, int devad)
40{
41 struct phy_driver *phydrv = phydev->drv;
42 int addr = phydev->mdio.addr;
43 int value = -1;
44
45 if (!phydrv->read_mmd_indirect) {
46 struct mii_bus *bus = phydev->mdio.bus;
47
48 mutex_lock(&bus->mdio_lock);
49 mmd_phy_indirect(bus, prtad, devad, addr);
50
51 /* Read the content of the MMD's selected register */
52 value = bus->read(bus, addr, MII_MMD_DATA);
53 mutex_unlock(&bus->mdio_lock);
54 } else {
55 value = phydrv->read_mmd_indirect(phydev, prtad, devad, addr);
56 }
57 return value;
58}
59EXPORT_SYMBOL(phy_read_mmd_indirect);
60
61/**
62 * phy_read_mmd - Convenience function for reading a register
63 * from an MMD on a given PHY.
64 * @phydev: The phy_device struct
65 * @devad: The MMD to read from
66 * @regnum: The register on the MMD to read
67 *
68 * Same rules as for phy_read();
69 */
70int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
71{
72 if (!phydev->is_c45)
73 return -EOPNOTSUPP;
74
75 return mdiobus_read(phydev->mdio.bus, phydev->mdio.addr,
76 MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff));
77}
78EXPORT_SYMBOL(phy_read_mmd);
79
80/**
81 * phy_write_mmd_indirect - writes data to the MMD registers
82 * @phydev: The PHY device
83 * @prtad: MMD Address
84 * @devad: MMD DEVAD
85 * @data: data to write in the MMD register
86 *
87 * Description: Write data from the MMD registers of the specified
88 * phy address.
89 * To write these register we have:
90 * 1) Write reg 13 // DEVAD
91 * 2) Write reg 14 // MMD Address
92 * 3) Write reg 13 // MMD Data Command for MMD DEVAD
93 * 3) Write reg 14 // Write MMD data
94 */
95void phy_write_mmd_indirect(struct phy_device *phydev, int prtad,
96 int devad, u32 data)
97{
98 struct phy_driver *phydrv = phydev->drv;
99 int addr = phydev->mdio.addr;
100
101 if (!phydrv->write_mmd_indirect) {
102 struct mii_bus *bus = phydev->mdio.bus;
103
104 mutex_lock(&bus->mdio_lock);
105 mmd_phy_indirect(bus, prtad, devad, addr);
106
107 /* Write the data into MMD's selected register */
108 bus->write(bus, addr, MII_MMD_DATA, data);
109 mutex_unlock(&bus->mdio_lock);
110 } else {
111 phydrv->write_mmd_indirect(phydev, prtad, devad, addr, data);
112 }
113}
114EXPORT_SYMBOL(phy_write_mmd_indirect);
115
116/**
117 * phy_write_mmd - Convenience function for writing a register
118 * on an MMD on a given PHY.
119 * @phydev: The phy_device struct
120 * @devad: The MMD to read from
121 * @regnum: The register on the MMD to read
122 * @val: value to write to @regnum
123 *
124 * Same rules as for phy_write();
125 */
126int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
127{
128 if (!phydev->is_c45)
129 return -EOPNOTSUPP;
130
131 regnum = MII_ADDR_C45 | ((devad & 0x1f) << 16) | (regnum & 0xffff);
132
133 return mdiobus_write(phydev->mdio.bus, phydev->mdio.addr, regnum, val);
134}
135EXPORT_SYMBOL(phy_write_mmd);