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)
28 /* set a custom name for the MDIO device, if present in DT */
29 if (ofnode_valid(dev->node)) {
30 dt_name = ofnode_read_string(dev->node, "device-name");
32 debug("renaming dev %s to %s\n", dev->name, dt_name);
33 device_set_name(dev, dt_name);
38 * MDIO command doesn't like spaces in names, don't allow them to keep
41 if (strchr(dev->name, ' ')) {
42 debug("\nError: MDIO device name \"%s\" has a space!\n",
51 * Following read/write/reset functions are registered with legacy MII code.
52 * These are called for PHY operations by upper layers and we further call the
53 * DM MDIO driver functions.
55 static int mdio_read(struct mii_dev *mii_bus, int addr, int devad, int reg)
57 struct udevice *dev = mii_bus->priv;
59 return mdio_get_ops(dev)->read(dev, addr, devad, reg);
62 static int mdio_write(struct mii_dev *mii_bus, int addr, int devad, int reg,
65 struct udevice *dev = mii_bus->priv;
67 return mdio_get_ops(dev)->write(dev, addr, devad, reg, val);
70 static int mdio_reset(struct mii_dev *mii_bus)
72 struct udevice *dev = mii_bus->priv;
74 if (mdio_get_ops(dev)->reset)
75 return mdio_get_ops(dev)->reset(dev);
80 static int dm_mdio_post_probe(struct udevice *dev)
82 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
84 pdata->mii_bus = mdio_alloc();
85 pdata->mii_bus->read = mdio_read;
86 pdata->mii_bus->write = mdio_write;
87 pdata->mii_bus->reset = mdio_reset;
88 pdata->mii_bus->priv = dev;
89 strncpy(pdata->mii_bus->name, dev->name, MDIO_NAME_LEN - 1);
91 return mdio_register(pdata->mii_bus);
94 static int dm_mdio_pre_remove(struct udevice *dev)
96 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
97 struct mdio_ops *ops = mdio_get_ops(dev);
101 mdio_unregister(pdata->mii_bus);
102 mdio_free(pdata->mii_bus);
107 struct phy_device *dm_mdio_phy_connect(struct udevice *dev, int addr,
108 struct udevice *ethdev,
109 phy_interface_t interface)
111 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
113 if (device_probe(dev))
116 return phy_connect(pdata->mii_bus, addr, ethdev, interface);
119 UCLASS_DRIVER(mdio) = {
122 .post_bind = dm_mdio_post_bind,
123 .post_probe = dm_mdio_post_probe,
124 .pre_remove = dm_mdio_pre_remove,
125 .per_device_auto_alloc_size = sizeof(struct mdio_perdev_priv),