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>
| 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>
|
74 75 /* Bus interface. */ 76 DEVMETHOD(bus_print_child, central_print_child), 77 DEVMETHOD(bus_probe_nomatch, central_probe_nomatch), 78 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 79 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 80 DEVMETHOD(bus_alloc_resource, central_alloc_resource), 81 DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource), 82 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 83 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 84 DEVMETHOD(bus_get_resource_list, central_get_resource_list), 85 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 86 87 /* ofw_bus interface */ 88 DEVMETHOD(ofw_bus_get_devinfo, central_get_devinfo), 89 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), 90 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), 91 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), 92 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), 93 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_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 sbus_regs *reg; 124 struct central_softc *sc; 125 phandle_t child; 126 phandle_t node; 127 device_t cdev; 128 int nreg; 129 int i; 130 131 sc = device_get_softc(dev); 132 node = nexus_get_node(dev); 133 134 sc->sc_nrange = OF_getprop_alloc(node, "ranges", 135 sizeof(*sc->sc_ranges), (void **)&sc->sc_ranges); 136 if (sc->sc_nrange == -1) { 137 device_printf(dev, "can't get ranges\n"); 138 return (ENXIO); 139 } 140 141 for (child = OF_child(node); child != 0; child = OF_peer(child)) { 142 cdi = malloc(sizeof(*cdi), M_DEVBUF, M_WAITOK | M_ZERO); 143 if (ofw_bus_gen_setup_devinfo(&cdi->cdi_obdinfo, child) != 0) { 144 free(cdi, M_DEVBUF); 145 continue; 146 } 147 nreg = OF_getprop_alloc(child, "reg", sizeof(*reg), 148 (void **)®); 149 if (nreg == -1) { 150 device_printf(dev, "<%s>: incomplete\n", 151 cdi->cdi_obdinfo.obd_name); 152 ofw_bus_gen_destroy_devinfo(&cdi->cdi_obdinfo); 153 free(cdi, M_DEVBUF); 154 continue; 155 } 156 resource_list_init(&cdi->cdi_rl); 157 for (i = 0; i < nreg; i++) 158 resource_list_add(&cdi->cdi_rl, SYS_RES_MEMORY, i, 159 reg[i].sbr_offset, reg[i].sbr_offset + 160 reg[i].sbr_size, reg[i].sbr_size); 161 free(reg, M_OFWPROP); 162 cdev = device_add_child(dev, NULL, -1); 163 if (cdev == NULL) { 164 device_printf(dev, "<%s>: device_add_child failed\n", 165 cdi->cdi_obdinfo.obd_name); 166 resource_list_free(&cdi->cdi_rl); 167 ofw_bus_gen_destroy_devinfo(&cdi->cdi_obdinfo); 168 free(cdi, M_DEVBUF); 169 continue; 170 } 171 device_set_ivars(cdev, cdi); 172 } 173 174 return (bus_generic_attach(dev)); 175} 176 177static int 178central_print_child(device_t dev, device_t child) 179{ 180 int rv; 181 182 rv = bus_print_child_header(dev, child); 183 rv += central_print_res(device_get_ivars(child)); 184 rv += bus_print_child_footer(dev, child); 185 return (rv); 186} 187 188static void 189central_probe_nomatch(device_t dev, device_t child) 190{ 191 const char *type; 192 193 device_printf(dev, "<%s>", ofw_bus_get_name(child)); 194 central_print_res(device_get_ivars(child)); 195 type = ofw_bus_get_type(child); 196 printf(" type %s (no driver attached)\n", 197 type != NULL ? type : "unknown"); 198} 199 200static struct resource * 201central_alloc_resource(device_t bus, device_t child, int type, int *rid, 202 u_long start, u_long end, u_long count, u_int flags) 203{ 204 struct resource_list *rl; 205 struct resource_list_entry *rle; 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 isdefault; 212 int passthrough; 213 int i; 214 215 isdefault = (start == 0UL && end == ~0UL); 216 passthrough = (device_get_parent(child) != bus); 217 res = NULL; 218 rle = NULL; 219 rl = BUS_GET_RESOURCE_LIST(bus, child); 220 sc = device_get_softc(bus); 221 switch (type) { 222 case SYS_RES_IRQ: 223 return (resource_list_alloc(rl, bus, child, type, rid, start, 224 end, count, flags)); 225 case SYS_RES_MEMORY: 226 if (!passthrough) { 227 rle = resource_list_find(rl, type, *rid); 228 if (rle == NULL) 229 return (NULL); 230 if (rle->res != NULL) 231 panic("%s: resource entry is busy", __func__); 232 if (isdefault) { 233 start = rle->start; 234 count = ulmax(count, rle->count); 235 end = ulmax(rle->end, start + count - 1); 236 } 237 } 238 for (i = 0; i < sc->sc_nrange; i++) { 239 coffset = sc->sc_ranges[i].coffset; 240 cend = coffset + sc->sc_ranges[i].size - 1; 241 if (start >= coffset && end <= cend) { 242 start -= coffset; 243 end -= coffset; 244 phys = sc->sc_ranges[i].poffset | 245 ((bus_addr_t)sc->sc_ranges[i].pspace << 32); 246 res = bus_generic_alloc_resource(bus, child, 247 type, rid, phys + start, phys + end, 248 count, flags); 249 if (!passthrough) 250 rle->res = res; 251 break; 252 } 253 } 254 break; 255 } 256 return (res); 257} 258 259static struct resource_list * 260central_get_resource_list(device_t bus, device_t child) 261{ 262 struct central_devinfo *cdi; 263 264 cdi = device_get_ivars(child); 265 return (&cdi->cdi_rl); 266} 267 268static const struct ofw_bus_devinfo * 269central_get_devinfo(device_t bus, device_t child) 270{ 271 struct central_devinfo *cdi; 272 273 cdi = device_get_ivars(child); 274 return (&cdi->cdi_obdinfo); 275} 276 277static int 278central_print_res(struct central_devinfo *cdi) 279{ 280 281 return (resource_list_print_type(&cdi->cdi_rl, "mem", SYS_RES_MEMORY, 282 "%#lx")); 283}
| 77 78 /* Bus interface. */ 79 DEVMETHOD(bus_print_child, central_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_rl_release_resource), 85 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 86 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 87 DEVMETHOD(bus_get_resource_list, central_get_resource_list), 88 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 89 90 /* ofw_bus interface */ 91 DEVMETHOD(ofw_bus_get_devinfo, central_get_devinfo), 92 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), 93 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), 94 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), 95 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), 96 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), 97 98 { NULL, NULL } 99}; 100 101static driver_t central_driver = { 102 "central", 103 central_methods, 104 sizeof(struct central_softc), 105}; 106 107static devclass_t central_devclass; 108 109DRIVER_MODULE(central, nexus, central_driver, central_devclass, 0, 0); 110 111static int 112central_probe(device_t dev) 113{ 114 115 if (strcmp(nexus_get_name(dev), "central") == 0) { 116 device_set_desc(dev, "central"); 117 return (0); 118 } 119 return (ENXIO); 120} 121 122static int 123central_attach(device_t dev) 124{ 125 struct central_devinfo *cdi; 126 struct sbus_regs *reg; 127 struct central_softc *sc; 128 phandle_t child; 129 phandle_t node; 130 device_t cdev; 131 int nreg; 132 int i; 133 134 sc = device_get_softc(dev); 135 node = nexus_get_node(dev); 136 137 sc->sc_nrange = OF_getprop_alloc(node, "ranges", 138 sizeof(*sc->sc_ranges), (void **)&sc->sc_ranges); 139 if (sc->sc_nrange == -1) { 140 device_printf(dev, "can't get ranges\n"); 141 return (ENXIO); 142 } 143 144 for (child = OF_child(node); child != 0; child = OF_peer(child)) { 145 cdi = malloc(sizeof(*cdi), M_DEVBUF, M_WAITOK | M_ZERO); 146 if (ofw_bus_gen_setup_devinfo(&cdi->cdi_obdinfo, child) != 0) { 147 free(cdi, M_DEVBUF); 148 continue; 149 } 150 nreg = OF_getprop_alloc(child, "reg", sizeof(*reg), 151 (void **)®); 152 if (nreg == -1) { 153 device_printf(dev, "<%s>: incomplete\n", 154 cdi->cdi_obdinfo.obd_name); 155 ofw_bus_gen_destroy_devinfo(&cdi->cdi_obdinfo); 156 free(cdi, M_DEVBUF); 157 continue; 158 } 159 resource_list_init(&cdi->cdi_rl); 160 for (i = 0; i < nreg; i++) 161 resource_list_add(&cdi->cdi_rl, SYS_RES_MEMORY, i, 162 reg[i].sbr_offset, reg[i].sbr_offset + 163 reg[i].sbr_size, reg[i].sbr_size); 164 free(reg, M_OFWPROP); 165 cdev = device_add_child(dev, NULL, -1); 166 if (cdev == NULL) { 167 device_printf(dev, "<%s>: device_add_child failed\n", 168 cdi->cdi_obdinfo.obd_name); 169 resource_list_free(&cdi->cdi_rl); 170 ofw_bus_gen_destroy_devinfo(&cdi->cdi_obdinfo); 171 free(cdi, M_DEVBUF); 172 continue; 173 } 174 device_set_ivars(cdev, cdi); 175 } 176 177 return (bus_generic_attach(dev)); 178} 179 180static int 181central_print_child(device_t dev, device_t child) 182{ 183 int rv; 184 185 rv = bus_print_child_header(dev, child); 186 rv += central_print_res(device_get_ivars(child)); 187 rv += bus_print_child_footer(dev, child); 188 return (rv); 189} 190 191static void 192central_probe_nomatch(device_t dev, device_t child) 193{ 194 const char *type; 195 196 device_printf(dev, "<%s>", ofw_bus_get_name(child)); 197 central_print_res(device_get_ivars(child)); 198 type = ofw_bus_get_type(child); 199 printf(" type %s (no driver attached)\n", 200 type != NULL ? type : "unknown"); 201} 202 203static struct resource * 204central_alloc_resource(device_t bus, device_t child, int type, int *rid, 205 u_long start, u_long end, u_long count, u_int flags) 206{ 207 struct resource_list *rl; 208 struct resource_list_entry *rle; 209 struct central_softc *sc; 210 struct resource *res; 211 bus_addr_t coffset; 212 bus_addr_t cend; 213 bus_addr_t phys; 214 int isdefault; 215 int passthrough; 216 int i; 217 218 isdefault = (start == 0UL && end == ~0UL); 219 passthrough = (device_get_parent(child) != bus); 220 res = NULL; 221 rle = NULL; 222 rl = BUS_GET_RESOURCE_LIST(bus, child); 223 sc = device_get_softc(bus); 224 switch (type) { 225 case SYS_RES_IRQ: 226 return (resource_list_alloc(rl, bus, child, type, rid, start, 227 end, count, flags)); 228 case SYS_RES_MEMORY: 229 if (!passthrough) { 230 rle = resource_list_find(rl, type, *rid); 231 if (rle == NULL) 232 return (NULL); 233 if (rle->res != NULL) 234 panic("%s: resource entry is busy", __func__); 235 if (isdefault) { 236 start = rle->start; 237 count = ulmax(count, rle->count); 238 end = ulmax(rle->end, start + count - 1); 239 } 240 } 241 for (i = 0; i < sc->sc_nrange; i++) { 242 coffset = sc->sc_ranges[i].coffset; 243 cend = coffset + sc->sc_ranges[i].size - 1; 244 if (start >= coffset && end <= cend) { 245 start -= coffset; 246 end -= coffset; 247 phys = sc->sc_ranges[i].poffset | 248 ((bus_addr_t)sc->sc_ranges[i].pspace << 32); 249 res = bus_generic_alloc_resource(bus, child, 250 type, rid, phys + start, phys + end, 251 count, flags); 252 if (!passthrough) 253 rle->res = res; 254 break; 255 } 256 } 257 break; 258 } 259 return (res); 260} 261 262static struct resource_list * 263central_get_resource_list(device_t bus, device_t child) 264{ 265 struct central_devinfo *cdi; 266 267 cdi = device_get_ivars(child); 268 return (&cdi->cdi_rl); 269} 270 271static const struct ofw_bus_devinfo * 272central_get_devinfo(device_t bus, device_t child) 273{ 274 struct central_devinfo *cdi; 275 276 cdi = device_get_ivars(child); 277 return (&cdi->cdi_obdinfo); 278} 279 280static int 281central_print_res(struct central_devinfo *cdi) 282{ 283 284 return (resource_list_print_type(&cdi->cdi_rl, "mem", SYS_RES_MEMORY, 285 "%#lx")); 286}
|