1 // SPDX-License-Identifier: GPL-2.0+
10 #include <dm/device-internal.h>
11 #include <dm/uclass-internal.h>
13 void dm_mdio_probe_devices(void)
18 uclass_get(UCLASS_MDIO, &uc);
19 uclass_foreach_dev(it, uc) {
24 static int dm_mdio_post_bind(struct udevice *dev)
27 * MDIO command doesn't like spaces in names, don't allow them to keep
30 if (strchr(dev->name, ' ')) {
31 debug("\nError: MDIO device name \"%s\" has a space!\n",
40 * Following read/write/reset functions are registered with legacy MII code.
41 * These are called for PHY operations by upper layers and we further call the
42 * DM MDIO driver functions.
44 static int mdio_read(struct mii_dev *mii_bus, int addr, int devad, int reg)
46 struct udevice *dev = mii_bus->priv;
48 return mdio_get_ops(dev)->read(dev, addr, devad, reg);
51 static int mdio_write(struct mii_dev *mii_bus, int addr, int devad, int reg,
54 struct udevice *dev = mii_bus->priv;
56 return mdio_get_ops(dev)->write(dev, addr, devad, reg, val);
59 static int mdio_reset(struct mii_dev *mii_bus)
61 struct udevice *dev = mii_bus->priv;
63 if (mdio_get_ops(dev)->reset)
64 return mdio_get_ops(dev)->reset(dev);
69 static int dm_mdio_post_probe(struct udevice *dev)
71 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
73 pdata->mii_bus = mdio_alloc();
74 pdata->mii_bus->read = mdio_read;
75 pdata->mii_bus->write = mdio_write;
76 pdata->mii_bus->reset = mdio_reset;
77 pdata->mii_bus->priv = dev;
78 strncpy(pdata->mii_bus->name, dev->name, MDIO_NAME_LEN - 1);
80 return mdio_register(pdata->mii_bus);
83 static int dm_mdio_pre_remove(struct udevice *dev)
85 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
86 struct mdio_ops *ops = mdio_get_ops(dev);
90 mdio_unregister(pdata->mii_bus);
91 mdio_free(pdata->mii_bus);
96 struct phy_device *dm_mdio_phy_connect(struct udevice *dev, int addr,
97 struct udevice *ethdev,
98 phy_interface_t interface)
100 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
102 if (device_probe(dev))
105 return phy_connect(pdata->mii_bus, addr, ethdev, interface);
108 UCLASS_DRIVER(mdio) = {
111 .post_bind = dm_mdio_post_bind,
112 .post_probe = dm_mdio_post_probe,
113 .pre_remove = dm_mdio_pre_remove,
114 .per_device_auto_alloc_size = sizeof(struct mdio_perdev_priv),