blob: 7abed14392f3c2c08396ffffc0868f4df5589b7e [file] [log] [blame]
Ye Li5fe419e2020-05-03 22:41:14 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2020 NXP
4 */
5
6#include <common.h>
7#include <dm.h>
8#include <net.h>
Patrick Delaunayd33982d2021-07-20 20:09:51 +02009#include <asm-generic/gpio.h>
Ye Li5fe419e2020-05-03 22:41:14 +080010#include <dm/device-internal.h>
11#include <dm/uclass-internal.h>
12#include <dm/lists.h>
Patrick Delaunayd33982d2021-07-20 20:09:51 +020013#include <linux/delay.h>
Ye Li5fe419e2020-05-03 22:41:14 +080014
15struct eth_phy_device_priv {
16 struct mii_dev *mdio_bus;
Patrick Delaunayd33982d2021-07-20 20:09:51 +020017 struct gpio_desc reset_gpio;
18 u32 reset_assert_delay;
19 u32 reset_deassert_delay;
Ye Li5fe419e2020-05-03 22:41:14 +080020};
21
22int eth_phy_binds_nodes(struct udevice *eth_dev)
23{
24 ofnode mdio_node, phy_node;
25 const char *node_name;
26 int ret;
27
28 mdio_node = dev_read_subnode(eth_dev, "mdio");
29 if (!ofnode_valid(mdio_node)) {
30 debug("%s: %s mdio subnode not found!", __func__,
31 eth_dev->name);
32 return -ENXIO;
33 }
34
35 ofnode_for_each_subnode(phy_node, mdio_node) {
36 node_name = ofnode_get_name(phy_node);
37
38 debug("* Found child node: '%s'\n", node_name);
39
40 ret = device_bind_driver_to_node(eth_dev,
41 "eth_phy_generic_drv",
42 node_name, phy_node, NULL);
43 if (ret) {
44 debug(" - Eth phy binding error: %d\n", ret);
45 continue;
46 }
47
48 debug(" - bound phy device: '%s'\n", node_name);
49 }
50
51 return 0;
52}
53
54int eth_phy_set_mdio_bus(struct udevice *eth_dev, struct mii_dev *mdio_bus)
55{
56 struct udevice *dev;
57 struct eth_phy_device_priv *uc_priv;
58
59 for (uclass_first_device(UCLASS_ETH_PHY, &dev); dev;
60 uclass_next_device(&dev)) {
61 if (dev->parent == eth_dev) {
Simon Glass0fd3d912020-12-22 19:30:28 -070062 uc_priv = (struct eth_phy_device_priv *)(dev_get_uclass_priv(dev));
Ye Li5fe419e2020-05-03 22:41:14 +080063
64 if (!uc_priv->mdio_bus)
65 uc_priv->mdio_bus = mdio_bus;
66 }
67 }
68
69 return 0;
70}
71
72struct mii_dev *eth_phy_get_mdio_bus(struct udevice *eth_dev)
73{
74 int ret;
75 struct udevice *phy_dev;
76 struct eth_phy_device_priv *uc_priv;
77
78 /* Will probe the parent of phy device, then phy device */
79 ret = uclass_get_device_by_phandle(UCLASS_ETH_PHY, eth_dev,
80 "phy-handle", &phy_dev);
81 if (!ret) {
82 if (eth_dev != phy_dev->parent) {
83 /*
84 * phy_dev is shared and controlled by
85 * other eth controller
86 */
Simon Glass0fd3d912020-12-22 19:30:28 -070087 uc_priv = (struct eth_phy_device_priv *)(dev_get_uclass_priv(phy_dev));
Ye Li5fe419e2020-05-03 22:41:14 +080088 if (uc_priv->mdio_bus)
89 printf("Get shared mii bus on %s\n", eth_dev->name);
90 else
91 printf("Can't get shared mii bus on %s\n", eth_dev->name);
92
93 return uc_priv->mdio_bus;
94 }
95 } else {
96 printf("FEC: can't find phy-handle\n");
97 }
98
99 return NULL;
100}
101
102int eth_phy_get_addr(struct udevice *dev)
103{
104 struct ofnode_phandle_args phandle_args;
105 int reg;
106
107 if (dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
108 &phandle_args)) {
109 debug("Failed to find phy-handle");
110 return -ENODEV;
111 }
112
113 reg = ofnode_read_u32_default(phandle_args.node, "reg", 0);
114
115 return reg;
116}
117
Patrick Delaunayd33982d2021-07-20 20:09:51 +0200118/* parsing generic properties of devicetree/bindings/net/ethernet-phy.yaml */
119static int eth_phy_of_to_plat(struct udevice *dev)
120{
121 struct eth_phy_device_priv *uc_priv = dev_get_uclass_priv(dev);
122 int ret;
123
124 if (!CONFIG_IS_ENABLED(DM_GPIO))
125 return 0;
126
127 /* search "reset-gpios" in phy node */
128 ret = gpio_request_by_name(dev, "reset-gpios", 0,
129 &uc_priv->reset_gpio,
130 GPIOD_IS_OUT);
131 if (ret != -ENOENT)
132 return ret;
133
134 uc_priv->reset_assert_delay = dev_read_u32_default(dev, "reset-assert-us", 0);
135 uc_priv->reset_deassert_delay = dev_read_u32_default(dev, "reset-deassert-us", 0);
136
137 return 0;
138}
139
140void eth_phy_reset(struct udevice *dev, int value)
141{
142 struct eth_phy_device_priv *uc_priv = dev_get_uclass_priv(dev);
143 u32 delay;
144
145 if (!CONFIG_IS_ENABLED(DM_GPIO))
146 return;
147
148 if (!dm_gpio_is_valid(&uc_priv->reset_gpio))
149 return;
150
151 dm_gpio_set_value(&uc_priv->reset_gpio, value);
152
153 delay = value ? uc_priv->reset_assert_delay : uc_priv->reset_deassert_delay;
154 if (delay)
155 udelay(delay);
156}
157
158static int eth_phy_pre_probe(struct udevice *dev)
159{
160 /* Assert and deassert the reset signal */
161 eth_phy_reset(dev, 1);
162 eth_phy_reset(dev, 0);
163
164 return 0;
165}
166
Ye Li5fe419e2020-05-03 22:41:14 +0800167UCLASS_DRIVER(eth_phy_generic) = {
168 .id = UCLASS_ETH_PHY,
169 .name = "eth_phy_generic",
Simon Glass41575d82020-12-03 16:55:17 -0700170 .per_device_auto = sizeof(struct eth_phy_device_priv),
Patrick Delaunayd33982d2021-07-20 20:09:51 +0200171 .pre_probe = eth_phy_pre_probe,
Ye Li5fe419e2020-05-03 22:41:14 +0800172};
173
174U_BOOT_DRIVER(eth_phy_generic_drv) = {
175 .name = "eth_phy_generic_drv",
176 .id = UCLASS_ETH_PHY,
Patrick Delaunayd33982d2021-07-20 20:09:51 +0200177 .of_to_plat = eth_phy_of_to_plat,
Ye Li5fe419e2020-05-03 22:41:14 +0800178};