2 * Copyright (c) 2015 The FreeBSD Foundation
4 * This software was developed by Semihalf under
5 * the sponsorship of the FreeBSD Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
38 #include <dev/ofw/ofw_bus.h>
39 #include <dev/ofw/ofw_bus_subr.h>
40 #include <dev/fdt/simplebus.h>
42 #include <machine/bus.h>
43 #include <machine/resource.h>
45 #include "thunder_mdio_var.h"
47 static int thunder_mdio_fdt_probe(device_t);
48 static int thunder_mdio_fdt_attach(device_t);
50 static device_method_t thunder_mdio_fdt_methods[] = {
51 /* Device interface */
52 DEVMETHOD(device_probe, thunder_mdio_fdt_probe),
53 DEVMETHOD(device_attach, thunder_mdio_fdt_attach),
59 DEFINE_CLASS_1(thunder_mdio, thunder_mdio_fdt_driver, thunder_mdio_fdt_methods,
60 sizeof(struct thunder_mdio_softc), thunder_mdio_driver);
62 EARLY_DRIVER_MODULE(thunder_mdio, ofwbus, thunder_mdio_fdt_driver, 0, 0,
63 BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
64 EARLY_DRIVER_MODULE(thunder_mdio, mdionexus, thunder_mdio_fdt_driver, 0, 0,
65 BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
67 static struct ofw_compat_data mdio_compat_data[] = {
68 {"cavium,octeon-3860-mdio", true},
69 {"cavium,thunder-8890-mdio", true},
74 thunder_mdio_fdt_probe(device_t dev)
77 if (!ofw_bus_status_okay(dev))
79 if (!ofw_bus_search_compatible(dev, mdio_compat_data)->ocd_data)
82 device_set_desc(dev, THUNDER_MDIO_DEVSTR);
83 return (BUS_PROBE_DEFAULT);
87 thunder_mdio_fdt_attach(device_t dev)
92 /* Call core attach */
93 ret = thunder_mdio_attach(dev);
97 * Register device to this node/xref.
98 * Thanks to that we will be able to retrieve device_t structure
99 * while holding only node reference acquired from FDT.
101 node = ofw_bus_get_node(dev);
102 OF_device_register_xref(OF_xref_from_node(node), dev);
107 struct mdionexus_softc {
108 struct simplebus_softc simplebus_sc;
111 static device_probe_t mdionexus_fdt_probe;
112 static device_attach_t mdionexus_fdt_attach;
114 static const struct ofw_bus_devinfo * mdionexus_ofw_get_devinfo(device_t,
117 static device_method_t mdionexus_fdt_methods[] = {
118 /* Device interface */
119 DEVMETHOD(device_probe, mdionexus_fdt_probe),
120 DEVMETHOD(device_attach, mdionexus_fdt_attach),
123 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
124 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
125 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
127 /* ofw_bus interface */
128 DEVMETHOD(ofw_bus_get_devinfo, mdionexus_ofw_get_devinfo),
129 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
130 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
131 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
132 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
133 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
138 DEFINE_CLASS_0(mdionexus, mdionexus_fdt_driver, mdionexus_fdt_methods,
139 sizeof(struct mdionexus_softc));
141 EARLY_DRIVER_MODULE(mdionexus, mrmlbus, mdionexus_fdt_driver, 0, 0,
142 BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
144 static int mdionexus_ofw_fill_ranges(phandle_t, struct simplebus_softc *);
145 static int mdionexus_ofw_bus_attach(device_t);
148 mdionexus_fdt_probe(device_t dev)
151 if (!ofw_bus_status_okay(dev))
154 if (!ofw_bus_is_compatible(dev, "cavium,thunder-8890-mdio-nexus"))
157 device_set_desc(dev, "Cavium ThunderX MDIO nexus");
158 return (BUS_PROBE_SPECIFIC);
162 mdionexus_fdt_attach(device_t dev)
166 err = mdionexus_ofw_bus_attach(dev);
170 return (bus_generic_attach(dev));
173 /* OFW bus interface */
174 struct mdionexus_ofw_devinfo {
175 struct ofw_bus_devinfo di_dinfo;
176 struct resource_list di_rl;
179 static const struct ofw_bus_devinfo *
180 mdionexus_ofw_get_devinfo(device_t bus __unused, device_t child)
182 struct mdionexus_ofw_devinfo *di;
184 di = device_get_ivars(child);
185 return (&di->di_dinfo);
188 /* Helper functions */
191 mdionexus_ofw_fill_ranges(phandle_t node, struct simplebus_softc *sc)
193 int host_address_cells;
195 ssize_t nbase_ranges;
199 err = OF_searchencprop(OF_parent(node), "#address-cells",
200 &host_address_cells, sizeof(host_address_cells));
204 nbase_ranges = OF_getproplen(node, "ranges");
205 if (nbase_ranges < 0)
207 sc->nranges = nbase_ranges / sizeof(cell_t) /
208 (sc->acells + host_address_cells + sc->scells);
209 if (sc->nranges == 0)
212 sc->ranges = malloc(sc->nranges * sizeof(sc->ranges[0]),
213 M_THUNDER_MDIO, M_WAITOK);
214 base_ranges = malloc(nbase_ranges, M_THUNDER_MDIO, M_WAITOK);
215 OF_getencprop(node, "ranges", base_ranges, nbase_ranges);
217 for (i = 0, j = 0; i < sc->nranges; i++) {
218 sc->ranges[i].bus = 0;
219 for (k = 0; k < sc->acells; k++) {
220 sc->ranges[i].bus <<= 32;
221 sc->ranges[i].bus |= base_ranges[j++];
223 sc->ranges[i].host = 0;
224 for (k = 0; k < host_address_cells; k++) {
225 sc->ranges[i].host <<= 32;
226 sc->ranges[i].host |= base_ranges[j++];
228 sc->ranges[i].size = 0;
229 for (k = 0; k < sc->scells; k++) {
230 sc->ranges[i].size <<= 32;
231 sc->ranges[i].size |= base_ranges[j++];
235 free(base_ranges, M_THUNDER_MDIO);
236 return (sc->nranges);
240 mdionexus_ofw_bus_attach(device_t dev)
242 struct simplebus_softc *sc;
243 struct mdionexus_ofw_devinfo *di;
245 phandle_t parent, node;
247 parent = ofw_bus_get_node(dev);
248 simplebus_init(dev, parent);
250 sc = (struct simplebus_softc *)device_get_softc(dev);
252 if (mdionexus_ofw_fill_ranges(parent, sc) < 0) {
253 device_printf(dev, "could not get ranges\n");
256 /* Iterate through all bus subordinates */
257 for (node = OF_child(parent); node > 0; node = OF_peer(node)) {
258 /* Allocate and populate devinfo. */
259 di = malloc(sizeof(*di), M_THUNDER_MDIO, M_WAITOK | M_ZERO);
260 if (ofw_bus_gen_setup_devinfo(&di->di_dinfo, node) != 0) {
261 free(di, M_THUNDER_MDIO);
265 /* Initialize and populate resource list. */
266 resource_list_init(&di->di_rl);
267 ofw_bus_reg_to_rl(dev, node, sc->acells, sc->scells,
269 ofw_bus_intr_to_rl(dev, node, &di->di_rl, NULL);
271 /* Add newbus device for this FDT node */
272 child = device_add_child(dev, NULL, -1);
274 resource_list_free(&di->di_rl);
275 ofw_bus_gen_destroy_devinfo(&di->di_dinfo);
276 free(di, M_THUNDER_MDIO);
280 device_set_ivars(child, di);