1 // SPDX-License-Identifier: GPL-2.0+
11 #include <dm/device-internal.h>
12 #include <dm/device_compat.h>
13 #include <dm/uclass-internal.h>
14 #include <linux/compat.h>
16 /* DT node properties for MAC-PHY interface */
17 #define PHY_MODE_STR_CNT 2
18 static const char *phy_mode_str[PHY_MODE_STR_CNT] = { "phy-mode",
19 "phy-connection-type" };
20 /* DT node properties that reference a PHY node */
21 #define PHY_HANDLE_STR_CNT 3
22 const char *phy_handle_str[PHY_HANDLE_STR_CNT] = { "phy-handle",
26 void dm_mdio_probe_devices(void)
31 uclass_get(UCLASS_MDIO, &uc);
32 uclass_foreach_dev(it, uc) {
37 static int dm_mdio_post_bind(struct udevice *dev)
41 /* set a custom name for the MDIO device, if present in DT */
42 if (ofnode_valid(dev->node)) {
43 dt_name = ofnode_read_string(dev->node, "device-name");
45 debug("renaming dev %s to %s\n", dev->name, dt_name);
46 device_set_name(dev, dt_name);
51 * MDIO command doesn't like spaces in names, don't allow them to keep
54 if (strchr(dev->name, ' ')) {
55 debug("\nError: MDIO device name \"%s\" has a space!\n",
64 * Following read/write/reset functions are registered with legacy MII code.
65 * These are called for PHY operations by upper layers and we further call the
66 * DM MDIO driver functions.
68 static int mdio_read(struct mii_dev *mii_bus, int addr, int devad, int reg)
70 struct udevice *dev = mii_bus->priv;
72 return mdio_get_ops(dev)->read(dev, addr, devad, reg);
75 static int mdio_write(struct mii_dev *mii_bus, int addr, int devad, int reg,
78 struct udevice *dev = mii_bus->priv;
80 return mdio_get_ops(dev)->write(dev, addr, devad, reg, val);
83 static int mdio_reset(struct mii_dev *mii_bus)
85 struct udevice *dev = mii_bus->priv;
87 if (mdio_get_ops(dev)->reset)
88 return mdio_get_ops(dev)->reset(dev);
93 static int dm_mdio_post_probe(struct udevice *dev)
95 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
97 pdata->mii_bus = mdio_alloc();
98 pdata->mii_bus->read = mdio_read;
99 pdata->mii_bus->write = mdio_write;
100 pdata->mii_bus->reset = mdio_reset;
101 pdata->mii_bus->priv = dev;
102 strncpy(pdata->mii_bus->name, dev->name, MDIO_NAME_LEN - 1);
104 return mdio_register(pdata->mii_bus);
107 static int dm_mdio_pre_remove(struct udevice *dev)
109 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
110 struct mdio_ops *ops = mdio_get_ops(dev);
114 mdio_unregister(pdata->mii_bus);
115 mdio_free(pdata->mii_bus);
120 struct phy_device *dm_mdio_phy_connect(struct udevice *mdiodev, int phyaddr,
121 struct udevice *ethdev,
122 phy_interface_t interface)
124 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(mdiodev);
126 if (device_probe(mdiodev))
129 return phy_connect(pdata->mii_bus, phyaddr, ethdev, interface);
132 static struct phy_device *dm_eth_connect_phy_handle(struct udevice *ethdev,
133 phy_interface_t interface)
136 struct udevice *mdiodev;
137 struct phy_device *phy;
138 struct ofnode_phandle_args phandle = {.node = ofnode_null()};
141 for (i = 0; i < PHY_HANDLE_STR_CNT; i++)
142 if (!dev_read_phandle_with_args(ethdev, phy_handle_str[i], NULL,
146 if (!ofnode_valid(phandle.node)) {
147 dev_dbg(dev, "can't find PHY node\n");
152 * reading 'reg' directly should be fine. This is a PHY node, the
153 * address is always size 1 and requires no translation
155 if (ofnode_read_u32(phandle.node, "reg", &phy_addr)) {
156 dev_dbg(ethdev, "missing reg property in phy node\n");
160 if (uclass_get_device_by_ofnode(UCLASS_MDIO,
161 ofnode_get_parent(phandle.node),
163 dev_dbg(dev, "can't find MDIO bus for node %s\n",
164 ofnode_get_name(ofnode_get_parent(phandle.node)));
168 phy = dm_mdio_phy_connect(mdiodev, phy_addr, ethdev, interface);
171 phy->node = phandle.node;
176 /* Connect to a PHY linked in eth DT node */
177 struct phy_device *dm_eth_phy_connect(struct udevice *ethdev)
180 phy_interface_t interface;
181 struct phy_device *phy;
184 if (!ofnode_valid(ethdev->node)) {
185 debug("%s: supplied eth dev has no DT node!\n", ethdev->name);
189 interface = PHY_INTERFACE_MODE_NONE;
190 for (i = 0; i < PHY_MODE_STR_CNT; i++) {
191 if_str = ofnode_read_string(ethdev->node, phy_mode_str[i]);
193 interface = phy_get_interface_by_name(if_str);
198 interface = PHY_INTERFACE_MODE_NONE;
199 if (interface == PHY_INTERFACE_MODE_NONE)
200 dev_dbg(ethdev, "can't find interface mode, default to NONE\n");
202 phy = dm_eth_connect_phy_handle(ethdev, interface);
207 phy->interface = interface;
212 UCLASS_DRIVER(mdio) = {
215 .post_bind = dm_mdio_post_bind,
216 .post_probe = dm_mdio_post_probe,
217 .pre_remove = dm_mdio_pre_remove,
218 .per_device_auto_alloc_size = sizeof(struct mdio_perdev_priv),