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