xref: /openbmc/linux/drivers/net/mdio/of_mdio.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
114b26b12SCalvin Johnson // SPDX-License-Identifier: GPL-2.0-only
214b26b12SCalvin Johnson /*
314b26b12SCalvin Johnson  * OF helpers for the MDIO (Ethernet PHY) API
414b26b12SCalvin Johnson  *
514b26b12SCalvin Johnson  * Copyright (c) 2009 Secret Lab Technologies, Ltd.
614b26b12SCalvin Johnson  *
714b26b12SCalvin Johnson  * This file provides helper functions for extracting PHY device information
814b26b12SCalvin Johnson  * out of the OpenFirmware device tree and using it to populate an mii_bus.
914b26b12SCalvin Johnson  */
1014b26b12SCalvin Johnson 
1114b26b12SCalvin Johnson #include <linux/device.h>
1214b26b12SCalvin Johnson #include <linux/err.h>
13bc1bee3bSCalvin Johnson #include <linux/fwnode_mdio.h>
141bf34366SCalvin Johnson #include <linux/kernel.h>
151bf34366SCalvin Johnson #include <linux/module.h>
161bf34366SCalvin Johnson #include <linux/netdevice.h>
1714b26b12SCalvin Johnson #include <linux/of.h>
1814b26b12SCalvin Johnson #include <linux/of_irq.h>
1914b26b12SCalvin Johnson #include <linux/of_mdio.h>
2014b26b12SCalvin Johnson #include <linux/of_net.h>
211bf34366SCalvin Johnson #include <linux/phy.h>
221bf34366SCalvin Johnson #include <linux/phy_fixed.h>
2314b26b12SCalvin Johnson 
2414b26b12SCalvin Johnson #define DEFAULT_GPIO_RESET_DELAY	10	/* in microseconds */
2514b26b12SCalvin Johnson 
2614b26b12SCalvin Johnson MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
2714b26b12SCalvin Johnson MODULE_LICENSE("GPL");
2814b26b12SCalvin Johnson 
2914b26b12SCalvin Johnson /* Extract the clause 22 phy ID from the compatible string of the form
3014b26b12SCalvin Johnson  * ethernet-phy-idAAAA.BBBB */
of_get_phy_id(struct device_node * device,u32 * phy_id)3114b26b12SCalvin Johnson static int of_get_phy_id(struct device_node *device, u32 *phy_id)
3214b26b12SCalvin Johnson {
33cf996860SCalvin Johnson 	return fwnode_get_phy_id(of_fwnode_handle(device), phy_id);
3414b26b12SCalvin Johnson }
3514b26b12SCalvin Johnson 
of_mdiobus_phy_device_register(struct mii_bus * mdio,struct phy_device * phy,struct device_node * child,u32 addr)3614b26b12SCalvin Johnson int of_mdiobus_phy_device_register(struct mii_bus *mdio, struct phy_device *phy,
3714b26b12SCalvin Johnson 				   struct device_node *child, u32 addr)
3814b26b12SCalvin Johnson {
39bc1bee3bSCalvin Johnson 	return fwnode_mdiobus_phy_device_register(mdio, phy,
40bc1bee3bSCalvin Johnson 						  of_fwnode_handle(child),
41bc1bee3bSCalvin Johnson 						  addr);
4214b26b12SCalvin Johnson }
4314b26b12SCalvin Johnson EXPORT_SYMBOL(of_mdiobus_phy_device_register);
4414b26b12SCalvin Johnson 
of_mdiobus_register_phy(struct mii_bus * mdio,struct device_node * child,u32 addr)4514b26b12SCalvin Johnson static int of_mdiobus_register_phy(struct mii_bus *mdio,
4614b26b12SCalvin Johnson 				    struct device_node *child, u32 addr)
4714b26b12SCalvin Johnson {
488d2cb3adSCalvin Johnson 	return fwnode_mdiobus_register_phy(mdio, of_fwnode_handle(child), addr);
4914b26b12SCalvin Johnson }
5014b26b12SCalvin Johnson 
of_mdiobus_register_device(struct mii_bus * mdio,struct device_node * child,u32 addr)5114b26b12SCalvin Johnson static int of_mdiobus_register_device(struct mii_bus *mdio,
5214b26b12SCalvin Johnson 				      struct device_node *child, u32 addr)
5314b26b12SCalvin Johnson {
547e33d84dSIoana Ciornei 	struct fwnode_handle *fwnode = of_fwnode_handle(child);
5514b26b12SCalvin Johnson 	struct mdio_device *mdiodev;
5614b26b12SCalvin Johnson 	int rc;
5714b26b12SCalvin Johnson 
5814b26b12SCalvin Johnson 	mdiodev = mdio_device_create(mdio, addr);
5914b26b12SCalvin Johnson 	if (IS_ERR(mdiodev))
6014b26b12SCalvin Johnson 		return PTR_ERR(mdiodev);
6114b26b12SCalvin Johnson 
6214b26b12SCalvin Johnson 	/* Associate the OF node with the device structure so it
6314b26b12SCalvin Johnson 	 * can be looked up later.
6414b26b12SCalvin Johnson 	 */
657e33d84dSIoana Ciornei 	fwnode_handle_get(fwnode);
667e33d84dSIoana Ciornei 	device_set_node(&mdiodev->dev, fwnode);
6714b26b12SCalvin Johnson 
6814b26b12SCalvin Johnson 	/* All data is now stored in the mdiodev struct; register it. */
6914b26b12SCalvin Johnson 	rc = mdio_device_register(mdiodev);
7014b26b12SCalvin Johnson 	if (rc) {
71cb376176SZeng Heng 		device_set_node(&mdiodev->dev, NULL);
72cb376176SZeng Heng 		fwnode_handle_put(fwnode);
7314b26b12SCalvin Johnson 		mdio_device_free(mdiodev);
7414b26b12SCalvin Johnson 		return rc;
7514b26b12SCalvin Johnson 	}
7614b26b12SCalvin Johnson 
7714b26b12SCalvin Johnson 	dev_dbg(&mdio->dev, "registered mdio device %pOFn at address %i\n",
7814b26b12SCalvin Johnson 		child, addr);
7914b26b12SCalvin Johnson 	return 0;
8014b26b12SCalvin Johnson }
8114b26b12SCalvin Johnson 
8214b26b12SCalvin Johnson /* The following is a list of PHY compatible strings which appear in
8314b26b12SCalvin Johnson  * some DTBs. The compatible string is never matched against a PHY
8414b26b12SCalvin Johnson  * driver, so is pointless. We only expect devices which are not PHYs
8514b26b12SCalvin Johnson  * to have a compatible string, so they can be matched to an MDIO
8614b26b12SCalvin Johnson  * driver.  Encourage users to upgrade their DT blobs to remove these.
8714b26b12SCalvin Johnson  */
8814b26b12SCalvin Johnson static const struct of_device_id whitelist_phys[] = {
8914b26b12SCalvin Johnson 	{ .compatible = "brcm,40nm-ephy" },
9014b26b12SCalvin Johnson 	{ .compatible = "broadcom,bcm5241" },
9114b26b12SCalvin Johnson 	{ .compatible = "marvell,88E1111", },
9214b26b12SCalvin Johnson 	{ .compatible = "marvell,88e1116", },
9314b26b12SCalvin Johnson 	{ .compatible = "marvell,88e1118", },
9414b26b12SCalvin Johnson 	{ .compatible = "marvell,88e1145", },
9514b26b12SCalvin Johnson 	{ .compatible = "marvell,88e1149r", },
9614b26b12SCalvin Johnson 	{ .compatible = "marvell,88e1310", },
9714b26b12SCalvin Johnson 	{ .compatible = "marvell,88E1510", },
9814b26b12SCalvin Johnson 	{ .compatible = "marvell,88E1514", },
9914b26b12SCalvin Johnson 	{ .compatible = "moxa,moxart-rtl8201cp", },
10014b26b12SCalvin Johnson 	{}
10114b26b12SCalvin Johnson };
10214b26b12SCalvin Johnson 
10314b26b12SCalvin Johnson /*
10414b26b12SCalvin Johnson  * Return true if the child node is for a phy. It must either:
10514b26b12SCalvin Johnson  * o Compatible string of "ethernet-phy-idX.X"
10614b26b12SCalvin Johnson  * o Compatible string of "ethernet-phy-ieee802.3-c45"
10714b26b12SCalvin Johnson  * o Compatible string of "ethernet-phy-ieee802.3-c22"
10814b26b12SCalvin Johnson  * o In the white list above (and issue a warning)
10914b26b12SCalvin Johnson  * o No compatibility string
11014b26b12SCalvin Johnson  *
11114b26b12SCalvin Johnson  * A device which is not a phy is expected to have a compatible string
11214b26b12SCalvin Johnson  * indicating what sort of device it is.
11314b26b12SCalvin Johnson  */
of_mdiobus_child_is_phy(struct device_node * child)11414b26b12SCalvin Johnson bool of_mdiobus_child_is_phy(struct device_node *child)
11514b26b12SCalvin Johnson {
11614b26b12SCalvin Johnson 	u32 phy_id;
11714b26b12SCalvin Johnson 
11814b26b12SCalvin Johnson 	if (of_get_phy_id(child, &phy_id) != -EINVAL)
11914b26b12SCalvin Johnson 		return true;
12014b26b12SCalvin Johnson 
12114b26b12SCalvin Johnson 	if (of_device_is_compatible(child, "ethernet-phy-ieee802.3-c45"))
12214b26b12SCalvin Johnson 		return true;
12314b26b12SCalvin Johnson 
12414b26b12SCalvin Johnson 	if (of_device_is_compatible(child, "ethernet-phy-ieee802.3-c22"))
12514b26b12SCalvin Johnson 		return true;
12614b26b12SCalvin Johnson 
12714b26b12SCalvin Johnson 	if (of_match_node(whitelist_phys, child)) {
12814b26b12SCalvin Johnson 		pr_warn(FW_WARN
12914b26b12SCalvin Johnson 			"%pOF: Whitelisted compatible string. Please remove\n",
13014b26b12SCalvin Johnson 			child);
13114b26b12SCalvin Johnson 		return true;
13214b26b12SCalvin Johnson 	}
13314b26b12SCalvin Johnson 
134bcc85868SRob Herring 	if (!of_property_present(child, "compatible"))
13514b26b12SCalvin Johnson 		return true;
13614b26b12SCalvin Johnson 
13714b26b12SCalvin Johnson 	return false;
13814b26b12SCalvin Johnson }
13914b26b12SCalvin Johnson EXPORT_SYMBOL(of_mdiobus_child_is_phy);
14014b26b12SCalvin Johnson 
14114b26b12SCalvin Johnson /**
142*99669259SMaxime Bizon  * __of_mdiobus_register - Register mii_bus and create PHYs from the device tree
14314b26b12SCalvin Johnson  * @mdio: pointer to mii_bus structure
14414b26b12SCalvin Johnson  * @np: pointer to device_node of MDIO bus.
145*99669259SMaxime Bizon  * @owner: module owning the @mdio object.
14614b26b12SCalvin Johnson  *
14714b26b12SCalvin Johnson  * This function registers the mii_bus structure and registers a phy_device
14814b26b12SCalvin Johnson  * for each child node of @np.
14914b26b12SCalvin Johnson  */
__of_mdiobus_register(struct mii_bus * mdio,struct device_node * np,struct module * owner)150*99669259SMaxime Bizon int __of_mdiobus_register(struct mii_bus *mdio, struct device_node *np,
151*99669259SMaxime Bizon 			  struct module *owner)
15214b26b12SCalvin Johnson {
15314b26b12SCalvin Johnson 	struct device_node *child;
15414b26b12SCalvin Johnson 	bool scanphys = false;
15514b26b12SCalvin Johnson 	int addr, rc;
15614b26b12SCalvin Johnson 
15714b26b12SCalvin Johnson 	if (!np)
158*99669259SMaxime Bizon 		return __mdiobus_register(mdio, owner);
15914b26b12SCalvin Johnson 
16014b26b12SCalvin Johnson 	/* Do not continue if the node is disabled */
16114b26b12SCalvin Johnson 	if (!of_device_is_available(np))
16214b26b12SCalvin Johnson 		return -ENODEV;
16314b26b12SCalvin Johnson 
16414b26b12SCalvin Johnson 	/* Mask out all PHYs from auto probing.  Instead the PHYs listed in
16514b26b12SCalvin Johnson 	 * the device tree are populated after the bus has been registered */
16614b26b12SCalvin Johnson 	mdio->phy_mask = ~0;
16714b26b12SCalvin Johnson 
1687e33d84dSIoana Ciornei 	device_set_node(&mdio->dev, of_fwnode_handle(np));
16914b26b12SCalvin Johnson 
17014b26b12SCalvin Johnson 	/* Get bus level PHY reset GPIO details */
17114b26b12SCalvin Johnson 	mdio->reset_delay_us = DEFAULT_GPIO_RESET_DELAY;
17214b26b12SCalvin Johnson 	of_property_read_u32(np, "reset-delay-us", &mdio->reset_delay_us);
17314b26b12SCalvin Johnson 	mdio->reset_post_delay_us = 0;
17414b26b12SCalvin Johnson 	of_property_read_u32(np, "reset-post-delay-us", &mdio->reset_post_delay_us);
17514b26b12SCalvin Johnson 
17614b26b12SCalvin Johnson 	/* Register the MDIO bus */
177*99669259SMaxime Bizon 	rc = __mdiobus_register(mdio, owner);
17814b26b12SCalvin Johnson 	if (rc)
17914b26b12SCalvin Johnson 		return rc;
18014b26b12SCalvin Johnson 
18114b26b12SCalvin Johnson 	/* Loop over the child nodes and register a phy_device for each phy */
18214b26b12SCalvin Johnson 	for_each_available_child_of_node(np, child) {
18314b26b12SCalvin Johnson 		addr = of_mdio_parse_addr(&mdio->dev, child);
18414b26b12SCalvin Johnson 		if (addr < 0) {
18514b26b12SCalvin Johnson 			scanphys = true;
18614b26b12SCalvin Johnson 			continue;
18714b26b12SCalvin Johnson 		}
18814b26b12SCalvin Johnson 
18914b26b12SCalvin Johnson 		if (of_mdiobus_child_is_phy(child))
19014b26b12SCalvin Johnson 			rc = of_mdiobus_register_phy(mdio, child, addr);
19114b26b12SCalvin Johnson 		else
19214b26b12SCalvin Johnson 			rc = of_mdiobus_register_device(mdio, child, addr);
19314b26b12SCalvin Johnson 
19414b26b12SCalvin Johnson 		if (rc == -ENODEV)
19514b26b12SCalvin Johnson 			dev_err(&mdio->dev,
19614b26b12SCalvin Johnson 				"MDIO device at address %d is missing.\n",
19714b26b12SCalvin Johnson 				addr);
19814b26b12SCalvin Johnson 		else if (rc)
19914b26b12SCalvin Johnson 			goto unregister;
20014b26b12SCalvin Johnson 	}
20114b26b12SCalvin Johnson 
20214b26b12SCalvin Johnson 	if (!scanphys)
20314b26b12SCalvin Johnson 		return 0;
20414b26b12SCalvin Johnson 
20514b26b12SCalvin Johnson 	/* auto scan for PHYs with empty reg property */
20614b26b12SCalvin Johnson 	for_each_available_child_of_node(np, child) {
20714b26b12SCalvin Johnson 		/* Skip PHYs with reg property set */
208bcc85868SRob Herring 		if (of_property_present(child, "reg"))
20914b26b12SCalvin Johnson 			continue;
21014b26b12SCalvin Johnson 
21114b26b12SCalvin Johnson 		for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
21214b26b12SCalvin Johnson 			/* skip already registered PHYs */
21314b26b12SCalvin Johnson 			if (mdiobus_is_registered_device(mdio, addr))
21414b26b12SCalvin Johnson 				continue;
21514b26b12SCalvin Johnson 
21614b26b12SCalvin Johnson 			/* be noisy to encourage people to set reg property */
21714b26b12SCalvin Johnson 			dev_info(&mdio->dev, "scan phy %pOFn at address %i\n",
21814b26b12SCalvin Johnson 				 child, addr);
21914b26b12SCalvin Johnson 
22014b26b12SCalvin Johnson 			if (of_mdiobus_child_is_phy(child)) {
22114b26b12SCalvin Johnson 				/* -ENODEV is the return code that PHYLIB has
22214b26b12SCalvin Johnson 				 * standardized on to indicate that bus
22314b26b12SCalvin Johnson 				 * scanning should continue.
22414b26b12SCalvin Johnson 				 */
22514b26b12SCalvin Johnson 				rc = of_mdiobus_register_phy(mdio, child, addr);
22614b26b12SCalvin Johnson 				if (!rc)
22714b26b12SCalvin Johnson 					break;
22814b26b12SCalvin Johnson 				if (rc != -ENODEV)
22914b26b12SCalvin Johnson 					goto unregister;
23014b26b12SCalvin Johnson 			}
23114b26b12SCalvin Johnson 		}
23214b26b12SCalvin Johnson 	}
23314b26b12SCalvin Johnson 
23414b26b12SCalvin Johnson 	return 0;
23514b26b12SCalvin Johnson 
23614b26b12SCalvin Johnson unregister:
2371c48709eSLiang He 	of_node_put(child);
23814b26b12SCalvin Johnson 	mdiobus_unregister(mdio);
23914b26b12SCalvin Johnson 	return rc;
24014b26b12SCalvin Johnson }
241*99669259SMaxime Bizon EXPORT_SYMBOL(__of_mdiobus_register);
24214b26b12SCalvin Johnson 
24314b26b12SCalvin Johnson /**
24414b26b12SCalvin Johnson  * of_mdio_find_device - Given a device tree node, find the mdio_device
24514b26b12SCalvin Johnson  * @np: pointer to the mdio_device's device tree node
24614b26b12SCalvin Johnson  *
24714b26b12SCalvin Johnson  * If successful, returns a pointer to the mdio_device with the embedded
24814b26b12SCalvin Johnson  * struct device refcount incremented by one, or NULL on failure.
24914b26b12SCalvin Johnson  * The caller should call put_device() on the mdio_device after its use
25014b26b12SCalvin Johnson  */
of_mdio_find_device(struct device_node * np)25114b26b12SCalvin Johnson struct mdio_device *of_mdio_find_device(struct device_node *np)
25214b26b12SCalvin Johnson {
2530fb16976SCalvin Johnson 	return fwnode_mdio_find_device(of_fwnode_handle(np));
25414b26b12SCalvin Johnson }
25514b26b12SCalvin Johnson EXPORT_SYMBOL(of_mdio_find_device);
25614b26b12SCalvin Johnson 
25714b26b12SCalvin Johnson /**
25814b26b12SCalvin Johnson  * of_phy_find_device - Give a PHY node, find the phy_device
25914b26b12SCalvin Johnson  * @phy_np: Pointer to the phy's device tree node
26014b26b12SCalvin Johnson  *
26114b26b12SCalvin Johnson  * If successful, returns a pointer to the phy_device with the embedded
26214b26b12SCalvin Johnson  * struct device refcount incremented by one, or NULL on failure.
26314b26b12SCalvin Johnson  */
of_phy_find_device(struct device_node * phy_np)26414b26b12SCalvin Johnson struct phy_device *of_phy_find_device(struct device_node *phy_np)
26514b26b12SCalvin Johnson {
2662d7b8bf1SCalvin Johnson 	return fwnode_phy_find_device(of_fwnode_handle(phy_np));
26714b26b12SCalvin Johnson }
26814b26b12SCalvin Johnson EXPORT_SYMBOL(of_phy_find_device);
26914b26b12SCalvin Johnson 
27014b26b12SCalvin Johnson /**
27114b26b12SCalvin Johnson  * of_phy_connect - Connect to the phy described in the device tree
27214b26b12SCalvin Johnson  * @dev: pointer to net_device claiming the phy
27314b26b12SCalvin Johnson  * @phy_np: Pointer to device tree node for the PHY
27414b26b12SCalvin Johnson  * @hndlr: Link state callback for the network device
27514b26b12SCalvin Johnson  * @flags: flags to pass to the PHY
27614b26b12SCalvin Johnson  * @iface: PHY data interface type
27714b26b12SCalvin Johnson  *
27814b26b12SCalvin Johnson  * If successful, returns a pointer to the phy_device with the embedded
27914b26b12SCalvin Johnson  * struct device refcount incremented by one, or NULL on failure. The
28014b26b12SCalvin Johnson  * refcount must be dropped by calling phy_disconnect() or phy_detach().
28114b26b12SCalvin Johnson  */
of_phy_connect(struct net_device * dev,struct device_node * phy_np,void (* hndlr)(struct net_device *),u32 flags,phy_interface_t iface)28214b26b12SCalvin Johnson struct phy_device *of_phy_connect(struct net_device *dev,
28314b26b12SCalvin Johnson 				  struct device_node *phy_np,
28414b26b12SCalvin Johnson 				  void (*hndlr)(struct net_device *), u32 flags,
28514b26b12SCalvin Johnson 				  phy_interface_t iface)
28614b26b12SCalvin Johnson {
28714b26b12SCalvin Johnson 	struct phy_device *phy = of_phy_find_device(phy_np);
28814b26b12SCalvin Johnson 	int ret;
28914b26b12SCalvin Johnson 
29014b26b12SCalvin Johnson 	if (!phy)
29114b26b12SCalvin Johnson 		return NULL;
29214b26b12SCalvin Johnson 
29314b26b12SCalvin Johnson 	phy->dev_flags |= flags;
29414b26b12SCalvin Johnson 
29514b26b12SCalvin Johnson 	ret = phy_connect_direct(dev, phy, hndlr, iface);
29614b26b12SCalvin Johnson 
29714b26b12SCalvin Johnson 	/* refcount is held by phy_connect_direct() on success */
29814b26b12SCalvin Johnson 	put_device(&phy->mdio.dev);
29914b26b12SCalvin Johnson 
30014b26b12SCalvin Johnson 	return ret ? NULL : phy;
30114b26b12SCalvin Johnson }
30214b26b12SCalvin Johnson EXPORT_SYMBOL(of_phy_connect);
30314b26b12SCalvin Johnson 
30414b26b12SCalvin Johnson /**
30514b26b12SCalvin Johnson  * of_phy_get_and_connect
30614b26b12SCalvin Johnson  * - Get phy node and connect to the phy described in the device tree
30714b26b12SCalvin Johnson  * @dev: pointer to net_device claiming the phy
30814b26b12SCalvin Johnson  * @np: Pointer to device tree node for the net_device claiming the phy
30914b26b12SCalvin Johnson  * @hndlr: Link state callback for the network device
31014b26b12SCalvin Johnson  *
31114b26b12SCalvin Johnson  * If successful, returns a pointer to the phy_device with the embedded
31214b26b12SCalvin Johnson  * struct device refcount incremented by one, or NULL on failure. The
31314b26b12SCalvin Johnson  * refcount must be dropped by calling phy_disconnect() or phy_detach().
31414b26b12SCalvin Johnson  */
of_phy_get_and_connect(struct net_device * dev,struct device_node * np,void (* hndlr)(struct net_device *))31514b26b12SCalvin Johnson struct phy_device *of_phy_get_and_connect(struct net_device *dev,
31614b26b12SCalvin Johnson 					  struct device_node *np,
31714b26b12SCalvin Johnson 					  void (*hndlr)(struct net_device *))
31814b26b12SCalvin Johnson {
31914b26b12SCalvin Johnson 	phy_interface_t iface;
32014b26b12SCalvin Johnson 	struct device_node *phy_np;
32114b26b12SCalvin Johnson 	struct phy_device *phy;
32214b26b12SCalvin Johnson 	int ret;
32314b26b12SCalvin Johnson 
32414b26b12SCalvin Johnson 	ret = of_get_phy_mode(np, &iface);
32514b26b12SCalvin Johnson 	if (ret)
32614b26b12SCalvin Johnson 		return NULL;
32714b26b12SCalvin Johnson 	if (of_phy_is_fixed_link(np)) {
32814b26b12SCalvin Johnson 		ret = of_phy_register_fixed_link(np);
32914b26b12SCalvin Johnson 		if (ret < 0) {
33014b26b12SCalvin Johnson 			netdev_err(dev, "broken fixed-link specification\n");
33114b26b12SCalvin Johnson 			return NULL;
33214b26b12SCalvin Johnson 		}
33314b26b12SCalvin Johnson 		phy_np = of_node_get(np);
33414b26b12SCalvin Johnson 	} else {
33514b26b12SCalvin Johnson 		phy_np = of_parse_phandle(np, "phy-handle", 0);
33614b26b12SCalvin Johnson 		if (!phy_np)
33714b26b12SCalvin Johnson 			return NULL;
33814b26b12SCalvin Johnson 	}
33914b26b12SCalvin Johnson 
34014b26b12SCalvin Johnson 	phy = of_phy_connect(dev, phy_np, hndlr, 0, iface);
34114b26b12SCalvin Johnson 
34214b26b12SCalvin Johnson 	of_node_put(phy_np);
34314b26b12SCalvin Johnson 
34414b26b12SCalvin Johnson 	return phy;
34514b26b12SCalvin Johnson }
34614b26b12SCalvin Johnson EXPORT_SYMBOL(of_phy_get_and_connect);
34714b26b12SCalvin Johnson 
34814b26b12SCalvin Johnson /*
34914b26b12SCalvin Johnson  * of_phy_is_fixed_link() and of_phy_register_fixed_link() must
35014b26b12SCalvin Johnson  * support two DT bindings:
35114b26b12SCalvin Johnson  * - the old DT binding, where 'fixed-link' was a property with 5
352e65c2793SZheng Yongjun  *   cells encoding various information about the fixed PHY
35314b26b12SCalvin Johnson  * - the new DT binding, where 'fixed-link' is a sub-node of the
35414b26b12SCalvin Johnson  *   Ethernet device.
35514b26b12SCalvin Johnson  */
of_phy_is_fixed_link(struct device_node * np)35614b26b12SCalvin Johnson bool of_phy_is_fixed_link(struct device_node *np)
35714b26b12SCalvin Johnson {
35814b26b12SCalvin Johnson 	struct device_node *dn;
35914b26b12SCalvin Johnson 	int len, err;
36014b26b12SCalvin Johnson 	const char *managed;
36114b26b12SCalvin Johnson 
36214b26b12SCalvin Johnson 	/* New binding */
36314b26b12SCalvin Johnson 	dn = of_get_child_by_name(np, "fixed-link");
36414b26b12SCalvin Johnson 	if (dn) {
36514b26b12SCalvin Johnson 		of_node_put(dn);
36614b26b12SCalvin Johnson 		return true;
36714b26b12SCalvin Johnson 	}
36814b26b12SCalvin Johnson 
36914b26b12SCalvin Johnson 	err = of_property_read_string(np, "managed", &managed);
37014b26b12SCalvin Johnson 	if (err == 0 && strcmp(managed, "auto") != 0)
37114b26b12SCalvin Johnson 		return true;
37214b26b12SCalvin Johnson 
37314b26b12SCalvin Johnson 	/* Old binding */
37414b26b12SCalvin Johnson 	if (of_get_property(np, "fixed-link", &len) &&
37514b26b12SCalvin Johnson 	    len == (5 * sizeof(__be32)))
37614b26b12SCalvin Johnson 		return true;
37714b26b12SCalvin Johnson 
37814b26b12SCalvin Johnson 	return false;
37914b26b12SCalvin Johnson }
38014b26b12SCalvin Johnson EXPORT_SYMBOL(of_phy_is_fixed_link);
38114b26b12SCalvin Johnson 
of_phy_register_fixed_link(struct device_node * np)38214b26b12SCalvin Johnson int of_phy_register_fixed_link(struct device_node *np)
38314b26b12SCalvin Johnson {
38414b26b12SCalvin Johnson 	struct fixed_phy_status status = {};
38514b26b12SCalvin Johnson 	struct device_node *fixed_link_node;
38614b26b12SCalvin Johnson 	u32 fixed_link_prop[5];
38714b26b12SCalvin Johnson 	const char *managed;
38814b26b12SCalvin Johnson 
38914b26b12SCalvin Johnson 	if (of_property_read_string(np, "managed", &managed) == 0 &&
39014b26b12SCalvin Johnson 	    strcmp(managed, "in-band-status") == 0) {
39114b26b12SCalvin Johnson 		/* status is zeroed, namely its .link member */
39214b26b12SCalvin Johnson 		goto register_phy;
39314b26b12SCalvin Johnson 	}
39414b26b12SCalvin Johnson 
39514b26b12SCalvin Johnson 	/* New binding */
39614b26b12SCalvin Johnson 	fixed_link_node = of_get_child_by_name(np, "fixed-link");
39714b26b12SCalvin Johnson 	if (fixed_link_node) {
39814b26b12SCalvin Johnson 		status.link = 1;
39914b26b12SCalvin Johnson 		status.duplex = of_property_read_bool(fixed_link_node,
40014b26b12SCalvin Johnson 						      "full-duplex");
40114b26b12SCalvin Johnson 		if (of_property_read_u32(fixed_link_node, "speed",
40214b26b12SCalvin Johnson 					 &status.speed)) {
40314b26b12SCalvin Johnson 			of_node_put(fixed_link_node);
40414b26b12SCalvin Johnson 			return -EINVAL;
40514b26b12SCalvin Johnson 		}
40614b26b12SCalvin Johnson 		status.pause = of_property_read_bool(fixed_link_node, "pause");
40714b26b12SCalvin Johnson 		status.asym_pause = of_property_read_bool(fixed_link_node,
40814b26b12SCalvin Johnson 							  "asym-pause");
40914b26b12SCalvin Johnson 		of_node_put(fixed_link_node);
41014b26b12SCalvin Johnson 
41114b26b12SCalvin Johnson 		goto register_phy;
41214b26b12SCalvin Johnson 	}
41314b26b12SCalvin Johnson 
41414b26b12SCalvin Johnson 	/* Old binding */
41514b26b12SCalvin Johnson 	if (of_property_read_u32_array(np, "fixed-link", fixed_link_prop,
41614b26b12SCalvin Johnson 				       ARRAY_SIZE(fixed_link_prop)) == 0) {
41714b26b12SCalvin Johnson 		status.link = 1;
41814b26b12SCalvin Johnson 		status.duplex = fixed_link_prop[1];
41914b26b12SCalvin Johnson 		status.speed  = fixed_link_prop[2];
42014b26b12SCalvin Johnson 		status.pause  = fixed_link_prop[3];
42114b26b12SCalvin Johnson 		status.asym_pause = fixed_link_prop[4];
42214b26b12SCalvin Johnson 		goto register_phy;
42314b26b12SCalvin Johnson 	}
42414b26b12SCalvin Johnson 
42514b26b12SCalvin Johnson 	return -ENODEV;
42614b26b12SCalvin Johnson 
42714b26b12SCalvin Johnson register_phy:
42814b26b12SCalvin Johnson 	return PTR_ERR_OR_ZERO(fixed_phy_register(PHY_POLL, &status, np));
42914b26b12SCalvin Johnson }
43014b26b12SCalvin Johnson EXPORT_SYMBOL(of_phy_register_fixed_link);
43114b26b12SCalvin Johnson 
of_phy_deregister_fixed_link(struct device_node * np)43214b26b12SCalvin Johnson void of_phy_deregister_fixed_link(struct device_node *np)
43314b26b12SCalvin Johnson {
43414b26b12SCalvin Johnson 	struct phy_device *phydev;
43514b26b12SCalvin Johnson 
43614b26b12SCalvin Johnson 	phydev = of_phy_find_device(np);
43714b26b12SCalvin Johnson 	if (!phydev)
43814b26b12SCalvin Johnson 		return;
43914b26b12SCalvin Johnson 
44014b26b12SCalvin Johnson 	fixed_phy_unregister(phydev);
44114b26b12SCalvin Johnson 
44214b26b12SCalvin Johnson 	put_device(&phydev->mdio.dev);	/* of_phy_find_device() */
44314b26b12SCalvin Johnson 	phy_device_free(phydev);	/* fixed_phy_register() */
44414b26b12SCalvin Johnson }
44514b26b12SCalvin Johnson EXPORT_SYMBOL(of_phy_deregister_fixed_link);
446