apb.c revision 188883
1/*- 2 * Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@FreeBSD.org> 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 unmodified, this list of conditions, and the following 10 * disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28#include <sys/cdefs.h> 29__FBSDID("$FreeBSD$"); 30 31#include <sys/param.h> 32#include <sys/systm.h> 33#include <sys/bus.h> 34#include <sys/interrupt.h> 35#include <sys/kernel.h> 36#include <sys/module.h> 37#include <sys/rman.h> 38#include <sys/malloc.h> 39 40#include <machine/bus.h> 41 42#include <mips/atheros/apbvar.h> 43#include <mips/atheros/ar71xxreg.h> 44 45#undef APB_DEBUG 46#ifdef APB_DEBUG 47#define dprintf printf 48#else 49#define dprintf(x, arg...) 50#endif /* APB_DEBUG */ 51 52static int apb_activate_resource(device_t, device_t, int, int, 53 struct resource *); 54static device_t apb_add_child(device_t, int, const char *, int); 55static struct resource * 56 apb_alloc_resource(device_t, device_t, int, int *, u_long, 57 u_long, u_long, u_int); 58static int apb_attach(device_t); 59static int apb_deactivate_resource(device_t, device_t, int, int, 60 struct resource *); 61static struct resource_list * 62 apb_get_resource_list(device_t, device_t); 63static void apb_hinted_child(device_t, const char *, int); 64static int apb_intr(void *); 65static int apb_probe(device_t); 66static int apb_release_resource(device_t, device_t, int, int, 67 struct resource *); 68static int apb_setup_intr(device_t, device_t, struct resource *, int, 69 driver_filter_t *, driver_intr_t *, void *, void **); 70static int apb_teardown_intr(device_t, device_t, struct resource *, 71 void *); 72 73static void apb_mask_irq(unsigned int irq) 74{ 75 uint32_t reg; 76 77 reg = ATH_READ_REG(AR71XX_MISC_INTR_MASK); 78 ATH_WRITE_REG(AR71XX_MISC_INTR_MASK, reg & ~(1 << irq)); 79 80} 81 82static void apb_unmask_irq(unsigned int irq) 83{ 84 uint32_t reg; 85 86 reg = ATH_READ_REG(AR71XX_MISC_INTR_MASK); 87 ATH_WRITE_REG(AR71XX_MISC_INTR_MASK, reg | (1 << irq)); 88} 89 90static int 91apb_probe(device_t dev) 92{ 93 94 return (0); 95} 96 97static int 98apb_attach(device_t dev) 99{ 100 struct apb_softc *sc = device_get_softc(dev); 101 int rid = 0; 102 103 device_set_desc(dev, "APB Bus bridge"); 104 sc->apb_irq_rman.rm_type = RMAN_ARRAY; 105 sc->apb_irq_rman.rm_descr = "APB IRQ"; 106 107 if (rman_init(&sc->apb_irq_rman) != 0 || 108 rman_manage_region(&sc->apb_irq_rman, 109 APB_IRQ_BASE, APB_IRQ_END) != 0) 110 panic("apb_attach: failed to set up IRQ rman"); 111 112 if ((sc->sc_misc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 113 RF_SHAREABLE | RF_ACTIVE)) == NULL) { 114 device_printf(dev, "unable to allocate IRQ resource\n"); 115 return (ENXIO); 116 } 117 118 if ((bus_setup_intr(dev, sc->sc_misc_irq, INTR_TYPE_MISC, 119 apb_intr, NULL, sc, &sc->sc_misc_ih))) { 120 device_printf(dev, 121 "WARNING: unable to register interrupt handler\n"); 122 return (ENXIO); 123 } 124 125 bus_generic_probe(dev); 126 bus_enumerate_hinted_children(dev); 127 bus_generic_attach(dev); 128 129 return (0); 130} 131 132static struct resource * 133apb_alloc_resource(device_t bus, device_t child, int type, int *rid, 134 u_long start, u_long end, u_long count, u_int flags) 135{ 136 struct apb_softc *sc = device_get_softc(bus); 137 struct apb_ivar *ivar = device_get_ivars(child); 138 struct resource *rv; 139 struct resource_list_entry *rle; 140 struct rman *rm; 141 int isdefault, needactivate, passthrough; 142 143 isdefault = (start == 0UL && end == ~0UL); 144 needactivate = flags & RF_ACTIVE; 145 /* 146 * Pass memory requests to nexus device 147 */ 148 passthrough = (device_get_parent(child) != bus) || 149 (type == SYS_RES_MEMORY); 150 rle = NULL; 151 152 dprintf("%s: entry (%p, %p, %d, %p, %p, %p, %ld, %d)\n", 153 __func__, bus, child, type, rid, (void *)(intptr_t)start, 154 (void *)(intptr_t)end, count, flags); 155 156 if (passthrough) 157 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, 158 rid, start, end, count, flags)); 159 160 /* 161 * If this is an allocation of the "default" range for a given RID, 162 * and we know what the resources for this device are (ie. they aren't 163 * maintained by a child bus), then work out the start/end values. 164 */ 165 166 if (isdefault) { 167 rle = resource_list_find(&ivar->resources, type, *rid); 168 printf("DEFAULT: %d, %p\n", *rid, rle); 169 if (rle == NULL) { 170 return (NULL); 171 } 172 173 if (rle->res != NULL) { 174 panic("%s: resource entry is busy", __func__); 175 } 176 start = rle->start; 177 end = rle->end; 178 count = rle->count; 179 180 dprintf("%s: default resource (%p, %p, %ld)\n", 181 __func__, (void *)(intptr_t)start, 182 (void *)(intptr_t)end, count); 183 } 184 185 switch (type) { 186 case SYS_RES_IRQ: 187 rm = &sc->apb_irq_rman; 188 break; 189 default: 190 printf("%s: unknown resource type %d\n", __func__, type); 191 return (0); 192 } 193 194 rv = rman_reserve_resource(rm, start, end, count, flags, child); 195 if (rv == 0) { 196 printf("%s: could not reserve resource\n", __func__); 197 return (0); 198 } 199 200 rman_set_rid(rv, *rid); 201 202 if (needactivate) { 203 if (bus_activate_resource(child, type, *rid, rv)) { 204 printf("%s: could not activate resource\n", __func__); 205 rman_release_resource(rv); 206 return (0); 207 } 208 } 209 210 return (rv); 211} 212 213static int 214apb_activate_resource(device_t bus, device_t child, int type, int rid, 215 struct resource *r) 216{ 217 218 /* XXX: should we mask/unmask IRQ here? */ 219 return (BUS_ACTIVATE_RESOURCE(device_get_parent(bus), child, 220 type, rid, r)); 221} 222 223static int 224apb_deactivate_resource(device_t bus, device_t child, int type, int rid, 225 struct resource *r) 226{ 227 228 /* XXX: should we mask/unmask IRQ here? */ 229 return (BUS_DEACTIVATE_RESOURCE(device_get_parent(bus), child, 230 type, rid, r)); 231} 232 233static int 234apb_release_resource(device_t dev, device_t child, int type, 235 int rid, struct resource *r) 236{ 237 struct resource_list *rl; 238 struct resource_list_entry *rle; 239 240 rl = apb_get_resource_list(dev, child); 241 if (rl == NULL) 242 return (EINVAL); 243 rle = resource_list_find(rl, type, rid); 244 if (rle == NULL) 245 return (EINVAL); 246 rman_release_resource(r); 247 rle->res = NULL; 248 249 return (0); 250} 251 252static int 253apb_setup_intr(device_t bus, device_t child, struct resource *ires, 254 int flags, driver_filter_t *filt, driver_intr_t *handler, 255 void *arg, void **cookiep) 256{ 257 struct apb_softc *sc = device_get_softc(bus); 258 struct intr_event *event; 259 int irq, error; 260 261 irq = rman_get_start(ires); 262 263 if (irq > APB_IRQ_END) 264 panic("%s: bad irq %d", __func__, irq); 265 266 event = sc->sc_eventstab[irq]; 267 if (event == NULL) { 268 error = intr_event_create(&event, (void *)irq, 0, irq, 269 (mask_fn)apb_mask_irq, (mask_fn)apb_unmask_irq, 270 NULL, NULL, 271 "apb intr%d:", irq); 272 273 sc->sc_eventstab[irq] = event; 274 } 275 276 intr_event_add_handler(event, device_get_nameunit(child), filt, 277 handler, arg, intr_priority(flags), flags, cookiep); 278 279 return (0); 280} 281 282static int 283apb_teardown_intr(device_t dev, device_t child, struct resource *ires, 284 void *cookie) 285{ 286 struct apb_softc *sc = device_get_softc(dev); 287 int irq, result; 288 289 irq = rman_get_start(ires); 290 if (irq > APB_IRQ_END) 291 panic("%s: bad irq %d", __func__, irq); 292 293 if (sc->sc_eventstab[irq] == NULL) 294 panic("Trying to teardown unoccupied IRQ"); 295 296 apb_mask_irq(irq); 297 298 result = intr_event_remove_handler(cookie); 299 if (!result) 300 sc->sc_eventstab[irq] = NULL; 301 302 return (result); 303} 304 305static int 306apb_intr(void *arg) 307{ 308 struct apb_softc *sc = arg; 309 struct intr_event *event; 310 uint32_t reg, irq; 311 312 reg = ATH_READ_REG(AR71XX_MISC_INTR_STATUS); 313 for (irq = 0; irq < APB_NIRQS; irq++) { 314 if (reg & (1 << irq)) { 315 event = sc->sc_eventstab[irq]; 316 if (!event || TAILQ_EMPTY(&event->ie_handlers)) { 317 printf("Stray IRQ %d\n", irq); 318 continue; 319 } 320 321 /* TODO: frame instead of NULL? */ 322 intr_event_handle(event, NULL); 323 } 324 } 325 326 return (FILTER_HANDLED); 327} 328 329static void 330apb_hinted_child(device_t bus, const char *dname, int dunit) 331{ 332 device_t child; 333 long maddr; 334 int msize; 335 int irq; 336 int result; 337 int mem_hints_count; 338 339 child = BUS_ADD_CHILD(bus, 0, dname, dunit); 340 341 /* 342 * Set hard-wired resources for hinted child using 343 * specific RIDs. 344 */ 345 mem_hints_count = 0; 346 if (resource_long_value(dname, dunit, "maddr", &maddr) == 0) 347 mem_hints_count++; 348 if (resource_int_value(dname, dunit, "msize", &msize) == 0) 349 mem_hints_count++; 350 351 /* check if all info for mem resource has been provided */ 352 if ((mem_hints_count > 0) && (mem_hints_count < 2)) { 353 printf("Either maddr or msize hint is missing for %s%d\n", 354 dname, dunit); 355 } else if (mem_hints_count) { 356 result = bus_set_resource(child, SYS_RES_MEMORY, 0, 357 maddr, msize); 358 if (result != 0) 359 device_printf(bus, 360 "warning: bus_set_resource() failed\n"); 361 } 362 363 if (resource_int_value(dname, dunit, "irq", &irq) == 0) { 364 result = bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1); 365 if (result != 0) 366 device_printf(bus, 367 "warning: bus_set_resource() failed\n"); 368 } 369} 370 371static device_t 372apb_add_child(device_t bus, int order, const char *name, int unit) 373{ 374 device_t child; 375 struct apb_ivar *ivar; 376 377 ivar = malloc(sizeof(struct apb_ivar), M_DEVBUF, M_WAITOK | M_ZERO); 378 if (ivar == NULL) { 379 printf("Failed to allocate ivar\n"); 380 return (0); 381 } 382 resource_list_init(&ivar->resources); 383 384 child = device_add_child_ordered(bus, order, name, unit); 385 if (child == NULL) { 386 printf("Can't add child %s%d ordered\n", name, unit); 387 return (0); 388 } 389 390 device_set_ivars(child, ivar); 391 392 return (child); 393} 394 395/* 396 * Helper routine for bus_generic_rl_get_resource/bus_generic_rl_set_resource 397 * Provides pointer to resource_list for these routines 398 */ 399static struct resource_list * 400apb_get_resource_list(device_t dev, device_t child) 401{ 402 struct apb_ivar *ivar; 403 404 ivar = device_get_ivars(child); 405 return (&(ivar->resources)); 406} 407 408static device_method_t apb_methods[] = { 409 DEVMETHOD(bus_activate_resource, apb_activate_resource), 410 DEVMETHOD(bus_add_child, apb_add_child), 411 DEVMETHOD(bus_alloc_resource, apb_alloc_resource), 412 DEVMETHOD(bus_deactivate_resource, apb_deactivate_resource), 413 DEVMETHOD(bus_get_resource_list, apb_get_resource_list), 414 DEVMETHOD(bus_hinted_child, apb_hinted_child), 415 DEVMETHOD(bus_print_child, bus_generic_print_child), 416 DEVMETHOD(bus_release_resource, apb_release_resource), 417 DEVMETHOD(bus_setup_intr, apb_setup_intr), 418 DEVMETHOD(bus_teardown_intr, apb_teardown_intr), 419 DEVMETHOD(device_attach, apb_attach), 420 DEVMETHOD(device_probe, apb_probe), 421 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 422 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource), 423 424 {0, 0}, 425}; 426 427static driver_t apb_driver = { 428 "apb", 429 apb_methods, 430 sizeof(struct apb_softc), 431}; 432static devclass_t apb_devclass; 433 434DRIVER_MODULE(apb, nexus, apb_driver, apb_devclass, 0, 0); 435