1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2013 Nathan Whitehorn 5 * All rights reserved. 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 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/module.h> 34 #include <sys/bus.h> 35 #include <sys/conf.h> 36 #include <sys/kernel.h> 37 #include <sys/rman.h> 38 39 #include <dev/ofw/openfirm.h> 40 #include <dev/ofw/ofw_bus.h> 41 #include <dev/ofw/ofw_bus_subr.h> 42 43 #include <dev/fdt/simplebus.h> 44 45 /* 46 * Bus interface. 47 */ 48 static int simplebus_probe(device_t dev); 49 static int simplebus_attach(device_t dev); 50 static struct resource *simplebus_alloc_resource(device_t, device_t, int, 51 int *, rman_res_t, rman_res_t, rman_res_t, u_int); 52 static void simplebus_probe_nomatch(device_t bus, device_t child); 53 static int simplebus_print_child(device_t bus, device_t child); 54 static device_t simplebus_add_child(device_t dev, u_int order, 55 const char *name, int unit); 56 static struct resource_list *simplebus_get_resource_list(device_t bus, 57 device_t child); 58 /* 59 * ofw_bus interface 60 */ 61 static const struct ofw_bus_devinfo *simplebus_get_devinfo(device_t bus, 62 device_t child); 63 64 /* 65 * Driver methods. 66 */ 67 static device_method_t simplebus_methods[] = { 68 /* Device interface */ 69 DEVMETHOD(device_probe, simplebus_probe), 70 DEVMETHOD(device_attach, simplebus_attach), 71 DEVMETHOD(device_detach, bus_generic_detach), 72 DEVMETHOD(device_shutdown, bus_generic_shutdown), 73 DEVMETHOD(device_suspend, bus_generic_suspend), 74 DEVMETHOD(device_resume, bus_generic_resume), 75 76 /* Bus interface */ 77 DEVMETHOD(bus_add_child, simplebus_add_child), 78 DEVMETHOD(bus_print_child, simplebus_print_child), 79 DEVMETHOD(bus_probe_nomatch, simplebus_probe_nomatch), 80 DEVMETHOD(bus_read_ivar, bus_generic_read_ivar), 81 DEVMETHOD(bus_write_ivar, bus_generic_write_ivar), 82 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 83 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 84 DEVMETHOD(bus_alloc_resource, simplebus_alloc_resource), 85 DEVMETHOD(bus_release_resource, bus_generic_release_resource), 86 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 87 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 88 DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource), 89 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource), 90 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 91 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str), 92 DEVMETHOD(bus_get_resource_list, simplebus_get_resource_list), 93 94 /* ofw_bus interface */ 95 DEVMETHOD(ofw_bus_get_devinfo, simplebus_get_devinfo), 96 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), 97 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), 98 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), 99 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), 100 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), 101 102 DEVMETHOD_END 103 }; 104 105 DEFINE_CLASS_0(simplebus, simplebus_driver, simplebus_methods, 106 sizeof(struct simplebus_softc)); 107 108 static devclass_t simplebus_devclass; 109 EARLY_DRIVER_MODULE(simplebus, ofwbus, simplebus_driver, simplebus_devclass, 110 0, 0, BUS_PASS_BUS); 111 EARLY_DRIVER_MODULE(simplebus, simplebus, simplebus_driver, simplebus_devclass, 112 0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE); 113 114 static int 115 simplebus_probe(device_t dev) 116 { 117 118 if (!ofw_bus_status_okay(dev)) 119 return (ENXIO); 120 121 /* 122 * FDT data puts a "simple-bus" compatible string on many things that 123 * have children but aren't really buses in our world. Without a 124 * ranges property we will fail to attach, so just fail to probe too. 125 */ 126 if (!(ofw_bus_is_compatible(dev, "simple-bus") && 127 ofw_bus_has_prop(dev, "ranges")) && 128 (ofw_bus_get_type(dev) == NULL || strcmp(ofw_bus_get_type(dev), 129 "soc") != 0)) 130 return (ENXIO); 131 132 device_set_desc(dev, "Flattened device tree simple bus"); 133 134 return (BUS_PROBE_GENERIC); 135 } 136 137 static int 138 simplebus_attach(device_t dev) 139 { 140 struct simplebus_softc *sc; 141 phandle_t node; 142 143 sc = device_get_softc(dev); 144 simplebus_init(dev, 0); 145 if (simplebus_fill_ranges(sc->node, sc) < 0) { 146 device_printf(dev, "could not get ranges\n"); 147 return (ENXIO); 148 } 149 150 /* 151 * In principle, simplebus could have an interrupt map, but ignore that 152 * for now 153 */ 154 155 for (node = OF_child(sc->node); node > 0; node = OF_peer(node)) 156 simplebus_add_device(dev, node, 0, NULL, -1, NULL); 157 return (bus_generic_attach(dev)); 158 } 159 160 void 161 simplebus_init(device_t dev, phandle_t node) 162 { 163 struct simplebus_softc *sc; 164 165 sc = device_get_softc(dev); 166 if (node == 0) 167 node = ofw_bus_get_node(dev); 168 sc->dev = dev; 169 sc->node = node; 170 171 /* 172 * Some important numbers 173 */ 174 sc->acells = 2; 175 OF_getencprop(node, "#address-cells", &sc->acells, sizeof(sc->acells)); 176 sc->scells = 1; 177 OF_getencprop(node, "#size-cells", &sc->scells, sizeof(sc->scells)); 178 } 179 180 int 181 simplebus_fill_ranges(phandle_t node, struct simplebus_softc *sc) 182 { 183 int host_address_cells; 184 cell_t *base_ranges; 185 ssize_t nbase_ranges; 186 int err; 187 int i, j, k; 188 189 err = OF_searchencprop(OF_parent(node), "#address-cells", 190 &host_address_cells, sizeof(host_address_cells)); 191 if (err <= 0) 192 return (-1); 193 194 nbase_ranges = OF_getproplen(node, "ranges"); 195 if (nbase_ranges < 0) 196 return (-1); 197 sc->nranges = nbase_ranges / sizeof(cell_t) / 198 (sc->acells + host_address_cells + sc->scells); 199 if (sc->nranges == 0) 200 return (0); 201 202 sc->ranges = malloc(sc->nranges * sizeof(sc->ranges[0]), 203 M_DEVBUF, M_WAITOK); 204 base_ranges = malloc(nbase_ranges, M_DEVBUF, M_WAITOK); 205 OF_getencprop(node, "ranges", base_ranges, nbase_ranges); 206 207 for (i = 0, j = 0; i < sc->nranges; i++) { 208 sc->ranges[i].bus = 0; 209 for (k = 0; k < sc->acells; k++) { 210 sc->ranges[i].bus <<= 32; 211 sc->ranges[i].bus |= base_ranges[j++]; 212 } 213 sc->ranges[i].host = 0; 214 for (k = 0; k < host_address_cells; k++) { 215 sc->ranges[i].host <<= 32; 216 sc->ranges[i].host |= base_ranges[j++]; 217 } 218 sc->ranges[i].size = 0; 219 for (k = 0; k < sc->scells; k++) { 220 sc->ranges[i].size <<= 32; 221 sc->ranges[i].size |= base_ranges[j++]; 222 } 223 } 224 225 free(base_ranges, M_DEVBUF); 226 return (sc->nranges); 227 } 228 229 struct simplebus_devinfo * 230 simplebus_setup_dinfo(device_t dev, phandle_t node, 231 struct simplebus_devinfo *di) 232 { 233 struct simplebus_softc *sc; 234 struct simplebus_devinfo *ndi; 235 236 sc = device_get_softc(dev); 237 if (di == NULL) 238 ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO); 239 else 240 ndi = di; 241 if (ofw_bus_gen_setup_devinfo(&ndi->obdinfo, node) != 0) { 242 if (di == NULL) 243 free(ndi, M_DEVBUF); 244 return (NULL); 245 } 246 247 resource_list_init(&ndi->rl); 248 ofw_bus_reg_to_rl(dev, node, sc->acells, sc->scells, &ndi->rl); 249 ofw_bus_intr_to_rl(dev, node, &ndi->rl, NULL); 250 251 return (ndi); 252 } 253 254 device_t 255 simplebus_add_device(device_t dev, phandle_t node, u_int order, 256 const char *name, int unit, struct simplebus_devinfo *di) 257 { 258 struct simplebus_devinfo *ndi; 259 device_t cdev; 260 261 if ((ndi = simplebus_setup_dinfo(dev, node, di)) == NULL) 262 return (NULL); 263 cdev = device_add_child_ordered(dev, order, name, unit); 264 if (cdev == NULL) { 265 device_printf(dev, "<%s>: device_add_child failed\n", 266 ndi->obdinfo.obd_name); 267 resource_list_free(&ndi->rl); 268 ofw_bus_gen_destroy_devinfo(&ndi->obdinfo); 269 if (di == NULL) 270 free(ndi, M_DEVBUF); 271 return (NULL); 272 } 273 device_set_ivars(cdev, ndi); 274 275 return(cdev); 276 } 277 278 static device_t 279 simplebus_add_child(device_t dev, u_int order, const char *name, int unit) 280 { 281 device_t cdev; 282 struct simplebus_devinfo *ndi; 283 284 cdev = device_add_child_ordered(dev, order, name, unit); 285 if (cdev == NULL) 286 return (NULL); 287 288 ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO); 289 ndi->obdinfo.obd_node = -1; 290 resource_list_init(&ndi->rl); 291 device_set_ivars(cdev, ndi); 292 293 return (cdev); 294 } 295 296 static const struct ofw_bus_devinfo * 297 simplebus_get_devinfo(device_t bus __unused, device_t child) 298 { 299 struct simplebus_devinfo *ndi; 300 301 ndi = device_get_ivars(child); 302 if (ndi == NULL) 303 return (NULL); 304 return (&ndi->obdinfo); 305 } 306 307 static struct resource_list * 308 simplebus_get_resource_list(device_t bus __unused, device_t child) 309 { 310 struct simplebus_devinfo *ndi; 311 312 ndi = device_get_ivars(child); 313 if (ndi == NULL) 314 return (NULL); 315 return (&ndi->rl); 316 } 317 318 static struct resource * 319 simplebus_alloc_resource(device_t bus, device_t child, int type, int *rid, 320 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 321 { 322 struct simplebus_softc *sc; 323 struct simplebus_devinfo *di; 324 struct resource_list_entry *rle; 325 int j; 326 327 sc = device_get_softc(bus); 328 329 /* 330 * Request for the default allocation with a given rid: use resource 331 * list stored in the local device info. 332 */ 333 if (RMAN_IS_DEFAULT_RANGE(start, end)) { 334 if ((di = device_get_ivars(child)) == NULL) 335 return (NULL); 336 337 if (type == SYS_RES_IOPORT) 338 type = SYS_RES_MEMORY; 339 340 rle = resource_list_find(&di->rl, type, *rid); 341 if (rle == NULL) { 342 if (bootverbose) 343 device_printf(bus, "no default resources for " 344 "rid = %d, type = %d\n", *rid, type); 345 return (NULL); 346 } 347 start = rle->start; 348 end = rle->end; 349 count = rle->count; 350 } 351 352 if (type == SYS_RES_MEMORY) { 353 /* Remap through ranges property */ 354 for (j = 0; j < sc->nranges; j++) { 355 if (start >= sc->ranges[j].bus && end < 356 sc->ranges[j].bus + sc->ranges[j].size) { 357 start -= sc->ranges[j].bus; 358 start += sc->ranges[j].host; 359 end -= sc->ranges[j].bus; 360 end += sc->ranges[j].host; 361 break; 362 } 363 } 364 if (j == sc->nranges && sc->nranges != 0) { 365 if (bootverbose) 366 device_printf(bus, "Could not map resource " 367 "%#jx-%#jx\n", start, end); 368 369 return (NULL); 370 } 371 } 372 373 return (bus_generic_alloc_resource(bus, child, type, rid, start, end, 374 count, flags)); 375 } 376 377 static int 378 simplebus_print_res(struct simplebus_devinfo *di) 379 { 380 int rv; 381 382 if (di == NULL) 383 return (0); 384 rv = 0; 385 rv += resource_list_print_type(&di->rl, "mem", SYS_RES_MEMORY, "%#jx"); 386 rv += resource_list_print_type(&di->rl, "irq", SYS_RES_IRQ, "%jd"); 387 return (rv); 388 } 389 390 static void 391 simplebus_probe_nomatch(device_t bus, device_t child) 392 { 393 const char *name, *type, *compat; 394 395 if (!bootverbose) 396 return; 397 398 compat = ofw_bus_get_compat(child); 399 if (compat == NULL) 400 return; 401 name = ofw_bus_get_name(child); 402 type = ofw_bus_get_type(child); 403 404 device_printf(bus, "<%s>", name != NULL ? name : "unknown"); 405 simplebus_print_res(device_get_ivars(child)); 406 if (!ofw_bus_status_okay(child)) 407 printf(" disabled"); 408 if (type) 409 printf(" type %s", type); 410 printf(" compat %s (no driver attached)\n", compat); 411 } 412 413 static int 414 simplebus_print_child(device_t bus, device_t child) 415 { 416 int rv; 417 418 rv = bus_print_child_header(bus, child); 419 rv += simplebus_print_res(device_get_ivars(child)); 420 if (!ofw_bus_status_okay(child)) 421 rv += printf(" disabled"); 422 rv += bus_print_child_footer(bus, child); 423 return (rv); 424 } 425