Skip to content

Commit c3aad35

Browse files
committed
Merge branch 'of_mdio'
Daniel Mack says: ==================== mdio: Parse DT nodes for auto-probed PHYs Here's v2. v1 -> v2: * Switch to of_property_read_u32() in patch #1 * Check for mdio->dev_of_node in patch #2 * Added Florian's Reviewed-by: tags ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents 6623b41 + 24f28dd commit c3aad35

File tree

3 files changed

+72
-14
lines changed

3 files changed

+72
-14
lines changed

drivers/net/phy/mdio_bus.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,12 @@ struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
300300
if (IS_ERR(phydev) || phydev == NULL)
301301
return phydev;
302302

303+
/*
304+
* For DT, see if the auto-probed phy has a correspoding child
305+
* in the bus node, and set the of_node pointer in this case.
306+
*/
307+
of_mdiobus_link_phydev(bus, phydev);
308+
303309
err = phy_device_register(phydev);
304310
if (err) {
305311
phy_device_free(phydev);

drivers/of/of_mdio.c

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,27 @@ static int of_mdiobus_register_phy(struct mii_bus *mdio, struct device_node *chi
8888
return 0;
8989
}
9090

91+
static int of_mdio_parse_addr(struct device *dev, const struct device_node *np)
92+
{
93+
u32 addr;
94+
int ret;
95+
96+
ret = of_property_read_u32(np, "reg", &addr);
97+
if (ret < 0) {
98+
dev_err(dev, "%s has invalid PHY address\n", np->full_name);
99+
return ret;
100+
}
101+
102+
/* A PHY must have a reg property in the range [0-31] */
103+
if (addr >= PHY_MAX_ADDR) {
104+
dev_err(dev, "%s PHY address %i is too large\n",
105+
np->full_name, addr);
106+
return -EINVAL;
107+
}
108+
109+
return addr;
110+
}
111+
91112
/**
92113
* of_mdiobus_register - Register mii_bus and create PHYs from the device tree
93114
* @mdio: pointer to mii_bus structure
@@ -102,7 +123,7 @@ int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np)
102123
const __be32 *paddr;
103124
u32 addr;
104125
bool scanphys = false;
105-
int rc, i, len;
126+
int rc, i;
106127

107128
/* Mask out all PHYs from auto probing. Instead the PHYs listed in
108129
* the device tree are populated after the bus has been registered */
@@ -122,19 +143,9 @@ int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np)
122143

123144
/* Loop over the child nodes and register a phy_device for each one */
124145
for_each_available_child_of_node(np, child) {
125-
/* A PHY must have a reg property in the range [0-31] */
126-
paddr = of_get_property(child, "reg", &len);
127-
if (!paddr || len < sizeof(*paddr)) {
146+
addr = of_mdio_parse_addr(&mdio->dev, child);
147+
if (addr < 0) {
128148
scanphys = true;
129-
dev_err(&mdio->dev, "%s has invalid PHY address\n",
130-
child->full_name);
131-
continue;
132-
}
133-
134-
addr = be32_to_cpup(paddr);
135-
if (addr >= PHY_MAX_ADDR) {
136-
dev_err(&mdio->dev, "%s PHY address %i is too large\n",
137-
child->full_name, addr);
138149
continue;
139150
}
140151

@@ -149,7 +160,7 @@ int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np)
149160
/* auto scan for PHYs with empty reg property */
150161
for_each_available_child_of_node(np, child) {
151162
/* Skip PHYs with reg property set */
152-
paddr = of_get_property(child, "reg", &len);
163+
paddr = of_get_property(child, "reg", NULL);
153164
if (paddr)
154165
continue;
155166

@@ -172,6 +183,39 @@ int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np)
172183
}
173184
EXPORT_SYMBOL(of_mdiobus_register);
174185

186+
/**
187+
* of_mdiobus_link_phydev - Find a device node for a phy
188+
* @mdio: pointer to mii_bus structure
189+
* @phydev: phydev for which the of_node pointer should be set
190+
*
191+
* Walk the list of subnodes of a mdio bus and look for a node that matches the
192+
* phy's address with its 'reg' property. If found, set the of_node pointer for
193+
* the phy. This allows auto-probed pyh devices to be supplied with information
194+
* passed in via DT.
195+
*/
196+
void of_mdiobus_link_phydev(struct mii_bus *mdio,
197+
struct phy_device *phydev)
198+
{
199+
struct device *dev = &phydev->dev;
200+
struct device_node *child;
201+
202+
if (dev->of_node || !mdio->dev.of_node)
203+
return;
204+
205+
for_each_available_child_of_node(mdio->dev.of_node, child) {
206+
int addr;
207+
208+
addr = of_mdio_parse_addr(&mdio->dev, child);
209+
if (addr < 0)
210+
continue;
211+
212+
if (addr == phydev->addr) {
213+
dev->of_node = child;
214+
return;
215+
}
216+
}
217+
}
218+
175219
/* Helper function for of_phy_find_device */
176220
static int of_phy_match(struct device *dev, void *phy_np)
177221
{

include/linux/of_mdio.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ struct phy_device *of_phy_attach(struct net_device *dev,
2525

2626
extern struct mii_bus *of_mdio_find_bus(struct device_node *mdio_np);
2727

28+
extern void of_mdiobus_link_phydev(struct mii_bus *mdio,
29+
struct phy_device *phydev);
30+
2831
#else /* CONFIG_OF */
2932
static inline int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np)
3033
{
@@ -60,6 +63,11 @@ static inline struct mii_bus *of_mdio_find_bus(struct device_node *mdio_np)
6063
{
6164
return NULL;
6265
}
66+
67+
static inline void of_mdiobus_link_phydev(struct mii_bus *mdio,
68+
struct phy_device *phydev)
69+
{
70+
}
6371
#endif /* CONFIG_OF */
6472

6573
#if defined(CONFIG_OF) && defined(CONFIG_FIXED_PHY)

0 commit comments

Comments
 (0)