xref: /freebsd/sys/dev/vnic/thunder_mdio_fdt.c (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
1 /*-
2  * Copyright (c) 2015 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Semihalf under
6  * the sponsorship of the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/bus.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 
39 #include <dev/ofw/ofw_bus.h>
40 #include <dev/ofw/ofw_bus_subr.h>
41 #include <dev/fdt/simplebus.h>
42 
43 #include <machine/bus.h>
44 #include <machine/resource.h>
45 
46 #include "thunder_mdio_var.h"
47 
48 static int thunder_mdio_fdt_probe(device_t);
49 static int thunder_mdio_fdt_attach(device_t);
50 
51 static device_method_t thunder_mdio_fdt_methods[] = {
52 	/* Device interface */
53 	DEVMETHOD(device_probe,		thunder_mdio_fdt_probe),
54 	DEVMETHOD(device_attach,	thunder_mdio_fdt_attach),
55 
56 	/* End */
57 	DEVMETHOD_END
58 };
59 
60 DEFINE_CLASS_1(thunder_mdio, thunder_mdio_fdt_driver, thunder_mdio_fdt_methods,
61     sizeof(struct thunder_mdio_softc), thunder_mdio_driver);
62 
63 static devclass_t thunder_mdio_fdt_devclass;
64 
65 EARLY_DRIVER_MODULE(thunder_mdio, ofwbus, thunder_mdio_fdt_driver,
66     thunder_mdio_fdt_devclass, 0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
67 EARLY_DRIVER_MODULE(thunder_mdio, mdionexus, thunder_mdio_fdt_driver,
68     thunder_mdio_fdt_devclass, 0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
69 
70 static struct ofw_compat_data mdio_compat_data[] = {
71 	{"cavium,octeon-3860-mdio",	true},
72 	{"cavium,thunder-8890-mdio",	true},
73 	{NULL,				false}
74 };
75 
76 static int
77 thunder_mdio_fdt_probe(device_t dev)
78 {
79 
80 	if (!ofw_bus_status_okay(dev))
81 		return (ENXIO);
82 	if (!ofw_bus_search_compatible(dev, mdio_compat_data)->ocd_data)
83 		return (ENXIO);
84 
85 	device_set_desc(dev, THUNDER_MDIO_DEVSTR);
86 	return (BUS_PROBE_DEFAULT);
87 }
88 
89 static int
90 thunder_mdio_fdt_attach(device_t dev)
91 {
92 	phandle_t node;
93 	int ret;
94 
95 	/* Call core attach */
96 	ret = thunder_mdio_attach(dev);
97 	if (ret != 0)
98 		return (ret);
99 	/*
100 	 * Register device to this node/xref.
101 	 * Thanks to that we will be able to retrieve device_t structure
102 	 * while holding only node reference acquired from FDT.
103 	 */
104 	node = ofw_bus_get_node(dev);
105 	OF_device_register_xref(OF_xref_from_node(node), dev);
106 
107 	return (0);
108 }
109 
110 struct mdionexus_softc {
111 	struct simplebus_softc simplebus_sc;
112 };
113 
114 static device_probe_t mdionexus_fdt_probe;
115 static device_attach_t mdionexus_fdt_attach;
116 
117 static const struct ofw_bus_devinfo * mdionexus_ofw_get_devinfo(device_t,
118     device_t);
119 
120 static device_method_t mdionexus_fdt_methods[] = {
121 	/* Device interface */
122 	DEVMETHOD(device_probe,		mdionexus_fdt_probe),
123 	DEVMETHOD(device_attach,	mdionexus_fdt_attach),
124 
125 	/* Bus interface */
126 	DEVMETHOD(bus_alloc_resource,		bus_generic_alloc_resource),
127 	DEVMETHOD(bus_release_resource,		bus_generic_release_resource),
128 	DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
129 
130 	/* ofw_bus interface */
131 	DEVMETHOD(ofw_bus_get_devinfo,	mdionexus_ofw_get_devinfo),
132 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
133 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
134 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
135 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
136 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
137 
138 	DEVMETHOD_END
139 };
140 
141 DEFINE_CLASS_0(mdionexus, mdionexus_fdt_driver, mdionexus_fdt_methods,
142     sizeof(struct mdionexus_softc));
143 
144 static devclass_t mdionexus_fdt_devclass;
145 
146 EARLY_DRIVER_MODULE(mdionexus, mrmlbus, mdionexus_fdt_driver,
147     mdionexus_fdt_devclass, 0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
148 
149 static int mdionexus_ofw_fill_ranges(phandle_t, struct simplebus_softc *);
150 static int mdionexus_ofw_bus_attach(device_t);
151 
152 static int
153 mdionexus_fdt_probe(device_t dev)
154 {
155 
156 	if (!ofw_bus_status_okay(dev))
157 		return (ENXIO);
158 
159 	if (!ofw_bus_is_compatible(dev, "cavium,thunder-8890-mdio-nexus"))
160 		return (ENXIO);
161 
162 	device_set_desc(dev, "Cavium ThunderX MDIO nexus");
163 	return (BUS_PROBE_SPECIFIC);
164 }
165 
166 static int
167 mdionexus_fdt_attach(device_t dev)
168 {
169 	int err;
170 
171 	err = mdionexus_ofw_bus_attach(dev);
172 	if (err != 0)
173 		return (err);
174 
175 	return (bus_generic_attach(dev));
176 }
177 
178 /* OFW bus interface */
179 struct mdionexus_ofw_devinfo {
180 	struct ofw_bus_devinfo	di_dinfo;
181 	struct resource_list	di_rl;
182 };
183 
184 static const struct ofw_bus_devinfo *
185 mdionexus_ofw_get_devinfo(device_t bus __unused, device_t child)
186 {
187 	struct mdionexus_ofw_devinfo *di;
188 
189 	di = device_get_ivars(child);
190 	return (&di->di_dinfo);
191 }
192 
193 /* Helper functions */
194 
195 static int
196 mdionexus_ofw_fill_ranges(phandle_t node, struct simplebus_softc *sc)
197 {
198 	int host_address_cells;
199 	cell_t *base_ranges;
200 	ssize_t nbase_ranges;
201 	int err;
202 	int i, j, k;
203 
204 	err = OF_searchencprop(OF_parent(node), "#address-cells",
205 	    &host_address_cells, sizeof(host_address_cells));
206 	if (err <= 0)
207 		return (-1);
208 
209 	nbase_ranges = OF_getproplen(node, "ranges");
210 	if (nbase_ranges < 0)
211 		return (-1);
212 	sc->nranges = nbase_ranges / sizeof(cell_t) /
213 	    (sc->acells + host_address_cells + sc->scells);
214 	if (sc->nranges == 0)
215 		return (0);
216 
217 	sc->ranges = malloc(sc->nranges * sizeof(sc->ranges[0]),
218 	    M_THUNDER_MDIO, M_WAITOK);
219 	base_ranges = malloc(nbase_ranges, M_THUNDER_MDIO, M_WAITOK);
220 	OF_getencprop(node, "ranges", base_ranges, nbase_ranges);
221 
222 	for (i = 0, j = 0; i < sc->nranges; i++) {
223 		sc->ranges[i].bus = 0;
224 		for (k = 0; k < sc->acells; k++) {
225 			sc->ranges[i].bus <<= 32;
226 			sc->ranges[i].bus |= base_ranges[j++];
227 		}
228 		sc->ranges[i].host = 0;
229 		for (k = 0; k < host_address_cells; k++) {
230 			sc->ranges[i].host <<= 32;
231 			sc->ranges[i].host |= base_ranges[j++];
232 		}
233 		sc->ranges[i].size = 0;
234 		for (k = 0; k < sc->scells; k++) {
235 			sc->ranges[i].size <<= 32;
236 			sc->ranges[i].size |= base_ranges[j++];
237 		}
238 	}
239 
240 	free(base_ranges, M_THUNDER_MDIO);
241 	return (sc->nranges);
242 }
243 
244 static int
245 mdionexus_ofw_bus_attach(device_t dev)
246 {
247 	struct simplebus_softc *sc;
248 	struct mdionexus_ofw_devinfo *di;
249 	device_t child;
250 	phandle_t parent, node;
251 
252 	parent = ofw_bus_get_node(dev);
253 	simplebus_init(dev, parent);
254 
255 	sc = (struct simplebus_softc *)device_get_softc(dev);
256 
257 	if (mdionexus_ofw_fill_ranges(parent, sc) < 0) {
258 		device_printf(dev, "could not get ranges\n");
259 		return (ENXIO);
260 	}
261 	/* Iterate through all bus subordinates */
262 	for (node = OF_child(parent); node > 0; node = OF_peer(node)) {
263 		/* Allocate and populate devinfo. */
264 		di = malloc(sizeof(*di), M_THUNDER_MDIO, M_WAITOK | M_ZERO);
265 		if (ofw_bus_gen_setup_devinfo(&di->di_dinfo, node) != 0) {
266 			free(di, M_THUNDER_MDIO);
267 			continue;
268 		}
269 
270 		/* Initialize and populate resource list. */
271 		resource_list_init(&di->di_rl);
272 		ofw_bus_reg_to_rl(dev, node, sc->acells, sc->scells,
273 		    &di->di_rl);
274 		ofw_bus_intr_to_rl(dev, node, &di->di_rl, NULL);
275 
276 		/* Add newbus device for this FDT node */
277 		child = device_add_child(dev, NULL, -1);
278 		if (child == NULL) {
279 			resource_list_free(&di->di_rl);
280 			ofw_bus_gen_destroy_devinfo(&di->di_dinfo);
281 			free(di, M_THUNDER_MDIO);
282 			continue;
283 		}
284 
285 		device_set_ivars(child, di);
286 	}
287 
288 	return (0);
289 }
290