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