1 // 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>
9 #include <log.h>
10 #include <malloc.h>
11 #include <miiphy.h>
12 #include <dm/device-internal.h>
13 #include <dm/device_compat.h>
14 #include <dm/uclass-internal.h>
15 #include <linux/compat.h>
16
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",
24 "phy",
25 "phy-device" };
26
dm_mdio_probe_devices(void)27 void dm_mdio_probe_devices(void)
28 {
29 struct udevice *it;
30 struct uclass *uc;
31
32 uclass_get(UCLASS_MDIO, &uc);
33 uclass_foreach_dev(it, uc) {
34 device_probe(it);
35 }
36 }
37
dm_mdio_post_bind(struct udevice * dev)38 static int dm_mdio_post_bind(struct udevice *dev)
39 {
40 const char *dt_name;
41
42 /* set a custom name for the MDIO device, if present in DT */
43 if (dev_has_ofnode(dev)) {
44 dt_name = dev_read_string(dev, "device-name");
45 if (dt_name) {
46 debug("renaming dev %s to %s\n", dev->name, dt_name);
47 device_set_name(dev, dt_name);
48 }
49 }
50
51 /*
52 * MDIO command doesn't like spaces in names, don't allow them to keep
53 * it happy
54 */
55 if (strchr(dev->name, ' ')) {
56 debug("\nError: MDIO device name \"%s\" has a space!\n",
57 dev->name);
58 return -EINVAL;
59 }
60
61 return 0;
62 }
63
64 /*
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.
68 */
mdio_read(struct mii_dev * mii_bus,int addr,int devad,int reg)69 static int mdio_read(struct mii_dev *mii_bus, int addr, int devad, int reg)
70 {
71 struct udevice *dev = mii_bus->priv;
72
73 return mdio_get_ops(dev)->read(dev, addr, devad, reg);
74 }
75
mdio_write(struct mii_dev * mii_bus,int addr,int devad,int reg,u16 val)76 static int mdio_write(struct mii_dev *mii_bus, int addr, int devad, int reg,
77 u16 val)
78 {
79 struct udevice *dev = mii_bus->priv;
80
81 return mdio_get_ops(dev)->write(dev, addr, devad, reg, val);
82 }
83
mdio_reset(struct mii_dev * mii_bus)84 static int mdio_reset(struct mii_dev *mii_bus)
85 {
86 struct udevice *dev = mii_bus->priv;
87
88 if (mdio_get_ops(dev)->reset)
89 return mdio_get_ops(dev)->reset(dev);
90 else
91 return 0;
92 }
93
dm_mdio_post_probe(struct udevice * dev)94 static int dm_mdio_post_probe(struct udevice *dev)
95 {
96 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
97
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);
104
105 return mdio_register(pdata->mii_bus);
106 }
107
dm_mdio_pre_remove(struct udevice * dev)108 static int dm_mdio_pre_remove(struct udevice *dev)
109 {
110 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
111 struct mdio_ops *ops = mdio_get_ops(dev);
112
113 if (ops->reset)
114 ops->reset(dev);
115 mdio_unregister(pdata->mii_bus);
116 mdio_free(pdata->mii_bus);
117
118 return 0;
119 }
120
dm_mdio_phy_connect(struct udevice * mdiodev,int phyaddr,struct udevice * ethdev,phy_interface_t interface)121 struct phy_device *dm_mdio_phy_connect(struct udevice *mdiodev, int phyaddr,
122 struct udevice *ethdev,
123 phy_interface_t interface)
124 {
125 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(mdiodev);
126
127 if (device_probe(mdiodev))
128 return NULL;
129
130 return phy_connect(pdata->mii_bus, phyaddr, ethdev, interface);
131 }
132
dm_eth_connect_phy_handle(struct udevice * ethdev,phy_interface_t interface)133 static struct phy_device *dm_eth_connect_phy_handle(struct udevice *ethdev,
134 phy_interface_t interface)
135 {
136 u32 phy_addr;
137 struct udevice *mdiodev;
138 struct phy_device *phy;
139 struct ofnode_phandle_args phandle = {.node = ofnode_null()};
140 int i;
141
142 if (CONFIG_IS_ENABLED(PHY_FIXED) &&
143 ofnode_valid(dev_read_subnode(ethdev, "fixed-link"))) {
144 phy = phy_connect(NULL, -1, ethdev, interface);
145 goto out;
146 }
147
148 for (i = 0; i < PHY_HANDLE_STR_CNT; i++)
149 if (!dev_read_phandle_with_args(ethdev, phy_handle_str[i], NULL,
150 0, 0, &phandle))
151 break;
152
153 if (!ofnode_valid(phandle.node)) {
154 dev_dbg(ethdev, "can't find PHY node\n");
155 return NULL;
156 }
157
158 /*
159 * reading 'reg' directly should be fine. This is a PHY node, the
160 * address is always size 1 and requires no translation
161 */
162 if (ofnode_read_u32(phandle.node, "reg", &phy_addr)) {
163 dev_dbg(ethdev, "missing reg property in phy node\n");
164 return NULL;
165 }
166
167 if (uclass_get_device_by_ofnode(UCLASS_MDIO,
168 ofnode_get_parent(phandle.node),
169 &mdiodev)) {
170 dev_dbg(ethdev, "can't find MDIO bus for node %s\n",
171 ofnode_get_name(ofnode_get_parent(phandle.node)));
172 return NULL;
173 }
174
175 phy = dm_mdio_phy_connect(mdiodev, phy_addr, ethdev, interface);
176
177 out:
178 if (phy)
179 phy->node = phandle.node;
180
181 return phy;
182 }
183
184 /* Connect to a PHY linked in eth DT node */
dm_eth_phy_connect(struct udevice * ethdev)185 struct phy_device *dm_eth_phy_connect(struct udevice *ethdev)
186 {
187 const char *if_str;
188 phy_interface_t interface;
189 struct phy_device *phy;
190 int i;
191
192 if (!dev_has_ofnode(ethdev)) {
193 debug("%s: supplied eth dev has no DT node!\n", ethdev->name);
194 return NULL;
195 }
196
197 interface = PHY_INTERFACE_MODE_NONE;
198 for (i = 0; i < PHY_MODE_STR_CNT; i++) {
199 if_str = dev_read_string(ethdev, phy_mode_str[i]);
200 if (if_str) {
201 interface = phy_get_interface_by_name(if_str);
202 break;
203 }
204 }
205 if (interface < 0)
206 interface = PHY_INTERFACE_MODE_NONE;
207 if (interface == PHY_INTERFACE_MODE_NONE)
208 dev_dbg(ethdev, "can't find interface mode, default to NONE\n");
209
210 phy = dm_eth_connect_phy_handle(ethdev, interface);
211
212 if (!phy)
213 return NULL;
214
215 phy->interface = interface;
216
217 return phy;
218 }
219
220 UCLASS_DRIVER(mdio) = {
221 .id = UCLASS_MDIO,
222 .name = "mdio",
223 .post_bind = dm_mdio_post_bind,
224 .post_probe = dm_mdio_post_probe,
225 .pre_remove = dm_mdio_pre_remove,
226 .per_device_auto = sizeof(struct mdio_perdev_priv),
227 };
228