xref: /linux/drivers/net/mdio/fwnode_mdio.c (revision 25489a4f556414445d342951615178368ee45cde)
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 
16 MODULE_AUTHOR("Calvin Johnson <calvin.johnson@oss.nxp.com>");
17 MODULE_LICENSE("GPL");
18 MODULE_DESCRIPTION("FWNODE MDIO bus (Ethernet PHY) accessors");
19 
20 static struct pse_control *
21 fwnode_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(np, phydev);
35 	if (PTR_ERR(psec) == -ENOENT)
36 		return NULL;
37 
38 	return psec;
39 }
40 
41 static struct mii_timestamper *
42 fwnode_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 					       "timestamper", 1, 0, &arg);
53 	if (err == -ENOENT)
54 		return NULL;
55 	else if (err)
56 		return ERR_PTR(err);
57 
58 	if (arg.args_count != 1) {
59 		mii_ts = ERR_PTR(-EINVAL);
60 		goto put_node;
61 	}
62 
63 	mii_ts = register_mii_timestamper(arg.np, arg.args[0]);
64 
65 put_node:
66 	of_node_put(arg.np);
67 	return mii_ts;
68 }
69 
70 int 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(child, 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(&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(child, "broken-turn-around"))
93 		mdio->phy_ignore_ta_mask |= 1 << addr;
94 
95 	fwnode_property_read_u32(child, "reset-assert-us",
96 				 &phy->mdio.reset_assert_delay);
97 	fwnode_property_read_u32(child, "reset-deassert-us",
98 				 &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(child);
104 	device_set_node(&phy->mdio.dev, 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(&phy->mdio.dev, NULL);
112 		fwnode_handle_put(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 }
120 EXPORT_SYMBOL(fwnode_mdiobus_phy_device_register);
121 
122 int 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(child);
133 	if (IS_ERR(mii_ts))
134 		return PTR_ERR(mii_ts);
135 
136 	is_c45 = fwnode_device_is_compatible(child, "ethernet-phy-ieee802.3-c45");
137 	if (is_c45 || fwnode_get_phy_id(child, &phy_id))
138 		phy = get_phy_device(bus, addr, is_c45);
139 	else
140 		phy = phy_device_create(bus, addr, phy_id, 0, NULL);
141 	if (IS_ERR(phy)) {
142 		rc = PTR_ERR(phy);
143 		goto clean_mii_ts;
144 	}
145 
146 	if (is_acpi_node(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(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(child);
159 			goto clean_phy;
160 		}
161 	} else if (is_of_node(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(child, phy);
168 	if (IS_ERR(psec)) {
169 		rc = PTR_ERR(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 
184 unregister_phy:
185 	if (is_acpi_node(child) || is_of_node(child))
186 		phy_device_remove(phy);
187 clean_phy:
188 	phy_device_free(phy);
189 clean_mii_ts:
190 	unregister_mii_timestamper(mii_ts);
191 
192 	return rc;
193 }
194 EXPORT_SYMBOL(fwnode_mdiobus_register_phy);
195