1 // SPDX-License-Identifier: GPL-2.0+
12 #include <dm/device-internal.h>
13 #include <dm/device_compat.h>
14 #include <dm/of_extra.h>
15 #include <dm/uclass-internal.h>
16 #include <linux/compat.h>
18 void dm_mdio_probe_devices(void)
23 uclass_get(UCLASS_MDIO, &uc);
24 uclass_foreach_dev(it, uc) {
29 static int dm_mdio_post_bind(struct udevice *dev)
33 /* set a custom name for the MDIO device, if present in DT */
34 if (dev_has_ofnode(dev)) {
35 dt_name = dev_read_string(dev, "device-name");
37 debug("renaming dev %s to %s\n", dev->name, dt_name);
38 device_set_name(dev, dt_name);
43 * MDIO command doesn't like spaces in names, don't allow them to keep
46 if (strchr(dev->name, ' ')) {
47 debug("\nError: MDIO device name \"%s\" has a space!\n",
55 int dm_mdio_read(struct udevice *mdio_dev, int addr, int devad, int reg)
57 struct mdio_ops *ops = mdio_get_ops(mdio_dev);
62 return ops->read(mdio_dev, addr, devad, reg);
65 int dm_mdio_write(struct udevice *mdio_dev, int addr, int devad, int reg,
68 struct mdio_ops *ops = mdio_get_ops(mdio_dev);
73 return ops->write(mdio_dev, addr, devad, reg, val);
76 int dm_mdio_reset(struct udevice *mdio_dev)
78 struct mdio_ops *ops = mdio_get_ops(mdio_dev);
83 return ops->reset(mdio_dev);
87 * Following read/write/reset functions are registered with legacy MII code.
88 * These are called for PHY operations by upper layers and we further call the
89 * DM MDIO driver functions.
91 static int mdio_read(struct mii_dev *mii_bus, int addr, int devad, int reg)
93 return dm_mdio_read(mii_bus->priv, addr, devad, reg);
96 static int mdio_write(struct mii_dev *mii_bus, int addr, int devad, int reg,
99 return dm_mdio_write(mii_bus->priv, addr, devad, reg, val);
102 static int mdio_reset(struct mii_dev *mii_bus)
104 return dm_mdio_reset(mii_bus->priv);
107 static int dm_mdio_post_probe(struct udevice *dev)
109 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
111 pdata->mii_bus = mdio_alloc();
112 pdata->mii_bus->read = mdio_read;
113 pdata->mii_bus->write = mdio_write;
114 pdata->mii_bus->reset = mdio_reset;
115 pdata->mii_bus->priv = dev;
116 strlcpy(pdata->mii_bus->name, dev->name, MDIO_NAME_LEN);
118 return mdio_register(pdata->mii_bus);
121 static int dm_mdio_pre_remove(struct udevice *dev)
123 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
126 mdio_unregister(pdata->mii_bus);
127 mdio_free(pdata->mii_bus);
132 struct phy_device *dm_phy_find_by_ofnode(ofnode phynode)
134 struct mdio_perdev_priv *pdata;
135 struct udevice *mdiodev;
138 if (ofnode_read_u32(phynode, "reg", &phy_addr))
141 if (uclass_get_device_by_ofnode(UCLASS_MDIO,
142 ofnode_get_parent(phynode),
146 if (device_probe(mdiodev))
149 pdata = dev_get_uclass_priv(mdiodev);
151 return phy_find_by_mask(pdata->mii_bus, BIT(phy_addr));
154 struct phy_device *dm_mdio_phy_connect(struct udevice *mdiodev, int phyaddr,
155 struct udevice *ethdev,
156 phy_interface_t interface)
158 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(mdiodev);
160 if (device_probe(mdiodev))
163 return phy_connect(pdata->mii_bus, phyaddr, ethdev, interface);
166 static struct phy_device *dm_eth_connect_phy_handle(struct udevice *ethdev,
167 phy_interface_t interface)
170 struct udevice *mdiodev;
171 struct phy_device *phy;
174 if (CONFIG_IS_ENABLED(PHY_FIXED) &&
175 ofnode_phy_is_fixed_link(dev_ofnode(ethdev), &phynode)) {
176 phy = phy_connect(NULL, 0, ethdev, interface);
180 phynode = dev_get_phy_node(ethdev);
181 if (!ofnode_valid(phynode)) {
182 dev_dbg(ethdev, "can't find PHY node\n");
187 * reading 'reg' directly should be fine. This is a PHY node, the
188 * address is always size 1 and requires no translation
190 if (ofnode_read_u32(phynode, "reg", &phy_addr)) {
191 dev_dbg(ethdev, "missing reg property in phy node\n");
195 if (uclass_get_device_by_ofnode(UCLASS_MDIO,
196 ofnode_get_parent(phynode),
198 dev_dbg(ethdev, "can't find MDIO bus for node %s\n",
199 ofnode_get_name(ofnode_get_parent(phynode)));
203 phy = dm_mdio_phy_connect(mdiodev, phy_addr, ethdev, interface);
212 /* Connect to a PHY linked in eth DT node */
213 struct phy_device *dm_eth_phy_connect(struct udevice *ethdev)
215 phy_interface_t interface;
216 struct phy_device *phy;
218 if (!dev_has_ofnode(ethdev)) {
219 debug("%s: supplied eth dev has no DT node!\n", ethdev->name);
223 interface = dev_read_phy_mode(ethdev);
224 if (interface == PHY_INTERFACE_MODE_NA)
225 dev_dbg(ethdev, "can't find interface mode, default to NA\n");
227 phy = dm_eth_connect_phy_handle(ethdev, interface);
232 phy->interface = interface;
237 UCLASS_DRIVER(mdio) = {
240 .post_bind = dm_mdio_post_bind,
241 .post_probe = dm_mdio_post_probe,
242 .pre_remove = dm_mdio_pre_remove,
243 .per_device_auto = sizeof(struct mdio_perdev_priv),