1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * OF helpers for the MDIO (Ethernet PHY) API
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (c) 2009 Secret Lab Technologies, Ltd.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * This file provides helper functions for extracting PHY device information
8*4882a593Smuzhiyun * out of the OpenFirmware device tree and using it to populate an mii_bus.
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <linux/device.h>
12*4882a593Smuzhiyun #include <linux/err.h>
13*4882a593Smuzhiyun #include <linux/kernel.h>
14*4882a593Smuzhiyun #include <linux/module.h>
15*4882a593Smuzhiyun #include <linux/netdevice.h>
16*4882a593Smuzhiyun #include <linux/of.h>
17*4882a593Smuzhiyun #include <linux/of_irq.h>
18*4882a593Smuzhiyun #include <linux/of_mdio.h>
19*4882a593Smuzhiyun #include <linux/of_net.h>
20*4882a593Smuzhiyun #include <linux/phy.h>
21*4882a593Smuzhiyun #include <linux/phy_fixed.h>
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun #define DEFAULT_GPIO_RESET_DELAY 10 /* in microseconds */
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
26*4882a593Smuzhiyun MODULE_LICENSE("GPL");
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun /* Extract the clause 22 phy ID from the compatible string of the form
29*4882a593Smuzhiyun * ethernet-phy-idAAAA.BBBB */
of_get_phy_id(struct device_node * device,u32 * phy_id)30*4882a593Smuzhiyun static int of_get_phy_id(struct device_node *device, u32 *phy_id)
31*4882a593Smuzhiyun {
32*4882a593Smuzhiyun struct property *prop;
33*4882a593Smuzhiyun const char *cp;
34*4882a593Smuzhiyun unsigned int upper, lower;
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun of_property_for_each_string(device, "compatible", prop, cp) {
37*4882a593Smuzhiyun if (sscanf(cp, "ethernet-phy-id%4x.%4x", &upper, &lower) == 2) {
38*4882a593Smuzhiyun *phy_id = ((upper & 0xFFFF) << 16) | (lower & 0xFFFF);
39*4882a593Smuzhiyun return 0;
40*4882a593Smuzhiyun }
41*4882a593Smuzhiyun }
42*4882a593Smuzhiyun return -EINVAL;
43*4882a593Smuzhiyun }
44*4882a593Smuzhiyun
of_find_mii_timestamper(struct device_node * node)45*4882a593Smuzhiyun static struct mii_timestamper *of_find_mii_timestamper(struct device_node *node)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun struct of_phandle_args arg;
48*4882a593Smuzhiyun int err;
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun err = of_parse_phandle_with_fixed_args(node, "timestamper", 1, 0, &arg);
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun if (err == -ENOENT)
53*4882a593Smuzhiyun return NULL;
54*4882a593Smuzhiyun else if (err)
55*4882a593Smuzhiyun return ERR_PTR(err);
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun if (arg.args_count != 1)
58*4882a593Smuzhiyun return ERR_PTR(-EINVAL);
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun return register_mii_timestamper(arg.np, arg.args[0]);
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun
of_mdiobus_phy_device_register(struct mii_bus * mdio,struct phy_device * phy,struct device_node * child,u32 addr)63*4882a593Smuzhiyun int of_mdiobus_phy_device_register(struct mii_bus *mdio, struct phy_device *phy,
64*4882a593Smuzhiyun struct device_node *child, u32 addr)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun int rc;
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun rc = of_irq_get(child, 0);
69*4882a593Smuzhiyun if (rc == -EPROBE_DEFER)
70*4882a593Smuzhiyun return rc;
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun if (rc > 0) {
73*4882a593Smuzhiyun phy->irq = rc;
74*4882a593Smuzhiyun mdio->irq[addr] = rc;
75*4882a593Smuzhiyun } else {
76*4882a593Smuzhiyun phy->irq = mdio->irq[addr];
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun if (of_property_read_bool(child, "broken-turn-around"))
80*4882a593Smuzhiyun mdio->phy_ignore_ta_mask |= 1 << addr;
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun of_property_read_u32(child, "reset-assert-us",
83*4882a593Smuzhiyun &phy->mdio.reset_assert_delay);
84*4882a593Smuzhiyun of_property_read_u32(child, "reset-deassert-us",
85*4882a593Smuzhiyun &phy->mdio.reset_deassert_delay);
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun /* Associate the OF node with the device structure so it
88*4882a593Smuzhiyun * can be looked up later */
89*4882a593Smuzhiyun of_node_get(child);
90*4882a593Smuzhiyun phy->mdio.dev.of_node = child;
91*4882a593Smuzhiyun phy->mdio.dev.fwnode = of_fwnode_handle(child);
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun /* All data is now stored in the phy struct;
94*4882a593Smuzhiyun * register it */
95*4882a593Smuzhiyun rc = phy_device_register(phy);
96*4882a593Smuzhiyun if (rc) {
97*4882a593Smuzhiyun of_node_put(child);
98*4882a593Smuzhiyun return rc;
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun dev_dbg(&mdio->dev, "registered phy %pOFn at address %i\n",
102*4882a593Smuzhiyun child, addr);
103*4882a593Smuzhiyun return 0;
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun EXPORT_SYMBOL(of_mdiobus_phy_device_register);
106*4882a593Smuzhiyun
of_mdiobus_register_phy(struct mii_bus * mdio,struct device_node * child,u32 addr)107*4882a593Smuzhiyun static int of_mdiobus_register_phy(struct mii_bus *mdio,
108*4882a593Smuzhiyun struct device_node *child, u32 addr)
109*4882a593Smuzhiyun {
110*4882a593Smuzhiyun struct mii_timestamper *mii_ts;
111*4882a593Smuzhiyun struct phy_device *phy;
112*4882a593Smuzhiyun bool is_c45;
113*4882a593Smuzhiyun int rc;
114*4882a593Smuzhiyun u32 phy_id;
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun mii_ts = of_find_mii_timestamper(child);
117*4882a593Smuzhiyun if (IS_ERR(mii_ts))
118*4882a593Smuzhiyun return PTR_ERR(mii_ts);
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun is_c45 = of_device_is_compatible(child,
121*4882a593Smuzhiyun "ethernet-phy-ieee802.3-c45");
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun if (!is_c45 && !of_get_phy_id(child, &phy_id))
124*4882a593Smuzhiyun phy = phy_device_create(mdio, addr, phy_id, 0, NULL);
125*4882a593Smuzhiyun else
126*4882a593Smuzhiyun phy = get_phy_device(mdio, addr, is_c45);
127*4882a593Smuzhiyun if (IS_ERR(phy)) {
128*4882a593Smuzhiyun if (mii_ts)
129*4882a593Smuzhiyun unregister_mii_timestamper(mii_ts);
130*4882a593Smuzhiyun return PTR_ERR(phy);
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun rc = of_mdiobus_phy_device_register(mdio, phy, child, addr);
134*4882a593Smuzhiyun if (rc) {
135*4882a593Smuzhiyun if (mii_ts)
136*4882a593Smuzhiyun unregister_mii_timestamper(mii_ts);
137*4882a593Smuzhiyun phy_device_free(phy);
138*4882a593Smuzhiyun return rc;
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun /* phy->mii_ts may already be defined by the PHY driver. A
142*4882a593Smuzhiyun * mii_timestamper probed via the device tree will still have
143*4882a593Smuzhiyun * precedence.
144*4882a593Smuzhiyun */
145*4882a593Smuzhiyun if (mii_ts)
146*4882a593Smuzhiyun phy->mii_ts = mii_ts;
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun return 0;
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun
of_mdiobus_register_device(struct mii_bus * mdio,struct device_node * child,u32 addr)151*4882a593Smuzhiyun static int of_mdiobus_register_device(struct mii_bus *mdio,
152*4882a593Smuzhiyun struct device_node *child, u32 addr)
153*4882a593Smuzhiyun {
154*4882a593Smuzhiyun struct mdio_device *mdiodev;
155*4882a593Smuzhiyun int rc;
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun mdiodev = mdio_device_create(mdio, addr);
158*4882a593Smuzhiyun if (IS_ERR(mdiodev))
159*4882a593Smuzhiyun return PTR_ERR(mdiodev);
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun /* Associate the OF node with the device structure so it
162*4882a593Smuzhiyun * can be looked up later.
163*4882a593Smuzhiyun */
164*4882a593Smuzhiyun of_node_get(child);
165*4882a593Smuzhiyun mdiodev->dev.of_node = child;
166*4882a593Smuzhiyun mdiodev->dev.fwnode = of_fwnode_handle(child);
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun /* All data is now stored in the mdiodev struct; register it. */
169*4882a593Smuzhiyun rc = mdio_device_register(mdiodev);
170*4882a593Smuzhiyun if (rc) {
171*4882a593Smuzhiyun mdio_device_free(mdiodev);
172*4882a593Smuzhiyun of_node_put(child);
173*4882a593Smuzhiyun return rc;
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun dev_dbg(&mdio->dev, "registered mdio device %pOFn at address %i\n",
177*4882a593Smuzhiyun child, addr);
178*4882a593Smuzhiyun return 0;
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun /* The following is a list of PHY compatible strings which appear in
182*4882a593Smuzhiyun * some DTBs. The compatible string is never matched against a PHY
183*4882a593Smuzhiyun * driver, so is pointless. We only expect devices which are not PHYs
184*4882a593Smuzhiyun * to have a compatible string, so they can be matched to an MDIO
185*4882a593Smuzhiyun * driver. Encourage users to upgrade their DT blobs to remove these.
186*4882a593Smuzhiyun */
187*4882a593Smuzhiyun static const struct of_device_id whitelist_phys[] = {
188*4882a593Smuzhiyun { .compatible = "brcm,40nm-ephy" },
189*4882a593Smuzhiyun { .compatible = "broadcom,bcm5241" },
190*4882a593Smuzhiyun { .compatible = "marvell,88E1111", },
191*4882a593Smuzhiyun { .compatible = "marvell,88e1116", },
192*4882a593Smuzhiyun { .compatible = "marvell,88e1118", },
193*4882a593Smuzhiyun { .compatible = "marvell,88e1145", },
194*4882a593Smuzhiyun { .compatible = "marvell,88e1149r", },
195*4882a593Smuzhiyun { .compatible = "marvell,88e1310", },
196*4882a593Smuzhiyun { .compatible = "marvell,88E1510", },
197*4882a593Smuzhiyun { .compatible = "marvell,88E1514", },
198*4882a593Smuzhiyun { .compatible = "moxa,moxart-rtl8201cp", },
199*4882a593Smuzhiyun {}
200*4882a593Smuzhiyun };
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun /*
203*4882a593Smuzhiyun * Return true if the child node is for a phy. It must either:
204*4882a593Smuzhiyun * o Compatible string of "ethernet-phy-idX.X"
205*4882a593Smuzhiyun * o Compatible string of "ethernet-phy-ieee802.3-c45"
206*4882a593Smuzhiyun * o Compatible string of "ethernet-phy-ieee802.3-c22"
207*4882a593Smuzhiyun * o In the white list above (and issue a warning)
208*4882a593Smuzhiyun * o No compatibility string
209*4882a593Smuzhiyun *
210*4882a593Smuzhiyun * A device which is not a phy is expected to have a compatible string
211*4882a593Smuzhiyun * indicating what sort of device it is.
212*4882a593Smuzhiyun */
of_mdiobus_child_is_phy(struct device_node * child)213*4882a593Smuzhiyun bool of_mdiobus_child_is_phy(struct device_node *child)
214*4882a593Smuzhiyun {
215*4882a593Smuzhiyun u32 phy_id;
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun if (of_get_phy_id(child, &phy_id) != -EINVAL)
218*4882a593Smuzhiyun return true;
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun if (of_device_is_compatible(child, "ethernet-phy-ieee802.3-c45"))
221*4882a593Smuzhiyun return true;
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun if (of_device_is_compatible(child, "ethernet-phy-ieee802.3-c22"))
224*4882a593Smuzhiyun return true;
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun if (of_match_node(whitelist_phys, child)) {
227*4882a593Smuzhiyun pr_warn(FW_WARN
228*4882a593Smuzhiyun "%pOF: Whitelisted compatible string. Please remove\n",
229*4882a593Smuzhiyun child);
230*4882a593Smuzhiyun return true;
231*4882a593Smuzhiyun }
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun if (!of_find_property(child, "compatible", NULL))
234*4882a593Smuzhiyun return true;
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun return false;
237*4882a593Smuzhiyun }
238*4882a593Smuzhiyun EXPORT_SYMBOL(of_mdiobus_child_is_phy);
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun /**
241*4882a593Smuzhiyun * of_mdiobus_register - Register mii_bus and create PHYs from the device tree
242*4882a593Smuzhiyun * @mdio: pointer to mii_bus structure
243*4882a593Smuzhiyun * @np: pointer to device_node of MDIO bus.
244*4882a593Smuzhiyun *
245*4882a593Smuzhiyun * This function registers the mii_bus structure and registers a phy_device
246*4882a593Smuzhiyun * for each child node of @np.
247*4882a593Smuzhiyun */
of_mdiobus_register(struct mii_bus * mdio,struct device_node * np)248*4882a593Smuzhiyun int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np)
249*4882a593Smuzhiyun {
250*4882a593Smuzhiyun struct device_node *child;
251*4882a593Smuzhiyun bool scanphys = false;
252*4882a593Smuzhiyun int addr, rc;
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun if (!np)
255*4882a593Smuzhiyun return mdiobus_register(mdio);
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun /* Do not continue if the node is disabled */
258*4882a593Smuzhiyun if (!of_device_is_available(np))
259*4882a593Smuzhiyun return -ENODEV;
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun /* Mask out all PHYs from auto probing. Instead the PHYs listed in
262*4882a593Smuzhiyun * the device tree are populated after the bus has been registered */
263*4882a593Smuzhiyun mdio->phy_mask = ~0;
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun mdio->dev.of_node = np;
266*4882a593Smuzhiyun mdio->dev.fwnode = of_fwnode_handle(np);
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun /* Get bus level PHY reset GPIO details */
269*4882a593Smuzhiyun mdio->reset_delay_us = DEFAULT_GPIO_RESET_DELAY;
270*4882a593Smuzhiyun of_property_read_u32(np, "reset-delay-us", &mdio->reset_delay_us);
271*4882a593Smuzhiyun mdio->reset_post_delay_us = 0;
272*4882a593Smuzhiyun of_property_read_u32(np, "reset-post-delay-us", &mdio->reset_post_delay_us);
273*4882a593Smuzhiyun
274*4882a593Smuzhiyun /* Register the MDIO bus */
275*4882a593Smuzhiyun rc = mdiobus_register(mdio);
276*4882a593Smuzhiyun if (rc)
277*4882a593Smuzhiyun return rc;
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun /* Loop over the child nodes and register a phy_device for each phy */
280*4882a593Smuzhiyun for_each_available_child_of_node(np, child) {
281*4882a593Smuzhiyun addr = of_mdio_parse_addr(&mdio->dev, child);
282*4882a593Smuzhiyun if (addr < 0) {
283*4882a593Smuzhiyun scanphys = true;
284*4882a593Smuzhiyun continue;
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun if (of_mdiobus_child_is_phy(child))
288*4882a593Smuzhiyun rc = of_mdiobus_register_phy(mdio, child, addr);
289*4882a593Smuzhiyun else
290*4882a593Smuzhiyun rc = of_mdiobus_register_device(mdio, child, addr);
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun if (rc == -ENODEV)
293*4882a593Smuzhiyun dev_err(&mdio->dev,
294*4882a593Smuzhiyun "MDIO device at address %d is missing.\n",
295*4882a593Smuzhiyun addr);
296*4882a593Smuzhiyun else if (rc)
297*4882a593Smuzhiyun goto unregister;
298*4882a593Smuzhiyun }
299*4882a593Smuzhiyun
300*4882a593Smuzhiyun if (!scanphys)
301*4882a593Smuzhiyun return 0;
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun /* auto scan for PHYs with empty reg property */
304*4882a593Smuzhiyun for_each_available_child_of_node(np, child) {
305*4882a593Smuzhiyun /* Skip PHYs with reg property set */
306*4882a593Smuzhiyun if (of_find_property(child, "reg", NULL))
307*4882a593Smuzhiyun continue;
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
310*4882a593Smuzhiyun /* skip already registered PHYs */
311*4882a593Smuzhiyun if (mdiobus_is_registered_device(mdio, addr))
312*4882a593Smuzhiyun continue;
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun /* be noisy to encourage people to set reg property */
315*4882a593Smuzhiyun dev_info(&mdio->dev, "scan phy %pOFn at address %i\n",
316*4882a593Smuzhiyun child, addr);
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun if (of_mdiobus_child_is_phy(child)) {
319*4882a593Smuzhiyun /* -ENODEV is the return code that PHYLIB has
320*4882a593Smuzhiyun * standardized on to indicate that bus
321*4882a593Smuzhiyun * scanning should continue.
322*4882a593Smuzhiyun */
323*4882a593Smuzhiyun rc = of_mdiobus_register_phy(mdio, child, addr);
324*4882a593Smuzhiyun if (!rc)
325*4882a593Smuzhiyun break;
326*4882a593Smuzhiyun if (rc != -ENODEV)
327*4882a593Smuzhiyun goto unregister;
328*4882a593Smuzhiyun }
329*4882a593Smuzhiyun }
330*4882a593Smuzhiyun }
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun return 0;
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun unregister:
335*4882a593Smuzhiyun of_node_put(child);
336*4882a593Smuzhiyun mdiobus_unregister(mdio);
337*4882a593Smuzhiyun return rc;
338*4882a593Smuzhiyun }
339*4882a593Smuzhiyun EXPORT_SYMBOL(of_mdiobus_register);
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun /**
342*4882a593Smuzhiyun * of_mdio_find_device - Given a device tree node, find the mdio_device
343*4882a593Smuzhiyun * @np: pointer to the mdio_device's device tree node
344*4882a593Smuzhiyun *
345*4882a593Smuzhiyun * If successful, returns a pointer to the mdio_device with the embedded
346*4882a593Smuzhiyun * struct device refcount incremented by one, or NULL on failure.
347*4882a593Smuzhiyun * The caller should call put_device() on the mdio_device after its use
348*4882a593Smuzhiyun */
of_mdio_find_device(struct device_node * np)349*4882a593Smuzhiyun struct mdio_device *of_mdio_find_device(struct device_node *np)
350*4882a593Smuzhiyun {
351*4882a593Smuzhiyun struct device *d;
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun if (!np)
354*4882a593Smuzhiyun return NULL;
355*4882a593Smuzhiyun
356*4882a593Smuzhiyun d = bus_find_device_by_of_node(&mdio_bus_type, np);
357*4882a593Smuzhiyun if (!d)
358*4882a593Smuzhiyun return NULL;
359*4882a593Smuzhiyun
360*4882a593Smuzhiyun return to_mdio_device(d);
361*4882a593Smuzhiyun }
362*4882a593Smuzhiyun EXPORT_SYMBOL(of_mdio_find_device);
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun /**
365*4882a593Smuzhiyun * of_phy_find_device - Give a PHY node, find the phy_device
366*4882a593Smuzhiyun * @phy_np: Pointer to the phy's device tree node
367*4882a593Smuzhiyun *
368*4882a593Smuzhiyun * If successful, returns a pointer to the phy_device with the embedded
369*4882a593Smuzhiyun * struct device refcount incremented by one, or NULL on failure.
370*4882a593Smuzhiyun */
of_phy_find_device(struct device_node * phy_np)371*4882a593Smuzhiyun struct phy_device *of_phy_find_device(struct device_node *phy_np)
372*4882a593Smuzhiyun {
373*4882a593Smuzhiyun struct mdio_device *mdiodev;
374*4882a593Smuzhiyun
375*4882a593Smuzhiyun mdiodev = of_mdio_find_device(phy_np);
376*4882a593Smuzhiyun if (!mdiodev)
377*4882a593Smuzhiyun return NULL;
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY)
380*4882a593Smuzhiyun return to_phy_device(&mdiodev->dev);
381*4882a593Smuzhiyun
382*4882a593Smuzhiyun put_device(&mdiodev->dev);
383*4882a593Smuzhiyun
384*4882a593Smuzhiyun return NULL;
385*4882a593Smuzhiyun }
386*4882a593Smuzhiyun EXPORT_SYMBOL(of_phy_find_device);
387*4882a593Smuzhiyun
388*4882a593Smuzhiyun /**
389*4882a593Smuzhiyun * of_phy_connect - Connect to the phy described in the device tree
390*4882a593Smuzhiyun * @dev: pointer to net_device claiming the phy
391*4882a593Smuzhiyun * @phy_np: Pointer to device tree node for the PHY
392*4882a593Smuzhiyun * @hndlr: Link state callback for the network device
393*4882a593Smuzhiyun * @flags: flags to pass to the PHY
394*4882a593Smuzhiyun * @iface: PHY data interface type
395*4882a593Smuzhiyun *
396*4882a593Smuzhiyun * If successful, returns a pointer to the phy_device with the embedded
397*4882a593Smuzhiyun * struct device refcount incremented by one, or NULL on failure. The
398*4882a593Smuzhiyun * refcount must be dropped by calling phy_disconnect() or phy_detach().
399*4882a593Smuzhiyun */
of_phy_connect(struct net_device * dev,struct device_node * phy_np,void (* hndlr)(struct net_device *),u32 flags,phy_interface_t iface)400*4882a593Smuzhiyun struct phy_device *of_phy_connect(struct net_device *dev,
401*4882a593Smuzhiyun struct device_node *phy_np,
402*4882a593Smuzhiyun void (*hndlr)(struct net_device *), u32 flags,
403*4882a593Smuzhiyun phy_interface_t iface)
404*4882a593Smuzhiyun {
405*4882a593Smuzhiyun struct phy_device *phy = of_phy_find_device(phy_np);
406*4882a593Smuzhiyun int ret;
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun if (!phy)
409*4882a593Smuzhiyun return NULL;
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun phy->dev_flags |= flags;
412*4882a593Smuzhiyun
413*4882a593Smuzhiyun ret = phy_connect_direct(dev, phy, hndlr, iface);
414*4882a593Smuzhiyun
415*4882a593Smuzhiyun /* refcount is held by phy_connect_direct() on success */
416*4882a593Smuzhiyun put_device(&phy->mdio.dev);
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun return ret ? NULL : phy;
419*4882a593Smuzhiyun }
420*4882a593Smuzhiyun EXPORT_SYMBOL(of_phy_connect);
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun /**
423*4882a593Smuzhiyun * of_phy_get_and_connect
424*4882a593Smuzhiyun * - Get phy node and connect to the phy described in the device tree
425*4882a593Smuzhiyun * @dev: pointer to net_device claiming the phy
426*4882a593Smuzhiyun * @np: Pointer to device tree node for the net_device claiming the phy
427*4882a593Smuzhiyun * @hndlr: Link state callback for the network device
428*4882a593Smuzhiyun *
429*4882a593Smuzhiyun * If successful, returns a pointer to the phy_device with the embedded
430*4882a593Smuzhiyun * struct device refcount incremented by one, or NULL on failure. The
431*4882a593Smuzhiyun * refcount must be dropped by calling phy_disconnect() or phy_detach().
432*4882a593Smuzhiyun */
of_phy_get_and_connect(struct net_device * dev,struct device_node * np,void (* hndlr)(struct net_device *))433*4882a593Smuzhiyun struct phy_device *of_phy_get_and_connect(struct net_device *dev,
434*4882a593Smuzhiyun struct device_node *np,
435*4882a593Smuzhiyun void (*hndlr)(struct net_device *))
436*4882a593Smuzhiyun {
437*4882a593Smuzhiyun phy_interface_t iface;
438*4882a593Smuzhiyun struct device_node *phy_np;
439*4882a593Smuzhiyun struct phy_device *phy;
440*4882a593Smuzhiyun int ret;
441*4882a593Smuzhiyun
442*4882a593Smuzhiyun ret = of_get_phy_mode(np, &iface);
443*4882a593Smuzhiyun if (ret)
444*4882a593Smuzhiyun return NULL;
445*4882a593Smuzhiyun if (of_phy_is_fixed_link(np)) {
446*4882a593Smuzhiyun ret = of_phy_register_fixed_link(np);
447*4882a593Smuzhiyun if (ret < 0) {
448*4882a593Smuzhiyun netdev_err(dev, "broken fixed-link specification\n");
449*4882a593Smuzhiyun return NULL;
450*4882a593Smuzhiyun }
451*4882a593Smuzhiyun phy_np = of_node_get(np);
452*4882a593Smuzhiyun } else {
453*4882a593Smuzhiyun phy_np = of_parse_phandle(np, "phy-handle", 0);
454*4882a593Smuzhiyun if (!phy_np)
455*4882a593Smuzhiyun return NULL;
456*4882a593Smuzhiyun }
457*4882a593Smuzhiyun
458*4882a593Smuzhiyun phy = of_phy_connect(dev, phy_np, hndlr, 0, iface);
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun of_node_put(phy_np);
461*4882a593Smuzhiyun
462*4882a593Smuzhiyun return phy;
463*4882a593Smuzhiyun }
464*4882a593Smuzhiyun EXPORT_SYMBOL(of_phy_get_and_connect);
465*4882a593Smuzhiyun
466*4882a593Smuzhiyun /**
467*4882a593Smuzhiyun * of_phy_attach - Attach to a PHY without starting the state machine
468*4882a593Smuzhiyun * @dev: pointer to net_device claiming the phy
469*4882a593Smuzhiyun * @phy_np: Node pointer for the PHY
470*4882a593Smuzhiyun * @flags: flags to pass to the PHY
471*4882a593Smuzhiyun * @iface: PHY data interface type
472*4882a593Smuzhiyun *
473*4882a593Smuzhiyun * If successful, returns a pointer to the phy_device with the embedded
474*4882a593Smuzhiyun * struct device refcount incremented by one, or NULL on failure. The
475*4882a593Smuzhiyun * refcount must be dropped by calling phy_disconnect() or phy_detach().
476*4882a593Smuzhiyun */
of_phy_attach(struct net_device * dev,struct device_node * phy_np,u32 flags,phy_interface_t iface)477*4882a593Smuzhiyun struct phy_device *of_phy_attach(struct net_device *dev,
478*4882a593Smuzhiyun struct device_node *phy_np, u32 flags,
479*4882a593Smuzhiyun phy_interface_t iface)
480*4882a593Smuzhiyun {
481*4882a593Smuzhiyun struct phy_device *phy = of_phy_find_device(phy_np);
482*4882a593Smuzhiyun int ret;
483*4882a593Smuzhiyun
484*4882a593Smuzhiyun if (!phy)
485*4882a593Smuzhiyun return NULL;
486*4882a593Smuzhiyun
487*4882a593Smuzhiyun ret = phy_attach_direct(dev, phy, flags, iface);
488*4882a593Smuzhiyun
489*4882a593Smuzhiyun /* refcount is held by phy_attach_direct() on success */
490*4882a593Smuzhiyun put_device(&phy->mdio.dev);
491*4882a593Smuzhiyun
492*4882a593Smuzhiyun return ret ? NULL : phy;
493*4882a593Smuzhiyun }
494*4882a593Smuzhiyun EXPORT_SYMBOL(of_phy_attach);
495*4882a593Smuzhiyun
496*4882a593Smuzhiyun /*
497*4882a593Smuzhiyun * of_phy_is_fixed_link() and of_phy_register_fixed_link() must
498*4882a593Smuzhiyun * support two DT bindings:
499*4882a593Smuzhiyun * - the old DT binding, where 'fixed-link' was a property with 5
500*4882a593Smuzhiyun * cells encoding various informations about the fixed PHY
501*4882a593Smuzhiyun * - the new DT binding, where 'fixed-link' is a sub-node of the
502*4882a593Smuzhiyun * Ethernet device.
503*4882a593Smuzhiyun */
of_phy_is_fixed_link(struct device_node * np)504*4882a593Smuzhiyun bool of_phy_is_fixed_link(struct device_node *np)
505*4882a593Smuzhiyun {
506*4882a593Smuzhiyun struct device_node *dn;
507*4882a593Smuzhiyun int len, err;
508*4882a593Smuzhiyun const char *managed;
509*4882a593Smuzhiyun
510*4882a593Smuzhiyun /* New binding */
511*4882a593Smuzhiyun dn = of_get_child_by_name(np, "fixed-link");
512*4882a593Smuzhiyun if (dn) {
513*4882a593Smuzhiyun of_node_put(dn);
514*4882a593Smuzhiyun return true;
515*4882a593Smuzhiyun }
516*4882a593Smuzhiyun
517*4882a593Smuzhiyun err = of_property_read_string(np, "managed", &managed);
518*4882a593Smuzhiyun if (err == 0 && strcmp(managed, "auto") != 0)
519*4882a593Smuzhiyun return true;
520*4882a593Smuzhiyun
521*4882a593Smuzhiyun /* Old binding */
522*4882a593Smuzhiyun if (of_get_property(np, "fixed-link", &len) &&
523*4882a593Smuzhiyun len == (5 * sizeof(__be32)))
524*4882a593Smuzhiyun return true;
525*4882a593Smuzhiyun
526*4882a593Smuzhiyun return false;
527*4882a593Smuzhiyun }
528*4882a593Smuzhiyun EXPORT_SYMBOL(of_phy_is_fixed_link);
529*4882a593Smuzhiyun
of_phy_register_fixed_link(struct device_node * np)530*4882a593Smuzhiyun int of_phy_register_fixed_link(struct device_node *np)
531*4882a593Smuzhiyun {
532*4882a593Smuzhiyun struct fixed_phy_status status = {};
533*4882a593Smuzhiyun struct device_node *fixed_link_node;
534*4882a593Smuzhiyun u32 fixed_link_prop[5];
535*4882a593Smuzhiyun const char *managed;
536*4882a593Smuzhiyun
537*4882a593Smuzhiyun if (of_property_read_string(np, "managed", &managed) == 0 &&
538*4882a593Smuzhiyun strcmp(managed, "in-band-status") == 0) {
539*4882a593Smuzhiyun /* status is zeroed, namely its .link member */
540*4882a593Smuzhiyun goto register_phy;
541*4882a593Smuzhiyun }
542*4882a593Smuzhiyun
543*4882a593Smuzhiyun /* New binding */
544*4882a593Smuzhiyun fixed_link_node = of_get_child_by_name(np, "fixed-link");
545*4882a593Smuzhiyun if (fixed_link_node) {
546*4882a593Smuzhiyun status.link = 1;
547*4882a593Smuzhiyun status.duplex = of_property_read_bool(fixed_link_node,
548*4882a593Smuzhiyun "full-duplex");
549*4882a593Smuzhiyun if (of_property_read_u32(fixed_link_node, "speed",
550*4882a593Smuzhiyun &status.speed)) {
551*4882a593Smuzhiyun of_node_put(fixed_link_node);
552*4882a593Smuzhiyun return -EINVAL;
553*4882a593Smuzhiyun }
554*4882a593Smuzhiyun status.pause = of_property_read_bool(fixed_link_node, "pause");
555*4882a593Smuzhiyun status.asym_pause = of_property_read_bool(fixed_link_node,
556*4882a593Smuzhiyun "asym-pause");
557*4882a593Smuzhiyun of_node_put(fixed_link_node);
558*4882a593Smuzhiyun
559*4882a593Smuzhiyun goto register_phy;
560*4882a593Smuzhiyun }
561*4882a593Smuzhiyun
562*4882a593Smuzhiyun /* Old binding */
563*4882a593Smuzhiyun if (of_property_read_u32_array(np, "fixed-link", fixed_link_prop,
564*4882a593Smuzhiyun ARRAY_SIZE(fixed_link_prop)) == 0) {
565*4882a593Smuzhiyun status.link = 1;
566*4882a593Smuzhiyun status.duplex = fixed_link_prop[1];
567*4882a593Smuzhiyun status.speed = fixed_link_prop[2];
568*4882a593Smuzhiyun status.pause = fixed_link_prop[3];
569*4882a593Smuzhiyun status.asym_pause = fixed_link_prop[4];
570*4882a593Smuzhiyun goto register_phy;
571*4882a593Smuzhiyun }
572*4882a593Smuzhiyun
573*4882a593Smuzhiyun return -ENODEV;
574*4882a593Smuzhiyun
575*4882a593Smuzhiyun register_phy:
576*4882a593Smuzhiyun return PTR_ERR_OR_ZERO(fixed_phy_register(PHY_POLL, &status, np));
577*4882a593Smuzhiyun }
578*4882a593Smuzhiyun EXPORT_SYMBOL(of_phy_register_fixed_link);
579*4882a593Smuzhiyun
of_phy_deregister_fixed_link(struct device_node * np)580*4882a593Smuzhiyun void of_phy_deregister_fixed_link(struct device_node *np)
581*4882a593Smuzhiyun {
582*4882a593Smuzhiyun struct phy_device *phydev;
583*4882a593Smuzhiyun
584*4882a593Smuzhiyun phydev = of_phy_find_device(np);
585*4882a593Smuzhiyun if (!phydev)
586*4882a593Smuzhiyun return;
587*4882a593Smuzhiyun
588*4882a593Smuzhiyun fixed_phy_unregister(phydev);
589*4882a593Smuzhiyun
590*4882a593Smuzhiyun put_device(&phydev->mdio.dev); /* of_phy_find_device() */
591*4882a593Smuzhiyun phy_device_free(phydev); /* fixed_phy_register() */
592*4882a593Smuzhiyun }
593*4882a593Smuzhiyun EXPORT_SYMBOL(of_phy_deregister_fixed_link);
594