Skip to content

Commit bc1bee3

Browse files
calvinjolinuxdavem330
authored andcommitted
net: mdiobus: Introduce fwnode_mdiobus_register_phy()
Introduce fwnode_mdiobus_register_phy() to register PHYs on the mdiobus. From the compatible string, identify whether the PHY is c45 and based on this create a PHY device instance which is registered on the mdiobus. Along with fwnode_mdiobus_register_phy() also introduce fwnode_find_mii_timestamper() and fwnode_mdiobus_phy_device_register() since they are needed. While at it, also use the newly introduced fwnode operation in of_mdiobus_phy_device_register(). Signed-off-by: Calvin Johnson <[email protected]> Signed-off-by: Ioana Ciornei <[email protected]> Acked-by: Grant Likely <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent b9926da commit bc1bee3

File tree

6 files changed

+194
-40
lines changed

6 files changed

+194
-40
lines changed

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6811,6 +6811,7 @@ F: Documentation/devicetree/bindings/net/mdio*
68116811
F: Documentation/devicetree/bindings/net/qca,ar803x.yaml
68126812
F: Documentation/networking/phy.rst
68136813
F: drivers/net/mdio/
6814+
F: drivers/net/mdio/fwnode_mdio.c
68146815
F: drivers/net/mdio/of_mdio.c
68156816
F: drivers/net/pcs/
68166817
F: drivers/net/phy/

drivers/net/mdio/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ config MDIO_BUS
1919
reflects whether the mdio_bus/mdio_device code is built as a
2020
loadable module or built-in.
2121

22+
config FWNODE_MDIO
23+
def_tristate PHYLIB
24+
depends on (ACPI || OF) || COMPILE_TEST
25+
select FIXED_PHY
26+
help
27+
FWNODE MDIO bus (Ethernet PHY) accessors
28+
2229
config OF_MDIO
2330
def_tristate PHYLIB
2431
depends on OF

drivers/net/mdio/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# SPDX-License-Identifier: GPL-2.0
22
# Makefile for Linux MDIO bus drivers
33

4-
obj-$(CONFIG_OF_MDIO) += of_mdio.o
4+
obj-$(CONFIG_FWNODE_MDIO) += fwnode_mdio.o
5+
obj-$(CONFIG_OF_MDIO) += of_mdio.o
56

67
obj-$(CONFIG_MDIO_ASPEED) += mdio-aspeed.o
78
obj-$(CONFIG_MDIO_BCM_IPROC) += mdio-bcm-iproc.o

drivers/net/mdio/fwnode_mdio.c

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* fwnode helpers for the MDIO (Ethernet PHY) API
4+
*
5+
* This file provides helper functions for extracting PHY device information
6+
* out of the fwnode and using it to populate an mii_bus.
7+
*/
8+
9+
#include <linux/acpi.h>
10+
#include <linux/fwnode_mdio.h>
11+
#include <linux/of.h>
12+
#include <linux/phy.h>
13+
14+
MODULE_AUTHOR("Calvin Johnson <[email protected]>");
15+
MODULE_LICENSE("GPL");
16+
17+
static struct mii_timestamper *
18+
fwnode_find_mii_timestamper(struct fwnode_handle *fwnode)
19+
{
20+
struct of_phandle_args arg;
21+
int err;
22+
23+
if (is_acpi_node(fwnode))
24+
return NULL;
25+
26+
err = of_parse_phandle_with_fixed_args(to_of_node(fwnode),
27+
"timestamper", 1, 0, &arg);
28+
if (err == -ENOENT)
29+
return NULL;
30+
else if (err)
31+
return ERR_PTR(err);
32+
33+
if (arg.args_count != 1)
34+
return ERR_PTR(-EINVAL);
35+
36+
return register_mii_timestamper(arg.np, arg.args[0]);
37+
}
38+
39+
int fwnode_mdiobus_phy_device_register(struct mii_bus *mdio,
40+
struct phy_device *phy,
41+
struct fwnode_handle *child, u32 addr)
42+
{
43+
int rc;
44+
45+
rc = fwnode_irq_get(child, 0);
46+
if (rc == -EPROBE_DEFER)
47+
return rc;
48+
49+
if (rc > 0) {
50+
phy->irq = rc;
51+
mdio->irq[addr] = rc;
52+
} else {
53+
phy->irq = mdio->irq[addr];
54+
}
55+
56+
if (fwnode_property_read_bool(child, "broken-turn-around"))
57+
mdio->phy_ignore_ta_mask |= 1 << addr;
58+
59+
fwnode_property_read_u32(child, "reset-assert-us",
60+
&phy->mdio.reset_assert_delay);
61+
fwnode_property_read_u32(child, "reset-deassert-us",
62+
&phy->mdio.reset_deassert_delay);
63+
64+
/* Associate the fwnode with the device structure so it
65+
* can be looked up later
66+
*/
67+
fwnode_handle_get(child);
68+
phy->mdio.dev.fwnode = child;
69+
70+
/* All data is now stored in the phy struct;
71+
* register it
72+
*/
73+
rc = phy_device_register(phy);
74+
if (rc) {
75+
fwnode_handle_put(child);
76+
return rc;
77+
}
78+
79+
dev_dbg(&mdio->dev, "registered phy %p fwnode at address %i\n",
80+
child, addr);
81+
return 0;
82+
}
83+
EXPORT_SYMBOL(fwnode_mdiobus_phy_device_register);
84+
85+
int fwnode_mdiobus_register_phy(struct mii_bus *bus,
86+
struct fwnode_handle *child, u32 addr)
87+
{
88+
struct mii_timestamper *mii_ts = NULL;
89+
struct phy_device *phy;
90+
bool is_c45 = false;
91+
u32 phy_id;
92+
int rc;
93+
94+
mii_ts = fwnode_find_mii_timestamper(child);
95+
if (IS_ERR(mii_ts))
96+
return PTR_ERR(mii_ts);
97+
98+
rc = fwnode_property_match_string(child, "compatible",
99+
"ethernet-phy-ieee802.3-c45");
100+
if (rc >= 0)
101+
is_c45 = true;
102+
103+
if (is_c45 || fwnode_get_phy_id(child, &phy_id))
104+
phy = get_phy_device(bus, addr, is_c45);
105+
else
106+
phy = phy_device_create(bus, addr, phy_id, 0, NULL);
107+
if (IS_ERR(phy)) {
108+
unregister_mii_timestamper(mii_ts);
109+
return PTR_ERR(phy);
110+
}
111+
112+
if (is_acpi_node(child)) {
113+
phy->irq = bus->irq[addr];
114+
115+
/* Associate the fwnode with the device structure so it
116+
* can be looked up later.
117+
*/
118+
phy->mdio.dev.fwnode = child;
119+
120+
/* All data is now stored in the phy struct, so register it */
121+
rc = phy_device_register(phy);
122+
if (rc) {
123+
phy_device_free(phy);
124+
fwnode_handle_put(phy->mdio.dev.fwnode);
125+
return rc;
126+
}
127+
} else if (is_of_node(child)) {
128+
rc = fwnode_mdiobus_phy_device_register(bus, phy, child, addr);
129+
if (rc) {
130+
unregister_mii_timestamper(mii_ts);
131+
phy_device_free(phy);
132+
return rc;
133+
}
134+
}
135+
136+
/* phy->mii_ts may already be defined by the PHY driver. A
137+
* mii_timestamper probed via the device tree will still have
138+
* precedence.
139+
*/
140+
if (mii_ts)
141+
phy->mii_ts = mii_ts;
142+
return 0;
143+
}
144+
EXPORT_SYMBOL(fwnode_mdiobus_register_phy);

