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