central.c revision 143128
1/*- 2 * Copyright (c) 2003 Jake Burkholder. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: head/sys/sparc64/central/central.c 143128 2005-03-04 15:17:05Z marius $"); 29 30#include <sys/param.h> 31#include <sys/systm.h> 32#include <sys/bus.h> 33#include <sys/kernel.h> 34#include <sys/malloc.h> 35#include <sys/module.h> 36 37#include <dev/ofw/ofw_bus.h> 38#include <dev/ofw/openfirm.h> 39 40#include <machine/bus.h> 41#include <machine/nexusvar.h> 42#include <machine/ofw_upa.h> 43#include <machine/resource.h> 44 45#include <sys/rman.h> 46 47#include <sparc64/sbus/ofw_sbus.h> 48 49struct central_devinfo { 50 char *cdi_compat; 51 char *cdi_model; 52 char *cdi_name; 53 char *cdi_type; 54 phandle_t cdi_node; 55}; 56 57struct central_softc { 58 phandle_t sc_node; 59 int sc_nrange; 60 struct sbus_ranges *sc_ranges; 61}; 62 63static device_probe_t central_probe; 64static device_attach_t central_attach; 65static bus_probe_nomatch_t central_probe_nomatch; 66static bus_alloc_resource_t central_alloc_resource; 67static ofw_bus_get_compat_t central_get_compat; 68static ofw_bus_get_model_t central_get_model; 69static ofw_bus_get_name_t central_get_name; 70static ofw_bus_get_node_t central_get_node; 71static ofw_bus_get_type_t central_get_type; 72 73static device_method_t central_methods[] = { 74 /* Device interface. */ 75 DEVMETHOD(device_probe, central_probe), 76 DEVMETHOD(device_attach, central_attach), 77 78 /* Bus interface. */ 79 DEVMETHOD(bus_print_child, bus_generic_print_child), 80 DEVMETHOD(bus_probe_nomatch, central_probe_nomatch), 81 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 82 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 83 DEVMETHOD(bus_alloc_resource, central_alloc_resource), 84 DEVMETHOD(bus_release_resource, bus_generic_release_resource), 85 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 86 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 87 88 /* ofw_bus interface */ 89 DEVMETHOD(ofw_bus_get_compat, central_get_compat), 90 DEVMETHOD(ofw_bus_get_model, central_get_model), 91 DEVMETHOD(ofw_bus_get_name, central_get_name), 92 DEVMETHOD(ofw_bus_get_node, central_get_node), 93 DEVMETHOD(ofw_bus_get_type, central_get_type), 94 95 { NULL, NULL } 96}; 97 98static driver_t central_driver = { 99 "central", 100 central_methods, 101 sizeof(struct central_softc), 102}; 103 104static devclass_t central_devclass; 105 106DRIVER_MODULE(central, nexus, central_driver, central_devclass, 0, 0); 107 108static int 109central_probe(device_t dev) 110{ 111 112 if (strcmp(nexus_get_name(dev), "central") == 0) { 113 device_set_desc(dev, "central"); 114 return (0); 115 } 116 return (ENXIO); 117} 118 119static int 120central_attach(device_t dev) 121{ 122 struct central_devinfo *cdi; 123 struct central_softc *sc; 124 phandle_t child; 125 phandle_t node; 126 device_t cdev; 127 char *name; 128 129 sc = device_get_softc(dev); 130 node = nexus_get_node(dev); 131 sc->sc_node = node; 132 133 sc->sc_nrange = OF_getprop_alloc(node, "ranges", 134 sizeof(*sc->sc_ranges), (void **)&sc->sc_ranges); 135 if (sc->sc_nrange == -1) { 136 device_printf(dev, "can't get ranges\n"); 137 return (ENXIO); 138 } 139 140 for (child = OF_child(node); child != 0; child = OF_peer(child)) { 141 if ((OF_getprop_alloc(child, "name", 1, (void **)&name)) == -1) 142 continue; 143 cdev = device_add_child(dev, NULL, -1); 144 if (cdev != NULL) { 145 cdi = malloc(sizeof(*cdi), M_DEVBUF, M_WAITOK | M_ZERO); 146 if (cdi == NULL) 147 continue; 148 cdi->cdi_name = name; 149 cdi->cdi_node = child; 150 OF_getprop_alloc(child, "compatible", 1, 151 (void **)&cdi->cdi_compat); 152 OF_getprop_alloc(child, "device_type", 1, 153 (void **)&cdi->cdi_type); 154 OF_getprop_alloc(child, "model", 1, 155 (void **)&cdi->cdi_model); 156 device_set_ivars(cdev, cdi); 157 } else 158 free(name, M_OFWPROP); 159 } 160 161 return (bus_generic_attach(dev)); 162} 163 164static void 165central_probe_nomatch(device_t dev, device_t child) 166{ 167 struct central_devinfo *cdi; 168 169 cdi = device_get_ivars(child); 170 device_printf(dev, "<%s> type %s (no driver attached)\n", 171 cdi->cdi_name, cdi->cdi_type != NULL ? cdi->cdi_type : "unknown"); 172} 173 174static struct resource * 175central_alloc_resource(device_t bus, device_t child, int type, int *rid, 176 u_long start, u_long end, u_long count, u_int flags) 177{ 178 struct central_softc *sc; 179 struct resource *res; 180 bus_addr_t coffset; 181 bus_addr_t cend; 182 bus_addr_t phys; 183 int i; 184 185 if (type != SYS_RES_MEMORY) { 186 return (bus_generic_alloc_resource(bus, child, type, rid, 187 start, end, count, flags)); 188 } 189 res = NULL; 190 sc = device_get_softc(bus); 191 for (i = 0; i < sc->sc_nrange; i++) { 192 coffset = sc->sc_ranges[i].coffset; 193 cend = coffset + sc->sc_ranges[i].size - 1; 194 if (start >= coffset && end <= cend) { 195 start -= coffset; 196 end -= coffset; 197 phys = sc->sc_ranges[i].poffset | 198 ((bus_addr_t)sc->sc_ranges[i].pspace << 32); 199 res = bus_generic_alloc_resource(bus, child, type, 200 rid, phys + start, phys + end, count, flags); 201 break; 202 } 203 } 204 return (res); 205} 206 207static const char * 208central_get_compat(device_t bus, device_t dev) 209{ 210 struct central_devinfo *dinfo; 211 212 dinfo = device_get_ivars(dev); 213 return (dinfo->cdi_compat); 214} 215 216static const char * 217central_get_model(device_t bus, device_t dev) 218{ 219 struct central_devinfo *dinfo; 220 221 dinfo = device_get_ivars(dev); 222 return (dinfo->cdi_model); 223} 224 225static const char * 226central_get_name(device_t bus, device_t dev) 227{ 228 struct central_devinfo *dinfo; 229 230 dinfo = device_get_ivars(dev); 231 return (dinfo->cdi_name); 232} 233 234static phandle_t 235central_get_node(device_t bus, device_t dev) 236{ 237 struct central_devinfo *dinfo; 238 239 dinfo = device_get_ivars(dev); 240 return (dinfo->cdi_node); 241} 242 243static const char * 244central_get_type(device_t bus, device_t dev) 245{ 246 struct central_devinfo *dinfo; 247 248 dinfo = device_get_ivars(dev); 249 return (dinfo->cdi_type); 250} 251