central.c revision 143824
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 143824 2005-03-19 00:47:02Z 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 struct resource_list cdi_rl; 56}; 57 58struct central_softc { 59 phandle_t sc_node; 60 int sc_nrange; 61 struct sbus_ranges *sc_ranges; 62}; 63 64static device_probe_t central_probe; 65static device_attach_t central_attach; 66static bus_print_child_t central_print_child; 67static bus_probe_nomatch_t central_probe_nomatch; 68static bus_alloc_resource_t central_alloc_resource; 69static bus_get_resource_list_t central_get_resource_list; 70static ofw_bus_get_compat_t central_get_compat; 71static ofw_bus_get_model_t central_get_model; 72static ofw_bus_get_name_t central_get_name; 73static ofw_bus_get_node_t central_get_node; 74static ofw_bus_get_type_t central_get_type; 75 76static device_method_t central_methods[] = { 77 /* Device interface. */ 78 DEVMETHOD(device_probe, central_probe), 79 DEVMETHOD(device_attach, central_attach), 80 81 /* Bus interface. */ 82 DEVMETHOD(bus_print_child, central_print_child), 83 DEVMETHOD(bus_probe_nomatch, central_probe_nomatch), 84 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 85 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 86 DEVMETHOD(bus_alloc_resource, central_alloc_resource), 87 DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource), 88 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 89 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 90 DEVMETHOD(bus_get_resource_list, central_get_resource_list), 91 92 /* ofw_bus interface */ 93 DEVMETHOD(ofw_bus_get_compat, central_get_compat), 94 DEVMETHOD(ofw_bus_get_model, central_get_model), 95 DEVMETHOD(ofw_bus_get_name, central_get_name), 96 DEVMETHOD(ofw_bus_get_node, central_get_node), 97 DEVMETHOD(ofw_bus_get_type, central_get_type), 98 99 { NULL, NULL } 100}; 101 102static driver_t central_driver = { 103 "central", 104 central_methods, 105 sizeof(struct central_softc), 106}; 107 108static devclass_t central_devclass; 109 110DRIVER_MODULE(central, nexus, central_driver, central_devclass, 0, 0); 111 112static int 113central_probe(device_t dev) 114{ 115 116 if (strcmp(nexus_get_name(dev), "central") == 0) { 117 device_set_desc(dev, "central"); 118 return (0); 119 } 120 return (ENXIO); 121} 122 123static int 124central_attach(device_t dev) 125{ 126 struct central_devinfo *cdi; 127 struct sbus_regs *reg; 128 struct central_softc *sc; 129 phandle_t child; 130 phandle_t node; 131 bus_addr_t size; 132 bus_addr_t off; 133 device_t cdev; 134 char *name; 135 int nreg; 136 int i; 137 138 sc = device_get_softc(dev); 139 node = nexus_get_node(dev); 140 sc->sc_node = node; 141 142 sc->sc_nrange = OF_getprop_alloc(node, "ranges", 143 sizeof(*sc->sc_ranges), (void **)&sc->sc_ranges); 144 if (sc->sc_nrange == -1) { 145 device_printf(dev, "can't get ranges\n"); 146 return (ENXIO); 147 } 148 149 for (child = OF_child(node); child != 0; child = OF_peer(child)) { 150 if ((OF_getprop_alloc(child, "name", 1, (void **)&name)) == -1) 151 continue; 152 cdev = device_add_child(dev, NULL, -1); 153 if (cdev != NULL) { 154 cdi = malloc(sizeof(*cdi), M_DEVBUF, M_WAITOK | M_ZERO); 155 if (cdi == NULL) 156 continue; 157 cdi->cdi_name = name; 158 cdi->cdi_node = child; 159 OF_getprop_alloc(child, "compatible", 1, 160 (void **)&cdi->cdi_compat); 161 OF_getprop_alloc(child, "device_type", 1, 162 (void **)&cdi->cdi_type); 163 OF_getprop_alloc(child, "model", 1, 164 (void **)&cdi->cdi_model); 165 resource_list_init(&cdi->cdi_rl); 166 nreg = OF_getprop_alloc(child, "reg", sizeof(*reg), 167 (void **)®); 168 if (nreg != -1) { 169 for (i = 0; i < nreg; i++) { 170 off = reg[i].sbr_offset; 171 size = reg[i].sbr_size; 172 resource_list_add(&cdi->cdi_rl, 173 SYS_RES_MEMORY, i, off, off + size, 174 size); 175 } 176 free(reg, M_OFWPROP); 177 } 178 device_set_ivars(cdev, cdi); 179 } else 180 free(name, M_OFWPROP); 181 } 182 183 return (bus_generic_attach(dev)); 184} 185 186static int 187central_print_child(device_t dev, device_t child) 188{ 189 struct central_devinfo *cdi; 190 int rv; 191 192 cdi = device_get_ivars(child); 193 rv = bus_print_child_header(dev, child); 194 rv += resource_list_print_type(&cdi->cdi_rl, "mem", 195 SYS_RES_MEMORY, "%#lx"); 196 rv += bus_print_child_footer(dev, child); 197 return (rv); 198} 199 200static void 201central_probe_nomatch(device_t dev, device_t child) 202{ 203 struct central_devinfo *cdi; 204 205 cdi = device_get_ivars(child); 206 device_printf(dev, "<%s>", cdi->cdi_name); 207 resource_list_print_type(&cdi->cdi_rl, "mem", SYS_RES_MEMORY, "%#lx"); 208 printf(" type %s (no driver attached)\n", 209 cdi->cdi_type != NULL ? cdi->cdi_type : "unknown"); 210} 211 212static struct resource * 213central_alloc_resource(device_t bus, device_t child, int type, int *rid, 214 u_long start, u_long end, u_long count, u_int flags) 215{ 216 struct resource_list *rl; 217 struct resource_list_entry *rle; 218 struct central_softc *sc; 219 struct resource *res; 220 bus_addr_t coffset; 221 bus_addr_t cend; 222 bus_addr_t phys; 223 int isdefault; 224 int passthrough; 225 int i; 226 227 isdefault = (start == 0UL && end == ~0UL); 228 passthrough = (device_get_parent(child) != bus); 229 res = NULL; 230 rle = NULL; 231 rl = BUS_GET_RESOURCE_LIST(bus, child); 232 sc = device_get_softc(bus); 233 switch (type) { 234 case SYS_RES_IRQ: 235 return (resource_list_alloc(rl, bus, child, type, rid, start, 236 end, count, flags)); 237 break; 238 case SYS_RES_MEMORY: 239 if (!passthrough) { 240 rle = resource_list_find(rl, type, *rid); 241 if (rle == NULL) 242 return (NULL); 243 if (rle->res != NULL) 244 panic("%s: resource entry is busy", __func__); 245 if (isdefault) { 246 start = rle->start; 247 count = ulmax(count, rle->count); 248 end = ulmax(rle->end, start + count - 1); 249 } 250 } 251 for (i = 0; i < sc->sc_nrange; i++) { 252 coffset = sc->sc_ranges[i].coffset; 253 cend = coffset + sc->sc_ranges[i].size - 1; 254 if (start >= coffset && end <= cend) { 255 start -= coffset; 256 end -= coffset; 257 phys = sc->sc_ranges[i].poffset | 258 ((bus_addr_t)sc->sc_ranges[i].pspace << 32); 259 res = bus_generic_alloc_resource(bus, child, 260 type, rid, phys + start, phys + end, 261 count, flags); 262 if (!passthrough) 263 rle->res = res; 264 break; 265 } 266 } 267 break; 268 default: 269 break; 270 } 271 return (res); 272} 273 274static struct resource_list * 275central_get_resource_list(device_t bus, device_t child) 276{ 277 struct central_devinfo *cdi; 278 279 cdi = device_get_ivars(child); 280 return (&cdi->cdi_rl); 281} 282 283static const char * 284central_get_compat(device_t bus, device_t dev) 285{ 286 struct central_devinfo *dinfo; 287 288 dinfo = device_get_ivars(dev); 289 return (dinfo->cdi_compat); 290} 291 292static const char * 293central_get_model(device_t bus, device_t dev) 294{ 295 struct central_devinfo *dinfo; 296 297 dinfo = device_get_ivars(dev); 298 return (dinfo->cdi_model); 299} 300 301static const char * 302central_get_name(device_t bus, device_t dev) 303{ 304 struct central_devinfo *dinfo; 305 306 dinfo = device_get_ivars(dev); 307 return (dinfo->cdi_name); 308} 309 310static phandle_t 311central_get_node(device_t bus, device_t dev) 312{ 313 struct central_devinfo *dinfo; 314 315 dinfo = device_get_ivars(dev); 316 return (dinfo->cdi_node); 317} 318 319static const char * 320central_get_type(device_t bus, device_t dev) 321{ 322 struct central_devinfo *dinfo; 323 324 dinfo = device_get_ivars(dev); 325 return (dinfo->cdi_type); 326} 327