obio.c revision 182901
1178173Simp/* $NetBSD: obio.c,v 1.11 2003/07/15 00:25:05 lukem Exp $ */ 2178173Simp 3178173Simp/*- 4178173Simp * Copyright (c) 2001, 2002, 2003 Wasabi Systems, Inc. 5178173Simp * All rights reserved. 6178173Simp * 7178173Simp * Written by Jason R. Thorpe for Wasabi Systems, Inc. 8178173Simp * 9178173Simp * Redistribution and use in source and binary forms, with or without 10178173Simp * modification, are permitted provided that the following conditions 11178173Simp * are met: 12178173Simp * 1. Redistributions of source code must retain the above copyright 13178173Simp * notice, this list of conditions and the following disclaimer. 14178173Simp * 2. Redistributions in binary form must reproduce the above copyright 15178173Simp * notice, this list of conditions and the following disclaimer in the 16178173Simp * documentation and/or other materials provided with the distribution. 17178173Simp * 3. All advertising materials mentioning features or use of this software 18178173Simp * must display the following acknowledgement: 19178173Simp * This product includes software developed for the NetBSD Project by 20178173Simp * Wasabi Systems, Inc. 21178173Simp * 4. The name of Wasabi Systems, Inc. may not be used to endorse 22178173Simp * or promote products derived from this software without specific prior 23178173Simp * written permission. 24178173Simp * 25178173Simp * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 26178173Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27178173Simp * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28178173Simp * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 29178173Simp * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30178173Simp * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31178173Simp * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32178173Simp * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33178173Simp * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34178173Simp * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35178173Simp * POSSIBILITY OF SUCH DAMAGE. 36178173Simp */ 37178173Simp 38178173Simp#include <sys/cdefs.h> 39178173Simp__FBSDID("$FreeBSD: head/sys/mips/adm5120/obio.c 182901 2008-09-10 03:49:08Z gonzo $"); 40178173Simp 41178173Simp#include <sys/param.h> 42178173Simp#include <sys/systm.h> 43178173Simp#include <sys/bus.h> 44178173Simp#include <sys/interrupt.h> 45178173Simp#include <sys/kernel.h> 46178173Simp#include <sys/module.h> 47178173Simp#include <sys/rman.h> 48178173Simp#include <sys/malloc.h> 49178173Simp 50178173Simp#include <machine/bus.h> 51178173Simp 52182901Sgonzo#include <mips/adm5120/adm5120reg.h> 53182901Sgonzo#include <mips/adm5120/obiovar.h> 54178173Simp 55178173Simp/* MIPS HW interrupts of IRQ/FIQ respectively */ 56178173Simp#define ADM5120_INTR 0 57178173Simp#define ADM5120_FAST_INTR 1 58178173Simp 59178173Simp/* Interrupt levels */ 60178173Simp#define INTR_IRQ 0 61178173Simp#define INTR_FIQ 1 62178173Simp 63178173Simpint irq_priorities[NIRQS] = { 64178173Simp INTR_IRQ, /* flash */ 65178173Simp INTR_FIQ, /* uart0 */ 66178173Simp INTR_FIQ, /* uart1 */ 67178173Simp INTR_IRQ, /* ahci */ 68178173Simp INTR_IRQ, /* unknown */ 69178173Simp INTR_IRQ, /* unknown */ 70178173Simp INTR_IRQ, /* unknown */ 71178173Simp INTR_IRQ, /* unknown */ 72178173Simp INTR_IRQ, /* unknown */ 73178173Simp INTR_IRQ, /* admsw */ 74178173Simp INTR_IRQ, /* unknown */ 75178173Simp INTR_IRQ, /* unknown */ 76178173Simp INTR_IRQ, /* unknown */ 77178173Simp INTR_IRQ, /* unknown */ 78178173Simp INTR_IRQ, /* unknown */ 79178173Simp INTR_IRQ, /* unknown */ 80178173Simp INTR_IRQ, /* unknown */ 81178173Simp INTR_IRQ, /* unknown */ 82178173Simp INTR_IRQ, /* unknown */ 83178173Simp INTR_IRQ, /* unknown */ 84178173Simp INTR_IRQ, /* unknown */ 85178173Simp INTR_IRQ, /* unknown */ 86178173Simp INTR_IRQ, /* unknown */ 87178173Simp INTR_IRQ, /* unknown */ 88178173Simp INTR_IRQ, /* unknown */ 89178173Simp INTR_IRQ, /* unknown */ 90178173Simp INTR_IRQ, /* unknown */ 91178173Simp INTR_IRQ, /* unknown */ 92178173Simp INTR_IRQ, /* unknown */ 93178173Simp INTR_IRQ, /* unknown */ 94178173Simp INTR_IRQ, /* unknown */ 95178173Simp INTR_IRQ, /* unknown */ 96178173Simp}; 97178173Simp 98178173Simp 99178173Simp#define REG_READ(o) *((volatile uint32_t *)MIPS_PHYS_TO_KSEG1(ADM5120_BASE_ICU + (o))) 100178173Simp#define REG_WRITE(o,v) (REG_READ(o)) = (v) 101178173Simp 102178173Simpstatic int obio_activate_resource(device_t, device_t, int, int, 103178173Simp struct resource *); 104178173Simpstatic device_t obio_add_child(device_t, int, const char *, int); 105178173Simpstatic struct resource * 106178173Simp obio_alloc_resource(device_t, device_t, int, int *, u_long, 107178173Simp u_long, u_long, u_int); 108178173Simpstatic int obio_attach(device_t); 109178173Simpstatic int obio_deactivate_resource(device_t, device_t, int, int, 110178173Simp struct resource *); 111178173Simpstatic struct resource_list * 112178173Simp obio_get_resource_list(device_t, device_t); 113178173Simpstatic void obio_hinted_child(device_t, const char *, int); 114178173Simpstatic int obio_intr(void *); 115178173Simpstatic int obio_probe(device_t); 116178173Simpstatic int obio_release_resource(device_t, device_t, int, int, 117178173Simp struct resource *); 118178173Simpstatic int obio_setup_intr(device_t, device_t, struct resource *, int, 119178173Simp driver_filter_t *, driver_intr_t *, void *, void **); 120178173Simpstatic int obio_teardown_intr(device_t, device_t, struct resource *, 121178173Simp void *); 122178173Simp 123178173Simpstatic int 124178173Simpobio_probe(device_t dev) 125178173Simp{ 126178173Simp 127178173Simp return (0); 128178173Simp} 129178173Simp 130178173Simpstatic int 131178173Simpobio_attach(device_t dev) 132178173Simp{ 133178173Simp struct obio_softc *sc = device_get_softc(dev); 134178173Simp int rid; 135178173Simp 136178173Simp sc->oba_mem_rman.rm_type = RMAN_ARRAY; 137178173Simp sc->oba_mem_rman.rm_descr = "OBIO memeory"; 138178173Simp if (rman_init(&sc->oba_mem_rman) != 0 || 139178173Simp rman_manage_region(&sc->oba_mem_rman, OBIO_MEM_START, 140178173Simp OBIO_MEM_START + OBIO_MEM_SIZE) != 0) 141178173Simp panic("obio_attach: failed to set up I/O rman"); 142178173Simp 143178173Simp sc->oba_irq_rman.rm_type = RMAN_ARRAY; 144178173Simp sc->oba_irq_rman.rm_descr = "OBIO IRQ"; 145178173Simp 146178173Simp if (rman_init(&sc->oba_irq_rman) != 0 || 147178173Simp rman_manage_region(&sc->oba_irq_rman, 0, NIRQS-1) != 0) 148178173Simp panic("obio_attach: failed to set up IRQ rman"); 149178173Simp 150178173Simp /* Hook up our interrupt handler. */ 151178173Simp if ((sc->sc_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 152178173Simp ADM5120_INTR, ADM5120_INTR, 1, 153178173Simp RF_SHAREABLE | RF_ACTIVE)) == NULL) { 154178173Simp device_printf(dev, "unable to allocate IRQ resource\n"); 155178173Simp return (ENXIO); 156178173Simp } 157178173Simp 158178173Simp if ((bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_MISC, obio_intr, NULL, 159178173Simp sc, &sc->sc_ih))) { 160178173Simp device_printf(dev, 161178173Simp "WARNING: unable to register interrupt handler\n"); 162178173Simp return (ENXIO); 163178173Simp } 164178173Simp 165178173Simp /* Hook up our FAST interrupt handler. */ 166178173Simp if ((sc->sc_fast_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 167178173Simp ADM5120_FAST_INTR, ADM5120_FAST_INTR, 1, 168178173Simp RF_SHAREABLE | RF_ACTIVE)) == NULL) { 169178173Simp device_printf(dev, "unable to allocate IRQ resource\n"); 170178173Simp return (ENXIO); 171178173Simp } 172178173Simp 173178173Simp if ((bus_setup_intr(dev, sc->sc_fast_irq, INTR_TYPE_MISC, obio_intr, 174178173Simp NULL, sc, &sc->sc_fast_ih))) { 175178173Simp device_printf(dev, 176178173Simp "WARNING: unable to register interrupt handler\n"); 177178173Simp return (ENXIO); 178178173Simp } 179178173Simp 180178173Simp /* disable all interrupts */ 181178173Simp REG_WRITE(ICU_ENABLE_REG, ICU_INT_MASK); 182178173Simp 183178173Simp bus_generic_probe(dev); 184178173Simp bus_enumerate_hinted_children(dev); 185178173Simp bus_generic_attach(dev); 186178173Simp 187178173Simp return (0); 188178173Simp} 189178173Simp 190178173Simpstatic struct resource * 191178173Simpobio_alloc_resource(device_t bus, device_t child, int type, int *rid, 192178173Simp u_long start, u_long end, u_long count, u_int flags) 193178173Simp{ 194178173Simp struct obio_softc *sc = device_get_softc(bus); 195178173Simp struct obio_ivar *ivar = device_get_ivars(child); 196178173Simp struct resource *rv; 197178173Simp struct resource_list_entry *rle; 198178173Simp struct rman *rm; 199178173Simp int isdefault, needactivate, passthrough; 200178173Simp 201178173Simp isdefault = (start == 0UL && end == ~0UL && count == 1); 202178173Simp needactivate = flags & RF_ACTIVE; 203178173Simp passthrough = (device_get_parent(child) != bus); 204178173Simp rle = NULL; 205178173Simp 206178173Simp if (passthrough) 207178173Simp return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, 208178173Simp rid, start, end, count, flags)); 209178173Simp 210178173Simp /* 211178173Simp * If this is an allocation of the "default" range for a given RID, 212178173Simp * and we know what the resources for this device are (ie. they aren't 213178173Simp * maintained by a child bus), then work out the start/end values. 214178173Simp */ 215178173Simp if (isdefault) { 216178173Simp rle = resource_list_find(&ivar->resources, type, *rid); 217178173Simp if (rle == NULL) 218178173Simp return (NULL); 219178173Simp if (rle->res != NULL) { 220178173Simp panic("%s: resource entry is busy", __func__); 221178173Simp } 222178173Simp start = rle->start; 223178173Simp end = rle->end; 224178173Simp count = rle->count; 225178173Simp } 226178173Simp 227178173Simp switch (type) { 228178173Simp case SYS_RES_IRQ: 229178173Simp rm = &sc->oba_irq_rman; 230178173Simp break; 231178173Simp case SYS_RES_MEMORY: 232178173Simp rm = &sc->oba_mem_rman; 233178173Simp break; 234178173Simp default: 235178173Simp printf("%s: unknown resource type %d\n", __func__, type); 236178173Simp return (0); 237178173Simp } 238178173Simp 239178173Simp rv = rman_reserve_resource(rm, start, end, count, flags, child); 240178173Simp if (rv == 0) { 241178173Simp printf("%s: could not reserve resource\n", __func__); 242178173Simp return (0); 243178173Simp } 244178173Simp 245178173Simp rman_set_rid(rv, *rid); 246178173Simp 247178173Simp if (needactivate) { 248178173Simp if (bus_activate_resource(child, type, *rid, rv)) { 249178173Simp printf("%s: could not activate resource\n", __func__); 250178173Simp rman_release_resource(rv); 251178173Simp return (0); 252178173Simp } 253178173Simp } 254178173Simp 255178173Simp return (rv); 256178173Simp} 257178173Simp 258178173Simpstatic int 259178173Simpobio_activate_resource(device_t bus, device_t child, int type, int rid, 260178173Simp struct resource *r) 261178173Simp{ 262178173Simp 263178173Simp /* 264178173Simp * If this is a memory resource, track the direct mapping 265178173Simp * in the uncached MIPS KSEG1 segment. 266178173Simp */ 267178173Simp if (type == SYS_RES_MEMORY) { 268178173Simp void *vaddr; 269178173Simp 270178173Simp vaddr = (void *)MIPS_PHYS_TO_KSEG1((intptr_t)rman_get_start(r)); 271178173Simp rman_set_virtual(r, vaddr); 272178173Simp rman_set_bustag(r, MIPS_BUS_SPACE_MEM); 273178173Simp rman_set_bushandle(r, (bus_space_handle_t)vaddr); 274178173Simp } 275178173Simp 276178173Simp return (rman_activate_resource(r)); 277178173Simp} 278178173Simp 279178173Simpstatic int 280178173Simpobio_deactivate_resource(device_t bus, device_t child, int type, int rid, 281178173Simp struct resource *r) 282178173Simp{ 283178173Simp 284178173Simp return (rman_deactivate_resource(r)); 285178173Simp} 286178173Simp 287178173Simpstatic int 288178173Simpobio_release_resource(device_t dev, device_t child, int type, 289178173Simp int rid, struct resource *r) 290178173Simp{ 291178173Simp struct resource_list *rl; 292178173Simp struct resource_list_entry *rle; 293178173Simp 294178173Simp rl = obio_get_resource_list(dev, child); 295178173Simp if (rl == NULL) 296178173Simp return (EINVAL); 297178173Simp rle = resource_list_find(rl, type, rid); 298178173Simp if (rle == NULL) 299178173Simp return (EINVAL); 300178173Simp rman_release_resource(r); 301178173Simp rle->res = NULL; 302178173Simp 303178173Simp return (0); 304178173Simp} 305178173Simp 306178173Simpstatic int 307178173Simpobio_setup_intr(device_t dev, device_t child, struct resource *ires, 308178173Simp int flags, driver_filter_t *filt, driver_intr_t *handler, 309178173Simp void *arg, void **cookiep) 310178173Simp{ 311178173Simp struct obio_softc *sc = device_get_softc(dev); 312178173Simp struct intr_event *event; 313178173Simp int irq, error, priority; 314178173Simp uint32_t irqmask; 315178173Simp 316178173Simp irq = rman_get_start(ires); 317178173Simp 318178173Simp if (irq >= NIRQS) 319178173Simp panic("%s: bad irq %d", __func__, irq); 320178173Simp 321178173Simp event = sc->sc_eventstab[irq]; 322178173Simp if (event == NULL) { 323178173Simp error = intr_event_create(&event, (void *)irq, 0, 324178173Simp (mask_fn)mips_mask_irq, (mask_fn)mips_unmask_irq, 325178173Simp NULL, NULL, "obio intr%d:", irq); 326178173Simp 327178173Simp sc->sc_eventstab[irq] = event; 328178173Simp } 329178173Simp else 330178173Simp panic("obio: Can't share IRQs"); 331178173Simp 332178173Simp intr_event_add_handler(event, device_get_nameunit(child), filt, 333178173Simp handler, arg, intr_priority(flags), flags, cookiep); 334178173Simp 335178173Simp irqmask = 1 << irq; 336178173Simp priority = irq_priorities[irq]; 337178173Simp 338178173Simp if (priority == INTR_FIQ) 339178173Simp REG_WRITE(ICU_MODE_REG, REG_READ(ICU_MODE_REG) | irqmask); 340178173Simp else 341178173Simp REG_WRITE(ICU_MODE_REG, REG_READ(ICU_MODE_REG) & ~irqmask); 342178173Simp 343178173Simp /* enable */ 344178173Simp REG_WRITE(ICU_ENABLE_REG, irqmask); 345178173Simp 346178173Simp return (0); 347178173Simp} 348178173Simp 349178173Simpstatic int 350178173Simpobio_teardown_intr(device_t dev, device_t child, struct resource *ires, 351178173Simp void *cookie) 352178173Simp{ 353178173Simp struct obio_softc *sc = device_get_softc(dev); 354178173Simp int irq, result; 355178173Simp uint32_t irqmask; 356178173Simp 357178173Simp irq = rman_get_start(ires); 358178173Simp if (irq >= NIRQS) 359178173Simp panic("%s: bad irq %d", __func__, irq); 360178173Simp 361178173Simp if (sc->sc_eventstab[irq] == NULL) 362178173Simp panic("Trying to teardown unoccupied IRQ"); 363178173Simp 364178173Simp irqmask = 1 << irq; /* only used as a mask from here on */ 365178173Simp 366178173Simp /* disable this irq in HW */ 367178173Simp REG_WRITE(ICU_DISABLE_REG, irqmask); 368178173Simp 369178173Simp result = intr_event_remove_handler(cookie); 370178173Simp if (!result) { 371178173Simp sc->sc_eventstab[irq] = NULL; 372178173Simp } 373178173Simp 374178173Simp return (result); 375178173Simp} 376178173Simp 377178173Simpstatic int 378178173Simpobio_intr(void *arg) 379178173Simp{ 380178173Simp struct obio_softc *sc = arg; 381178173Simp struct intr_event *event; 382178173Simp struct intr_handler *ih; 383178173Simp uint32_t irqstat; 384178173Simp int irq, thread = 0; 385178173Simp 386178173Simp irqstat = REG_READ(ICU_FIQ_STATUS_REG); 387178173Simp irqstat |= REG_READ(ICU_STATUS_REG); 388178173Simp 389178173Simp irq = 0; 390178173Simp while (irqstat != 0) { 391178173Simp if ((irqstat & 1) == 1) { 392178173Simp event = sc->sc_eventstab[irq]; 393178173Simp if (event && !TAILQ_EMPTY(&event->ie_handlers)) { 394178173Simp /* Execute fast handlers. */ 395178173Simp TAILQ_FOREACH(ih, &event->ie_handlers, 396178173Simp ih_next) { 397178173Simp if (ih->ih_filter == NULL) 398178173Simp thread = 1; 399178173Simp else 400178173Simp ih->ih_filter(ih->ih_argument); 401178173Simp } 402178173Simp } 403178173Simp 404178173Simp /* Schedule thread if needed. */ 405178173Simp if (thread) 406178173Simp intr_event_schedule_thread(event); 407178173Simp } 408178173Simp 409178173Simp irq++; 410178173Simp irqstat >>= 1; 411178173Simp } 412178173Simp 413178173Simp return (FILTER_HANDLED); 414178173Simp} 415178173Simp 416178173Simpstatic void 417178173Simpobio_hinted_child(device_t bus, const char *dname, int dunit) 418178173Simp{ 419178173Simp device_t child; 420178173Simp long maddr; 421178173Simp int msize; 422178173Simp int irq; 423178173Simp int result; 424178173Simp 425178173Simp child = BUS_ADD_CHILD(bus, 0, dname, dunit); 426178173Simp 427178173Simp /* 428178173Simp * Set hard-wired resources for hinted child using 429178173Simp * specific RIDs. 430178173Simp */ 431178173Simp resource_long_value(dname, dunit, "maddr", &maddr); 432178173Simp resource_int_value(dname, dunit, "msize", &msize); 433178173Simp 434178173Simp 435178173Simp result = bus_set_resource(child, SYS_RES_MEMORY, 0, 436178173Simp maddr, msize); 437178173Simp if (result != 0) 438178173Simp device_printf(bus, "warning: bus_set_resource() failed\n"); 439178173Simp 440178173Simp if (resource_int_value(dname, dunit, "irq", &irq) == 0) { 441178173Simp result = bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1); 442178173Simp if (result != 0) 443178173Simp device_printf(bus, 444178173Simp "warning: bus_set_resource() failed\n"); 445178173Simp } 446178173Simp} 447178173Simp 448178173Simpstatic device_t 449178173Simpobio_add_child(device_t bus, int order, const char *name, int unit) 450178173Simp{ 451178173Simp device_t child; 452178173Simp struct obio_ivar *ivar; 453178173Simp 454178173Simp ivar = malloc(sizeof(struct obio_ivar), M_DEVBUF, M_WAITOK | M_ZERO); 455178173Simp if (ivar == NULL) { 456178173Simp printf("Failed to allocate ivar\n"); 457178173Simp return (0); 458178173Simp } 459178173Simp resource_list_init(&ivar->resources); 460178173Simp 461178173Simp child = device_add_child_ordered(bus, order, name, unit); 462178173Simp if (child == NULL) { 463178173Simp printf("Can't add child %s%d ordered\n", name, unit); 464178173Simp return (0); 465178173Simp } 466178173Simp 467178173Simp device_set_ivars(child, ivar); 468178173Simp 469178173Simp return (child); 470178173Simp} 471178173Simp 472178173Simp/* 473178173Simp * Helper routine for bus_generic_rl_get_resource/bus_generic_rl_set_resource 474178173Simp * Provides pointer to resource_list for these routines 475178173Simp */ 476178173Simpstatic struct resource_list * 477178173Simpobio_get_resource_list(device_t dev, device_t child) 478178173Simp{ 479178173Simp struct obio_ivar *ivar; 480178173Simp 481178173Simp ivar = device_get_ivars(child); 482178173Simp return (&(ivar->resources)); 483178173Simp} 484178173Simp 485178173Simpstatic device_method_t obio_methods[] = { 486178173Simp DEVMETHOD(bus_activate_resource, obio_activate_resource), 487178173Simp DEVMETHOD(bus_add_child, obio_add_child), 488178173Simp DEVMETHOD(bus_alloc_resource, obio_alloc_resource), 489178173Simp DEVMETHOD(bus_deactivate_resource, obio_deactivate_resource), 490178173Simp DEVMETHOD(bus_get_resource_list, obio_get_resource_list), 491178173Simp DEVMETHOD(bus_hinted_child, obio_hinted_child), 492178173Simp DEVMETHOD(bus_release_resource, obio_release_resource), 493178173Simp DEVMETHOD(bus_setup_intr, obio_setup_intr), 494178173Simp DEVMETHOD(bus_teardown_intr, obio_teardown_intr), 495178173Simp DEVMETHOD(device_attach, obio_attach), 496178173Simp DEVMETHOD(device_probe, obio_probe), 497178173Simp DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 498178173Simp DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource), 499178173Simp 500178173Simp {0, 0}, 501178173Simp}; 502178173Simp 503178173Simpstatic driver_t obio_driver = { 504178173Simp "obio", 505178173Simp obio_methods, 506178173Simp sizeof(struct obio_softc), 507178173Simp}; 508178173Simpstatic devclass_t obio_devclass; 509178173Simp 510178173SimpDRIVER_MODULE(obio, nexus, obio_driver, obio_devclass, 0, 0); 511