apb.c revision 263279
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: head/sys/mips/atheros/apb.c 263279 2014-03-17 23:21:31Z adrian $"); 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#include <sys/pcpu.h> 40#include <sys/proc.h> 41#include <sys/pmc.h> 42#include <sys/pmckern.h> 43 44#include <machine/bus.h> 45#include <machine/intr_machdep.h> 46 47#include <mips/atheros/apbvar.h> 48#include <mips/atheros/ar71xxreg.h> 49#include <mips/atheros/ar71xx_setup.h> 50 51#define APB_INTR_PMC 5 52 53#undef APB_DEBUG 54#ifdef APB_DEBUG 55#define dprintf printf 56#else 57#define dprintf(x, arg...) 58#endif /* APB_DEBUG */ 59 60#define DEVTOAPB(dev) ((struct apb_ivar *) device_get_ivars(dev)) 61 62static int apb_activate_resource(device_t, device_t, int, int, 63 struct resource *); 64static device_t apb_add_child(device_t, u_int, const char *, int); 65static struct resource * 66 apb_alloc_resource(device_t, device_t, int, int *, u_long, 67 u_long, u_long, u_int); 68static int apb_attach(device_t); 69static int apb_deactivate_resource(device_t, device_t, int, int, 70 struct resource *); 71static struct resource_list * 72 apb_get_resource_list(device_t, device_t); 73static void apb_hinted_child(device_t, const char *, int); 74static int apb_filter(void *); 75static int apb_probe(device_t); 76static int apb_release_resource(device_t, device_t, int, int, 77 struct resource *); 78static int apb_setup_intr(device_t, device_t, struct resource *, int, 79 driver_filter_t *, driver_intr_t *, void *, void **); 80static int apb_teardown_intr(device_t, device_t, struct resource *, 81 void *); 82 83static void 84apb_mask_irq(void *source) 85{ 86 unsigned int irq = (unsigned int)source; 87 uint32_t reg; 88 89 reg = ATH_READ_REG(AR71XX_MISC_INTR_MASK); 90 ATH_WRITE_REG(AR71XX_MISC_INTR_MASK, reg & ~(1 << irq)); 91 92} 93 94static void 95apb_unmask_irq(void *source) 96{ 97 uint32_t reg; 98 unsigned int irq = (unsigned int)source; 99 100 reg = ATH_READ_REG(AR71XX_MISC_INTR_MASK); 101 ATH_WRITE_REG(AR71XX_MISC_INTR_MASK, reg | (1 << irq)); 102} 103 104static int 105apb_probe(device_t dev) 106{ 107 108 return (BUS_PROBE_NOWILDCARD); 109} 110 111static int 112apb_attach(device_t dev) 113{ 114 struct apb_softc *sc = device_get_softc(dev); 115 int rid = 0; 116 117 device_set_desc(dev, "APB Bus bridge"); 118 119 sc->apb_mem_rman.rm_type = RMAN_ARRAY; 120 sc->apb_mem_rman.rm_descr = "APB memory window"; 121 122 if (rman_init(&sc->apb_mem_rman) != 0 || 123 rman_manage_region(&sc->apb_mem_rman, 124 AR71XX_APB_BASE, 125 AR71XX_APB_BASE + AR71XX_APB_SIZE - 1) != 0) 126 panic("apb_attach: failed to set up memory rman"); 127 128 sc->apb_irq_rman.rm_type = RMAN_ARRAY; 129 sc->apb_irq_rman.rm_descr = "APB IRQ"; 130 131 if (rman_init(&sc->apb_irq_rman) != 0 || 132 rman_manage_region(&sc->apb_irq_rman, 133 APB_IRQ_BASE, APB_IRQ_END) != 0) 134 panic("apb_attach: failed to set up IRQ rman"); 135 136 if ((sc->sc_misc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 137 RF_SHAREABLE | RF_ACTIVE)) == NULL) { 138 device_printf(dev, "unable to allocate IRQ resource\n"); 139 return (ENXIO); 140 } 141 142 if ((bus_setup_intr(dev, sc->sc_misc_irq, INTR_TYPE_MISC, 143 apb_filter, NULL, sc, &sc->sc_misc_ih))) { 144 device_printf(dev, 145 "WARNING: unable to register interrupt handler\n"); 146 return (ENXIO); 147 } 148 149 bus_generic_probe(dev); 150 bus_enumerate_hinted_children(dev); 151 bus_generic_attach(dev); 152 153 /* 154 * Unmask performance counter IRQ 155 */ 156 apb_unmask_irq((void*)APB_INTR_PMC); 157 sc->sc_intr_counter[APB_INTR_PMC] = mips_intrcnt_create("apb irq5: pmc"); 158 159 return (0); 160} 161 162static struct resource * 163apb_alloc_resource(device_t bus, device_t child, int type, int *rid, 164 u_long start, u_long end, u_long count, u_int flags) 165{ 166 struct apb_softc *sc = device_get_softc(bus); 167 struct apb_ivar *ivar = device_get_ivars(child); 168 struct resource *rv; 169 struct resource_list_entry *rle; 170 struct rman *rm; 171 int isdefault, needactivate, passthrough; 172 173 isdefault = (start == 0UL && end == ~0UL); 174 needactivate = flags & RF_ACTIVE; 175 /* 176 * Pass memory requests to nexus device 177 */ 178 passthrough = (device_get_parent(child) != bus); 179 rle = NULL; 180 181 dprintf("%s: entry (%p, %p, %d, %d, %p, %p, %ld, %d)\n", 182 __func__, bus, child, type, *rid, (void *)(intptr_t)start, 183 (void *)(intptr_t)end, count, flags); 184 185 if (passthrough) 186 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, 187 rid, start, end, count, flags)); 188 189 /* 190 * If this is an allocation of the "default" range for a given RID, 191 * and we know what the resources for this device are (ie. they aren't 192 * maintained by a child bus), then work out the start/end values. 193 */ 194 195 if (isdefault) { 196 rle = resource_list_find(&ivar->resources, type, *rid); 197 if (rle == NULL) { 198 return (NULL); 199 } 200 201 if (rle->res != NULL) { 202 panic("%s: resource entry is busy", __func__); 203 } 204 start = rle->start; 205 end = rle->end; 206 count = rle->count; 207 208 dprintf("%s: default resource (%p, %p, %ld)\n", 209 __func__, (void *)(intptr_t)start, 210 (void *)(intptr_t)end, count); 211 } 212 213 switch (type) { 214 case SYS_RES_IRQ: 215 rm = &sc->apb_irq_rman; 216 break; 217 case SYS_RES_MEMORY: 218 rm = &sc->apb_mem_rman; 219 break; 220 default: 221 printf("%s: unknown resource type %d\n", __func__, type); 222 return (0); 223 } 224 225 rv = rman_reserve_resource(rm, start, end, count, flags, child); 226 if (rv == 0) { 227 printf("%s: could not reserve resource\n", __func__); 228 return (0); 229 } 230 231 rman_set_rid(rv, *rid); 232 233 if (needactivate) { 234 if (bus_activate_resource(child, type, *rid, rv)) { 235 printf("%s: could not activate resource\n", __func__); 236 rman_release_resource(rv); 237 return (0); 238 } 239 } 240 241 return (rv); 242} 243 244static int 245apb_activate_resource(device_t bus, device_t child, int type, int rid, 246 struct resource *r) 247{ 248 249 /* XXX: should we mask/unmask IRQ here? */ 250 return (BUS_ACTIVATE_RESOURCE(device_get_parent(bus), child, 251 type, rid, r)); 252} 253 254static int 255apb_deactivate_resource(device_t bus, device_t child, int type, int rid, 256 struct resource *r) 257{ 258 259 /* XXX: should we mask/unmask IRQ here? */ 260 return (BUS_DEACTIVATE_RESOURCE(device_get_parent(bus), child, 261 type, rid, r)); 262} 263 264static int 265apb_release_resource(device_t dev, device_t child, int type, 266 int rid, struct resource *r) 267{ 268 struct resource_list *rl; 269 struct resource_list_entry *rle; 270 271 rl = apb_get_resource_list(dev, child); 272 if (rl == NULL) 273 return (EINVAL); 274 rle = resource_list_find(rl, type, rid); 275 if (rle == NULL) 276 return (EINVAL); 277 rman_release_resource(r); 278 rle->res = NULL; 279 280 return (0); 281} 282 283static int 284apb_setup_intr(device_t bus, device_t child, struct resource *ires, 285 int flags, driver_filter_t *filt, driver_intr_t *handler, 286 void *arg, void **cookiep) 287{ 288 struct apb_softc *sc = device_get_softc(bus); 289 struct intr_event *event; 290 int irq, error; 291 292 irq = rman_get_start(ires); 293 294 if (irq > APB_IRQ_END) 295 panic("%s: bad irq %d", __func__, irq); 296 297 event = sc->sc_eventstab[irq]; 298 if (event == NULL) { 299 error = intr_event_create(&event, (void *)irq, 0, irq, 300 apb_mask_irq, apb_unmask_irq, 301 NULL, NULL, 302 "apb intr%d:", irq); 303 304 if (error == 0) { 305 sc->sc_eventstab[irq] = event; 306 sc->sc_intr_counter[irq] = 307 mips_intrcnt_create(event->ie_name); 308 } 309 else 310 return (error); 311 } 312 313 intr_event_add_handler(event, device_get_nameunit(child), filt, 314 handler, arg, intr_priority(flags), flags, cookiep); 315 mips_intrcnt_setname(sc->sc_intr_counter[irq], event->ie_fullname); 316 317 apb_unmask_irq((void*)irq); 318 319 return (0); 320} 321 322static int 323apb_teardown_intr(device_t dev, device_t child, struct resource *ires, 324 void *cookie) 325{ 326 struct apb_softc *sc = device_get_softc(dev); 327 int irq, result; 328 329 irq = rman_get_start(ires); 330 if (irq > APB_IRQ_END) 331 panic("%s: bad irq %d", __func__, irq); 332 333 if (sc->sc_eventstab[irq] == NULL) 334 panic("Trying to teardown unoccupied IRQ"); 335 336 apb_mask_irq((void*)irq); 337 338 result = intr_event_remove_handler(cookie); 339 if (!result) 340 sc->sc_eventstab[irq] = NULL; 341 342 return (result); 343} 344 345static int 346apb_filter(void *arg) 347{ 348 struct apb_softc *sc = arg; 349 struct intr_event *event; 350 uint32_t reg, irq; 351 struct thread *td; 352 struct trapframe *tf; 353 354 reg = ATH_READ_REG(AR71XX_MISC_INTR_STATUS); 355 for (irq = 0; irq < APB_NIRQS; irq++) { 356 if (reg & (1 << irq)) { 357 358 switch (ar71xx_soc) { 359 case AR71XX_SOC_AR7240: 360 case AR71XX_SOC_AR7241: 361 case AR71XX_SOC_AR7242: 362 case AR71XX_SOC_AR9330: 363 case AR71XX_SOC_AR9331: 364 case AR71XX_SOC_AR9341: 365 case AR71XX_SOC_AR9342: 366 case AR71XX_SOC_AR9344: 367 /* ACK/clear the given interrupt */ 368 ATH_WRITE_REG(AR71XX_MISC_INTR_STATUS, 369 (1 << irq)); 370 break; 371 default: 372 /* fallthrough */ 373 break; 374 } 375 376 event = sc->sc_eventstab[irq]; 377 if (!event || TAILQ_EMPTY(&event->ie_handlers)) { 378 if (irq == APB_INTR_PMC) { 379 td = PCPU_GET(curthread); 380 tf = td->td_intr_frame; 381 382 if (pmc_intr) 383 (*pmc_intr)(PCPU_GET(cpuid), tf); 384 385 mips_intrcnt_inc(sc->sc_intr_counter[irq]); 386 387 continue; 388 } 389 /* Ignore timer interrupts */ 390 if (irq != 0 && irq != 8 && irq != 9 && irq != 10) 391 printf("Stray APB IRQ %d\n", irq); 392 continue; 393 } 394 395 intr_event_handle(event, PCPU_GET(curthread)->td_intr_frame); 396 mips_intrcnt_inc(sc->sc_intr_counter[irq]); 397 } 398 } 399 400 return (FILTER_HANDLED); 401} 402 403static void 404apb_hinted_child(device_t bus, const char *dname, int dunit) 405{ 406 device_t child; 407 long maddr; 408 int msize; 409 int irq; 410 int result; 411 int mem_hints_count; 412 413 child = BUS_ADD_CHILD(bus, 0, dname, dunit); 414 415 /* 416 * Set hard-wired resources for hinted child using 417 * specific RIDs. 418 */ 419 mem_hints_count = 0; 420 if (resource_long_value(dname, dunit, "maddr", &maddr) == 0) 421 mem_hints_count++; 422 if (resource_int_value(dname, dunit, "msize", &msize) == 0) 423 mem_hints_count++; 424 425 /* check if all info for mem resource has been provided */ 426 if ((mem_hints_count > 0) && (mem_hints_count < 2)) { 427 printf("Either maddr or msize hint is missing for %s%d\n", 428 dname, dunit); 429 } else if (mem_hints_count) { 430 result = bus_set_resource(child, SYS_RES_MEMORY, 0, 431 maddr, msize); 432 if (result != 0) 433 device_printf(bus, 434 "warning: bus_set_resource() failed\n"); 435 } 436 437 if (resource_int_value(dname, dunit, "irq", &irq) == 0) { 438 result = bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1); 439 if (result != 0) 440 device_printf(bus, 441 "warning: bus_set_resource() failed\n"); 442 } 443} 444 445static device_t 446apb_add_child(device_t bus, u_int order, const char *name, int unit) 447{ 448 device_t child; 449 struct apb_ivar *ivar; 450 451 ivar = malloc(sizeof(struct apb_ivar), M_DEVBUF, M_WAITOK | M_ZERO); 452 if (ivar == NULL) { 453 printf("Failed to allocate ivar\n"); 454 return (0); 455 } 456 resource_list_init(&ivar->resources); 457 458 child = device_add_child_ordered(bus, order, name, unit); 459 if (child == NULL) { 460 printf("Can't add child %s%d ordered\n", name, unit); 461 return (0); 462 } 463 464 device_set_ivars(child, ivar); 465 466 return (child); 467} 468 469/* 470 * Helper routine for bus_generic_rl_get_resource/bus_generic_rl_set_resource 471 * Provides pointer to resource_list for these routines 472 */ 473static struct resource_list * 474apb_get_resource_list(device_t dev, device_t child) 475{ 476 struct apb_ivar *ivar; 477 478 ivar = device_get_ivars(child); 479 return (&(ivar->resources)); 480} 481 482static int 483apb_print_all_resources(device_t dev) 484{ 485 struct apb_ivar *ndev = DEVTOAPB(dev); 486 struct resource_list *rl = &ndev->resources; 487 int retval = 0; 488 489 if (STAILQ_FIRST(rl)) 490 retval += printf(" at"); 491 492 retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx"); 493 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 494 495 return (retval); 496} 497 498static int 499apb_print_child(device_t bus, device_t child) 500{ 501 int retval = 0; 502 503 retval += bus_print_child_header(bus, child); 504 retval += apb_print_all_resources(child); 505 if (device_get_flags(child)) 506 retval += printf(" flags %#x", device_get_flags(child)); 507 retval += printf(" on %s\n", device_get_nameunit(bus)); 508 509 return (retval); 510} 511 512 513static device_method_t apb_methods[] = { 514 DEVMETHOD(bus_activate_resource, apb_activate_resource), 515 DEVMETHOD(bus_add_child, apb_add_child), 516 DEVMETHOD(bus_alloc_resource, apb_alloc_resource), 517 DEVMETHOD(bus_deactivate_resource, apb_deactivate_resource), 518 DEVMETHOD(bus_get_resource_list, apb_get_resource_list), 519 DEVMETHOD(bus_hinted_child, apb_hinted_child), 520 DEVMETHOD(bus_release_resource, apb_release_resource), 521 DEVMETHOD(bus_setup_intr, apb_setup_intr), 522 DEVMETHOD(bus_teardown_intr, apb_teardown_intr), 523 DEVMETHOD(device_attach, apb_attach), 524 DEVMETHOD(device_probe, apb_probe), 525 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 526 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource), 527 DEVMETHOD(bus_print_child, apb_print_child), 528 529 DEVMETHOD_END 530}; 531 532static driver_t apb_driver = { 533 "apb", 534 apb_methods, 535 sizeof(struct apb_softc), 536}; 537static devclass_t apb_devclass; 538 539DRIVER_MODULE(apb, nexus, apb_driver, apb_devclass, 0, 0); 540