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