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