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 /* DT node properties for MAC-PHY interface */
19 static const char * const phy_mode_str[] = {
20 "phy-mode", "phy-connection-type"
23 void dm_mdio_probe_devices(void)
28 uclass_get(UCLASS_MDIO, &uc);
29 uclass_foreach_dev(it, uc) {
34 static int dm_mdio_post_bind(struct udevice *dev)
38 /* set a custom name for the MDIO device, if present in DT */
39 if (dev_has_ofnode(dev)) {
40 dt_name = dev_read_string(dev, "device-name");
42 debug("renaming dev %s to %s\n", dev->name, dt_name);
43 device_set_name(dev, dt_name);
48 * MDIO command doesn't like spaces in names, don't allow them to keep
51 if (strchr(dev->name, ' ')) {
52 debug("\nError: MDIO device name \"%s\" has a space!\n",
61 * Following read/write/reset functions are registered with legacy MII code.
62 * These are called for PHY operations by upper layers and we further call the
63 * DM MDIO driver functions.
65 static int mdio_read(struct mii_dev *mii_bus, int addr, int devad, int reg)
67 struct udevice *dev = mii_bus->priv;
69 return mdio_get_ops(dev)->read(dev, addr, devad, reg);
72 static int mdio_write(struct mii_dev *mii_bus, int addr, int devad, int reg,
75 struct udevice *dev = mii_bus->priv;
77 return mdio_get_ops(dev)->write(dev, addr, devad, reg, val);
80 static int mdio_reset(struct mii_dev *mii_bus)
82 struct udevice *dev = mii_bus->priv;
84 if (mdio_get_ops(dev)->reset)
85 return mdio_get_ops(dev)->reset(dev);
90 static int dm_mdio_post_probe(struct udevice *dev)
92 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
94 pdata->mii_bus = mdio_alloc();
95 pdata->mii_bus->read = mdio_read;
96 pdata->mii_bus->write = mdio_write;
97 pdata->mii_bus->reset = mdio_reset;
98 pdata->mii_bus->priv = dev;
99 strlcpy(pdata->mii_bus->name, dev->name, MDIO_NAME_LEN);
101 return mdio_register(pdata->mii_bus);
104 static int dm_mdio_pre_remove(struct udevice *dev)
106 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
107 struct mdio_ops *ops = mdio_get_ops(dev);
111 mdio_unregister(pdata->mii_bus);
112 mdio_free(pdata->mii_bus);
117 struct phy_device *dm_mdio_phy_connect(struct udevice *mdiodev, int phyaddr,
118 struct udevice *ethdev,
119 phy_interface_t interface)
121 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(mdiodev);
123 if (device_probe(mdiodev))
126 return phy_connect(pdata->mii_bus, phyaddr, ethdev, interface);
129 static struct phy_device *dm_eth_connect_phy_handle(struct udevice *ethdev,
130 phy_interface_t interface)
133 struct udevice *mdiodev;
134 struct phy_device *phy;
137 if (CONFIG_IS_ENABLED(PHY_FIXED) &&
138 ofnode_phy_is_fixed_link(dev_ofnode(ethdev), &phynode)) {
139 phy = phy_connect(NULL, 0, ethdev, interface);
143 phynode = dev_get_phy_node(ethdev);
144 if (!ofnode_valid(phynode)) {
145 dev_dbg(ethdev, "can't find PHY node\n");
150 * reading 'reg' directly should be fine. This is a PHY node, the
151 * address is always size 1 and requires no translation
153 if (ofnode_read_u32(phynode, "reg", &phy_addr)) {
154 dev_dbg(ethdev, "missing reg property in phy node\n");
158 if (uclass_get_device_by_ofnode(UCLASS_MDIO,
159 ofnode_get_parent(phynode),
161 dev_dbg(ethdev, "can't find MDIO bus for node %s\n",
162 ofnode_get_name(ofnode_get_parent(phynode)));
166 phy = dm_mdio_phy_connect(mdiodev, phy_addr, ethdev, interface);
175 /* Connect to a PHY linked in eth DT node */
176 struct phy_device *dm_eth_phy_connect(struct udevice *ethdev)
179 phy_interface_t interface;
180 struct phy_device *phy;
183 if (!dev_has_ofnode(ethdev)) {
184 debug("%s: supplied eth dev has no DT node!\n", ethdev->name);
188 interface = PHY_INTERFACE_MODE_NONE;
189 for (i = 0; i < ARRAY_SIZE(phy_mode_str); i++) {
190 if_str = dev_read_string(ethdev, phy_mode_str[i]);
192 interface = phy_get_interface_by_name(if_str);
197 interface = PHY_INTERFACE_MODE_NONE;
198 if (interface == PHY_INTERFACE_MODE_NONE)
199 dev_dbg(ethdev, "can't find interface mode, default to NONE\n");
201 phy = dm_eth_connect_phy_handle(ethdev, interface);
206 phy->interface = interface;
211 UCLASS_DRIVER(mdio) = {
214 .post_bind = dm_mdio_post_bind,
215 .post_probe = dm_mdio_post_probe,
216 .pre_remove = dm_mdio_pre_remove,
217 .per_device_auto = sizeof(struct mdio_perdev_priv),