central.c revision 130025
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 * $FreeBSD: head/sys/sparc64/central/central.c 130025 2004-06-03 05:58:30Z phk $ 27 */ 28 29#include <sys/param.h> 30#include <sys/systm.h> 31#include <sys/bus.h> 32#include <sys/kernel.h> 33#include <sys/malloc.h> 34#include <sys/module.h> 35 36#include <dev/ofw/openfirm.h> 37 38#include <machine/bus.h> 39#include <machine/nexusvar.h> 40#include <machine/ofw_upa.h> 41#include <machine/resource.h> 42 43#include <sys/rman.h> 44 45#include <sparc64/central/centralvar.h> 46#include <sparc64/sbus/ofw_sbus.h> 47#include <sparc64/sbus/sbusreg.h> 48 49struct central_devinfo { 50 char *cdi_name; 51 char *cdi_type; 52 phandle_t cdi_node; 53}; 54 55struct central_softc { 56 phandle_t sc_node; 57 int sc_nrange; 58 struct sbus_ranges *sc_ranges; 59}; 60 61static int central_probe(device_t dev); 62static int central_attach(device_t dev); 63 64static void central_probe_nomatch(device_t dev, device_t child); 65static int central_read_ivar(device_t, device_t, int, uintptr_t *); 66static int central_write_ivar(device_t, device_t, int, uintptr_t); 67static struct resource *central_alloc_resource(device_t, device_t, int, int *, 68 u_long, u_long, u_long, u_int); 69 70static device_method_t central_methods[] = { 71 /* Device interface. */ 72 DEVMETHOD(device_probe, central_probe), 73 DEVMETHOD(device_attach, central_attach), 74 75 /* Bus interface. */ 76 DEVMETHOD(bus_print_child, bus_generic_print_child), 77 DEVMETHOD(bus_probe_nomatch, central_probe_nomatch), 78 DEVMETHOD(bus_read_ivar, central_read_ivar), 79 DEVMETHOD(bus_write_ivar, central_write_ivar), 80 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 81 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 82 DEVMETHOD(bus_alloc_resource, central_alloc_resource), 83 DEVMETHOD(bus_release_resource, bus_generic_release_resource), 84 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 85 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 86 87 { NULL, NULL } 88}; 89 90static driver_t central_driver = { 91 "central", 92 central_methods, 93 sizeof(struct central_softc), 94}; 95 96static devclass_t central_devclass; 97 98DRIVER_MODULE(central, nexus, central_driver, central_devclass, 0, 0); 99 100static int 101central_probe(device_t dev) 102{ 103 104 if (strcmp(nexus_get_name(dev), "central") == 0) { 105 device_set_desc(dev, "central"); 106 return (0); 107 } 108 return (ENXIO); 109} 110 111static int 112central_attach(device_t dev) 113{ 114 struct central_devinfo *cdi; 115 struct central_softc *sc; 116 phandle_t child; 117 phandle_t node; 118 device_t cdev; 119 char *name; 120 121 sc = device_get_softc(dev); 122 node = nexus_get_node(dev); 123 sc->sc_node = node; 124 125 sc->sc_nrange = OF_getprop_alloc(node, "ranges", 126 sizeof(*sc->sc_ranges), (void **)&sc->sc_ranges); 127 if (sc->sc_nrange == -1) { 128 device_printf(dev, "can't get ranges"); 129 return (ENXIO); 130 } 131 132 for (child = OF_child(node); child != 0; child = OF_peer(child)) { 133 if ((OF_getprop_alloc(child, "name", 1, (void **)&name)) == -1) 134 continue; 135 cdev = device_add_child(dev, NULL, -1); 136 if (cdev != NULL) { 137 cdi = malloc(sizeof(*cdi), M_DEVBUF, 138 M_WAITOK | M_ZERO); 139 cdi->cdi_name = name; 140 cdi->cdi_node = child; 141 OF_getprop_alloc(child, "device_type", 1, 142 (void **)&cdi->cdi_type); 143 device_set_ivars(cdev, cdi); 144 } else 145 free(name, M_OFWPROP); 146 } 147 148 return (bus_generic_attach(dev)); 149} 150 151static void 152central_probe_nomatch(device_t dev, device_t child) 153{ 154 struct central_devinfo *cdi; 155 156 cdi = device_get_ivars(child); 157 device_printf(dev, "<%s> type %s (no driver attached)\n", 158 cdi->cdi_name, cdi->cdi_type != NULL ? cdi->cdi_type : "unknown"); 159} 160 161static int 162central_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 163{ 164 struct central_devinfo *cdi; 165 166 if ((cdi = device_get_ivars(child)) == 0) 167 return (ENOENT); 168 switch (which) { 169 case CENTRAL_IVAR_NAME: 170 *result = (uintptr_t)cdi->cdi_name; 171 break; 172 case CENTRAL_IVAR_NODE: 173 *result = cdi->cdi_node; 174 break; 175 case CENTRAL_IVAR_TYPE: 176 *result = (uintptr_t)cdi->cdi_type; 177 break; 178 default: 179 return (ENOENT); 180 } 181 return (0); 182} 183 184static int 185central_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 186{ 187 struct central_devinfo *cdi; 188 189 if ((cdi = device_get_ivars(child)) == 0) 190 return (ENOENT); 191 switch (which) { 192 case CENTRAL_IVAR_NAME: 193 case CENTRAL_IVAR_NODE: 194 case CENTRAL_IVAR_TYPE: 195 return (EINVAL); 196 default: 197 return (ENOENT); 198 } 199 return (0); 200} 201 202static struct resource * 203central_alloc_resource(device_t bus, device_t child, int type, int *rid, 204 u_long start, u_long end, u_long count, u_int flags) 205{ 206 struct central_softc *sc; 207 struct resource *res; 208 bus_addr_t coffset; 209 bus_addr_t cend; 210 bus_addr_t phys; 211 int i; 212 213 if (type != SYS_RES_MEMORY) { 214 return (bus_generic_alloc_resource(bus, child, type, rid, 215 start, end, count, flags)); 216 } 217 res = NULL; 218 sc = device_get_softc(bus); 219 for (i = 0; i < sc->sc_nrange; i++) { 220 coffset = sc->sc_ranges[i].coffset; 221 cend = coffset + sc->sc_ranges[i].size - 1; 222 if (start >= coffset && end <= cend) { 223 start -= coffset; 224 end -= coffset; 225 phys = sc->sc_ranges[i].poffset | 226 ((bus_addr_t)sc->sc_ranges[i].pspace << 32); 227 res = bus_generic_alloc_resource(bus, child, type, 228 rid, phys + start, phys + end, count, flags); 229 break; 230 } 231 } 232 return (res); 233} 234