simplebus.c revision 248480
1264377Sdes/*- 2224638Sbrooks * Copyright (c) 2009-2010 The FreeBSD Foundation 357429Smarkm * All rights reserved. 457429Smarkm * 557429Smarkm * This software was developed by Semihalf under sponsorship from 657429Smarkm * the FreeBSD Foundation. 760576Skris * 865674Skris * Redistribution and use in source and binary forms, with or without 965674Skris * modification, are permitted provided that the following conditions 1065674Skris * are met: 1165674Skris * 1. Redistributions of source code must retain the above copyright 1265674Skris * notice, this list of conditions and the following disclaimer. 1360576Skris * 2. Redistributions in binary form must reproduce the above copyright 1465674Skris * notice, this list of conditions and the following disclaimer in the 1565674Skris * documentation and/or other materials provided with the distribution. 1692559Sdes * 1765674Skris * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1865674Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1965674Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2065674Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2165674Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2265674Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2365674Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2465674Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2565674Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2665674Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2765674Skris * SUCH DAMAGE. 2865674Skris */ 2965674Skris 3065674Skris#include <sys/cdefs.h> 3165674Skris__FBSDID("$FreeBSD: head/sys/dev/fdt/simplebus.c 248480 2013-03-18 23:35:01Z ray $"); 3265674Skris 3365674Skris#include "opt_platform.h" 3465674Skris#include <sys/param.h> 3565674Skris#include <sys/systm.h> 3665674Skris#include <sys/ktr.h> 3757429Smarkm#include <sys/kernel.h> 3857429Smarkm#include <sys/module.h> 3957429Smarkm#include <sys/bus.h> 4057429Smarkm#include <sys/rman.h> 41162856Sdes#include <sys/malloc.h> 42162856Sdes 43162856Sdes#include <machine/fdt.h> 44162856Sdes 45262566Sdes#include <dev/ofw/ofw_bus.h> 46162856Sdes#include <dev/ofw/ofw_bus_subr.h> 4760576Skris#include <dev/ofw/openfirm.h> 4876262Sgreen 49262566Sdes#include "fdt_common.h" 5076262Sgreen#include "ofw_bus_if.h" 51264377Sdes 52264377Sdes#ifdef DEBUG 5357429Smarkm#define debugf(fmt, args...) do { printf("%s(): ", __func__); \ 54149753Sdes printf(fmt,##args); } while (0) 55149753Sdes#else 5698941Sdes#define debugf(fmt, args...) 57124211Sdes#endif 58124211Sdes 59124211Sdesstatic MALLOC_DEFINE(M_SIMPLEBUS, "simplebus", "simplebus devices information"); 6057429Smarkm 6192559Sdesstruct simplebus_softc { 6292559Sdes int sc_addr_cells; 6392559Sdes int sc_size_cells; 6492559Sdes}; 6592559Sdes 66248619Sdesstruct simplebus_devinfo { 67248619Sdes struct ofw_bus_devinfo di_ofw; 68149753Sdes struct resource_list di_res; 69262566Sdes 70262566Sdes /* Interrupts sense-level info for this device */ 71262566Sdes struct fdt_sense_level di_intr_sl[DI_MAX_INTR_NUM]; 7298684Sdes}; 73255767Sdes 74255767Sdes/* 75255767Sdes * Prototypes. 76248619Sdes */ 77248619Sdesstatic int simplebus_probe(device_t); 78248619Sdesstatic int simplebus_attach(device_t); 79248619Sdes 8069591Sgreenstatic int simplebus_print_child(device_t, device_t); 81248619Sdesstatic int simplebus_setup_intr(device_t, device_t, struct resource *, int, 82248619Sdes driver_filter_t *, driver_intr_t *, void *, void **); 83248619Sdes 84248619Sdesstatic struct resource *simplebus_alloc_resource(device_t, device_t, int, 85248619Sdes int *, u_long, u_long, u_long, u_int); 86248619Sdesstatic struct resource_list *simplebus_get_resource_list(device_t, device_t); 87248619Sdes 88248619Sdesstatic ofw_bus_get_devinfo_t simplebus_get_devinfo; 89248619Sdes 90248619Sdes/* 91248619Sdes * Bus interface definition. 9298684Sdes */ 93248619Sdesstatic device_method_t simplebus_methods[] = { 94248619Sdes /* Device interface */ 95248619Sdes DEVMETHOD(device_probe, simplebus_probe), 96248619Sdes DEVMETHOD(device_attach, simplebus_attach), 97248619Sdes DEVMETHOD(device_detach, bus_generic_detach), 98248619Sdes DEVMETHOD(device_shutdown, bus_generic_shutdown), 99248619Sdes DEVMETHOD(device_suspend, bus_generic_suspend), 100248619Sdes DEVMETHOD(device_resume, bus_generic_resume), 101248619Sdes 10298684Sdes /* Bus interface */ 103262566Sdes DEVMETHOD(bus_print_child, simplebus_print_child), 104262566Sdes DEVMETHOD(bus_alloc_resource, simplebus_alloc_resource), 105248619Sdes DEVMETHOD(bus_release_resource, bus_generic_release_resource), 10692559Sdes DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 10769591Sgreen DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 10892559Sdes DEVMETHOD(bus_setup_intr, simplebus_setup_intr), 10957429Smarkm DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 110262566Sdes DEVMETHOD(bus_get_resource_list, simplebus_get_resource_list), 111255767Sdes 112262566Sdes /* OFW bus interface */ 113255767Sdes DEVMETHOD(ofw_bus_get_devinfo, simplebus_get_devinfo), 114255767Sdes DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), 115255767Sdes DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), 116255767Sdes DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), 117255767Sdes DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), 118255767Sdes DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), 119255767Sdes 120255767Sdes { 0, 0 } 121262566Sdes}; 122262566Sdes 123255767Sdesstatic driver_t simplebus_driver = { 124262566Sdes "simplebus", 125255767Sdes simplebus_methods, 126255767Sdes sizeof(struct simplebus_softc) 127255767Sdes}; 128255767Sdes 129255767Sdesdevclass_t simplebus_devclass; 130255767Sdes 131255767SdesDRIVER_MODULE(simplebus, fdtbus, simplebus_driver, simplebus_devclass, 0, 0); 132255767Sdes 13398684Sdesstatic int 134126277Sdessimplebus_probe(device_t dev) 13557429Smarkm{ 13692559Sdes 13757429Smarkm if (!ofw_bus_is_compatible(dev, "simple-bus")) 13899063Sdes return (ENXIO); 13998684Sdes 140126277Sdes device_set_desc(dev, "Flattened device tree simple bus"); 14169591Sgreen 14292559Sdes return (BUS_PROBE_GENERIC); 14369591Sgreen} 14499063Sdes 14598684Sdesstatic int 146262566Sdessimplebus_attach(device_t dev) 147262566Sdes{ 148262566Sdes device_t dev_child; 149262566Sdes struct simplebus_devinfo *di; 150262566Sdes struct simplebus_softc *sc; 151262566Sdes phandle_t dt_node, dt_child; 152262566Sdes 153262566Sdes sc = device_get_softc(dev); 154248619Sdes 155248619Sdes /* 156248619Sdes * Walk simple-bus and add direct subordinates as our children. 157248619Sdes */ 158248619Sdes dt_node = ofw_bus_get_node(dev); 159248619Sdes for (dt_child = OF_child(dt_node); dt_child != 0; 160248619Sdes dt_child = OF_peer(dt_child)) { 161248619Sdes 162262566Sdes /* Check and process 'status' property. */ 163262566Sdes if (!(fdt_is_enabled(dt_child))) 164262566Sdes continue; 165262566Sdes 166262566Sdes if (!(fdt_pm_is_enabled(dt_child))) 167262566Sdes continue; 168248619Sdes 169248619Sdes di = malloc(sizeof(*di), M_SIMPLEBUS, M_WAITOK | M_ZERO); 170248619Sdes 171126277Sdes if (ofw_bus_gen_setup_devinfo(&di->di_ofw, dt_child) != 0) { 17298684Sdes free(di, M_SIMPLEBUS); 17398684Sdes device_printf(dev, "could not set up devinfo\n"); 17498684Sdes continue; 17569591Sgreen } 17676262Sgreen 177192595Sdes resource_list_init(&di->di_res); 178192595Sdes if (fdt_reg_to_rl(dt_child, &di->di_res)) { 179262566Sdes device_printf(dev, 180192595Sdes "%s: could not process 'reg' " 181192595Sdes "property\n", di->di_ofw.obd_name); 182192595Sdes ofw_bus_gen_destroy_devinfo(&di->di_ofw); 18369591Sgreen free(di, M_SIMPLEBUS); 18457429Smarkm continue; 18576262Sgreen } 18692559Sdes 18757429Smarkm if (fdt_intr_to_rl(dt_child, &di->di_res, di->di_intr_sl)) { 18869591Sgreen device_printf(dev, "%s: could not process " 18969591Sgreen "'interrupts' property\n", di->di_ofw.obd_name); 19069591Sgreen resource_list_free(&di->di_res); 19157429Smarkm ofw_bus_gen_destroy_devinfo(&di->di_ofw); 19257429Smarkm free(di, M_SIMPLEBUS); 19369591Sgreen continue; 194255767Sdes } 19569591Sgreen 19660576Skris /* Add newbus device for this FDT node */ 197255767Sdes dev_child = device_add_child(dev, NULL, -1); 19869591Sgreen if (dev_child == NULL) { 199147005Sdes device_printf(dev, "could not add child: %s\n", 20069591Sgreen di->di_ofw.obd_name); 20169591Sgreen resource_list_free(&di->di_res); 20260576Skris ofw_bus_gen_destroy_devinfo(&di->di_ofw); 20357429Smarkm free(di, M_SIMPLEBUS); 204255767Sdes continue; 20569591Sgreen } 20657429Smarkm#ifdef DEBUG 207255767Sdes device_printf(dev, "added child: %s\n\n", di->di_ofw.obd_name); 20869591Sgreen#endif 20969591Sgreen device_set_ivars(dev_child, di); 21069591Sgreen } 21169591Sgreen 21257429Smarkm return (bus_generic_attach(dev)); 21357429Smarkm} 21460576Skris 21560576Skrisstatic int 21660576Skrissimplebus_print_child(device_t dev, device_t child) 21760576Skris{ 218255767Sdes struct simplebus_devinfo *di; 219137019Sdes struct resource_list *rl; 22060576Skris int rv; 22160576Skris 22261212Skris di = device_get_ivars(child); 22360576Skris rl = &di->di_res; 224137019Sdes 22569591Sgreen rv = 0; 22692559Sdes rv += bus_print_child_header(dev, child); 22769591Sgreen rv += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx"); 228224638Sbrooks rv += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 229224638Sbrooks rv += bus_print_child_footer(dev, child); 230224638Sbrooks 231224638Sbrooks return (rv); 232224638Sbrooks} 233224638Sbrooks 23469591Sgreenstatic struct resource * 235255767Sdessimplebus_alloc_resource(device_t bus, device_t child, int type, int *rid, 23660576Skris u_long start, u_long end, u_long count, u_int flags) 23760576Skris{ 23860576Skris struct simplebus_devinfo *di; 23969591Sgreen struct resource_list_entry *rle; 240255767Sdes 24160576Skris /* 24260576Skris * Request for the default allocation with a given rid: use resource 24360576Skris * list stored in the local device info. 24457429Smarkm */ 24557429Smarkm if ((start == 0UL) && (end == ~0UL)) { 24657429Smarkm if ((di = device_get_ivars(child)) == NULL) 24757429Smarkm return (NULL); 24857429Smarkm 24957429Smarkm if (type == SYS_RES_IOPORT) 25057429Smarkm type = SYS_RES_MEMORY; 25157429Smarkm 252255767Sdes rle = resource_list_find(&di->di_res, type, *rid); 25361212Skris if (rle == NULL) { 25461212Skris device_printf(bus, "no default resources for " 255147005Sdes "rid = %d, type = %d\n", *rid, type); 256147005Sdes return (NULL); 257147005Sdes } 258147005Sdes start = rle->start; 25957429Smarkm end = rle->end; 26057429Smarkm count = rle->count; 26169591Sgreen } 26269591Sgreen 26357429Smarkm return (bus_generic_alloc_resource(bus, child, type, rid, start, end, 264255767Sdes count, flags)); 26569591Sgreen} 26657429Smarkm 26757429Smarkmstatic struct resource_list * 26860576Skrissimplebus_get_resource_list(device_t bus, device_t child) 269255767Sdes{ 27092559Sdes struct simplebus_devinfo *di; 271137019Sdes 27257429Smarkm di = device_get_ivars(child); 27392559Sdes return (&di->di_res); 27498941Sdes} 27598941Sdes 27698941Sdesstatic int 27792559Sdessimplebus_setup_intr(device_t bus, device_t child, struct resource *res, 278149753Sdes int flags, driver_filter_t *filter, driver_intr_t *ihand, void *arg, 27998941Sdes void **cookiep) 280149753Sdes{ 28192559Sdes struct simplebus_devinfo *di; 28292559Sdes enum intr_trigger trig; 28392559Sdes enum intr_polarity pol; 28492559Sdes int error, rid; 28592559Sdes 28692559Sdes if (device_get_parent(child) != bus) 28792559Sdes return (ECHILD); 28892559Sdes 28992559Sdes di = device_get_ivars(child); 29092559Sdes if (di == NULL) 29192559Sdes return (ENXIO); 292248619Sdes 29392559Sdes if (res == NULL) 29469591Sgreen return (EINVAL); 29569591Sgreen 29669591Sgreen rid = rman_get_rid(res); 297248619Sdes if (rid >= DI_MAX_INTR_NUM) 29869591Sgreen return (ENOENT); 29969591Sgreen 30069591Sgreen trig = di->di_intr_sl[rid].trig; 30192559Sdes pol = di->di_intr_sl[rid].pol; 302262566Sdes if (trig != INTR_TRIGGER_CONFORM || pol != INTR_POLARITY_CONFORM) { 303262566Sdes error = bus_generic_config_intr(bus, rman_get_start(res), 304262566Sdes trig, pol); 305262566Sdes if (error) 30692559Sdes return (error); 30792559Sdes } 30898941Sdes 30998941Sdes error = bus_generic_setup_intr(bus, child, res, flags, filter, ihand, 31098941Sdes arg, cookiep); 31198941Sdes return (error); 31298941Sdes} 31398941Sdes 31498941Sdesstatic const struct ofw_bus_devinfo * 315137019Sdessimplebus_get_devinfo(device_t bus, device_t child) 31698941Sdes{ 31792559Sdes struct simplebus_devinfo *di; 318137019Sdes 31992559Sdes di = device_get_ivars(child); 32092559Sdes return (&di->di_ofw); 321248619Sdes} 322248619Sdes