1 // SPDX-License-Identifier: GPL-2.0+
6 #define LOG_CATEGORY UCLASS_ETH_PHY
12 #include <asm-generic/gpio.h>
13 #include <dm/device_compat.h>
14 #include <dm/device-internal.h>
15 #include <dm/uclass-internal.h>
17 #include <linux/delay.h>
19 struct eth_phy_device_priv {
20 struct mii_dev *mdio_bus;
21 struct gpio_desc reset_gpio;
22 u32 reset_assert_delay;
23 u32 reset_deassert_delay;
26 int eth_phy_binds_nodes(struct udevice *eth_dev)
28 ofnode mdio_node, phy_node;
29 const char *node_name;
32 /* search a subnode named "mdio.*" */
33 dev_for_each_subnode(mdio_node, eth_dev) {
34 node_name = ofnode_get_name(mdio_node);
35 if (!strncmp(node_name, "mdio", 4))
38 if (!ofnode_valid(mdio_node)) {
39 dev_dbg(eth_dev, "%s: %s mdio subnode not found!\n", __func__,
43 dev_dbg(eth_dev, "%s: %s subnode found!\n", __func__, node_name);
45 ofnode_for_each_subnode(phy_node, mdio_node) {
46 node_name = ofnode_get_name(phy_node);
48 dev_dbg(eth_dev, "* Found child node: '%s'\n", node_name);
50 ret = device_bind_driver_to_node(eth_dev,
51 "eth_phy_generic_drv",
52 node_name, phy_node, NULL);
54 dev_dbg(eth_dev, " - Eth phy binding error: %d\n", ret);
58 dev_dbg(eth_dev, " - bound phy device: '%s'\n", node_name);
64 int eth_phy_set_mdio_bus(struct udevice *eth_dev, struct mii_dev *mdio_bus)
67 struct eth_phy_device_priv *uc_priv;
69 for (uclass_first_device(UCLASS_ETH_PHY, &dev); dev;
70 uclass_next_device(&dev)) {
71 if (dev->parent == eth_dev) {
72 uc_priv = (struct eth_phy_device_priv *)(dev_get_uclass_priv(dev));
74 if (!uc_priv->mdio_bus)
75 uc_priv->mdio_bus = mdio_bus;
82 struct mii_dev *eth_phy_get_mdio_bus(struct udevice *eth_dev)
85 struct udevice *phy_dev;
86 struct eth_phy_device_priv *uc_priv;
88 /* Will probe the parent of phy device, then phy device */
89 ret = uclass_get_device_by_phandle(UCLASS_ETH_PHY, eth_dev,
90 "phy-handle", &phy_dev);
92 if (eth_dev != phy_dev->parent) {
94 * phy_dev is shared and controlled by
95 * other eth controller
97 uc_priv = (struct eth_phy_device_priv *)(dev_get_uclass_priv(phy_dev));
98 if (uc_priv->mdio_bus)
99 log_notice("Get shared mii bus on %s\n", eth_dev->name);
101 log_notice("Can't get shared mii bus on %s\n", eth_dev->name);
103 return uc_priv->mdio_bus;
106 log_debug("Can't find phy-handle for %s\n", eth_dev->name);
112 int eth_phy_get_addr(struct udevice *dev)
114 struct ofnode_phandle_args phandle_args;
117 if (dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
119 dev_dbg(dev, "Failed to find phy-handle");
123 reg = ofnode_read_u32_default(phandle_args.node, "reg", 0);
128 /* parsing generic properties of devicetree/bindings/net/ethernet-phy.yaml */
129 static int eth_phy_of_to_plat(struct udevice *dev)
131 struct eth_phy_device_priv *uc_priv = dev_get_uclass_priv(dev);
134 if (!CONFIG_IS_ENABLED(DM_GPIO))
137 /* search "reset-gpios" in phy node */
138 ret = gpio_request_by_name(dev, "reset-gpios", 0,
139 &uc_priv->reset_gpio,
141 if (ret && ret != -ENOENT)
144 uc_priv->reset_assert_delay = dev_read_u32_default(dev, "reset-assert-us", 0);
145 uc_priv->reset_deassert_delay = dev_read_u32_default(dev, "reset-deassert-us", 0);
150 void eth_phy_reset(struct udevice *dev, int value)
152 struct eth_phy_device_priv *uc_priv = dev_get_uclass_priv(dev);
155 if (!CONFIG_IS_ENABLED(DM_GPIO))
158 if (!dm_gpio_is_valid(&uc_priv->reset_gpio))
161 dm_gpio_set_value(&uc_priv->reset_gpio, value);
163 delay = value ? uc_priv->reset_assert_delay : uc_priv->reset_deassert_delay;
168 static int eth_phy_pre_probe(struct udevice *dev)
170 /* Assert and deassert the reset signal */
171 eth_phy_reset(dev, 1);
172 eth_phy_reset(dev, 0);
177 UCLASS_DRIVER(eth_phy_generic) = {
178 .id = UCLASS_ETH_PHY,
179 .name = "eth_phy_generic",
180 .per_device_auto = sizeof(struct eth_phy_device_priv),
181 .pre_probe = eth_phy_pre_probe,
184 U_BOOT_DRIVER(eth_phy_generic_drv) = {
185 .name = "eth_phy_generic_drv",
186 .id = UCLASS_ETH_PHY,
187 .of_to_plat = eth_phy_of_to_plat,