apb.c revision 188883
1187517Sgonzo/*- 2187517Sgonzo * Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@FreeBSD.org> 3187517Sgonzo * All rights reserved. 4187517Sgonzo * 5187517Sgonzo * Redistribution and use in source and binary forms, with or without 6187517Sgonzo * modification, are permitted provided that the following conditions 7187517Sgonzo * are met: 8187517Sgonzo * 1. Redistributions of source code must retain the above copyright 9187517Sgonzo * notice unmodified, this list of conditions, and the following 10187517Sgonzo * disclaimer. 11187517Sgonzo * 2. Redistributions in binary form must reproduce the above copyright 12187517Sgonzo * notice, this list of conditions and the following disclaimer in the 13187517Sgonzo * documentation and/or other materials provided with the distribution. 14187517Sgonzo * 15187517Sgonzo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16187517Sgonzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17187517Sgonzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18187517Sgonzo * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19187517Sgonzo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20187517Sgonzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21187517Sgonzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22187517Sgonzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23187517Sgonzo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24187517Sgonzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25187517Sgonzo * SUCH DAMAGE. 26187517Sgonzo */ 27187517Sgonzo 28187517Sgonzo#include <sys/cdefs.h> 29187517Sgonzo__FBSDID("$FreeBSD$"); 30187517Sgonzo 31187517Sgonzo#include <sys/param.h> 32187517Sgonzo#include <sys/systm.h> 33187517Sgonzo#include <sys/bus.h> 34187517Sgonzo#include <sys/interrupt.h> 35187517Sgonzo#include <sys/kernel.h> 36187517Sgonzo#include <sys/module.h> 37187517Sgonzo#include <sys/rman.h> 38187517Sgonzo#include <sys/malloc.h> 39187517Sgonzo 40187517Sgonzo#include <machine/bus.h> 41187517Sgonzo 42187517Sgonzo#include <mips/atheros/apbvar.h> 43187517Sgonzo#include <mips/atheros/ar71xxreg.h> 44187517Sgonzo 45188883Sgonzo#undef APB_DEBUG 46188883Sgonzo#ifdef APB_DEBUG 47188883Sgonzo#define dprintf printf 48188883Sgonzo#else 49188883Sgonzo#define dprintf(x, arg...) 50188883Sgonzo#endif /* APB_DEBUG */ 51188883Sgonzo 52187517Sgonzostatic int apb_activate_resource(device_t, device_t, int, int, 53187517Sgonzo struct resource *); 54187517Sgonzostatic device_t apb_add_child(device_t, int, const char *, int); 55187517Sgonzostatic struct resource * 56187517Sgonzo apb_alloc_resource(device_t, device_t, int, int *, u_long, 57187517Sgonzo u_long, u_long, u_int); 58187517Sgonzostatic int apb_attach(device_t); 59187517Sgonzostatic int apb_deactivate_resource(device_t, device_t, int, int, 60187517Sgonzo struct resource *); 61187517Sgonzostatic struct resource_list * 62187517Sgonzo apb_get_resource_list(device_t, device_t); 63187517Sgonzostatic void apb_hinted_child(device_t, const char *, int); 64187517Sgonzostatic int apb_intr(void *); 65187517Sgonzostatic int apb_probe(device_t); 66187517Sgonzostatic int apb_release_resource(device_t, device_t, int, int, 67187517Sgonzo struct resource *); 68187517Sgonzostatic int apb_setup_intr(device_t, device_t, struct resource *, int, 69187517Sgonzo driver_filter_t *, driver_intr_t *, void *, void **); 70187517Sgonzostatic int apb_teardown_intr(device_t, device_t, struct resource *, 71187517Sgonzo void *); 72187517Sgonzo 73187517Sgonzostatic void apb_mask_irq(unsigned int irq) 74187517Sgonzo{ 75187517Sgonzo uint32_t reg; 76187517Sgonzo 77187517Sgonzo reg = ATH_READ_REG(AR71XX_MISC_INTR_MASK); 78187517Sgonzo ATH_WRITE_REG(AR71XX_MISC_INTR_MASK, reg & ~(1 << irq)); 79187517Sgonzo 80187517Sgonzo} 81187517Sgonzo 82187517Sgonzostatic void apb_unmask_irq(unsigned int irq) 83187517Sgonzo{ 84187517Sgonzo uint32_t reg; 85187517Sgonzo 86187517Sgonzo reg = ATH_READ_REG(AR71XX_MISC_INTR_MASK); 87187517Sgonzo ATH_WRITE_REG(AR71XX_MISC_INTR_MASK, reg | (1 << irq)); 88187517Sgonzo} 89187517Sgonzo 90187517Sgonzostatic int 91187517Sgonzoapb_probe(device_t dev) 92187517Sgonzo{ 93187517Sgonzo 94187517Sgonzo return (0); 95187517Sgonzo} 96187517Sgonzo 97187517Sgonzostatic int 98187517Sgonzoapb_attach(device_t dev) 99187517Sgonzo{ 100187517Sgonzo struct apb_softc *sc = device_get_softc(dev); 101187517Sgonzo int rid = 0; 102187517Sgonzo 103187517Sgonzo device_set_desc(dev, "APB Bus bridge"); 104187517Sgonzo sc->apb_irq_rman.rm_type = RMAN_ARRAY; 105187517Sgonzo sc->apb_irq_rman.rm_descr = "APB IRQ"; 106187517Sgonzo 107187517Sgonzo if (rman_init(&sc->apb_irq_rman) != 0 || 108187517Sgonzo rman_manage_region(&sc->apb_irq_rman, 109187517Sgonzo APB_IRQ_BASE, APB_IRQ_END) != 0) 110187517Sgonzo panic("apb_attach: failed to set up IRQ rman"); 111187517Sgonzo 112187517Sgonzo if ((sc->sc_misc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 113187517Sgonzo RF_SHAREABLE | RF_ACTIVE)) == NULL) { 114187517Sgonzo device_printf(dev, "unable to allocate IRQ resource\n"); 115187517Sgonzo return (ENXIO); 116187517Sgonzo } 117187517Sgonzo 118187517Sgonzo if ((bus_setup_intr(dev, sc->sc_misc_irq, INTR_TYPE_MISC, 119187517Sgonzo apb_intr, NULL, sc, &sc->sc_misc_ih))) { 120187517Sgonzo device_printf(dev, 121187517Sgonzo "WARNING: unable to register interrupt handler\n"); 122187517Sgonzo return (ENXIO); 123187517Sgonzo } 124187517Sgonzo 125187517Sgonzo bus_generic_probe(dev); 126187517Sgonzo bus_enumerate_hinted_children(dev); 127187517Sgonzo bus_generic_attach(dev); 128187517Sgonzo 129187517Sgonzo return (0); 130187517Sgonzo} 131187517Sgonzo 132187517Sgonzostatic struct resource * 133187517Sgonzoapb_alloc_resource(device_t bus, device_t child, int type, int *rid, 134187517Sgonzo u_long start, u_long end, u_long count, u_int flags) 135187517Sgonzo{ 136187517Sgonzo struct apb_softc *sc = device_get_softc(bus); 137187517Sgonzo struct apb_ivar *ivar = device_get_ivars(child); 138187517Sgonzo struct resource *rv; 139187517Sgonzo struct resource_list_entry *rle; 140187517Sgonzo struct rman *rm; 141187517Sgonzo int isdefault, needactivate, passthrough; 142187517Sgonzo 143187517Sgonzo isdefault = (start == 0UL && end == ~0UL); 144187517Sgonzo needactivate = flags & RF_ACTIVE; 145188883Sgonzo /* 146188883Sgonzo * Pass memory requests to nexus device 147188883Sgonzo */ 148188883Sgonzo passthrough = (device_get_parent(child) != bus) || 149188883Sgonzo (type == SYS_RES_MEMORY); 150187517Sgonzo rle = NULL; 151187517Sgonzo 152188883Sgonzo dprintf("%s: entry (%p, %p, %d, %p, %p, %p, %ld, %d)\n", 153188883Sgonzo __func__, bus, child, type, rid, (void *)(intptr_t)start, 154188883Sgonzo (void *)(intptr_t)end, count, flags); 155188883Sgonzo 156187517Sgonzo if (passthrough) 157187517Sgonzo return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, 158187517Sgonzo rid, start, end, count, flags)); 159187517Sgonzo 160187517Sgonzo /* 161187517Sgonzo * If this is an allocation of the "default" range for a given RID, 162187517Sgonzo * and we know what the resources for this device are (ie. they aren't 163187517Sgonzo * maintained by a child bus), then work out the start/end values. 164187517Sgonzo */ 165187517Sgonzo 166187517Sgonzo if (isdefault) { 167187517Sgonzo rle = resource_list_find(&ivar->resources, type, *rid); 168188883Sgonzo printf("DEFAULT: %d, %p\n", *rid, rle); 169187517Sgonzo if (rle == NULL) { 170187517Sgonzo return (NULL); 171187517Sgonzo } 172187517Sgonzo 173187517Sgonzo if (rle->res != NULL) { 174187517Sgonzo panic("%s: resource entry is busy", __func__); 175187517Sgonzo } 176187517Sgonzo start = rle->start; 177187517Sgonzo end = rle->end; 178187517Sgonzo count = rle->count; 179188883Sgonzo 180188883Sgonzo dprintf("%s: default resource (%p, %p, %ld)\n", 181188883Sgonzo __func__, (void *)(intptr_t)start, 182188883Sgonzo (void *)(intptr_t)end, count); 183187517Sgonzo } 184187517Sgonzo 185187517Sgonzo switch (type) { 186187517Sgonzo case SYS_RES_IRQ: 187187517Sgonzo rm = &sc->apb_irq_rman; 188187517Sgonzo break; 189187517Sgonzo default: 190187517Sgonzo printf("%s: unknown resource type %d\n", __func__, type); 191187517Sgonzo return (0); 192187517Sgonzo } 193187517Sgonzo 194187517Sgonzo rv = rman_reserve_resource(rm, start, end, count, flags, child); 195187517Sgonzo if (rv == 0) { 196187517Sgonzo printf("%s: could not reserve resource\n", __func__); 197187517Sgonzo return (0); 198187517Sgonzo } 199187517Sgonzo 200187517Sgonzo rman_set_rid(rv, *rid); 201187517Sgonzo 202187517Sgonzo if (needactivate) { 203187517Sgonzo if (bus_activate_resource(child, type, *rid, rv)) { 204187517Sgonzo printf("%s: could not activate resource\n", __func__); 205187517Sgonzo rman_release_resource(rv); 206187517Sgonzo return (0); 207187517Sgonzo } 208187517Sgonzo } 209187517Sgonzo 210187517Sgonzo return (rv); 211187517Sgonzo} 212187517Sgonzo 213187517Sgonzostatic int 214187517Sgonzoapb_activate_resource(device_t bus, device_t child, int type, int rid, 215187517Sgonzo struct resource *r) 216187517Sgonzo{ 217187517Sgonzo 218187517Sgonzo /* XXX: should we mask/unmask IRQ here? */ 219187517Sgonzo return (BUS_ACTIVATE_RESOURCE(device_get_parent(bus), child, 220187517Sgonzo type, rid, r)); 221187517Sgonzo} 222187517Sgonzo 223187517Sgonzostatic int 224187517Sgonzoapb_deactivate_resource(device_t bus, device_t child, int type, int rid, 225187517Sgonzo struct resource *r) 226187517Sgonzo{ 227187517Sgonzo 228187517Sgonzo /* XXX: should we mask/unmask IRQ here? */ 229187517Sgonzo return (BUS_DEACTIVATE_RESOURCE(device_get_parent(bus), child, 230187517Sgonzo type, rid, r)); 231187517Sgonzo} 232187517Sgonzo 233187517Sgonzostatic int 234187517Sgonzoapb_release_resource(device_t dev, device_t child, int type, 235187517Sgonzo int rid, struct resource *r) 236187517Sgonzo{ 237187517Sgonzo struct resource_list *rl; 238187517Sgonzo struct resource_list_entry *rle; 239187517Sgonzo 240187517Sgonzo rl = apb_get_resource_list(dev, child); 241187517Sgonzo if (rl == NULL) 242187517Sgonzo return (EINVAL); 243187517Sgonzo rle = resource_list_find(rl, type, rid); 244187517Sgonzo if (rle == NULL) 245187517Sgonzo return (EINVAL); 246187517Sgonzo rman_release_resource(r); 247187517Sgonzo rle->res = NULL; 248187517Sgonzo 249187517Sgonzo return (0); 250187517Sgonzo} 251187517Sgonzo 252187517Sgonzostatic int 253187517Sgonzoapb_setup_intr(device_t bus, device_t child, struct resource *ires, 254187517Sgonzo int flags, driver_filter_t *filt, driver_intr_t *handler, 255187517Sgonzo void *arg, void **cookiep) 256187517Sgonzo{ 257187517Sgonzo struct apb_softc *sc = device_get_softc(bus); 258187517Sgonzo struct intr_event *event; 259187517Sgonzo int irq, error; 260187517Sgonzo 261187517Sgonzo irq = rman_get_start(ires); 262187517Sgonzo 263187517Sgonzo if (irq > APB_IRQ_END) 264187517Sgonzo panic("%s: bad irq %d", __func__, irq); 265187517Sgonzo 266187517Sgonzo event = sc->sc_eventstab[irq]; 267187517Sgonzo if (event == NULL) { 268187517Sgonzo error = intr_event_create(&event, (void *)irq, 0, irq, 269187517Sgonzo (mask_fn)apb_mask_irq, (mask_fn)apb_unmask_irq, 270187517Sgonzo NULL, NULL, 271187517Sgonzo "apb intr%d:", irq); 272187517Sgonzo 273187517Sgonzo sc->sc_eventstab[irq] = event; 274187517Sgonzo } 275187517Sgonzo 276187517Sgonzo intr_event_add_handler(event, device_get_nameunit(child), filt, 277187517Sgonzo handler, arg, intr_priority(flags), flags, cookiep); 278187517Sgonzo 279187517Sgonzo return (0); 280187517Sgonzo} 281187517Sgonzo 282187517Sgonzostatic int 283187517Sgonzoapb_teardown_intr(device_t dev, device_t child, struct resource *ires, 284187517Sgonzo void *cookie) 285187517Sgonzo{ 286187517Sgonzo struct apb_softc *sc = device_get_softc(dev); 287187517Sgonzo int irq, result; 288187517Sgonzo 289187517Sgonzo irq = rman_get_start(ires); 290187517Sgonzo if (irq > APB_IRQ_END) 291187517Sgonzo panic("%s: bad irq %d", __func__, irq); 292187517Sgonzo 293187517Sgonzo if (sc->sc_eventstab[irq] == NULL) 294187517Sgonzo panic("Trying to teardown unoccupied IRQ"); 295187517Sgonzo 296187517Sgonzo apb_mask_irq(irq); 297187517Sgonzo 298187517Sgonzo result = intr_event_remove_handler(cookie); 299187517Sgonzo if (!result) 300187517Sgonzo sc->sc_eventstab[irq] = NULL; 301187517Sgonzo 302187517Sgonzo return (result); 303187517Sgonzo} 304187517Sgonzo 305187517Sgonzostatic int 306187517Sgonzoapb_intr(void *arg) 307187517Sgonzo{ 308187517Sgonzo struct apb_softc *sc = arg; 309187517Sgonzo struct intr_event *event; 310187517Sgonzo uint32_t reg, irq; 311187517Sgonzo 312187517Sgonzo reg = ATH_READ_REG(AR71XX_MISC_INTR_STATUS); 313187517Sgonzo for (irq = 0; irq < APB_NIRQS; irq++) { 314187517Sgonzo if (reg & (1 << irq)) { 315187517Sgonzo event = sc->sc_eventstab[irq]; 316187517Sgonzo if (!event || TAILQ_EMPTY(&event->ie_handlers)) { 317187517Sgonzo printf("Stray IRQ %d\n", irq); 318187517Sgonzo continue; 319187517Sgonzo } 320187517Sgonzo 321187517Sgonzo /* TODO: frame instead of NULL? */ 322187517Sgonzo intr_event_handle(event, NULL); 323187517Sgonzo } 324187517Sgonzo } 325187517Sgonzo 326187517Sgonzo return (FILTER_HANDLED); 327187517Sgonzo} 328187517Sgonzo 329187517Sgonzostatic void 330187517Sgonzoapb_hinted_child(device_t bus, const char *dname, int dunit) 331187517Sgonzo{ 332187517Sgonzo device_t child; 333187517Sgonzo long maddr; 334187517Sgonzo int msize; 335187517Sgonzo int irq; 336187517Sgonzo int result; 337187517Sgonzo int mem_hints_count; 338187517Sgonzo 339187517Sgonzo child = BUS_ADD_CHILD(bus, 0, dname, dunit); 340187517Sgonzo 341187517Sgonzo /* 342187517Sgonzo * Set hard-wired resources for hinted child using 343187517Sgonzo * specific RIDs. 344187517Sgonzo */ 345187517Sgonzo mem_hints_count = 0; 346187517Sgonzo if (resource_long_value(dname, dunit, "maddr", &maddr) == 0) 347187517Sgonzo mem_hints_count++; 348187517Sgonzo if (resource_int_value(dname, dunit, "msize", &msize) == 0) 349187517Sgonzo mem_hints_count++; 350187517Sgonzo 351187517Sgonzo /* check if all info for mem resource has been provided */ 352187517Sgonzo if ((mem_hints_count > 0) && (mem_hints_count < 2)) { 353187517Sgonzo printf("Either maddr or msize hint is missing for %s%d\n", 354187517Sgonzo dname, dunit); 355187517Sgonzo } else if (mem_hints_count) { 356187517Sgonzo result = bus_set_resource(child, SYS_RES_MEMORY, 0, 357187517Sgonzo maddr, msize); 358187517Sgonzo if (result != 0) 359187517Sgonzo device_printf(bus, 360187517Sgonzo "warning: bus_set_resource() failed\n"); 361187517Sgonzo } 362187517Sgonzo 363187517Sgonzo if (resource_int_value(dname, dunit, "irq", &irq) == 0) { 364187517Sgonzo result = bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1); 365187517Sgonzo if (result != 0) 366187517Sgonzo device_printf(bus, 367187517Sgonzo "warning: bus_set_resource() failed\n"); 368187517Sgonzo } 369187517Sgonzo} 370187517Sgonzo 371187517Sgonzostatic device_t 372187517Sgonzoapb_add_child(device_t bus, int order, const char *name, int unit) 373187517Sgonzo{ 374187517Sgonzo device_t child; 375187517Sgonzo struct apb_ivar *ivar; 376187517Sgonzo 377187517Sgonzo ivar = malloc(sizeof(struct apb_ivar), M_DEVBUF, M_WAITOK | M_ZERO); 378187517Sgonzo if (ivar == NULL) { 379187517Sgonzo printf("Failed to allocate ivar\n"); 380187517Sgonzo return (0); 381187517Sgonzo } 382187517Sgonzo resource_list_init(&ivar->resources); 383187517Sgonzo 384187517Sgonzo child = device_add_child_ordered(bus, order, name, unit); 385187517Sgonzo if (child == NULL) { 386187517Sgonzo printf("Can't add child %s%d ordered\n", name, unit); 387187517Sgonzo return (0); 388187517Sgonzo } 389187517Sgonzo 390187517Sgonzo device_set_ivars(child, ivar); 391187517Sgonzo 392187517Sgonzo return (child); 393187517Sgonzo} 394187517Sgonzo 395187517Sgonzo/* 396187517Sgonzo * Helper routine for bus_generic_rl_get_resource/bus_generic_rl_set_resource 397187517Sgonzo * Provides pointer to resource_list for these routines 398187517Sgonzo */ 399187517Sgonzostatic struct resource_list * 400187517Sgonzoapb_get_resource_list(device_t dev, device_t child) 401187517Sgonzo{ 402187517Sgonzo struct apb_ivar *ivar; 403187517Sgonzo 404187517Sgonzo ivar = device_get_ivars(child); 405187517Sgonzo return (&(ivar->resources)); 406187517Sgonzo} 407187517Sgonzo 408187517Sgonzostatic device_method_t apb_methods[] = { 409187517Sgonzo DEVMETHOD(bus_activate_resource, apb_activate_resource), 410187517Sgonzo DEVMETHOD(bus_add_child, apb_add_child), 411187517Sgonzo DEVMETHOD(bus_alloc_resource, apb_alloc_resource), 412187517Sgonzo DEVMETHOD(bus_deactivate_resource, apb_deactivate_resource), 413187517Sgonzo DEVMETHOD(bus_get_resource_list, apb_get_resource_list), 414187517Sgonzo DEVMETHOD(bus_hinted_child, apb_hinted_child), 415187517Sgonzo DEVMETHOD(bus_print_child, bus_generic_print_child), 416187517Sgonzo DEVMETHOD(bus_release_resource, apb_release_resource), 417187517Sgonzo DEVMETHOD(bus_setup_intr, apb_setup_intr), 418187517Sgonzo DEVMETHOD(bus_teardown_intr, apb_teardown_intr), 419187517Sgonzo DEVMETHOD(device_attach, apb_attach), 420187517Sgonzo DEVMETHOD(device_probe, apb_probe), 421187517Sgonzo DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 422187517Sgonzo DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource), 423187517Sgonzo 424187517Sgonzo {0, 0}, 425187517Sgonzo}; 426187517Sgonzo 427187517Sgonzostatic driver_t apb_driver = { 428187517Sgonzo "apb", 429187517Sgonzo apb_methods, 430187517Sgonzo sizeof(struct apb_softc), 431187517Sgonzo}; 432187517Sgonzostatic devclass_t apb_devclass; 433187517Sgonzo 434187517SgonzoDRIVER_MODULE(apb, nexus, apb_driver, apb_devclass, 0, 0); 435