blob: 7593618d9adb29b1427e365c64a4a482031670fe [file] [log] [blame]
Alex Marginean1a5b0982019-06-03 19:10:30 +03001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2019
4 * Alex Marginean, NXP
5 */
6
7#include <common.h>
8#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070010#include <malloc.h>
Alex Marginean1a5b0982019-06-03 19:10:30 +030011#include <miiphy.h>
12#include <dm/device-internal.h>
Simon Glass9bc15642020-02-03 07:36:16 -070013#include <dm/device_compat.h>
Bin Menge0518f02021-03-14 20:14:47 +080014#include <dm/of_extra.h>
Alex Marginean1a5b0982019-06-03 19:10:30 +030015#include <dm/uclass-internal.h>
Simon Glass9bc15642020-02-03 07:36:16 -070016#include <linux/compat.h>
Alex Marginean1a5b0982019-06-03 19:10:30 +030017
18void dm_mdio_probe_devices(void)
19{
20 struct udevice *it;
21 struct uclass *uc;
22
23 uclass_get(UCLASS_MDIO, &uc);
24 uclass_foreach_dev(it, uc) {
25 device_probe(it);
26 }
27}
28
29static int dm_mdio_post_bind(struct udevice *dev)
30{
Alex Marginean2e859ad2019-07-25 12:33:17 +030031 const char *dt_name;
32
33 /* set a custom name for the MDIO device, if present in DT */
Simon Glassa7ece582020-12-19 10:40:14 -070034 if (dev_has_ofnode(dev)) {
35 dt_name = dev_read_string(dev, "device-name");
Alex Marginean2e859ad2019-07-25 12:33:17 +030036 if (dt_name) {
37 debug("renaming dev %s to %s\n", dev->name, dt_name);
38 device_set_name(dev, dt_name);
39 }
40 }
41
Alex Marginean1a5b0982019-06-03 19:10:30 +030042 /*
43 * MDIO command doesn't like spaces in names, don't allow them to keep
44 * it happy
45 */
46 if (strchr(dev->name, ' ')) {
47 debug("\nError: MDIO device name \"%s\" has a space!\n",
48 dev->name);
49 return -EINVAL;
50 }
51
52 return 0;
53}
54
Marek Behún0e0651d2022-04-07 00:32:58 +020055int dm_mdio_read(struct udevice *mdio_dev, int addr, int devad, int reg)
56{
57 struct mdio_ops *ops = mdio_get_ops(mdio_dev);
58
59 if (!ops->read)
60 return -ENOSYS;
61
62 return ops->read(mdio_dev, addr, devad, reg);
63}
64
65int dm_mdio_write(struct udevice *mdio_dev, int addr, int devad, int reg,
66 u16 val)
67{
68 struct mdio_ops *ops = mdio_get_ops(mdio_dev);
69
70 if (!ops->write)
71 return -ENOSYS;
72
73 return ops->write(mdio_dev, addr, devad, reg, val);
74}
75
76int dm_mdio_reset(struct udevice *mdio_dev)
77{
78 struct mdio_ops *ops = mdio_get_ops(mdio_dev);
79
80 if (!ops->reset)
81 return 0;
82
83 return ops->reset(mdio_dev);
84}
85
Alex Marginean1a5b0982019-06-03 19:10:30 +030086/*
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.
90 */
91static int mdio_read(struct mii_dev *mii_bus, int addr, int devad, int reg)
92{
Marek Behúne4dedf22022-04-07 00:32:59 +020093 return dm_mdio_read(mii_bus->priv, addr, devad, reg);
Alex Marginean1a5b0982019-06-03 19:10:30 +030094}
95
96static int mdio_write(struct mii_dev *mii_bus, int addr, int devad, int reg,
97 u16 val)
98{
Marek Behúne4dedf22022-04-07 00:32:59 +020099 return dm_mdio_write(mii_bus->priv, addr, devad, reg, val);
Alex Marginean1a5b0982019-06-03 19:10:30 +0300100}
101
102static int mdio_reset(struct mii_dev *mii_bus)
103{
Marek Behúne4dedf22022-04-07 00:32:59 +0200104 return dm_mdio_reset(mii_bus->priv);
Alex Marginean1a5b0982019-06-03 19:10:30 +0300105}
106
107static int dm_mdio_post_probe(struct udevice *dev)
108{
109 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
110
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;
Vladimir Oltean54045442021-09-27 14:22:00 +0300116 strlcpy(pdata->mii_bus->name, dev->name, MDIO_NAME_LEN);
Alex Marginean1a5b0982019-06-03 19:10:30 +0300117
118 return mdio_register(pdata->mii_bus);
119}
120
121static int dm_mdio_pre_remove(struct udevice *dev)
122{
123 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
Alex Marginean1a5b0982019-06-03 19:10:30 +0300124
Marek Behúne4dedf22022-04-07 00:32:59 +0200125 dm_mdio_reset(dev);
Alex Marginean1a5b0982019-06-03 19:10:30 +0300126 mdio_unregister(pdata->mii_bus);
127 mdio_free(pdata->mii_bus);
128
129 return 0;
130}
131
Alex Marginean6b3089e2019-11-25 17:15:11 +0200132struct phy_device *dm_mdio_phy_connect(struct udevice *mdiodev, int phyaddr,
Alex Marginean1a5b0982019-06-03 19:10:30 +0300133 struct udevice *ethdev,
134 phy_interface_t interface)
135{
Alex Marginean6b3089e2019-11-25 17:15:11 +0200136 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(mdiodev);
Alex Marginean1a5b0982019-06-03 19:10:30 +0300137
Alex Marginean6b3089e2019-11-25 17:15:11 +0200138 if (device_probe(mdiodev))
139 return NULL;
Alex Marginean1a5b0982019-06-03 19:10:30 +0300140
Alex Marginean6b3089e2019-11-25 17:15:11 +0200141 return phy_connect(pdata->mii_bus, phyaddr, ethdev, interface);
Alex Marginean1a5b0982019-06-03 19:10:30 +0300142}
143
Alex Margineancf640552019-11-25 17:15:12 +0200144static struct phy_device *dm_eth_connect_phy_handle(struct udevice *ethdev,
145 phy_interface_t interface)
146{
147 u32 phy_addr;
148 struct udevice *mdiodev;
149 struct phy_device *phy;
Vladimir Oltean6ca194a2021-03-14 20:14:48 +0800150 ofnode phynode;
Alex Margineancf640552019-11-25 17:15:12 +0200151
Rasmus Villemoese5051642020-10-05 15:15:16 +0200152 if (CONFIG_IS_ENABLED(PHY_FIXED) &&
Vladimir Oltean6ca194a2021-03-14 20:14:48 +0800153 ofnode_phy_is_fixed_link(dev_ofnode(ethdev), &phynode)) {
154 phy = phy_connect(NULL, 0, ethdev, interface);
Rasmus Villemoese5051642020-10-05 15:15:16 +0200155 goto out;
156 }
157
Marek Behúnf4f1ddc2022-04-07 00:32:57 +0200158 phynode = dev_get_phy_node(ethdev);
159 if (!ofnode_valid(phynode)) {
Sean Anderson836cb5e2020-09-15 10:44:53 -0400160 dev_dbg(ethdev, "can't find PHY node\n");
Alex Margineancf640552019-11-25 17:15:12 +0200161 return NULL;
162 }
163
164 /*
165 * reading 'reg' directly should be fine. This is a PHY node, the
166 * address is always size 1 and requires no translation
167 */
Marek Behúnf4f1ddc2022-04-07 00:32:57 +0200168 if (ofnode_read_u32(phynode, "reg", &phy_addr)) {
Alex Margineancf640552019-11-25 17:15:12 +0200169 dev_dbg(ethdev, "missing reg property in phy node\n");
170 return NULL;
171 }
172
173 if (uclass_get_device_by_ofnode(UCLASS_MDIO,
Marek Behúnf4f1ddc2022-04-07 00:32:57 +0200174 ofnode_get_parent(phynode),
Alex Margineancf640552019-11-25 17:15:12 +0200175 &mdiodev)) {
Sean Anderson836cb5e2020-09-15 10:44:53 -0400176 dev_dbg(ethdev, "can't find MDIO bus for node %s\n",
Marek Behúnf4f1ddc2022-04-07 00:32:57 +0200177 ofnode_get_name(ofnode_get_parent(phynode)));
Alex Margineancf640552019-11-25 17:15:12 +0200178 return NULL;
179 }
180
181 phy = dm_mdio_phy_connect(mdiodev, phy_addr, ethdev, interface);
182
Rasmus Villemoese5051642020-10-05 15:15:16 +0200183out:
Alex Margineancf640552019-11-25 17:15:12 +0200184 if (phy)
Marek Behúnf4f1ddc2022-04-07 00:32:57 +0200185 phy->node = phynode;
Alex Margineancf640552019-11-25 17:15:12 +0200186
187 return phy;
188}
189
190/* Connect to a PHY linked in eth DT node */
191struct phy_device *dm_eth_phy_connect(struct udevice *ethdev)
192{
Alex Margineancf640552019-11-25 17:15:12 +0200193 phy_interface_t interface;
194 struct phy_device *phy;
Alex Margineancf640552019-11-25 17:15:12 +0200195
Simon Glassa7ece582020-12-19 10:40:14 -0700196 if (!dev_has_ofnode(ethdev)) {
Alex Margineancf640552019-11-25 17:15:12 +0200197 debug("%s: supplied eth dev has no DT node!\n", ethdev->name);
198 return NULL;
199 }
200
Marek Behúnbc194772022-04-07 00:33:01 +0200201 interface = dev_read_phy_mode(ethdev);
Marek Behún48631e42022-04-07 00:33:03 +0200202 if (interface == PHY_INTERFACE_MODE_NA)
203 dev_dbg(ethdev, "can't find interface mode, default to NA\n");
Alex Margineancf640552019-11-25 17:15:12 +0200204
205 phy = dm_eth_connect_phy_handle(ethdev, interface);
206
207 if (!phy)
208 return NULL;
209
210 phy->interface = interface;
211
212 return phy;
213}
214
Alex Marginean1a5b0982019-06-03 19:10:30 +0300215UCLASS_DRIVER(mdio) = {
216 .id = UCLASS_MDIO,
217 .name = "mdio",
218 .post_bind = dm_mdio_post_bind,
219 .post_probe = dm_mdio_post_probe,
220 .pre_remove = dm_mdio_pre_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700221 .per_device_auto = sizeof(struct mdio_perdev_priv),
Alex Marginean1a5b0982019-06-03 19:10:30 +0300222};