1 /*- 2 * Copyright (c) 2015 The FreeBSD Foundation 3 * 4 * This software was developed by Semihalf under 5 * the sponsorship of the FreeBSD Foundation. 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 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/bus.h> 35 #include <sys/kernel.h> 36 #include <sys/module.h> 37 38 #include <dev/ofw/ofw_bus.h> 39 #include <dev/ofw/ofw_bus_subr.h> 40 #include <dev/fdt/simplebus.h> 41 42 #include <machine/bus.h> 43 #include <machine/resource.h> 44 45 #include "thunder_mdio_var.h" 46 47 static int thunder_mdio_fdt_probe(device_t); 48 static int thunder_mdio_fdt_attach(device_t); 49 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), 54 55 /* End */ 56 DEVMETHOD_END 57 }; 58 59 DEFINE_CLASS_1(thunder_mdio, thunder_mdio_fdt_driver, thunder_mdio_fdt_methods, 60 sizeof(struct thunder_mdio_softc), thunder_mdio_driver); 61 62 static devclass_t thunder_mdio_fdt_devclass; 63 64 EARLY_DRIVER_MODULE(thunder_mdio, ofwbus, thunder_mdio_fdt_driver, 65 thunder_mdio_fdt_devclass, 0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE); 66 EARLY_DRIVER_MODULE(thunder_mdio, mdionexus, thunder_mdio_fdt_driver, 67 thunder_mdio_fdt_devclass, 0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE); 68 69 static struct ofw_compat_data mdio_compat_data[] = { 70 {"cavium,octeon-3860-mdio", true}, 71 {"cavium,thunder-8890-mdio", true}, 72 {NULL, false} 73 }; 74 75 static int 76 thunder_mdio_fdt_probe(device_t dev) 77 { 78 79 if (!ofw_bus_status_okay(dev)) 80 return (ENXIO); 81 if (!ofw_bus_search_compatible(dev, mdio_compat_data)->ocd_data) 82 return (ENXIO); 83 84 device_set_desc(dev, THUNDER_MDIO_DEVSTR); 85 return (BUS_PROBE_DEFAULT); 86 } 87 88 static int 89 thunder_mdio_fdt_attach(device_t dev) 90 { 91 phandle_t node; 92 int ret; 93 94 /* Call core attach */ 95 ret = thunder_mdio_attach(dev); 96 if (ret != 0) 97 return (ret); 98 /* 99 * Register device to this node/xref. 100 * Thanks to that we will be able to retrieve device_t structure 101 * while holding only node reference acquired from FDT. 102 */ 103 node = ofw_bus_get_node(dev); 104 OF_device_register_xref(OF_xref_from_node(node), dev); 105 106 return (0); 107 } 108 109 struct mdionexus_softc { 110 struct simplebus_softc simplebus_sc; 111 }; 112 113 static device_probe_t mdionexus_fdt_probe; 114 static device_attach_t mdionexus_fdt_attach; 115 116 static const struct ofw_bus_devinfo * mdionexus_ofw_get_devinfo(device_t, 117 device_t); 118 119 static device_method_t mdionexus_fdt_methods[] = { 120 /* Device interface */ 121 DEVMETHOD(device_probe, mdionexus_fdt_probe), 122 DEVMETHOD(device_attach, mdionexus_fdt_attach), 123 124 /* Bus interface */ 125 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource), 126 DEVMETHOD(bus_release_resource, bus_generic_release_resource), 127 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 128 129 /* ofw_bus interface */ 130 DEVMETHOD(ofw_bus_get_devinfo, mdionexus_ofw_get_devinfo), 131 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), 132 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), 133 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), 134 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), 135 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), 136 137 DEVMETHOD_END 138 }; 139 140 DEFINE_CLASS_0(mdionexus, mdionexus_fdt_driver, mdionexus_fdt_methods, 141 sizeof(struct mdionexus_softc)); 142 143 static devclass_t mdionexus_fdt_devclass; 144 145 EARLY_DRIVER_MODULE(mdionexus, mrmlbus, mdionexus_fdt_driver, 146 mdionexus_fdt_devclass, 0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE); 147 148 static int mdionexus_ofw_fill_ranges(phandle_t, struct simplebus_softc *); 149 static int mdionexus_ofw_bus_attach(device_t); 150 151 static int 152 mdionexus_fdt_probe(device_t dev) 153 { 154 155 if (!ofw_bus_status_okay(dev)) 156 return (ENXIO); 157 158 if (!ofw_bus_is_compatible(dev, "cavium,thunder-8890-mdio-nexus")) 159 return (ENXIO); 160 161 device_set_desc(dev, "Cavium ThunderX MDIO nexus"); 162 return (BUS_PROBE_SPECIFIC); 163 } 164 165 static int 166 mdionexus_fdt_attach(device_t dev) 167 { 168 int err; 169 170 err = mdionexus_ofw_bus_attach(dev); 171 if (err != 0) 172 return (err); 173 174 return (bus_generic_attach(dev)); 175 } 176 177 /* OFW bus interface */ 178 struct mdionexus_ofw_devinfo { 179 struct ofw_bus_devinfo di_dinfo; 180 struct resource_list di_rl; 181 }; 182 183 static const struct ofw_bus_devinfo * 184 mdionexus_ofw_get_devinfo(device_t bus __unused, device_t child) 185 { 186 struct mdionexus_ofw_devinfo *di; 187 188 di = device_get_ivars(child); 189 return (&di->di_dinfo); 190 } 191 192 /* Helper functions */ 193 194 static int 195 mdionexus_ofw_fill_ranges(phandle_t node, struct simplebus_softc *sc) 196 { 197 int host_address_cells; 198 cell_t *base_ranges; 199 ssize_t nbase_ranges; 200 int err; 201 int i, j, k; 202 203 err = OF_searchencprop(OF_parent(node), "#address-cells", 204 &host_address_cells, sizeof(host_address_cells)); 205 if (err <= 0) 206 return (-1); 207 208 nbase_ranges = OF_getproplen(node, "ranges"); 209 if (nbase_ranges < 0) 210 return (-1); 211 sc->nranges = nbase_ranges / sizeof(cell_t) / 212 (sc->acells + host_address_cells + sc->scells); 213 if (sc->nranges == 0) 214 return (0); 215 216 sc->ranges = malloc(sc->nranges * sizeof(sc->ranges[0]), 217 M_THUNDER_MDIO, M_WAITOK); 218 base_ranges = malloc(nbase_ranges, M_THUNDER_MDIO, M_WAITOK); 219 OF_getencprop(node, "ranges", base_ranges, nbase_ranges); 220 221 for (i = 0, j = 0; i < sc->nranges; i++) { 222 sc->ranges[i].bus = 0; 223 for (k = 0; k < sc->acells; k++) { 224 sc->ranges[i].bus <<= 32; 225 sc->ranges[i].bus |= base_ranges[j++]; 226 } 227 sc->ranges[i].host = 0; 228 for (k = 0; k < host_address_cells; k++) { 229 sc->ranges[i].host <<= 32; 230 sc->ranges[i].host |= base_ranges[j++]; 231 } 232 sc->ranges[i].size = 0; 233 for (k = 0; k < sc->scells; k++) { 234 sc->ranges[i].size <<= 32; 235 sc->ranges[i].size |= base_ranges[j++]; 236 } 237 } 238 239 free(base_ranges, M_THUNDER_MDIO); 240 return (sc->nranges); 241 } 242 243 static int 244 mdionexus_ofw_bus_attach(device_t dev) 245 { 246 struct simplebus_softc *sc; 247 struct mdionexus_ofw_devinfo *di; 248 device_t child; 249 phandle_t parent, node; 250 251 parent = ofw_bus_get_node(dev); 252 simplebus_init(dev, parent); 253 254 sc = (struct simplebus_softc *)device_get_softc(dev); 255 256 if (mdionexus_ofw_fill_ranges(parent, sc) < 0) { 257 device_printf(dev, "could not get ranges\n"); 258 return (ENXIO); 259 } 260 /* Iterate through all bus subordinates */ 261 for (node = OF_child(parent); node > 0; node = OF_peer(node)) { 262 /* Allocate and populate devinfo. */ 263 di = malloc(sizeof(*di), M_THUNDER_MDIO, M_WAITOK | M_ZERO); 264 if (ofw_bus_gen_setup_devinfo(&di->di_dinfo, node) != 0) { 265 free(di, M_THUNDER_MDIO); 266 continue; 267 } 268 269 /* Initialize and populate resource list. */ 270 resource_list_init(&di->di_rl); 271 ofw_bus_reg_to_rl(dev, node, sc->acells, sc->scells, 272 &di->di_rl); 273 ofw_bus_intr_to_rl(dev, node, &di->di_rl, NULL); 274 275 /* Add newbus device for this FDT node */ 276 child = device_add_child(dev, NULL, -1); 277 if (child == NULL) { 278 resource_list_free(&di->di_rl); 279 ofw_bus_gen_destroy_devinfo(&di->di_dinfo); 280 free(di, M_THUNDER_MDIO); 281 continue; 282 } 283 284 device_set_ivars(child, di); 285 } 286 287 return (0); 288 } 289