drivers/net/mdio/of_mdio.c

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <linux/device.h>
1212
#include <linux/err.h>
13+
#include <linux/fwnode_mdio.h>
1314
#include <linux/kernel.h>
1415
#include <linux/module.h>
1516
#include <linux/netdevice.h>
@@ -51,46 +52,11 @@ static struct mii_timestamper *of_find_mii_timestamper(struct device_node *node)
5152
}
5253

5354
int of_mdiobus_phy_device_register(struct mii_bus *mdio, struct phy_device *phy,
54-
struct device_node *child, u32 addr)
55+
struct device_node *child, u32 addr)
5556
{
56-
int rc;
57-
58-
rc = of_irq_get(child, 0);
59-
if (rc == -EPROBE_DEFER)
60-
return rc;
61-
62-
if (rc > 0) {
63-
phy->irq = rc;
64-
mdio->irq[addr] = rc;
65-
} else {
66-
phy->irq = mdio->irq[addr];
67-
}
68-
69-
if (of_property_read_bool(child, "broken-turn-around"))
70-
mdio->phy_ignore_ta_mask |= 1 << addr;
71-
72-
of_property_read_u32(child, "reset-assert-us",
73-
&phy->mdio.reset_assert_delay);
74-
of_property_read_u32(child, "reset-deassert-us",
75-
&phy->mdio.reset_deassert_delay);
76-
77-
/* Associate the OF node with the device structure so it
78-
* can be looked up later */
79-
of_node_get(child);
80-
phy->mdio.dev.of_node = child;
81-
phy->mdio.dev.fwnode = of_fwnode_handle(child);
82-
83-
/* All data is now stored in the phy struct;
84-
* register it */
85-
rc = phy_device_register(phy);
86-
if (rc) {
87-
of_node_put(child);
88-
return rc;
89-
}
90-
91-
dev_dbg(&mdio->dev, "registered phy %pOFn at address %i\n",
92-
child, addr);
93-
return 0;
57+
return fwnode_mdiobus_phy_device_register(mdio, phy,
58+
of_fwnode_handle(child),
59+
addr);
9460
}
9561
EXPORT_SYMBOL(of_mdiobus_phy_device_register);
9662

include/linux/fwnode_mdio.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
/*
3+
* FWNODE helper for the MDIO (Ethernet PHY) API
4+
*/
5+
6+
#ifndef __LINUX_FWNODE_MDIO_H
7+
#define __LINUX_FWNODE_MDIO_H
8+
9+
#include <linux/phy.h>
10+
11+
#if IS_ENABLED(CONFIG_FWNODE_MDIO)
12+
int fwnode_mdiobus_phy_device_register(struct mii_bus *mdio,
13+
struct phy_device *phy,
14+
struct fwnode_handle *child, u32 addr);
15+
16+
int fwnode_mdiobus_register_phy(struct mii_bus *bus,
17+
struct fwnode_handle *child, u32 addr);
18+
19+
#else /* CONFIG_FWNODE_MDIO */
20+
int fwnode_mdiobus_phy_device_register(struct mii_bus *mdio,
21+
struct phy_device *phy,
22+
struct fwnode_handle *child, u32 addr)
23+
{
24+
return -EINVAL;
25+
}
26+
27+
static inline int fwnode_mdiobus_register_phy(struct mii_bus *bus,
28+
struct fwnode_handle *child,
29+
u32 addr)
30+
{
31+
return -EINVAL;
32+
}
33+
#endif
34+
35+
#endif /* __LINUX_FWNODE_MDIO_H */

0 commit comments

Comments
 (0)