blob: d791100afab24d37e8eb4fba0ce30450f129746d [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{
Russell King1ee6b9b2017-03-21 16:36:43 +000072 if (regnum > (u16)~0 || devad > 32)
73 return -EINVAL;
Russell King98601182017-03-21 16:36:37 +000074
Russell King1ee6b9b2017-03-21 16:36:43 +000075 if (phydev->drv->read_mmd)
76 return phydev->drv->read_mmd(phydev, devad, regnum);
77
78 if (phydev->is_c45) {
79 u32 addr = MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff);
80 return mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, addr);
81 }
82
83 return phy_read_mmd_indirect(phydev, regnum, devad);
Russell King98601182017-03-21 16:36:37 +000084}
85EXPORT_SYMBOL(phy_read_mmd);
86
87/**
88 * phy_write_mmd_indirect - writes data to the MMD registers
89 * @phydev: The PHY device
90 * @prtad: MMD Address
91 * @devad: MMD DEVAD
92 * @data: data to write in the MMD register
93 *
94 * Description: Write data from the MMD registers of the specified
95 * phy address.
96 * To write these register we have:
97 * 1) Write reg 13 // DEVAD
98 * 2) Write reg 14 // MMD Address
99 * 3) Write reg 13 // MMD Data Command for MMD DEVAD
100 * 3) Write reg 14 // Write MMD data
101 */
102void phy_write_mmd_indirect(struct phy_device *phydev, int prtad,
103 int devad, u32 data)
104{
105 struct phy_driver *phydrv = phydev->drv;
106 int addr = phydev->mdio.addr;
107
108 if (!phydrv->write_mmd_indirect) {
109 struct mii_bus *bus = phydev->mdio.bus;
110
111 mutex_lock(&bus->mdio_lock);
112 mmd_phy_indirect(bus, prtad, devad, addr);
113
114 /* Write the data into MMD's selected register */
115 bus->write(bus, addr, MII_MMD_DATA, data);
116 mutex_unlock(&bus->mdio_lock);
117 } else {
118 phydrv->write_mmd_indirect(phydev, prtad, devad, addr, data);
119 }
120}
121EXPORT_SYMBOL(phy_write_mmd_indirect);
122
123/**
124 * phy_write_mmd - Convenience function for writing a register
125 * on an MMD on a given PHY.
126 * @phydev: The phy_device struct
127 * @devad: The MMD to read from
128 * @regnum: The register on the MMD to read
129 * @val: value to write to @regnum
130 *
131 * Same rules as for phy_write();
132 */
133int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
134{
Russell King1ee6b9b2017-03-21 16:36:43 +0000135 if (regnum > (u16)~0 || devad > 32)
136 return -EINVAL;
Russell King98601182017-03-21 16:36:37 +0000137
Russell King1ee6b9b2017-03-21 16:36:43 +0000138 if (phydev->drv->read_mmd)
139 return phydev->drv->write_mmd(phydev, devad, regnum, val);
Russell King98601182017-03-21 16:36:37 +0000140
Russell King1ee6b9b2017-03-21 16:36:43 +0000141 if (phydev->is_c45) {
142 u32 addr = MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff);
143
144 return mdiobus_write(phydev->mdio.bus, phydev->mdio.addr,
145 addr, val);
146 }
147
148 phy_write_mmd_indirect(phydev, regnum, devad, val);
149
150 return 0;
Russell King98601182017-03-21 16:36:37 +0000151}
152EXPORT_SYMBOL(phy_write_mmd);