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 #define PHY_MODE_STR_CNT 2
20 static const char *phy_mode_str[PHY_MODE_STR_CNT] = { "phy-mode",
21 "phy-connection-type" };
22 /* DT node properties that reference a PHY node */
23 #define PHY_HANDLE_STR_CNT 3
24 const char *phy_handle_str[PHY_HANDLE_STR_CNT] = { "phy-handle",
28 void dm_mdio_probe_devices(void)
33 uclass_get(UCLASS_MDIO, &uc);
34 uclass_foreach_dev(it, uc) {
39 static int dm_mdio_post_bind(struct udevice *dev)
43 /* set a custom name for the MDIO device, if present in DT */
44 if (dev_has_ofnode(dev)) {
45 dt_name = dev_read_string(dev, "device-name");
47 debug("renaming dev %s to %s\n", dev->name, dt_name);
48 device_set_name(dev, dt_name);
53 * MDIO command doesn't like spaces in names, don't allow them to keep
56 if (strchr(dev->name, ' ')) {
57 debug("\nError: MDIO device name \"%s\" has a space!\n",
66 * Following read/write/reset functions are registered with legacy MII code.
67 * These are called for PHY operations by upper layers and we further call the
68 * DM MDIO driver functions.
70 static int mdio_read(struct mii_dev *mii_bus, int addr, int devad, int reg)
72 struct udevice *dev = mii_bus->priv;
74 return mdio_get_ops(dev)->read(dev, addr, devad, reg);
77 static int mdio_write(struct mii_dev *mii_bus, int addr, int devad, int reg,
80 struct udevice *dev = mii_bus->priv;
82 return mdio_get_ops(dev)->write(dev, addr, devad, reg, val);
85 static int mdio_reset(struct mii_dev *mii_bus)
87 struct udevice *dev = mii_bus->priv;
89 if (mdio_get_ops(dev)->reset)
90 return mdio_get_ops(dev)->reset(dev);
95 static int dm_mdio_post_probe(struct udevice *dev)
97 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
99 pdata->mii_bus = mdio_alloc();
100 pdata->mii_bus->read = mdio_read;
101 pdata->mii_bus->write = mdio_write;
102 pdata->mii_bus->reset = mdio_reset;
103 pdata->mii_bus->priv = dev;
104 strncpy(pdata->mii_bus->name, dev->name, MDIO_NAME_LEN - 1);
106 return mdio_register(pdata->mii_bus);
109 static int dm_mdio_pre_remove(struct udevice *dev)
111 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
112 struct mdio_ops *ops = mdio_get_ops(dev);
116 mdio_unregister(pdata->mii_bus);
117 mdio_free(pdata->mii_bus);
122 struct phy_device *dm_mdio_phy_connect(struct udevice *mdiodev, int phyaddr,
123 struct udevice *ethdev,
124 phy_interface_t interface)
126 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(mdiodev);
128 if (device_probe(mdiodev))
131 return phy_connect(pdata->mii_bus, phyaddr, ethdev, interface);
134 static struct phy_device *dm_eth_connect_phy_handle(struct udevice *ethdev,
135 phy_interface_t interface)
138 struct udevice *mdiodev;
139 struct phy_device *phy;
140 struct ofnode_phandle_args phandle = {.node = ofnode_null()};
144 if (CONFIG_IS_ENABLED(PHY_FIXED) &&
145 ofnode_phy_is_fixed_link(dev_ofnode(ethdev), &phynode)) {
146 phy = phy_connect(NULL, 0, ethdev, interface);
147 phandle.node = phynode;
151 for (i = 0; i < PHY_HANDLE_STR_CNT; i++)
152 if (!dev_read_phandle_with_args(ethdev, phy_handle_str[i], NULL,
156 if (!ofnode_valid(phandle.node)) {
157 dev_dbg(ethdev, "can't find PHY node\n");
162 * reading 'reg' directly should be fine. This is a PHY node, the
163 * address is always size 1 and requires no translation
165 if (ofnode_read_u32(phandle.node, "reg", &phy_addr)) {
166 dev_dbg(ethdev, "missing reg property in phy node\n");
170 if (uclass_get_device_by_ofnode(UCLASS_MDIO,
171 ofnode_get_parent(phandle.node),
173 dev_dbg(ethdev, "can't find MDIO bus for node %s\n",
174 ofnode_get_name(ofnode_get_parent(phandle.node)));
178 phy = dm_mdio_phy_connect(mdiodev, phy_addr, ethdev, interface);
182 phy->node = phandle.node;
187 /* Connect to a PHY linked in eth DT node */
188 struct phy_device *dm_eth_phy_connect(struct udevice *ethdev)
191 phy_interface_t interface;
192 struct phy_device *phy;
195 if (!dev_has_ofnode(ethdev)) {
196 debug("%s: supplied eth dev has no DT node!\n", ethdev->name);
200 interface = PHY_INTERFACE_MODE_NONE;
201 for (i = 0; i < PHY_MODE_STR_CNT; i++) {
202 if_str = dev_read_string(ethdev, phy_mode_str[i]);
204 interface = phy_get_interface_by_name(if_str);
209 interface = PHY_INTERFACE_MODE_NONE;
210 if (interface == PHY_INTERFACE_MODE_NONE)
211 dev_dbg(ethdev, "can't find interface mode, default to NONE\n");
213 phy = dm_eth_connect_phy_handle(ethdev, interface);
218 phy->interface = interface;
223 UCLASS_DRIVER(mdio) = {
226 .post_bind = dm_mdio_post_bind,
227 .post_probe = dm_mdio_post_probe,
228 .pre_remove = dm_mdio_pre_remove,
229 .per_device_auto = sizeof(struct mdio_perdev_priv),