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/dev_printk.h>
11#include <linux/fwnode_mdio.h>
12#include <linux/of.h>
13#include <linux/phy.h>
14#include <linux/pse-pd/pse.h>
15
16MODULE_AUTHOR("Calvin Johnson <calvin.johnson@oss.nxp.com>");
17MODULE_LICENSE("GPL");
18MODULE_DESCRIPTION("FWNODE MDIO bus (Ethernet PHY) accessors");
19
20static struct pse_control *
21fwnode_find_pse_control(struct fwnode_handle *fwnode,
22 struct phy_device *phydev)
23{
24 struct pse_control *psec;
25 struct device_node *np;
26
27 if (!IS_ENABLED(CONFIG_PSE_CONTROLLER))
28 return NULL;
29
30 np = to_of_node(fwnode);
31 if (!np)
32 return NULL;
33
34 psec = of_pse_control_get(node: np, phydev);
35 if (PTR_ERR(ptr: psec) == -ENOENT)
36 return NULL;
37
38 return psec;
39}
40
41static struct mii_timestamper *
42fwnode_find_mii_timestamper(struct fwnode_handle *fwnode)
43{
44 struct mii_timestamper *mii_ts;
45 struct of_phandle_args arg;
46 int err;
47
48 if (is_acpi_node(fwnode))
49 return NULL;
50
51 err = of_parse_phandle_with_fixed_args(to_of_node(fwnode),
52 list_name: "timestamper", cell_count: 1, index: 0, out_args: &arg);
53 if (err == -ENOENT)
54 return NULL;
55 else if (err)
56 return ERR_PTR(error: err);
57
58 if (arg.args_count != 1) {
59 mii_ts = ERR_PTR(error: -EINVAL);
60 goto put_node;
61 }
62
63 mii_ts = register_mii_timestamper(node: arg.np, port: arg.args[0]);
64
65put_node:
66 of_node_put(node: arg.np);
67 return mii_ts;
68}
69
70int fwnode_mdiobus_phy_device_register(struct mii_bus *mdio,
71 struct phy_device *phy,
72 struct fwnode_handle *child, u32 addr)
73{
74 int rc;
75
76 rc = fwnode_irq_get(fwnode: child, index: 0);
77 /* Don't wait forever if the IRQ provider doesn't become available,
78 * just fall back to poll mode
79 */
80 if (rc == -EPROBE_DEFER)
81 rc = driver_deferred_probe_check_state(dev: &phy->mdio.dev);
82 if (rc == -EPROBE_DEFER)
83 return rc;
84
85 if (rc > 0) {
86 phy->irq = rc;
87 mdio->irq[addr] = rc;
88 } else {
89 phy->irq = mdio->irq[addr];
90 }
91
92 if (fwnode_property_read_bool(fwnode: child, propname: "broken-turn-around"))
93 mdio->phy_ignore_ta_mask |= 1 << addr;
94
95 /* Associate the fwnode with the device structure so it
96 * can be looked up later
97 */
98 fwnode_handle_get(fwnode: child);
99 device_set_node(dev: &phy->mdio.dev, fwnode: child);
100
101 /* All data is now stored in the phy struct;
102 * register it
103 */
104 rc = phy_device_register(phy);
105 if (rc) {
106 device_set_node(dev: &phy->mdio.dev, NULL);
107 fwnode_handle_put(fwnode: child);
108 return rc;
109 }
110
111 dev_dbg(&mdio->dev, "registered phy fwnode %pfw at address %i\n",
112 child, addr);
113 return 0;
114}
115EXPORT_SYMBOL(fwnode_mdiobus_phy_device_register);
116
117int fwnode_mdiobus_register_phy(struct mii_bus *bus,
118 struct fwnode_handle *child, u32 addr)
119{
120 struct mii_timestamper *mii_ts = NULL;
121 struct pse_control *psec = NULL;
122 struct phy_device *phy;
123 bool is_c45;
124 u32 phy_id;
125 int rc;
126
127 mii_ts = fwnode_find_mii_timestamper(fwnode: child);
128 if (IS_ERR(ptr: mii_ts))
129 return PTR_ERR(ptr: mii_ts);
130
131 is_c45 = fwnode_device_is_compatible(fwnode: child, compat: "ethernet-phy-ieee802.3-c45");
132 if (is_c45 || fwnode_get_phy_id(fwnode: child, phy_id: &phy_id))
133 phy = get_phy_device(bus, addr, is_c45);
134 else
135 phy = phy_device_create(bus, addr, phy_id, is_c45: 0, NULL);
136 if (IS_ERR(ptr: phy)) {
137 rc = PTR_ERR(ptr: phy);
138 goto clean_mii_ts;
139 }
140
141 if (is_acpi_node(fwnode: child)) {
142 phy->irq = bus->irq[addr];
143
144 /* Associate the fwnode with the device structure so it
145 * can be looked up later.
146 */
147 phy->mdio.dev.fwnode = fwnode_handle_get(fwnode: child);
148
149 /* All data is now stored in the phy struct, so register it */
150 rc = phy_device_register(phy);
151 if (rc) {
152 phy->mdio.dev.fwnode = NULL;
153 fwnode_handle_put(fwnode: child);
154 goto clean_phy;
155 }
156 } else if (is_of_node(fwnode: child)) {
157 rc = fwnode_mdiobus_phy_device_register(bus, phy, child, addr);
158 if (rc)
159 goto clean_phy;
160 }
161
162 psec = fwnode_find_pse_control(fwnode: child, phydev: phy);
163 if (IS_ERR(ptr: psec)) {
164 rc = PTR_ERR(ptr: psec);
165 goto unregister_phy;
166 }
167
168 phy->psec = psec;
169
170 /* phy->mii_ts may already be defined by the PHY driver. A
171 * mii_timestamper probed via the device tree will still have
172 * precedence.
173 */
174 if (mii_ts)
175 phy->mii_ts = mii_ts;
176
177 return 0;
178
179unregister_phy:
180 if (is_acpi_node(fwnode: child) || is_of_node(fwnode: child))
181 phy_device_remove(phydev: phy);
182clean_phy:
183 phy_device_free(phydev: phy);
184clean_mii_ts:
185 unregister_mii_timestamper(mii_ts);
186
187 return rc;
188}
189EXPORT_SYMBOL(fwnode_mdiobus_register_phy);
190

source code of linux/drivers/net/mdio/fwnode_mdio.c