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