1bc1bee3bSCalvin Johnson // SPDX-License-Identifier: GPL-2.0-only
2bc1bee3bSCalvin Johnson /*
3bc1bee3bSCalvin Johnson * fwnode helpers for the MDIO (Ethernet PHY) API
4bc1bee3bSCalvin Johnson *
5bc1bee3bSCalvin Johnson * This file provides helper functions for extracting PHY device information
6bc1bee3bSCalvin Johnson * out of the fwnode and using it to populate an mii_bus.
7bc1bee3bSCalvin Johnson */
8bc1bee3bSCalvin Johnson
9bc1bee3bSCalvin Johnson #include <linux/acpi.h>
10bc1bee3bSCalvin Johnson #include <linux/fwnode_mdio.h>
11bc1bee3bSCalvin Johnson #include <linux/of.h>
12bc1bee3bSCalvin Johnson #include <linux/phy.h>
135e82147dSOleksij Rempel #include <linux/pse-pd/pse.h>
14bc1bee3bSCalvin Johnson
15bc1bee3bSCalvin Johnson MODULE_AUTHOR("Calvin Johnson <calvin.johnson@oss.nxp.com>");
16bc1bee3bSCalvin Johnson MODULE_LICENSE("GPL");
17bc1bee3bSCalvin Johnson
185e82147dSOleksij Rempel static struct pse_control *
fwnode_find_pse_control(struct fwnode_handle * fwnode)195e82147dSOleksij Rempel fwnode_find_pse_control(struct fwnode_handle *fwnode)
205e82147dSOleksij Rempel {
215e82147dSOleksij Rempel struct pse_control *psec;
225e82147dSOleksij Rempel struct device_node *np;
235e82147dSOleksij Rempel
245e82147dSOleksij Rempel if (!IS_ENABLED(CONFIG_PSE_CONTROLLER))
255e82147dSOleksij Rempel return NULL;
265e82147dSOleksij Rempel
275e82147dSOleksij Rempel np = to_of_node(fwnode);
285e82147dSOleksij Rempel if (!np)
295e82147dSOleksij Rempel return NULL;
305e82147dSOleksij Rempel
315e82147dSOleksij Rempel psec = of_pse_control_get(np);
325e82147dSOleksij Rempel if (PTR_ERR(psec) == -ENOENT)
335e82147dSOleksij Rempel return NULL;
345e82147dSOleksij Rempel
355e82147dSOleksij Rempel return psec;
365e82147dSOleksij Rempel }
375e82147dSOleksij Rempel
38bc1bee3bSCalvin Johnson static struct mii_timestamper *
fwnode_find_mii_timestamper(struct fwnode_handle * fwnode)39bc1bee3bSCalvin Johnson fwnode_find_mii_timestamper(struct fwnode_handle *fwnode)
40bc1bee3bSCalvin Johnson {
41*b15b92daSJoe Hattori struct mii_timestamper *mii_ts;
42bc1bee3bSCalvin Johnson struct of_phandle_args arg;
43bc1bee3bSCalvin Johnson int err;
44bc1bee3bSCalvin Johnson
45bc1bee3bSCalvin Johnson if (is_acpi_node(fwnode))
46bc1bee3bSCalvin Johnson return NULL;
47bc1bee3bSCalvin Johnson
48bc1bee3bSCalvin Johnson err = of_parse_phandle_with_fixed_args(to_of_node(fwnode),
49bc1bee3bSCalvin Johnson "timestamper", 1, 0, &arg);
50bc1bee3bSCalvin Johnson if (err == -ENOENT)
51bc1bee3bSCalvin Johnson return NULL;
52bc1bee3bSCalvin Johnson else if (err)
53bc1bee3bSCalvin Johnson return ERR_PTR(err);
54bc1bee3bSCalvin Johnson
55*b15b92daSJoe Hattori if (arg.args_count != 1) {
56*b15b92daSJoe Hattori mii_ts = ERR_PTR(-EINVAL);
57*b15b92daSJoe Hattori goto put_node;
58*b15b92daSJoe Hattori }
59bc1bee3bSCalvin Johnson
60*b15b92daSJoe Hattori mii_ts = register_mii_timestamper(arg.np, arg.args[0]);
61*b15b92daSJoe Hattori
62*b15b92daSJoe Hattori put_node:
63*b15b92daSJoe Hattori of_node_put(arg.np);
64*b15b92daSJoe Hattori return mii_ts;
65bc1bee3bSCalvin Johnson }
66bc1bee3bSCalvin Johnson
fwnode_mdiobus_phy_device_register(struct mii_bus * mdio,struct phy_device * phy,struct fwnode_handle * child,u32 addr)67bc1bee3bSCalvin Johnson int fwnode_mdiobus_phy_device_register(struct mii_bus *mdio,
68bc1bee3bSCalvin Johnson struct phy_device *phy,
69bc1bee3bSCalvin Johnson struct fwnode_handle *child, u32 addr)
70bc1bee3bSCalvin Johnson {
71bc1bee3bSCalvin Johnson int rc;
72bc1bee3bSCalvin Johnson
73bc1bee3bSCalvin Johnson rc = fwnode_irq_get(child, 0);
7474befa44SVladimir Oltean /* Don't wait forever if the IRQ provider doesn't become available,
7574befa44SVladimir Oltean * just fall back to poll mode
7674befa44SVladimir Oltean */
7774befa44SVladimir Oltean if (rc == -EPROBE_DEFER)
78cffaf972SSaravana Kannan rc = driver_deferred_probe_check_state(&phy->mdio.dev);
79cffaf972SSaravana Kannan if (rc == -EPROBE_DEFER)
80cffaf972SSaravana Kannan return rc;
81bc1bee3bSCalvin Johnson
82bc1bee3bSCalvin Johnson if (rc > 0) {
83bc1bee3bSCalvin Johnson phy->irq = rc;
84bc1bee3bSCalvin Johnson mdio->irq[addr] = rc;
85bc1bee3bSCalvin Johnson } else {
86bc1bee3bSCalvin Johnson phy->irq = mdio->irq[addr];
87bc1bee3bSCalvin Johnson }
88bc1bee3bSCalvin Johnson
89bc1bee3bSCalvin Johnson if (fwnode_property_read_bool(child, "broken-turn-around"))
90bc1bee3bSCalvin Johnson mdio->phy_ignore_ta_mask |= 1 << addr;
91bc1bee3bSCalvin Johnson
92bc1bee3bSCalvin Johnson fwnode_property_read_u32(child, "reset-assert-us",
93bc1bee3bSCalvin Johnson &phy->mdio.reset_assert_delay);
94bc1bee3bSCalvin Johnson fwnode_property_read_u32(child, "reset-deassert-us",
95bc1bee3bSCalvin Johnson &phy->mdio.reset_deassert_delay);
96bc1bee3bSCalvin Johnson
97bc1bee3bSCalvin Johnson /* Associate the fwnode with the device structure so it
98bc1bee3bSCalvin Johnson * can be looked up later
99bc1bee3bSCalvin Johnson */
100bc1bee3bSCalvin Johnson fwnode_handle_get(child);
1017e33d84dSIoana Ciornei device_set_node(&phy->mdio.dev, child);
102bc1bee3bSCalvin Johnson
103bc1bee3bSCalvin Johnson /* All data is now stored in the phy struct;
104bc1bee3bSCalvin Johnson * register it
105bc1bee3bSCalvin Johnson */
106bc1bee3bSCalvin Johnson rc = phy_device_register(phy);
107bc1bee3bSCalvin Johnson if (rc) {
108165df241SYang Yingliang device_set_node(&phy->mdio.dev, NULL);
109bc1bee3bSCalvin Johnson fwnode_handle_put(child);
110bc1bee3bSCalvin Johnson return rc;
111bc1bee3bSCalvin Johnson }
112bc1bee3bSCalvin Johnson
113bc1bee3bSCalvin Johnson dev_dbg(&mdio->dev, "registered phy %p fwnode at address %i\n",
114bc1bee3bSCalvin Johnson child, addr);
115bc1bee3bSCalvin Johnson return 0;
116bc1bee3bSCalvin Johnson }
117bc1bee3bSCalvin Johnson EXPORT_SYMBOL(fwnode_mdiobus_phy_device_register);
118bc1bee3bSCalvin Johnson
fwnode_mdiobus_register_phy(struct mii_bus * bus,struct fwnode_handle * child,u32 addr)119bc1bee3bSCalvin Johnson int fwnode_mdiobus_register_phy(struct mii_bus *bus,
120bc1bee3bSCalvin Johnson struct fwnode_handle *child, u32 addr)
121bc1bee3bSCalvin Johnson {
122bc1bee3bSCalvin Johnson struct mii_timestamper *mii_ts = NULL;
1235e82147dSOleksij Rempel struct pse_control *psec = NULL;
124bc1bee3bSCalvin Johnson struct phy_device *phy;
125d408ec0bSAndy Shevchenko bool is_c45;
126bc1bee3bSCalvin Johnson u32 phy_id;
127bc1bee3bSCalvin Johnson int rc;
128bc1bee3bSCalvin Johnson
1295e82147dSOleksij Rempel psec = fwnode_find_pse_control(child);
1305e82147dSOleksij Rempel if (IS_ERR(psec))
1315e82147dSOleksij Rempel return PTR_ERR(psec);
1325e82147dSOleksij Rempel
133bc1bee3bSCalvin Johnson mii_ts = fwnode_find_mii_timestamper(child);
1345e82147dSOleksij Rempel if (IS_ERR(mii_ts)) {
1355e82147dSOleksij Rempel rc = PTR_ERR(mii_ts);
1365e82147dSOleksij Rempel goto clean_pse;
1375e82147dSOleksij Rempel }
138bc1bee3bSCalvin Johnson
139d408ec0bSAndy Shevchenko is_c45 = fwnode_device_is_compatible(child, "ethernet-phy-ieee802.3-c45");
140bc1bee3bSCalvin Johnson if (is_c45 || fwnode_get_phy_id(child, &phy_id))
141bc1bee3bSCalvin Johnson phy = get_phy_device(bus, addr, is_c45);
142bc1bee3bSCalvin Johnson else
143bc1bee3bSCalvin Johnson phy = phy_device_create(bus, addr, phy_id, 0, NULL);
144bc1bee3bSCalvin Johnson if (IS_ERR(phy)) {
145cfaa202aSOleksij Rempel rc = PTR_ERR(phy);
146cfaa202aSOleksij Rempel goto clean_mii_ts;
147bc1bee3bSCalvin Johnson }
148bc1bee3bSCalvin Johnson
149bc1bee3bSCalvin Johnson if (is_acpi_node(child)) {
150bc1bee3bSCalvin Johnson phy->irq = bus->irq[addr];
151bc1bee3bSCalvin Johnson
152bc1bee3bSCalvin Johnson /* Associate the fwnode with the device structure so it
153bc1bee3bSCalvin Johnson * can be looked up later.
154bc1bee3bSCalvin Johnson */
155cdde1560SYang Yingliang phy->mdio.dev.fwnode = fwnode_handle_get(child);
156bc1bee3bSCalvin Johnson
157bc1bee3bSCalvin Johnson /* All data is now stored in the phy struct, so register it */
158bc1bee3bSCalvin Johnson rc = phy_device_register(phy);
159bc1bee3bSCalvin Johnson if (rc) {
160165df241SYang Yingliang phy->mdio.dev.fwnode = NULL;
161165df241SYang Yingliang fwnode_handle_put(child);
162cfaa202aSOleksij Rempel goto clean_phy;
163bc1bee3bSCalvin Johnson }
164bc1bee3bSCalvin Johnson } else if (is_of_node(child)) {
165bc1bee3bSCalvin Johnson rc = fwnode_mdiobus_phy_device_register(bus, phy, child, addr);
166cfaa202aSOleksij Rempel if (rc)
167cfaa202aSOleksij Rempel goto clean_phy;
168bc1bee3bSCalvin Johnson }
169bc1bee3bSCalvin Johnson
1705e82147dSOleksij Rempel phy->psec = psec;
1715e82147dSOleksij Rempel
172bc1bee3bSCalvin Johnson /* phy->mii_ts may already be defined by the PHY driver. A
173bc1bee3bSCalvin Johnson * mii_timestamper probed via the device tree will still have
174bc1bee3bSCalvin Johnson * precedence.
175bc1bee3bSCalvin Johnson */
176bc1bee3bSCalvin Johnson if (mii_ts)
177bc1bee3bSCalvin Johnson phy->mii_ts = mii_ts;
1785e82147dSOleksij Rempel
179bc1bee3bSCalvin Johnson return 0;
180cfaa202aSOleksij Rempel
181cfaa202aSOleksij Rempel clean_phy:
182cfaa202aSOleksij Rempel phy_device_free(phy);
183cfaa202aSOleksij Rempel clean_mii_ts:
184cfaa202aSOleksij Rempel unregister_mii_timestamper(mii_ts);
1855e82147dSOleksij Rempel clean_pse:
1865e82147dSOleksij Rempel pse_control_put(psec);
187cfaa202aSOleksij Rempel
188cfaa202aSOleksij Rempel return rc;
189bc1bee3bSCalvin Johnson }
190bc1bee3bSCalvin Johnson EXPORT_SYMBOL(fwnode_mdiobus_register_phy);
191