1 // SPDX-License-Identifier: GPL-2.0+
13 #include <dm/device-internal.h>
14 #include <dm/device_compat.h>
15 #include <dm/of_extra.h>
16 #include <dm/uclass-internal.h>
17 #include <linux/compat.h>
18 #include <linux/delay.h>
20 #define DEFAULT_GPIO_RESET_DELAY 10 /* in microseconds */
22 void dm_mdio_probe_devices(void)
27 uclass_get(UCLASS_MDIO, &uc);
28 uclass_foreach_dev(it, uc) {
33 static int dm_mdio_post_bind(struct udevice *dev)
37 /* set a custom name for the MDIO device, if present in DT */
38 if (dev_has_ofnode(dev)) {
39 dt_name = dev_read_string(dev, "device-name");
41 debug("renaming dev %s to %s\n", dev->name, dt_name);
42 device_set_name(dev, dt_name);
47 * MDIO command doesn't like spaces in names, don't allow them to keep
50 if (strchr(dev->name, ' ')) {
51 debug("\nError: MDIO device name \"%s\" has a space!\n",
56 #if CONFIG_IS_ENABLED(OF_REAL)
57 return dm_scan_fdt_dev(dev);
63 int dm_mdio_read(struct udevice *mdio_dev, int addr, int devad, int reg)
65 struct mdio_ops *ops = mdio_get_ops(mdio_dev);
70 return ops->read(mdio_dev, addr, devad, reg);
73 int dm_mdio_write(struct udevice *mdio_dev, int addr, int devad, int reg,
76 struct mdio_ops *ops = mdio_get_ops(mdio_dev);
81 return ops->write(mdio_dev, addr, devad, reg, val);
84 int dm_mdio_reset(struct udevice *mdio_dev)
86 struct mdio_ops *ops = mdio_get_ops(mdio_dev);
87 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(mdio_dev);
88 struct mii_dev *mii_bus = pdata->mii_bus;
90 if (CONFIG_IS_ENABLED(DM_GPIO) && dm_gpio_is_valid(&mii_bus->reset_gpiod)) {
91 dm_gpio_set_value(&mii_bus->reset_gpiod, 1);
92 udelay(mii_bus->reset_delay_us);
93 dm_gpio_set_value(&mii_bus->reset_gpiod, 0);
94 if (mii_bus->reset_post_delay_us > 0)
95 udelay(mii_bus->reset_post_delay_us);
101 return ops->reset(mdio_dev);
105 * Following read/write/reset functions are registered with legacy MII code.
106 * These are called for PHY operations by upper layers and we further call the
107 * DM MDIO driver functions.
109 static int mdio_read(struct mii_dev *mii_bus, int addr, int devad, int reg)
111 return dm_mdio_read(mii_bus->priv, addr, devad, reg);
114 static int mdio_write(struct mii_dev *mii_bus, int addr, int devad, int reg,
117 return dm_mdio_write(mii_bus->priv, addr, devad, reg, val);
120 static int mdio_reset(struct mii_dev *mii_bus)
122 return dm_mdio_reset(mii_bus->priv);
125 static int mdio_bind_phy_nodes(struct udevice *mdio_dev)
127 ofnode mdio_node, phy_node;
128 struct udevice *phy_dev;
129 const char *node_name;
132 mdio_node = dev_ofnode(mdio_dev);
133 if (!ofnode_valid(mdio_node)) {
134 dev_dbg(mdio_dev, "invalid ofnode for mdio_dev\n");
138 ofnode_for_each_subnode(phy_node, mdio_node) {
139 node_name = ofnode_get_name(phy_node);
140 dev_dbg(mdio_dev, "* Found child node: '%s'\n", node_name);
141 ret = device_bind_driver_to_node(mdio_dev,
142 "eth_phy_generic_drv",
143 node_name, phy_node, &phy_dev);
145 dev_dbg(mdio_dev, " - Eth phy binding error: %d\n", ret);
149 dev_dbg(mdio_dev, " - bound phy device: '%s'\n", node_name);
150 ret = device_probe(phy_dev);
152 dev_dbg(mdio_dev, "Device '%s' probe failed\n", phy_dev->name);
153 device_unbind(phy_dev);
161 static int dm_mdio_post_probe(struct udevice *dev)
163 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
164 struct mii_dev *mii_bus;
167 mii_bus = mdio_alloc();
169 dev_err(dev, "couldn't allocate mii_bus\n");
172 pdata->mii_bus = mii_bus;
173 pdata->mii_bus->read = mdio_read;
174 pdata->mii_bus->write = mdio_write;
175 pdata->mii_bus->reset = mdio_reset;
176 pdata->mii_bus->priv = dev;
177 strlcpy(pdata->mii_bus->name, dev->name, MDIO_NAME_LEN);
179 if (IS_ENABLED(CONFIG_DM_GPIO)) {
180 /* Get bus level PHY reset GPIO details */
181 mii_bus->reset_delay_us = dev_read_u32_default(dev, "reset-delay-us",
182 DEFAULT_GPIO_RESET_DELAY);
183 mii_bus->reset_post_delay_us = dev_read_u32_default(dev,
184 "reset-post-delay-us",
186 ret = gpio_request_by_name(dev, "reset-gpios", 0, &mii_bus->reset_gpiod,
187 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
188 if (ret && ret != -ENOENT) {
189 dev_err(dev, "couldn't get reset-gpios: %d\n", ret);
194 if (CONFIG_IS_ENABLED(DM_ETH_PHY))
195 mdio_bind_phy_nodes(dev);
197 return mdio_register(pdata->mii_bus);
200 static int dm_mdio_pre_remove(struct udevice *dev)
202 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
205 mdio_unregister(pdata->mii_bus);
206 mdio_free(pdata->mii_bus);
211 struct phy_device *dm_phy_find_by_ofnode(ofnode phynode)
213 struct mdio_perdev_priv *pdata;
214 struct udevice *mdiodev;
217 if (ofnode_read_u32(phynode, "reg", &phy_addr))
220 if (uclass_get_device_by_ofnode(UCLASS_MDIO,
221 ofnode_get_parent(phynode),
225 if (device_probe(mdiodev))
228 pdata = dev_get_uclass_priv(mdiodev);
230 return phy_find_by_mask(pdata->mii_bus, BIT(phy_addr));
233 struct phy_device *dm_mdio_phy_connect(struct udevice *mdiodev, int phyaddr,
234 struct udevice *ethdev,
235 phy_interface_t interface)
237 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(mdiodev);
239 if (device_probe(mdiodev))
242 return phy_connect(pdata->mii_bus, phyaddr, ethdev, interface);
245 static struct phy_device *dm_eth_connect_phy_handle(struct udevice *ethdev,
246 phy_interface_t interface)
249 struct udevice *mdiodev;
250 struct phy_device *phy;
253 if (IS_ENABLED(CONFIG_PHY_FIXED) &&
254 ofnode_phy_is_fixed_link(dev_ofnode(ethdev), &phynode)) {
255 phy = phy_connect(NULL, 0, ethdev, interface);
259 phynode = dev_get_phy_node(ethdev);
260 if (!ofnode_valid(phynode)) {
261 dev_dbg(ethdev, "can't find PHY node\n");
266 * reading 'reg' directly should be fine. This is a PHY node, the
267 * address is always size 1 and requires no translation
269 if (ofnode_read_u32(phynode, "reg", &phy_addr)) {
270 dev_dbg(ethdev, "missing reg property in phy node\n");
274 if (uclass_get_device_by_ofnode(UCLASS_MDIO,
275 ofnode_get_parent(phynode),
277 dev_dbg(ethdev, "can't find MDIO bus for node %s\n",
278 ofnode_get_name(ofnode_get_parent(phynode)));
282 phy = dm_mdio_phy_connect(mdiodev, phy_addr, ethdev, interface);
291 /* Connect to a PHY linked in eth DT node */
292 struct phy_device *dm_eth_phy_connect(struct udevice *ethdev)
294 phy_interface_t interface;
295 struct phy_device *phy;
297 if (!dev_has_ofnode(ethdev)) {
298 debug("%s: supplied eth dev has no DT node!\n", ethdev->name);
302 interface = dev_read_phy_mode(ethdev);
303 if (interface == PHY_INTERFACE_MODE_NA)
304 dev_dbg(ethdev, "can't find interface mode, default to NA\n");
306 phy = dm_eth_connect_phy_handle(ethdev, interface);
311 phy->interface = interface;
316 UCLASS_DRIVER(mdio) = {
319 .post_bind = dm_mdio_post_bind,
320 .post_probe = dm_mdio_post_probe,
321 .pre_remove = dm_mdio_pre_remove,
322 .per_device_auto = sizeof(struct mdio_perdev_priv),