xref: /freebsd/sys/dev/mii/mii_fdt.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1 /*-
2  * Copyright (c) 2017 Ian Lepore <ian@freebsd.org>
3  * All rights reserved.
4  *
5  * Development sponsored by Microsemi, Inc.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 /*
31  * Utility functions for PHY drivers on systems configured using FDT data.
32  */
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/socket.h>
37 #include <sys/bus.h>
38 #include <sys/malloc.h>
39 
40 #include <net/if.h>
41 #include <net/if_media.h>
42 
43 #include <dev/ofw/openfirm.h>
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
46 
47 #include <dev/mii/mii.h>
48 #include <dev/mii/miivar.h>
49 #include <dev/mii/mii_fdt.h>
50 
51 /*
52  * Table to translate MII_CONTYPE_xxxx constants to/from devicetree strings.
53  * We explicitly associate the enum values with the strings in a table to avoid
54  * relying on this list being sorted in the same order as the enum in miivar.h,
55  * and to avoid problems if the enum gains new types that aren't in the FDT
56  * data.  However, the "unknown" entry must be first because it is referenced
57  * using subscript 0 in mii_fdt_contype_to_name().
58  */
59 static struct contype_names {
60 	mii_contype_t type;
61 	const char   *name;
62 } fdt_contype_names[] = {
63 	{MII_CONTYPE_UNKNOWN,		"unknown"},
64 	{MII_CONTYPE_MII,		"mii"},
65 	{MII_CONTYPE_GMII,		"gmii"},
66 	{MII_CONTYPE_SGMII,		"sgmii"},
67 	{MII_CONTYPE_QSGMII,		"qsgmii"},
68 	{MII_CONTYPE_TBI,		"tbi"},
69 	{MII_CONTYPE_REVMII,		"rev-mii"},
70 	{MII_CONTYPE_RMII,		"rmii"},
71 	{MII_CONTYPE_RGMII,		"rgmii"},
72 	{MII_CONTYPE_RGMII_ID,		"rgmii-id"},
73 	{MII_CONTYPE_RGMII_RXID,	"rgmii-rxid"},
74 	{MII_CONTYPE_RGMII_TXID,	"rgmii-txid"},
75 	{MII_CONTYPE_RTBI,		"rtbi"},
76 	{MII_CONTYPE_SMII,		"smii"},
77 	{MII_CONTYPE_XGMII,		"xgmii"},
78 	{MII_CONTYPE_TRGMII,		"trgmii"},
79 	{MII_CONTYPE_2000BX,		"2000base-x"},
80 	{MII_CONTYPE_2500BX,		"2500base-x"},
81 	{MII_CONTYPE_RXAUI,		"rxaui"},
82 };
83 
84 static phandle_t
85 mii_fdt_get_phynode(phandle_t macnode)
86 {
87 	static const char *props[] = {
88 	    "phy-handle", "phy", "phy-device"
89 	};
90 	pcell_t xref;
91 	u_int i;
92 
93 	for (i = 0; i < nitems(props); ++i) {
94 		if (OF_getencprop(macnode, props[i], &xref, sizeof(xref)) > 0)
95 			return (OF_node_from_xref(xref));
96 	}
97 	return (-1);
98 }
99 
100 static phandle_t
101 mii_fdt_lookup_phy(phandle_t node, int addr)
102 {
103 	phandle_t ports, phynode, child;
104 	int reg;
105 
106 	/* First try to see if we have a direct xref pointing to a PHY. */
107 	phynode = mii_fdt_get_phynode(node);
108 	if (phynode != -1)
109 		return (phynode);
110 
111 	/*
112 	 * Now handle the "switch" case.
113 	 * Search "ports" subnode for nodes that describe a switch port
114 	 * including a PHY xref.
115 	 * Since we have multiple candidates select one based on PHY address.
116 	 */
117 	ports = ofw_bus_find_child(node, "ports");
118 	if (ports <= 0)
119 		return (-1);
120 
121 	for (child = OF_child(ports); child != 0; child = OF_peer(child)) {
122 		if (ofw_bus_node_status_okay(child) == 0)
123 			continue;
124 
125 		phynode = mii_fdt_get_phynode(child);
126 		if (phynode <= 0)
127 			continue;
128 
129 		if (OF_getencprop(phynode, "reg", &reg, sizeof(reg)) <= 0)
130 			continue;
131 
132 		if (reg == addr)
133 			return (phynode);
134 	}
135 	return (-1);
136 }
137 
138 mii_contype_t
139 mii_fdt_contype_from_name(const char *name)
140 {
141 	u_int i;
142 
143 	for (i = 0; i < nitems(fdt_contype_names); ++i) {
144 		if (strcmp(name, fdt_contype_names[i].name) == 0)
145 			return (fdt_contype_names[i].type);
146 	}
147 	return (MII_CONTYPE_UNKNOWN);
148 }
149 
150 const char *
151 mii_fdt_contype_to_name(mii_contype_t contype)
152 {
153 	u_int i;
154 
155 	for (i = 0; i < nitems(fdt_contype_names); ++i) {
156 		if (contype == fdt_contype_names[i].type)
157 			return (fdt_contype_names[i].name);
158 	}
159 	return (fdt_contype_names[0].name);
160 }
161 
162 mii_contype_t
163 mii_fdt_get_contype(phandle_t macnode)
164 {
165 	char val[32];
166 
167 	if (OF_getprop(macnode, "phy-mode", val, sizeof(val)) <= 0 &&
168 	    OF_getprop(macnode, "phy-connection-type", val, sizeof(val)) <= 0) {
169                 return (MII_CONTYPE_UNKNOWN);
170 	}
171 	return (mii_fdt_contype_from_name(val));
172 }
173 
174 void
175 mii_fdt_free_config(struct mii_fdt_phy_config *cfg)
176 {
177 
178 	free(cfg, M_OFWPROP);
179 }
180 
181 mii_fdt_phy_config_t *
182 mii_fdt_get_config(device_t phydev)
183 {
184 	struct mii_attach_args *ma;
185 	mii_fdt_phy_config_t *cfg;
186 	device_t miibus, macdev;
187 	pcell_t val;
188 
189 	ma = device_get_ivars(phydev);
190 	miibus = device_get_parent(phydev);
191 	macdev = device_get_parent(miibus);
192 
193 	cfg = malloc(sizeof(*cfg), M_OFWPROP, M_ZERO | M_WAITOK);
194 
195 	/*
196 	 * If we can't find our parent MAC's node, there's nothing more we can
197 	 * fill in; cfg is already full of zero/default values, return it.
198 	 */
199 	if ((cfg->macnode = ofw_bus_get_node(macdev)) == -1)
200 		return (cfg);
201 
202 	cfg->con_type = mii_fdt_get_contype(cfg->macnode);
203 
204 	/*
205 	 * If we can't find our own PHY node, there's nothing more we can fill
206 	 * in, just return what we've got.
207 	 */
208 	cfg->phynode = mii_fdt_lookup_phy(cfg->macnode, ma->mii_phyno);
209 	if (cfg->phynode == -1)
210 		return (cfg);
211 
212 	if (OF_getencprop(cfg->phynode, "max-speed", &val, sizeof(val)) > 0)
213 		cfg->max_speed = val;
214 
215 	if (ofw_bus_node_is_compatible(cfg->phynode,
216 	    "ethernet-phy-ieee802.3-c45"))
217 		cfg->flags |= MIIF_FDT_COMPAT_CLAUSE45;
218 
219 	if (OF_hasprop(cfg->phynode, "broken-turn-around"))
220 		cfg->flags |= MIIF_FDT_BROKEN_TURNAROUND;
221 	if (OF_hasprop(cfg->phynode, "enet-phy-lane-swap"))
222 		cfg->flags |= MIIF_FDT_LANE_SWAP;
223 	if (OF_hasprop(cfg->phynode, "enet-phy-lane-no-swap"))
224 		cfg->flags |= MIIF_FDT_NO_LANE_SWAP;
225 	if (OF_hasprop(cfg->phynode, "eee-broken-100tx"))
226 		cfg->flags |= MIIF_FDT_EEE_BROKEN_100TX;
227 	if (OF_hasprop(cfg->phynode, "eee-broken-1000t"))
228 		cfg->flags |= MIIF_FDT_EEE_BROKEN_1000T;
229 	if (OF_hasprop(cfg->phynode, "eee-broken-10gt"))
230 		cfg->flags |= MIIF_FDT_EEE_BROKEN_10GT;
231 	if (OF_hasprop(cfg->phynode, "eee-broken-1000kx"))
232 		cfg->flags |= MIIF_FDT_EEE_BROKEN_1000KX;
233 	if (OF_hasprop(cfg->phynode, "eee-broken-10gkx4"))
234 		cfg->flags |= MIIF_FDT_EEE_BROKEN_10GKX4;
235 	if (OF_hasprop(cfg->phynode, "eee-broken-10gkr"))
236 		cfg->flags |= MIIF_FDT_EEE_BROKEN_10GKR;
237 
238 	return (cfg);
239 }
240 
241 static int
242 miibus_fdt_probe(device_t dev)
243 {
244 	device_t parent;
245 
246 	parent = device_get_parent(dev);
247 	if (ofw_bus_get_node(parent) == -1)
248 		return (ENXIO);
249 
250 	device_set_desc(dev, "OFW MII bus");
251 	return (BUS_PROBE_DEFAULT);
252 }
253 
254 static int
255 miibus_fdt_attach(device_t dev)
256 {
257 	struct mii_attach_args *ma;
258 	int i, error, nchildren;
259 	device_t parent, *children;
260 	phandle_t phy_node;
261 
262 	parent = device_get_parent(dev);
263 
264 	error = device_get_children(dev, &children, &nchildren);
265 	if (error != 0 || nchildren == 0)
266 		return (ENXIO);
267 
268 	for (i = 0; i < nchildren; i++) {
269 		ma = device_get_ivars(children[i]);
270 		bzero(&ma->obd, sizeof(ma->obd));
271 		phy_node = mii_fdt_lookup_phy(ofw_bus_get_node(parent),
272 		    ma->mii_phyno);
273 		if (phy_node == -1) {
274 			device_printf(dev,
275 			    "Warning: failed to find OFW node for PHY%d\n",
276 			    ma->mii_phyno);
277 			continue;
278 		}
279 		error = ofw_bus_gen_setup_devinfo(&ma->obd, phy_node);
280 		if (error != 0) {
281 			device_printf(dev,
282 			    "Warning: failed to setup OFW devinfo for PHY%d\n",
283 			    ma->mii_phyno);
284 			continue;
285 		}
286 		/*
287 		 * Setup interrupt resources.
288 		 * Only a handful of PHYs support those,
289 		 * so it's fine if we fail here.
290 		 */
291 		resource_list_init(&ma->rl);
292 		(void)ofw_bus_intr_to_rl(children[i], phy_node, &ma->rl, NULL);
293 	}
294 
295 	free(children, M_TEMP);
296 	return (miibus_attach(dev));
297 }
298 
299 static struct resource_list *
300 miibus_fdt_get_resource_list(device_t bus, device_t child)
301 {
302 	struct mii_attach_args *ma;
303 
304 	ma = device_get_ivars(child);
305 
306 	if (ma->obd.obd_node == 0)
307 		return (NULL);
308 
309 	return (&ma->rl);
310 }
311 
312 static const struct ofw_bus_devinfo*
313 miibus_fdt_get_devinfo(device_t bus, device_t child)
314 {
315 	struct mii_attach_args *ma;
316 
317 	ma = device_get_ivars(child);
318 
319 	if (ma->obd.obd_node == 0)
320 		return (NULL);
321 
322 	return (&ma->obd);
323 }
324 
325 static device_method_t miibus_fdt_methods[] = {
326 	DEVMETHOD(device_probe,		miibus_fdt_probe),
327 	DEVMETHOD(device_attach,	miibus_fdt_attach),
328 
329 	/* ofw_bus interface */
330 	DEVMETHOD(ofw_bus_get_devinfo,	miibus_fdt_get_devinfo),
331 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
332 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
333 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
334 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
335 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
336 
337 	DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
338 	DEVMETHOD(bus_teardown_intr,		bus_generic_teardown_intr),
339 	DEVMETHOD(bus_release_resource,		bus_generic_release_resource),
340 	DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
341 	DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
342 	DEVMETHOD(bus_adjust_resource,		bus_generic_adjust_resource),
343 	DEVMETHOD(bus_alloc_resource,		bus_generic_rl_alloc_resource),
344 	DEVMETHOD(bus_get_resource,		bus_generic_rl_get_resource),
345 	DEVMETHOD(bus_set_resource,		bus_generic_rl_set_resource),
346 	DEVMETHOD(bus_get_resource_list,	miibus_fdt_get_resource_list),
347 
348 	DEVMETHOD_END
349 };
350 
351 DEFINE_CLASS_1(miibus, miibus_fdt_driver, miibus_fdt_methods,
352     sizeof(struct mii_data), miibus_driver);
353