1208747Sraj/*- 2208747Sraj * Copyright (c) 2009-2010 The FreeBSD Foundation 3208747Sraj * All rights reserved. 4208747Sraj * 5208747Sraj * This software was developed by Semihalf under sponsorship from 6208747Sraj * the FreeBSD Foundation. 7208747Sraj * 8208747Sraj * Redistribution and use in source and binary forms, with or without 9208747Sraj * modification, are permitted provided that the following conditions 10208747Sraj * are met: 11208747Sraj * 1. Redistributions of source code must retain the above copyright 12208747Sraj * notice, this list of conditions and the following disclaimer. 13208747Sraj * 2. Redistributions in binary form must reproduce the above copyright 14208747Sraj * notice, this list of conditions and the following disclaimer in the 15208747Sraj * documentation and/or other materials provided with the distribution. 16208747Sraj * 17208747Sraj * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18208747Sraj * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19208747Sraj * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20208747Sraj * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21208747Sraj * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22208747Sraj * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23208747Sraj * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24208747Sraj * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25208747Sraj * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26208747Sraj * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27208747Sraj * SUCH DAMAGE. 28208747Sraj */ 29208747Sraj 30208747Sraj#include <sys/cdefs.h> 31208747Sraj__FBSDID("$FreeBSD$"); 32208747Sraj 33208747Sraj#include "opt_platform.h" 34208747Sraj#include <sys/param.h> 35208747Sraj#include <sys/systm.h> 36208747Sraj#include <sys/ktr.h> 37208747Sraj#include <sys/kernel.h> 38208747Sraj#include <sys/module.h> 39208747Sraj#include <sys/bus.h> 40208747Sraj#include <sys/rman.h> 41208747Sraj#include <sys/malloc.h> 42208747Sraj 43208747Sraj#include <machine/fdt.h> 44208747Sraj 45208747Sraj#include <dev/ofw/ofw_bus.h> 46208747Sraj#include <dev/ofw/ofw_bus_subr.h> 47208747Sraj#include <dev/ofw/openfirm.h> 48208747Sraj 49208747Sraj#include "fdt_common.h" 50208747Sraj#include "ofw_bus_if.h" 51208747Sraj 52208747Sraj#define DEBUG 53208747Sraj#undef DEBUG 54208747Sraj 55208747Sraj#ifdef DEBUG 56208747Sraj#define debugf(fmt, args...) do { printf("%s(): ", __func__); \ 57208747Sraj printf(fmt,##args); } while (0) 58208747Sraj#else 59208747Sraj#define debugf(fmt, args...) 60208747Sraj#endif 61208747Sraj 62208747Srajstatic MALLOC_DEFINE(M_SIMPLEBUS, "simplebus", "simplebus devices information"); 63208747Sraj 64208747Srajstruct simplebus_softc { 65208747Sraj int sc_addr_cells; 66208747Sraj int sc_size_cells; 67208747Sraj u_long sc_start_pa; 68208747Sraj u_long sc_start_va; 69208747Sraj u_long sc_size; 70208747Sraj}; 71208747Sraj 72208747Srajstruct simplebus_devinfo { 73208747Sraj struct ofw_bus_devinfo di_ofw; 74208747Sraj struct resource_list di_res; 75208747Sraj 76208747Sraj /* Interrupts sense-level info for this device */ 77208747Sraj struct fdt_sense_level di_intr_sl[DI_MAX_INTR_NUM]; 78208747Sraj}; 79208747Sraj 80208747Sraj/* 81208747Sraj * Prototypes. 82208747Sraj */ 83208747Srajstatic int simplebus_probe(device_t); 84208747Srajstatic int simplebus_attach(device_t); 85208747Sraj 86208747Srajstatic int simplebus_print_child(device_t, device_t); 87208747Srajstatic int simplebus_setup_intr(device_t, device_t, struct resource *, int, 88208747Sraj driver_filter_t *, driver_intr_t *, void *, void **); 89208747Sraj 90208747Srajstatic struct resource *simplebus_alloc_resource(device_t, device_t, int, 91208747Sraj int *, u_long, u_long, u_long, u_int); 92208747Srajstatic struct resource_list *simplebus_get_resource_list(device_t, device_t); 93208747Sraj 94208747Srajstatic ofw_bus_get_devinfo_t simplebus_get_devinfo; 95208747Sraj 96208747Sraj/* 97208747Sraj * Bus interface definition. 98208747Sraj */ 99208747Srajstatic device_method_t simplebus_methods[] = { 100208747Sraj /* Device interface */ 101208747Sraj DEVMETHOD(device_probe, simplebus_probe), 102208747Sraj DEVMETHOD(device_attach, simplebus_attach), 103208747Sraj DEVMETHOD(device_detach, bus_generic_detach), 104208747Sraj DEVMETHOD(device_shutdown, bus_generic_shutdown), 105208747Sraj DEVMETHOD(device_suspend, bus_generic_suspend), 106208747Sraj DEVMETHOD(device_resume, bus_generic_resume), 107208747Sraj 108208747Sraj /* Bus interface */ 109208747Sraj DEVMETHOD(bus_print_child, simplebus_print_child), 110208747Sraj DEVMETHOD(bus_alloc_resource, simplebus_alloc_resource), 111208747Sraj DEVMETHOD(bus_release_resource, bus_generic_release_resource), 112208747Sraj DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 113208747Sraj DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 114208747Sraj DEVMETHOD(bus_setup_intr, simplebus_setup_intr), 115208747Sraj DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 116208747Sraj DEVMETHOD(bus_get_resource_list, simplebus_get_resource_list), 117208747Sraj 118208747Sraj /* OFW bus interface */ 119208747Sraj DEVMETHOD(ofw_bus_get_devinfo, simplebus_get_devinfo), 120208747Sraj DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), 121208747Sraj DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), 122208747Sraj DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), 123208747Sraj DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), 124208747Sraj DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), 125208747Sraj 126208747Sraj { 0, 0 } 127208747Sraj}; 128208747Sraj 129208747Srajstatic driver_t simplebus_driver = { 130208747Sraj "simplebus", 131208747Sraj simplebus_methods, 132208747Sraj sizeof(struct simplebus_softc) 133208747Sraj}; 134208747Sraj 135208747Srajdevclass_t simplebus_devclass; 136208747Sraj 137208747SrajDRIVER_MODULE(simplebus, fdtbus, simplebus_driver, simplebus_devclass, 0, 0); 138208747Sraj 139208747Srajstatic int 140208747Srajsimplebus_probe(device_t dev) 141208747Sraj{ 142208747Sraj 143208747Sraj if (!ofw_bus_is_compatible_strict(dev, "simple-bus")) 144208747Sraj return (ENXIO); 145208747Sraj 146208747Sraj device_set_desc(dev, "Flattened device tree simple bus"); 147208747Sraj 148208747Sraj return (BUS_PROBE_DEFAULT); 149208747Sraj} 150208747Sraj 151208747Srajstatic int 152208747Srajsimplebus_attach(device_t dev) 153208747Sraj{ 154208747Sraj device_t dev_child; 155208747Sraj struct simplebus_devinfo *di; 156208747Sraj struct simplebus_softc *sc; 157208747Sraj phandle_t dt_node, dt_child; 158208747Sraj 159208747Sraj sc = device_get_softc(dev); 160208747Sraj 161208747Sraj sc->sc_start_pa = fdt_immr_pa; 162208747Sraj sc->sc_start_va = fdt_immr_va; 163208747Sraj sc->sc_size = fdt_immr_size; 164208747Sraj 165208747Sraj /* 166208747Sraj * Walk simple-bus and add direct subordinates as our children. 167208747Sraj */ 168208747Sraj dt_node = ofw_bus_get_node(dev); 169208747Sraj for (dt_child = OF_child(dt_node); dt_child != 0; 170208747Sraj dt_child = OF_peer(dt_child)) { 171208747Sraj 172208747Sraj /* Check and process 'status' property. */ 173208747Sraj if (!(fdt_is_enabled(dt_child))) 174208747Sraj continue; 175208747Sraj 176208747Sraj if (!(fdt_pm_is_enabled(dt_child))) 177208747Sraj continue; 178208747Sraj 179208747Sraj di = malloc(sizeof(*di), M_SIMPLEBUS, M_WAITOK | M_ZERO); 180208747Sraj 181208747Sraj if (ofw_bus_gen_setup_devinfo(&di->di_ofw, dt_child) != 0) { 182208747Sraj free(di, M_SIMPLEBUS); 183208747Sraj device_printf(dev, "could not set up devinfo\n"); 184208747Sraj continue; 185208747Sraj } 186208747Sraj 187208747Sraj resource_list_init(&di->di_res); 188208747Sraj 189208747Sraj if (fdt_reg_to_rl(dt_child, &di->di_res, sc->sc_start_va)) { 190209904Sraj device_printf(dev, "%s: could not process 'reg' " 191209904Sraj "property\n", di->di_ofw.obd_name); 192208747Sraj ofw_bus_gen_destroy_devinfo(&di->di_ofw); 193208747Sraj free(di, M_SIMPLEBUS); 194208747Sraj continue; 195208747Sraj } 196208747Sraj 197208747Sraj if (fdt_intr_to_rl(dt_child, &di->di_res, di->di_intr_sl)) { 198209904Sraj device_printf(dev, "%s: could not process " 199209904Sraj "'interrupts' property\n", di->di_ofw.obd_name); 200208747Sraj resource_list_free(&di->di_res); 201208747Sraj ofw_bus_gen_destroy_devinfo(&di->di_ofw); 202208747Sraj free(di, M_SIMPLEBUS); 203208747Sraj continue; 204208747Sraj } 205208747Sraj 206208747Sraj /* Add newbus device for this FDT node */ 207208747Sraj dev_child = device_add_child(dev, NULL, -1); 208208747Sraj if (dev_child == NULL) { 209208747Sraj device_printf(dev, "could not add child: %s\n", 210208747Sraj di->di_ofw.obd_name); 211208747Sraj resource_list_free(&di->di_res); 212208747Sraj ofw_bus_gen_destroy_devinfo(&di->di_ofw); 213208747Sraj free(di, M_SIMPLEBUS); 214208747Sraj continue; 215208747Sraj } 216209904Sraj#ifdef DEBUG 217209904Sraj device_printf(dev, "added child: %s\n\n", di->di_ofw.obd_name); 218209904Sraj#endif 219208747Sraj device_set_ivars(dev_child, di); 220208747Sraj } 221208747Sraj 222208747Sraj return (bus_generic_attach(dev)); 223208747Sraj} 224208747Sraj 225208747Srajstatic int 226208747Srajsimplebus_print_child(device_t dev, device_t child) 227208747Sraj{ 228208747Sraj struct simplebus_devinfo *di; 229208747Sraj struct resource_list *rl; 230208747Sraj int rv; 231208747Sraj 232208747Sraj di = device_get_ivars(child); 233208747Sraj rl = &di->di_res; 234208747Sraj 235208747Sraj rv = 0; 236208747Sraj rv += bus_print_child_header(dev, child); 237208747Sraj rv += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx"); 238208747Sraj rv += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 239208747Sraj rv += bus_print_child_footer(dev, child); 240208747Sraj 241208747Sraj return (rv); 242208747Sraj} 243208747Sraj 244208747Srajstatic struct resource * 245208747Srajsimplebus_alloc_resource(device_t bus, device_t child, int type, int *rid, 246208747Sraj u_long start, u_long end, u_long count, u_int flags) 247208747Sraj{ 248208747Sraj struct simplebus_devinfo *di; 249208747Sraj struct resource_list_entry *rle; 250208747Sraj 251208747Sraj /* 252208747Sraj * Request for the default allocation with a given rid: use resource 253208747Sraj * list stored in the local device info. 254208747Sraj */ 255208747Sraj if ((start == 0UL) && (end == ~0UL)) { 256208747Sraj if ((di = device_get_ivars(child)) == NULL) 257208747Sraj return (NULL); 258208747Sraj 259208747Sraj if (type == SYS_RES_IOPORT) 260208747Sraj type = SYS_RES_MEMORY; 261208747Sraj 262208747Sraj rle = resource_list_find(&di->di_res, type, *rid); 263208747Sraj if (rle == NULL) { 264208747Sraj device_printf(bus, "no default resources for " 265208747Sraj "rid = %d, type = %d\n", *rid, type); 266208747Sraj return (NULL); 267208747Sraj } 268208747Sraj start = rle->start; 269208747Sraj end = rle->end; 270208747Sraj count = rle->count; 271208747Sraj } 272208747Sraj 273208747Sraj return (bus_generic_alloc_resource(bus, child, type, rid, start, end, 274208747Sraj count, flags)); 275208747Sraj} 276208747Sraj 277208747Srajstatic struct resource_list * 278208747Srajsimplebus_get_resource_list(device_t bus, device_t child) 279208747Sraj{ 280208747Sraj struct simplebus_devinfo *di; 281208747Sraj 282208747Sraj di = device_get_ivars(child); 283208747Sraj return (&di->di_res); 284208747Sraj} 285208747Sraj 286208747Srajstatic int 287208747Srajsimplebus_setup_intr(device_t bus, device_t child, struct resource *res, 288208747Sraj int flags, driver_filter_t *filter, driver_intr_t *ihand, void *arg, 289208747Sraj void **cookiep) 290208747Sraj{ 291208747Sraj struct simplebus_devinfo *di; 292208747Sraj enum intr_trigger trig; 293208747Sraj enum intr_polarity pol; 294208747Sraj int irq, rid; 295208747Sraj 296208747Sraj if (res == NULL) 297208747Sraj panic("simplebus_setup_intr: NULL irq resource!"); 298208747Sraj 299208747Sraj rid = rman_get_rid(res); 300208747Sraj if (rid > DI_MAX_INTR_NUM) { 301208747Sraj device_printf(child, "rid out of range rid = %d\n", rid); 302208747Sraj return (ERANGE); 303208747Sraj } 304208747Sraj 305208747Sraj irq = rman_get_start(res); 306208747Sraj 307208747Sraj if ((di = device_get_ivars(child)) == NULL) { 308208747Sraj device_printf(child, "could not retrieve devinfo\n"); 309208747Sraj return (ENXIO); 310208747Sraj } 311208747Sraj 312208747Sraj trig = di->di_intr_sl[rid].trig; 313208747Sraj pol = di->di_intr_sl[rid].pol; 314208747Sraj 315208747Sraj debugf("intr config: irq = %d, trig = %d, pol = %d\n", irq, trig, pol); 316208747Sraj 317208747Sraj#if defined(__powerpc__) 318208747Sraj int err; 319208747Sraj 320208747Sraj err = powerpc_config_intr(irq, trig, pol); 321208747Sraj if (err) 322208747Sraj return (err); 323208747Sraj#endif 324208747Sraj 325208747Sraj return (bus_generic_setup_intr(bus, child, res, flags, filter, ihand, 326208747Sraj arg, cookiep)); 327208747Sraj} 328208747Sraj 329208747Srajstatic const struct ofw_bus_devinfo * 330208747Srajsimplebus_get_devinfo(device_t bus, device_t child) 331208747Sraj{ 332208747Sraj struct simplebus_devinfo *di; 333208747Sraj 334208747Sraj di = device_get_ivars(child); 335208747Sraj return (&di->di_ofw); 336208747Sraj} 337