1 // SPDX-License-Identifier: GPL-2.0+
12 #include <dm/device-internal.h>
13 #include <dm/device_compat.h>
14 #include <dm/uclass-internal.h>
15 #include <linux/compat.h>
17 /* DT node properties for MAC-PHY interface */
18 #define PHY_MODE_STR_CNT 2
19 static const char *phy_mode_str[PHY_MODE_STR_CNT] = { "phy-mode",
20 "phy-connection-type" };
21 /* DT node properties that reference a PHY node */
22 #define PHY_HANDLE_STR_CNT 3
23 const char *phy_handle_str[PHY_HANDLE_STR_CNT] = { "phy-handle",
27 void dm_mdio_probe_devices(void)
32 uclass_get(UCLASS_MDIO, &uc);
33 uclass_foreach_dev(it, uc) {
38 static int dm_mdio_post_bind(struct udevice *dev)
42 /* set a custom name for the MDIO device, if present in DT */
43 if (ofnode_valid(dev->node)) {
44 dt_name = ofnode_read_string(dev->node, "device-name");
46 debug("renaming dev %s to %s\n", dev->name, dt_name);
47 device_set_name(dev, dt_name);
52 * MDIO command doesn't like spaces in names, don't allow them to keep
55 if (strchr(dev->name, ' ')) {
56 debug("\nError: MDIO device name \"%s\" has a space!\n",
65 * Following read/write/reset functions are registered with legacy MII code.
66 * These are called for PHY operations by upper layers and we further call the
67 * DM MDIO driver functions.
69 static int mdio_read(struct mii_dev *mii_bus, int addr, int devad, int reg)
71 struct udevice *dev = mii_bus->priv;
73 return mdio_get_ops(dev)->read(dev, addr, devad, reg);
76 static int mdio_write(struct mii_dev *mii_bus, int addr, int devad, int reg,
79 struct udevice *dev = mii_bus->priv;
81 return mdio_get_ops(dev)->write(dev, addr, devad, reg, val);
84 static int mdio_reset(struct mii_dev *mii_bus)
86 struct udevice *dev = mii_bus->priv;
88 if (mdio_get_ops(dev)->reset)
89 return mdio_get_ops(dev)->reset(dev);
94 static int dm_mdio_post_probe(struct udevice *dev)
96 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
98 pdata->mii_bus = mdio_alloc();
99 pdata->mii_bus->read = mdio_read;
100 pdata->mii_bus->write = mdio_write;
101 pdata->mii_bus->reset = mdio_reset;
102 pdata->mii_bus->priv = dev;
103 strncpy(pdata->mii_bus->name, dev->name, MDIO_NAME_LEN - 1);
105 return mdio_register(pdata->mii_bus);
108 static int dm_mdio_pre_remove(struct udevice *dev)
110 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
111 struct mdio_ops *ops = mdio_get_ops(dev);
115 mdio_unregister(pdata->mii_bus);
116 mdio_free(pdata->mii_bus);
121 struct phy_device *dm_mdio_phy_connect(struct udevice *mdiodev, int phyaddr,
122 struct udevice *ethdev,
123 phy_interface_t interface)
125 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(mdiodev);
127 if (device_probe(mdiodev))
130 return phy_connect(pdata->mii_bus, phyaddr, ethdev, interface);
133 static struct phy_device *dm_eth_connect_phy_handle(struct udevice *ethdev,
134 phy_interface_t interface)
137 struct udevice *mdiodev;
138 struct phy_device *phy;
139 struct ofnode_phandle_args phandle = {.node = ofnode_null()};
142 for (i = 0; i < PHY_HANDLE_STR_CNT; i++)
143 if (!dev_read_phandle_with_args(ethdev, phy_handle_str[i], NULL,
147 if (!ofnode_valid(phandle.node)) {
148 dev_dbg(ethdev, "can't find PHY node\n");
153 * reading 'reg' directly should be fine. This is a PHY node, the
154 * address is always size 1 and requires no translation
156 if (ofnode_read_u32(phandle.node, "reg", &phy_addr)) {
157 dev_dbg(ethdev, "missing reg property in phy node\n");
161 if (uclass_get_device_by_ofnode(UCLASS_MDIO,
162 ofnode_get_parent(phandle.node),
164 dev_dbg(ethdev, "can't find MDIO bus for node %s\n",
165 ofnode_get_name(ofnode_get_parent(phandle.node)));
169 phy = dm_mdio_phy_connect(mdiodev, phy_addr, ethdev, interface);
172 phy->node = phandle.node;
177 /* Connect to a PHY linked in eth DT node */
178 struct phy_device *dm_eth_phy_connect(struct udevice *ethdev)
181 phy_interface_t interface;
182 struct phy_device *phy;
185 if (!ofnode_valid(ethdev->node)) {
186 debug("%s: supplied eth dev has no DT node!\n", ethdev->name);
190 interface = PHY_INTERFACE_MODE_NONE;
191 for (i = 0; i < PHY_MODE_STR_CNT; i++) {
192 if_str = ofnode_read_string(ethdev->node, phy_mode_str[i]);
194 interface = phy_get_interface_by_name(if_str);
199 interface = PHY_INTERFACE_MODE_NONE;
200 if (interface == PHY_INTERFACE_MODE_NONE)
201 dev_dbg(ethdev, "can't find interface mode, default to NONE\n");
203 phy = dm_eth_connect_phy_handle(ethdev, interface);
208 phy->interface = interface;
213 UCLASS_DRIVER(mdio) = {
216 .post_bind = dm_mdio_post_bind,
217 .post_probe = dm_mdio_post_probe,
218 .pre_remove = dm_mdio_pre_remove,
219 .per_device_auto_alloc_size = sizeof(struct mdio_perdev_priv),