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(np: 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 fwnode_property_read_u32(fwnode: child, propname: "reset-assert-us",
96 val: &phy->mdio.reset_assert_delay);
97 fwnode_property_read_u32(fwnode: child, propname: "reset-deassert-us",
98 val: &phy->mdio.reset_deassert_delay);
99
100 /* Associate the fwnode with the device structure so it
101 * can be looked up later
102 */
103 fwnode_handle_get(fwnode: child);
104 device_set_node(dev: &phy->mdio.dev, fwnode: child);
105
106 /* All data is now stored in the phy struct;
107 * register it
108 */
109 rc = phy_device_register(phy);
110 if (rc) {
111 device_set_node(dev: &phy->mdio.dev, NULL);
112 fwnode_handle_put(fwnode: child);
113 return rc;
114 }
115
116 dev_dbg(&mdio->dev, "registered phy fwnode %pfw at address %i\n",
117 child, addr);
118 return 0;
119}
120EXPORT_SYMBOL(fwnode_mdiobus_phy_device_register);
121
122int fwnode_mdiobus_register_phy(struct mii_bus *bus,
123 struct fwnode_handle *child, u32 addr)
124{
125 struct mii_timestamper *mii_ts = NULL;
126 struct pse_control *psec = NULL;
127 struct phy_device *phy;
128 bool is_c45;
129 u32 phy_id;
130 int rc;
131
132 mii_ts = fwnode_find_mii_timestamper(fwnode: child);
133 if (IS_ERR(ptr: mii_ts))
134 return PTR_ERR(ptr: mii_ts);
135
136 is_c45 = fwnode_device_is_compatible(fwnode: child, compat: "ethernet-phy-ieee802.3-c45");
137 if (is_c45 || fwnode_get_phy_id(fwnode: child, phy_id: &phy_id))
138 phy = get_phy_device(bus, addr, is_c45);
139 else
140 phy = phy_device_create(bus, addr, phy_id, is_c45: 0, NULL);
141 if (IS_ERR(ptr: phy)) {
142 rc = PTR_ERR(ptr: phy);
143 goto clean_mii_ts;
144 }
145
146 if (is_acpi_node(fwnode: child)) {
147 phy->irq = bus->irq[addr];
148
149 /* Associate the fwnode with the device structure so it
150 * can be looked up later.
151 */
152 phy->mdio.dev.fwnode = fwnode_handle_get(fwnode: child);
153
154 /* All data is now stored in the phy struct, so register it */
155 rc = phy_device_register(phy);
156 if (rc) {
157 phy->mdio.dev.fwnode = NULL;
158 fwnode_handle_put(fwnode: child);
159 goto clean_phy;
160 }
161 } else if (is_of_node(fwnode: child)) {
162 rc = fwnode_mdiobus_phy_device_register(bus, phy, child, addr);
163 if (rc)
164 goto clean_phy;
165 }
166
167 psec = fwnode_find_pse_control(fwnode: child, phydev: phy);
168 if (IS_ERR(ptr: psec)) {
169 rc = PTR_ERR(ptr: psec);
170 goto unregister_phy;
171 }
172
173 phy->psec = psec;
174
175 /* phy->mii_ts may already be defined by the PHY driver. A
176 * mii_timestamper probed via the device tree will still have
177 * precedence.
178 */
179 if (mii_ts)
180 phy->mii_ts = mii_ts;
181
182 return 0;
183
184unregister_phy:
185 if (is_acpi_node(fwnode: child) || is_of_node(fwnode: child))
186 phy_device_remove(phydev: phy);
187clean_phy:
188 phy_device_free(phydev: phy);
189clean_mii_ts:
190 unregister_mii_timestamper(mii_ts);
191
192 return rc;
193}
194EXPORT_SYMBOL(fwnode_mdiobus_register_phy);
195