obio.c revision 178173
1/*- 2 * Copyright (c) 2007, Oleksandr Tymoshenko <gonzo@freebsd.org> 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 3. All advertising materials mentioning features or use of this software 13 * must display the following acknowledgement: 14 * This product includes software developed for the NetBSD Project by 15 * Wasabi Systems, Inc. 16 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 17 * or promote products derived from this software without specific prior 18 * written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33#include <sys/cdefs.h> 34__FBSDID("$FreeBSD: head/sys/mips/mips32/idt/obio.c 178173 2008-04-13 07:44:55Z imp $"); 35 36#include <sys/param.h> 37#include <sys/systm.h> 38#include <sys/bus.h> 39#include <sys/interrupt.h> 40#include <sys/kernel.h> 41#include <sys/module.h> 42#include <sys/rman.h> 43#include <sys/malloc.h> 44 45#include <machine/bus.h> 46 47#include <mips/mips32/idt/idtreg.h> 48#include <mips/mips32/idt/obiovar.h> 49 50#define ICU_REG_READ(o) \ 51 *((volatile uint32_t *)MIPS_PHYS_TO_KSEG1(IDT_BASE_ICU + (o))) 52#define ICU_REG_WRITE(o,v) (ICU_REG_READ(o)) = (v) 53 54#define GPIO_REG_READ(o) \ 55 *((volatile uint32_t *)MIPS_PHYS_TO_KSEG1(IDT_BASE_GPIO + (o))) 56#define GPIO_REG_WRITE(o,v) (GPIO_REG_READ(o)) = (v) 57 58static int obio_activate_resource(device_t, device_t, int, int, 59 struct resource *); 60static device_t obio_add_child(device_t, int, const char *, int); 61static struct resource * 62 obio_alloc_resource(device_t, device_t, int, int *, u_long, 63 u_long, u_long, u_int); 64static int obio_attach(device_t); 65static int obio_deactivate_resource(device_t, device_t, int, int, 66 struct resource *); 67static struct resource_list * 68 obio_get_resource_list(device_t, device_t); 69static void obio_hinted_child(device_t, const char *, int); 70static int obio_intr(void *); 71static int obio_probe(device_t); 72static int obio_release_resource(device_t, device_t, int, int, 73 struct resource *); 74static int obio_setup_intr(device_t, device_t, struct resource *, int, 75 driver_filter_t *, driver_intr_t *, void *, void **); 76static int obio_teardown_intr(device_t, device_t, struct resource *, 77 void *); 78 79static void obio_mask_irq(unsigned int irq) 80{ 81 int ip_bit, mask, mask_register; 82 83 /* mask IRQ */ 84 mask_register = ICU_IRQ_MASK_REG(irq); 85 ip_bit = ICU_IP_BIT(irq); 86 87 mask = ICU_REG_READ(mask_register); 88 ICU_REG_WRITE(mask_register, mask | ip_bit); 89} 90 91static void obio_unmask_irq(unsigned int irq) 92{ 93 int ip_bit, mask, mask_register; 94 95 /* unmask IRQ */ 96 mask_register = ICU_IRQ_MASK_REG(irq); 97 ip_bit = ICU_IP_BIT(irq); 98 99 mask = ICU_REG_READ(mask_register); 100 ICU_REG_WRITE(mask_register, mask & ~ip_bit); 101} 102 103static int 104obio_probe(device_t dev) 105{ 106 107 return (0); 108} 109 110static int 111obio_attach(device_t dev) 112{ 113 struct obio_softc *sc = device_get_softc(dev); 114 int rid, irq; 115 116 sc->oba_mem_rman.rm_type = RMAN_ARRAY; 117 sc->oba_mem_rman.rm_descr = "OBIO memeory"; 118 if (rman_init(&sc->oba_mem_rman) != 0 || 119 rman_manage_region(&sc->oba_mem_rman, OBIO_MEM_START, 120 OBIO_MEM_START + OBIO_MEM_SIZE) != 0) 121 panic("obio_attach: failed to set up I/O rman"); 122 123 sc->oba_irq_rman.rm_type = RMAN_ARRAY; 124 sc->oba_irq_rman.rm_descr = "OBIO IRQ"; 125 126 if (rman_init(&sc->oba_irq_rman) != 0 || 127 rman_manage_region(&sc->oba_irq_rman, IRQ_BASE, IRQ_END) != 0) 128 panic("obio_attach: failed to set up IRQ rman"); 129 130 /* Hook up our interrupt handlers. We should handle IRQ0..IRQ4*/ 131 for(irq = 0; irq < 5; irq++) { 132 if ((sc->sc_irq[irq] = bus_alloc_resource(dev, SYS_RES_IRQ, 133 &rid, irq, irq, 1, RF_SHAREABLE | RF_ACTIVE)) == NULL) { 134 device_printf(dev, "unable to allocate IRQ resource\n"); 135 return (ENXIO); 136 } 137 138 if ((bus_setup_intr(dev, sc->sc_irq[irq], INTR_TYPE_MISC, 139 obio_intr, NULL, sc, &sc->sc_ih[irq]))) { 140 device_printf(dev, 141 "WARNING: unable to register interrupt handler\n"); 142 return (ENXIO); 143 } 144 } 145 146 bus_generic_probe(dev); 147 bus_enumerate_hinted_children(dev); 148 bus_generic_attach(dev); 149 150 return (0); 151} 152 153static struct resource * 154obio_alloc_resource(device_t bus, device_t child, int type, int *rid, 155 u_long start, u_long end, u_long count, u_int flags) 156{ 157 struct obio_softc *sc = device_get_softc(bus); 158 struct obio_ivar *ivar = device_get_ivars(child); 159 struct resource *rv; 160 struct resource_list_entry *rle; 161 struct rman *rm; 162 int isdefault, needactivate, passthrough; 163 164 isdefault = (start == 0UL && end == ~0UL); 165 needactivate = flags & RF_ACTIVE; 166 passthrough = (device_get_parent(child) != bus); 167 rle = NULL; 168 169 if (passthrough) 170 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, 171 rid, start, end, count, flags)); 172 173 /* 174 * If this is an allocation of the "default" range for a given RID, 175 * and we know what the resources for this device are (ie. they aren't 176 * maintained by a child bus), then work out the start/end values. 177 */ 178 if (isdefault) { 179 rle = resource_list_find(&ivar->resources, type, *rid); 180 if (rle == NULL) 181 return (NULL); 182 if (rle->res != NULL) { 183 panic("%s: resource entry is busy", __func__); 184 } 185 start = rle->start; 186 end = rle->end; 187 count = rle->count; 188 } 189 190 switch (type) { 191 case SYS_RES_IRQ: 192 rm = &sc->oba_irq_rman; 193 break; 194 case SYS_RES_MEMORY: 195 rm = &sc->oba_mem_rman; 196 break; 197 default: 198 printf("%s: unknown resource type %d\n", __func__, type); 199 return (0); 200 } 201 202 rv = rman_reserve_resource(rm, start, end, count, flags, child); 203 if (rv == 0) { 204 printf("%s: could not reserve resource\n", __func__); 205 return (0); 206 } 207 208 rman_set_rid(rv, *rid); 209 210 if (needactivate) { 211 if (bus_activate_resource(child, type, *rid, rv)) { 212 printf("%s: could not activate resource\n", __func__); 213 rman_release_resource(rv); 214 return (0); 215 } 216 } 217 218 return (rv); 219} 220 221static int 222obio_activate_resource(device_t bus, device_t child, int type, int rid, 223 struct resource *r) 224{ 225 226 /* XXX: should we mask/unmask IRQ here? */ 227 return (BUS_ACTIVATE_RESOURCE(device_get_parent(bus), child, 228 type, rid, r)); 229} 230 231static int 232obio_deactivate_resource(device_t bus, device_t child, int type, int rid, 233 struct resource *r) 234{ 235 236 /* XXX: should we mask/unmask IRQ here? */ 237 return (BUS_DEACTIVATE_RESOURCE(device_get_parent(bus), child, 238 type, rid, r)); 239} 240 241static int 242obio_release_resource(device_t dev, device_t child, int type, 243 int rid, struct resource *r) 244{ 245 struct resource_list *rl; 246 struct resource_list_entry *rle; 247 248 rl = obio_get_resource_list(dev, child); 249 if (rl == NULL) 250 return (EINVAL); 251 rle = resource_list_find(rl, type, rid); 252 if (rle == NULL) 253 return (EINVAL); 254 rman_release_resource(r); 255 rle->res = NULL; 256 257 return (0); 258} 259 260static int 261obio_setup_intr(device_t dev, device_t child, struct resource *ires, 262 int flags, driver_filter_t *filt, driver_intr_t *handler, 263 void *arg, void **cookiep) 264{ 265 struct obio_softc *sc = device_get_softc(dev); 266 struct intr_event *event; 267 int irq, ip_bit, error, mask, mask_register; 268 269 irq = rman_get_start(ires); 270 271 if (irq >= NIRQS) 272 panic("%s: bad irq %d", __func__, irq); 273 274 event = sc->sc_eventstab[irq]; 275 if (event == NULL) { 276 error = intr_event_create(&event, (void *)irq, 0, 277 (mask_fn)obio_mask_irq, (mask_fn)obio_unmask_irq, 278 NULL, NULL, 279 "obio intr%d:", irq); 280 281 sc->sc_eventstab[irq] = event; 282 } 283 284 intr_event_add_handler(event, device_get_nameunit(child), filt, 285 handler, arg, intr_priority(flags), flags, cookiep); 286 287 /* unmask IRQ */ 288 mask_register = ICU_IRQ_MASK_REG(irq); 289 ip_bit = ICU_IP_BIT(irq); 290 291 mask = ICU_REG_READ(mask_register); 292 ICU_REG_WRITE(mask_register, mask & ~ip_bit); 293 294 return (0); 295} 296 297static int 298obio_teardown_intr(device_t dev, device_t child, struct resource *ires, 299 void *cookie) 300{ 301 struct obio_softc *sc = device_get_softc(dev); 302 int irq, result; 303 uint32_t mask_register, mask, ip_bit; 304 305 irq = rman_get_start(ires); 306 if (irq >= NIRQS) 307 panic("%s: bad irq %d", __func__, irq); 308 309 if (sc->sc_eventstab[irq] == NULL) 310 panic("Trying to teardown unoccupied IRQ"); 311 312 /* mask IRQ */ 313 mask_register = ICU_IRQ_MASK_REG(irq); 314 ip_bit = ICU_IP_BIT(irq); 315 316 mask = ICU_REG_READ(mask_register); 317 ICU_REG_WRITE(mask_register, mask | ip_bit); 318 319 result = intr_event_remove_handler(cookie); 320 if (!result) 321 sc->sc_eventstab[irq] = NULL; 322 323 return (result); 324} 325 326static int 327obio_intr(void *arg) 328{ 329 struct obio_softc *sc = arg; 330 struct intr_event *event; 331 struct intr_handler *ih; 332 uint32_t irqstat, ipend, imask, xpend; 333 int irq, thread, group, i, ret; 334 335 irqstat = 0; 336 irq = 0; 337 for (group = 2; group <= 6; group++) { 338 ipend = ICU_REG_READ(ICU_GROUP_IPEND_REG(group)); 339 imask = ICU_REG_READ(ICU_GROUP_MASK_REG(group)); 340 xpend = ipend; 341 ipend &= ~imask; 342 343 while ((i = fls(xpend)) != 0) { 344 xpend &= ~(1 << (i - 1)); 345 irq = IP_IRQ(group, i - 1); 346 } 347 348 while ((i = fls(ipend)) != 0) { 349 ipend &= ~(1 << (i - 1)); 350 irq = IP_IRQ(group, i - 1); 351 event = sc->sc_eventstab[irq]; 352 thread = 0; 353#ifndef INTR_FILTER 354 obio_mask_irq(irq); 355#endif 356 if (!event || TAILQ_EMPTY(&event->ie_handlers)) { 357#ifdef INTR_FILTER 358 obio_unmask_irq(irq); 359#endif 360 continue; 361 } 362 363#ifdef INTR_FILTER 364 /* TODO: frame instead of NULL? */ 365 intr_event_handle(event, NULL); 366 /* XXX: Log stray IRQs */ 367#else 368 /* Execute fast handlers. */ 369 TAILQ_FOREACH(ih, &event->ie_handlers, 370 ih_next) { 371 if (ih->ih_filter == NULL) 372 thread = 1; 373 else 374 ret = ih->ih_filter(ih->ih_argument); 375 /* 376 * Wrapper handler special case: see 377 * intr_execute_handlers() in 378 * i386/intr_machdep.c 379 */ 380 if (!thread) { 381 if (ret == FILTER_SCHEDULE_THREAD) 382 thread = 1; 383 } 384 } 385 386 /* Schedule thread if needed. */ 387 if (thread) 388 intr_event_schedule_thread(event); 389 else 390 obio_unmask_irq(irq); 391 } 392 } 393#endif 394#if 0 395 ipend = ICU_REG_READ(ICU_IPEND2); 396 printf("ipend2 = %08x!\n", ipend); 397 398 ipend = ICU_REG_READ(ICU_IPEND3); 399 printf("ipend3 = %08x!\n", ipend); 400 401 ipend = ICU_REG_READ(ICU_IPEND4); 402 printf("ipend4 = %08x!\n", ipend); 403 ipend = ICU_REG_READ(ICU_IPEND5); 404 printf("ipend5 = %08x!\n", ipend); 405 406 ipend = ICU_REG_READ(ICU_IPEND6); 407 printf("ipend6 = %08x!\n", ipend); 408#endif 409 while (irqstat != 0) { 410 if ((irqstat & 1) == 1) { 411 } 412 413 irq++; 414 irqstat >>= 1; 415 } 416 417 return (FILTER_HANDLED); 418} 419 420static void 421obio_hinted_child(device_t bus, const char *dname, int dunit) 422{ 423 device_t child; 424 long maddr; 425 int msize; 426 int irq; 427 int result; 428 429 child = BUS_ADD_CHILD(bus, 0, dname, dunit); 430 431 /* 432 * Set hard-wired resources for hinted child using 433 * specific RIDs. 434 */ 435 resource_long_value(dname, dunit, "maddr", &maddr); 436 resource_int_value(dname, dunit, "msize", &msize); 437 438 439 result = bus_set_resource(child, SYS_RES_MEMORY, 0, 440 maddr, msize); 441 if (result != 0) 442 device_printf(bus, "warning: bus_set_resource() failed\n"); 443 444 if (resource_int_value(dname, dunit, "irq", &irq) == 0) { 445 result = bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1); 446 if (result != 0) 447 device_printf(bus, 448 "warning: bus_set_resource() failed\n"); 449 } 450} 451 452static device_t 453obio_add_child(device_t bus, int order, const char *name, int unit) 454{ 455 device_t child; 456 struct obio_ivar *ivar; 457 458 ivar = malloc(sizeof(struct obio_ivar), M_DEVBUF, M_WAITOK | M_ZERO); 459 if (ivar == NULL) { 460 printf("Failed to allocate ivar\n"); 461 return (0); 462 } 463 resource_list_init(&ivar->resources); 464 465 child = device_add_child_ordered(bus, order, name, unit); 466 if (child == NULL) { 467 printf("Can't add child %s%d ordered\n", name, unit); 468 return (0); 469 } 470 471 device_set_ivars(child, ivar); 472 473 return (child); 474} 475 476/* 477 * Helper routine for bus_generic_rl_get_resource/bus_generic_rl_set_resource 478 * Provides pointer to resource_list for these routines 479 */ 480static struct resource_list * 481obio_get_resource_list(device_t dev, device_t child) 482{ 483 struct obio_ivar *ivar; 484 485 ivar = device_get_ivars(child); 486 return (&(ivar->resources)); 487} 488 489static device_method_t obio_methods[] = { 490 DEVMETHOD(bus_activate_resource, obio_activate_resource), 491 DEVMETHOD(bus_add_child, obio_add_child), 492 DEVMETHOD(bus_alloc_resource, obio_alloc_resource), 493 DEVMETHOD(bus_deactivate_resource, obio_deactivate_resource), 494 DEVMETHOD(bus_get_resource_list, obio_get_resource_list), 495 DEVMETHOD(bus_hinted_child, obio_hinted_child), 496 DEVMETHOD(bus_release_resource, obio_release_resource), 497 DEVMETHOD(bus_setup_intr, obio_setup_intr), 498 DEVMETHOD(bus_teardown_intr, obio_teardown_intr), 499 DEVMETHOD(device_attach, obio_attach), 500 DEVMETHOD(device_probe, obio_probe), 501 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 502 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource), 503 504 {0, 0}, 505}; 506 507static driver_t obio_driver = { 508 "obio", 509 obio_methods, 510 sizeof(struct obio_softc), 511}; 512static devclass_t obio_devclass; 513 514DRIVER_MODULE(obio, nexus, obio_driver, obio_devclass, 0, 0); 515