simplebus.c revision 266000
1/*- 2 * Copyright (c) 2009-2010 The FreeBSD Foundation 3 * All rights reserved. 4 * 5 * This software was developed by Semihalf under sponsorship from 6 * 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: stable/10/sys/dev/fdt/simplebus.c 266000 2014-05-14 01:53:20Z ian $"); 32 33#include "opt_platform.h" 34#include <sys/param.h> 35#include <sys/systm.h> 36#include <sys/ktr.h> 37#include <sys/kernel.h> 38#include <sys/module.h> 39#include <sys/bus.h> 40#include <sys/rman.h> 41#include <sys/malloc.h> 42 43#include <machine/fdt.h> 44 45#include <dev/ofw/ofw_bus.h> 46#include <dev/ofw/ofw_bus_subr.h> 47#include <dev/ofw/openfirm.h> 48 49#include "fdt_common.h" 50#include "fdt_ic_if.h" 51#include "ofw_bus_if.h" 52 53#ifdef DEBUG 54#define debugf(fmt, args...) do { printf("%s(): ", __func__); \ 55 printf(fmt,##args); } while (0) 56#else 57#define debugf(fmt, args...) 58#endif 59 60static MALLOC_DEFINE(M_SIMPLEBUS, "simplebus", "simplebus devices information"); 61 62struct simplebus_softc { 63 int sc_addr_cells; 64 int sc_size_cells; 65}; 66 67struct simplebus_devinfo { 68 struct ofw_bus_devinfo di_ofw; 69 struct resource_list di_res; 70 71 /* Interrupts sense-level info for this device */ 72 struct fdt_sense_level di_intr_sl[DI_MAX_INTR_NUM]; 73}; 74 75/* 76 * Prototypes. 77 */ 78static int simplebus_probe(device_t); 79static int simplebus_attach(device_t); 80 81static int simplebus_print_child(device_t, device_t); 82static int simplebus_setup_intr(device_t, device_t, struct resource *, int, 83 driver_filter_t *, driver_intr_t *, void *, void **); 84static int simplebus_teardown_intr(device_t, device_t, struct resource *, 85 void *); 86 87static int simplebus_activate_resource(device_t, device_t, int, int, 88 struct resource *); 89static struct resource *simplebus_alloc_resource(device_t, device_t, int, 90 int *, u_long, u_long, u_long, u_int); 91static int simplebus_deactivate_resource(device_t, device_t, int, int, 92 struct resource *); 93static int simplebus_release_resource(device_t, device_t, int, int, 94 struct resource *); 95static device_t simplebus_get_interrupt_parent(device_t); 96static struct resource_list *simplebus_get_resource_list(device_t, device_t); 97 98static ofw_bus_get_devinfo_t simplebus_get_devinfo; 99 100/* 101 * Bus interface definition. 102 */ 103static device_method_t simplebus_methods[] = { 104 /* Device interface */ 105 DEVMETHOD(device_probe, simplebus_probe), 106 DEVMETHOD(device_attach, simplebus_attach), 107 DEVMETHOD(device_detach, bus_generic_detach), 108 DEVMETHOD(device_shutdown, bus_generic_shutdown), 109 DEVMETHOD(device_suspend, bus_generic_suspend), 110 DEVMETHOD(device_resume, bus_generic_resume), 111 112 /* Bus interface */ 113 DEVMETHOD(bus_print_child, simplebus_print_child), 114 DEVMETHOD(bus_alloc_resource, simplebus_alloc_resource), 115 DEVMETHOD(bus_release_resource, simplebus_release_resource), 116 DEVMETHOD(bus_activate_resource, simplebus_activate_resource), 117 DEVMETHOD(bus_deactivate_resource, simplebus_deactivate_resource), 118 DEVMETHOD(bus_setup_intr, simplebus_setup_intr), 119 DEVMETHOD(bus_teardown_intr, simplebus_teardown_intr), 120 DEVMETHOD(bus_get_resource_list, simplebus_get_resource_list), 121 122 /* OFW bus interface */ 123 DEVMETHOD(ofw_bus_get_devinfo, simplebus_get_devinfo), 124 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), 125 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), 126 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), 127 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), 128 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), 129 130 { 0, 0 } 131}; 132 133static driver_t simplebus_driver = { 134 "simplebus", 135 simplebus_methods, 136 sizeof(struct simplebus_softc) 137}; 138 139devclass_t simplebus_devclass; 140 141DRIVER_MODULE(simplebus, nexus, simplebus_driver, simplebus_devclass, 0, 0); 142DRIVER_MODULE(simplebus, simplebus, simplebus_driver, simplebus_devclass, 0, 143 0); 144 145static int 146simplebus_probe(device_t dev) 147{ 148 149 if (!ofw_bus_is_compatible(dev, "simple-bus")) 150 return (ENXIO); 151 152 device_set_desc(dev, "Flattened device tree simple bus"); 153 154 return (BUS_PROBE_GENERIC); 155} 156 157static int 158simplebus_attach(device_t dev) 159{ 160 device_t dev_child; 161 struct simplebus_devinfo *di; 162 struct simplebus_softc *sc; 163 phandle_t dt_node, dt_child; 164 165 sc = device_get_softc(dev); 166 167 /* 168 * Walk simple-bus and add direct subordinates as our children. 169 */ 170 dt_node = ofw_bus_get_node(dev); 171 for (dt_child = OF_child(dt_node); dt_child != 0; 172 dt_child = OF_peer(dt_child)) { 173 174 /* Check and process 'status' property. */ 175 if (!(fdt_is_enabled(dt_child))) 176 continue; 177 178 if (!(fdt_pm_is_enabled(dt_child))) 179 continue; 180 181 di = malloc(sizeof(*di), M_SIMPLEBUS, M_WAITOK | M_ZERO); 182 183 if (ofw_bus_gen_setup_devinfo(&di->di_ofw, dt_child) != 0) { 184 free(di, M_SIMPLEBUS); 185 device_printf(dev, "could not set up devinfo\n"); 186 continue; 187 } 188 189 resource_list_init(&di->di_res); 190 if (fdt_reg_to_rl(dt_child, &di->di_res)) { 191 device_printf(dev, 192 "%s: could not process 'reg' " 193 "property\n", di->di_ofw.obd_name); 194 ofw_bus_gen_destroy_devinfo(&di->di_ofw); 195 free(di, M_SIMPLEBUS); 196 continue; 197 } 198 199 if (fdt_intr_to_rl(dt_child, &di->di_res, di->di_intr_sl)) { 200 device_printf(dev, "%s: could not process " 201 "'interrupts' property\n", di->di_ofw.obd_name); 202 resource_list_free(&di->di_res); 203 ofw_bus_gen_destroy_devinfo(&di->di_ofw); 204 free(di, M_SIMPLEBUS); 205 continue; 206 } 207 208 /* Add newbus device for this FDT node */ 209 dev_child = device_add_child(dev, NULL, -1); 210 if (dev_child == NULL) { 211 device_printf(dev, "could not add child: %s\n", 212 di->di_ofw.obd_name); 213 resource_list_free(&di->di_res); 214 ofw_bus_gen_destroy_devinfo(&di->di_ofw); 215 free(di, M_SIMPLEBUS); 216 continue; 217 } 218#ifdef DEBUG 219 device_printf(dev, "added child: %s\n\n", di->di_ofw.obd_name); 220#endif 221 device_set_ivars(dev_child, di); 222 } 223 224 return (bus_generic_attach(dev)); 225} 226 227static int 228simplebus_print_child(device_t dev, device_t child) 229{ 230 device_t ip; 231 struct simplebus_devinfo *di; 232 struct resource_list *rl; 233 int rv; 234 235 di = device_get_ivars(child); 236 rl = &di->di_res; 237 238 rv = 0; 239 rv += bus_print_child_header(dev, child); 240 rv += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx"); 241 rv += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 242 if ((ip = simplebus_get_interrupt_parent(child)) != NULL) 243 rv += printf(" (%s)", device_get_nameunit(ip)); 244 rv += bus_print_child_footer(dev, child); 245 246 return (rv); 247} 248 249static struct resource * 250simplebus_alloc_resource(device_t bus, device_t child, int type, int *rid, 251 u_long start, u_long end, u_long count, u_int flags) 252{ 253 device_t ic; 254 struct simplebus_devinfo *di; 255 struct resource_list_entry *rle; 256 257 /* 258 * Request for the default allocation with a given rid: use resource 259 * list stored in the local device info. 260 */ 261 if ((start == 0UL) && (end == ~0UL)) { 262 if ((di = device_get_ivars(child)) == NULL) 263 return (NULL); 264 265 if (type == SYS_RES_IOPORT) 266 type = SYS_RES_MEMORY; 267 268 rle = resource_list_find(&di->di_res, type, *rid); 269 if (rle == NULL) { 270 if (bootverbose) 271 device_printf(bus, "no default resources for " 272 "rid = %d, type = %d\n", *rid, type); 273 return (NULL); 274 } 275 start = rle->start; 276 end = rle->end; 277 count = rle->count; 278 } 279 280 if (type == SYS_RES_IRQ && 281 (ic = simplebus_get_interrupt_parent(child)) != NULL) 282 return(FDT_IC_ALLOC_INTR(ic, child, rid, start, flags)); 283 284 return (bus_generic_alloc_resource(bus, child, type, rid, start, end, 285 count, flags)); 286} 287 288static int 289simplebus_activate_resource(device_t dev, device_t child, int type, int rid, 290 struct resource *r) 291{ 292 device_t ic; 293 294 if (type == SYS_RES_IRQ && 295 (ic = simplebus_get_interrupt_parent(child)) != NULL) 296 return (FDT_IC_ACTIVATE_INTR(ic, r)); 297 298 return (bus_generic_activate_resource(dev, child, type, rid, r)); 299} 300 301static int 302simplebus_deactivate_resource(device_t dev, device_t child, int type, int rid, 303 struct resource *r) 304{ 305 device_t ic; 306 307 if (type == SYS_RES_IRQ && 308 (ic = simplebus_get_interrupt_parent(child)) != NULL) 309 return (FDT_IC_DEACTIVATE_INTR(ic, r)); 310 311 return (bus_generic_deactivate_resource(dev, child, type, rid, r)); 312} 313 314static int 315simplebus_release_resource(device_t dev, device_t child, int type, int rid, 316 struct resource *r) 317{ 318 device_t ic; 319 320 if (type == SYS_RES_IRQ && 321 (ic = simplebus_get_interrupt_parent(child)) != NULL) 322 return (FDT_IC_RELEASE_INTR(ic, r)); 323 324 return (bus_generic_release_resource(dev, child, type, rid, r)); 325} 326 327static struct resource_list * 328simplebus_get_resource_list(device_t bus, device_t child) 329{ 330 struct simplebus_devinfo *di; 331 332 di = device_get_ivars(child); 333 return (&di->di_res); 334} 335 336static device_t 337simplebus_get_interrupt_parent(device_t dev) 338{ 339 struct simplebus_devinfo *di; 340 struct fdt_ic *ic; 341 device_t ip; 342 phandle_t ph, iph; 343 344 ip = NULL; 345 346 di = device_get_ivars(dev); 347 if (di == NULL) 348 return (NULL); 349 350 if (OF_getencprop(di->di_ofw.obd_node, "interrupt-parent", &iph, 351 sizeof(iph)) > 0) { 352 ph = OF_xref_phandle(iph); 353 SLIST_FOREACH(ic, &fdt_ic_list_head, fdt_ics) { 354 if (ic->iph == ph) { 355 ip = ic->dev; 356 break; 357 } 358 } 359 } 360 return (ip); 361} 362 363static int 364simplebus_setup_intr(device_t bus, device_t child, struct resource *res, 365 int flags, driver_filter_t *filter, driver_intr_t *ihand, void *arg, 366 void **cookiep) 367{ 368 struct simplebus_devinfo *di; 369 device_t ic; 370 enum intr_trigger trig; 371 enum intr_polarity pol; 372 int error, irq, rid; 373 374 di = device_get_ivars(child); 375 if (di == NULL) 376 return (ENXIO); 377 378 if (res == NULL) 379 return (EINVAL); 380 381 rid = rman_get_rid(res); 382 if (rid >= DI_MAX_INTR_NUM) 383 return (ENOENT); 384 385 ic = simplebus_get_interrupt_parent(child); 386 387 trig = di->di_intr_sl[rid].trig; 388 pol = di->di_intr_sl[rid].pol; 389 if (trig != INTR_TRIGGER_CONFORM || pol != INTR_POLARITY_CONFORM) { 390 irq = rman_get_start(res); 391 if (ic != NULL) 392 error = FDT_IC_CONFIG_INTR(ic, irq, trig, pol); 393 else 394 error = bus_generic_config_intr(bus, irq, trig, pol); 395 if (error) 396 return (error); 397 } 398 399 if (ic != NULL) 400 error = FDT_IC_SETUP_INTR(ic, child, res, flags, filter, 401 ihand, arg, cookiep); 402 else 403 error = bus_generic_setup_intr(bus, child, res, flags, filter, 404 ihand, arg, cookiep); 405 return (error); 406} 407 408static int 409simplebus_teardown_intr(device_t bus, device_t child, struct resource *res, 410 void *cookie) 411{ 412 device_t ic; 413 414 if ((ic = simplebus_get_interrupt_parent(child)) != NULL) 415 return (FDT_IC_TEARDOWN_INTR(ic, child, res, cookie)); 416 417 return (bus_generic_teardown_intr(bus, child, res, cookie)); 418} 419 420static const struct ofw_bus_devinfo * 421simplebus_get_devinfo(device_t bus, device_t child) 422{ 423 struct simplebus_devinfo *di; 424 425 di = device_get_ivars(child); 426 return (&di->di_ofw); 427} 428