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$"); 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/idt/idtreg.h> 48#include <mips/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, u_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 80obio_mask_irq(void *arg) 81{ 82 unsigned int irq = (unsigned int)arg; 83 int ip_bit, mask, mask_register; 84 85 /* mask IRQ */ 86 mask_register = ICU_IRQ_MASK_REG(irq); 87 ip_bit = ICU_IP_BIT(irq); 88 89 mask = ICU_REG_READ(mask_register); 90 ICU_REG_WRITE(mask_register, mask | ip_bit); 91} 92 93static void 94obio_unmask_irq(void *arg) 95{ 96 unsigned int irq = (unsigned int)arg; 97 int ip_bit, mask, mask_register; 98 99 /* unmask IRQ */ 100 mask_register = ICU_IRQ_MASK_REG(irq); 101 ip_bit = ICU_IP_BIT(irq); 102 103 mask = ICU_REG_READ(mask_register); 104 ICU_REG_WRITE(mask_register, mask & ~ip_bit); 105} 106 107static int 108obio_probe(device_t dev) 109{ 110 111 return (0); 112} 113 114static int 115obio_attach(device_t dev) 116{ 117 struct obio_softc *sc = device_get_softc(dev); 118 int rid, irq; 119 120 sc->oba_mem_rman.rm_type = RMAN_ARRAY; 121 sc->oba_mem_rman.rm_descr = "OBIO memeory"; 122 if (rman_init(&sc->oba_mem_rman) != 0 || 123 rman_manage_region(&sc->oba_mem_rman, OBIO_MEM_START, 124 OBIO_MEM_START + OBIO_MEM_SIZE) != 0) 125 panic("obio_attach: failed to set up I/O rman"); 126 127 sc->oba_irq_rman.rm_type = RMAN_ARRAY; 128 sc->oba_irq_rman.rm_descr = "OBIO IRQ"; 129 130 if (rman_init(&sc->oba_irq_rman) != 0 || 131 rman_manage_region(&sc->oba_irq_rman, IRQ_BASE, IRQ_END) != 0) 132 panic("obio_attach: failed to set up IRQ rman"); 133 134 /* Hook up our interrupt handlers. We should handle IRQ0..IRQ4*/ 135 for(irq = 0; irq < 5; irq++) { 136 if ((sc->sc_irq[irq] = bus_alloc_resource(dev, SYS_RES_IRQ, 137 &rid, irq, irq, 1, 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_irq[irq], INTR_TYPE_MISC, 143 obio_intr, NULL, sc, &sc->sc_ih[irq]))) { 144 device_printf(dev, 145 "WARNING: unable to register interrupt handler\n"); 146 return (ENXIO); 147 } 148 } 149 150 bus_generic_probe(dev); 151 bus_enumerate_hinted_children(dev); 152 bus_generic_attach(dev); 153 154 return (0); 155} 156 157static struct resource * 158obio_alloc_resource(device_t bus, device_t child, int type, int *rid, 159 u_long start, u_long end, u_long count, u_int flags) 160{ 161 struct obio_softc *sc = device_get_softc(bus); 162 struct obio_ivar *ivar = device_get_ivars(child); 163 struct resource *rv; 164 struct resource_list_entry *rle; 165 struct rman *rm; 166 int isdefault, needactivate, passthrough; 167 168 isdefault = (start == 0UL && end == ~0UL); 169 needactivate = flags & RF_ACTIVE; 170 passthrough = (device_get_parent(child) != bus); 171 rle = NULL; 172 173 if (passthrough) 174 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, 175 rid, start, end, count, flags)); 176 177 /* 178 * If this is an allocation of the "default" range for a given RID, 179 * and we know what the resources for this device are (ie. they aren't 180 * maintained by a child bus), then work out the start/end values. 181 */ 182 if (isdefault) { 183 rle = resource_list_find(&ivar->resources, type, *rid); 184 if (rle == NULL) 185 return (NULL); 186 if (rle->res != NULL) { 187 panic("%s: resource entry is busy", __func__); 188 } 189 start = rle->start; 190 end = rle->end; 191 count = rle->count; 192 } 193 194 switch (type) { 195 case SYS_RES_IRQ: 196 rm = &sc->oba_irq_rman; 197 break; 198 case SYS_RES_MEMORY: 199 rm = &sc->oba_mem_rman; 200 break; 201 default: 202 printf("%s: unknown resource type %d\n", __func__, type); 203 return (0); 204 } 205 206 rv = rman_reserve_resource(rm, start, end, count, flags, child); 207 if (rv == 0) { 208 printf("%s: could not reserve resource\n", __func__); 209 return (0); 210 } 211 212 rman_set_rid(rv, *rid); 213 214 if (needactivate) { 215 if (bus_activate_resource(child, type, *rid, rv)) { 216 printf("%s: could not activate resource\n", __func__); 217 rman_release_resource(rv); 218 return (0); 219 } 220 } 221 222 return (rv); 223} 224 225static int 226obio_activate_resource(device_t bus, device_t child, int type, int rid, 227 struct resource *r) 228{ 229 230 /* XXX: should we mask/unmask IRQ here? */ 231 return (BUS_ACTIVATE_RESOURCE(device_get_parent(bus), child, 232 type, rid, r)); 233} 234 235static int 236obio_deactivate_resource(device_t bus, device_t child, int type, int rid, 237 struct resource *r) 238{ 239 240 /* XXX: should we mask/unmask IRQ here? */ 241 return (BUS_DEACTIVATE_RESOURCE(device_get_parent(bus), child, 242 type, rid, r)); 243} 244 245static int 246obio_release_resource(device_t dev, device_t child, int type, 247 int rid, struct resource *r) 248{ 249 struct resource_list *rl; 250 struct resource_list_entry *rle; 251 252 rl = obio_get_resource_list(dev, child); 253 if (rl == NULL) 254 return (EINVAL); 255 rle = resource_list_find(rl, type, rid); 256 if (rle == NULL) 257 return (EINVAL); 258 rman_release_resource(r); 259 rle->res = NULL; 260 261 return (0); 262} 263 264static int 265obio_setup_intr(device_t dev, device_t child, struct resource *ires, 266 int flags, driver_filter_t *filt, driver_intr_t *handler, 267 void *arg, void **cookiep) 268{ 269 struct obio_softc *sc = device_get_softc(dev); 270 struct intr_event *event; 271 int irq, ip_bit, error, mask, mask_register; 272 273 irq = rman_get_start(ires); 274 275 if (irq >= NIRQS) 276 panic("%s: bad irq %d", __func__, irq); 277 278 event = sc->sc_eventstab[irq]; 279 if (event == NULL) { 280 error = intr_event_create(&event, (void *)irq, 0, irq, 281 obio_mask_irq, obio_unmask_irq, 282 NULL, NULL, 283 "obio intr%d:", irq); 284 285 sc->sc_eventstab[irq] = event; 286 } 287 288 intr_event_add_handler(event, device_get_nameunit(child), filt, 289 handler, arg, intr_priority(flags), flags, cookiep); 290 291 /* unmask IRQ */ 292 mask_register = ICU_IRQ_MASK_REG(irq); 293 ip_bit = ICU_IP_BIT(irq); 294 295 mask = ICU_REG_READ(mask_register); 296 ICU_REG_WRITE(mask_register, mask & ~ip_bit); 297 298 return (0); 299} 300 301static int 302obio_teardown_intr(device_t dev, device_t child, struct resource *ires, 303 void *cookie) 304{ 305 struct obio_softc *sc = device_get_softc(dev); 306 int irq, result; 307 uint32_t mask_register, mask, ip_bit; 308 309 irq = rman_get_start(ires); 310 if (irq >= NIRQS) 311 panic("%s: bad irq %d", __func__, irq); 312 313 if (sc->sc_eventstab[irq] == NULL) 314 panic("Trying to teardown unoccupied IRQ"); 315 316 /* mask IRQ */ 317 mask_register = ICU_IRQ_MASK_REG(irq); 318 ip_bit = ICU_IP_BIT(irq); 319 320 mask = ICU_REG_READ(mask_register); 321 ICU_REG_WRITE(mask_register, mask | ip_bit); 322 323 result = intr_event_remove_handler(cookie); 324 if (!result) 325 sc->sc_eventstab[irq] = NULL; 326 327 return (result); 328} 329 330static int 331obio_intr(void *arg) 332{ 333 struct obio_softc *sc = arg; 334 struct intr_event *event; 335 uint32_t irqstat, ipend, imask, xpend; 336 int irq, thread, group, i; 337 338 irqstat = 0; 339 irq = 0; 340 for (group = 2; group <= 6; group++) { 341 ipend = ICU_REG_READ(ICU_GROUP_IPEND_REG(group)); 342 imask = ICU_REG_READ(ICU_GROUP_MASK_REG(group)); 343 xpend = ipend; 344 ipend &= ~imask; 345 346 while ((i = fls(xpend)) != 0) { 347 xpend &= ~(1 << (i - 1)); 348 irq = IP_IRQ(group, i - 1); 349 } 350 351 while ((i = fls(ipend)) != 0) { 352 ipend &= ~(1 << (i - 1)); 353 irq = IP_IRQ(group, i - 1); 354 event = sc->sc_eventstab[irq]; 355 thread = 0; 356 if (!event || TAILQ_EMPTY(&event->ie_handlers)) { 357 /* TODO: Log stray IRQs */ 358 continue; 359 } 360 361 /* TODO: frame instead of NULL? */ 362 intr_event_handle(event, NULL); 363 /* XXX: Log stray IRQs */ 364 } 365 } 366#if 0 367 ipend = ICU_REG_READ(ICU_IPEND2); 368 printf("ipend2 = %08x!\n", ipend); 369 370 ipend = ICU_REG_READ(ICU_IPEND3); 371 printf("ipend3 = %08x!\n", ipend); 372 373 ipend = ICU_REG_READ(ICU_IPEND4); 374 printf("ipend4 = %08x!\n", ipend); 375 ipend = ICU_REG_READ(ICU_IPEND5); 376 printf("ipend5 = %08x!\n", ipend); 377 378 ipend = ICU_REG_READ(ICU_IPEND6); 379 printf("ipend6 = %08x!\n", ipend); 380#endif 381 while (irqstat != 0) { 382 if ((irqstat & 1) == 1) { 383 } 384 385 irq++; 386 irqstat >>= 1; 387 } 388 389 return (FILTER_HANDLED); 390} 391 392static void 393obio_hinted_child(device_t bus, const char *dname, int dunit) 394{ 395 device_t child; 396 long maddr; 397 int msize; 398 int irq; 399 int result; 400 401 child = BUS_ADD_CHILD(bus, 0, dname, dunit); 402 403 /* 404 * Set hard-wired resources for hinted child using 405 * specific RIDs. 406 */ 407 resource_long_value(dname, dunit, "maddr", &maddr); 408 resource_int_value(dname, dunit, "msize", &msize); 409 410 411 result = bus_set_resource(child, SYS_RES_MEMORY, 0, 412 maddr, msize); 413 if (result != 0) 414 device_printf(bus, "warning: bus_set_resource() failed\n"); 415 416 if (resource_int_value(dname, dunit, "irq", &irq) == 0) { 417 result = bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1); 418 if (result != 0) 419 device_printf(bus, 420 "warning: bus_set_resource() failed\n"); 421 } 422} 423 424static device_t 425obio_add_child(device_t bus, u_int order, const char *name, int unit) 426{ 427 device_t child; 428 struct obio_ivar *ivar; 429 430 ivar = malloc(sizeof(struct obio_ivar), M_DEVBUF, M_WAITOK | M_ZERO); 431 if (ivar == NULL) { 432 printf("Failed to allocate ivar\n"); 433 return (0); 434 } 435 resource_list_init(&ivar->resources); 436 437 child = device_add_child_ordered(bus, order, name, unit); 438 if (child == NULL) { 439 printf("Can't add child %s%d ordered\n", name, unit); 440 return (0); 441 } 442 443 device_set_ivars(child, ivar); 444 445 return (child); 446} 447 448/* 449 * Helper routine for bus_generic_rl_get_resource/bus_generic_rl_set_resource 450 * Provides pointer to resource_list for these routines 451 */ 452static struct resource_list * 453obio_get_resource_list(device_t dev, device_t child) 454{ 455 struct obio_ivar *ivar; 456 457 ivar = device_get_ivars(child); 458 return (&(ivar->resources)); 459} 460 461static device_method_t obio_methods[] = { 462 DEVMETHOD(bus_activate_resource, obio_activate_resource), 463 DEVMETHOD(bus_add_child, obio_add_child), 464 DEVMETHOD(bus_alloc_resource, obio_alloc_resource), 465 DEVMETHOD(bus_deactivate_resource, obio_deactivate_resource), 466 DEVMETHOD(bus_get_resource_list, obio_get_resource_list), 467 DEVMETHOD(bus_hinted_child, obio_hinted_child), 468 DEVMETHOD(bus_release_resource, obio_release_resource), 469 DEVMETHOD(bus_setup_intr, obio_setup_intr), 470 DEVMETHOD(bus_teardown_intr, obio_teardown_intr), 471 DEVMETHOD(device_attach, obio_attach), 472 DEVMETHOD(device_probe, obio_probe), 473 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 474 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource), 475 476 {0, 0}, 477}; 478 479static driver_t obio_driver = { 480 "obio", 481 obio_methods, 482 sizeof(struct obio_softc), 483}; 484static devclass_t obio_devclass; 485 486DRIVER_MODULE(obio, nexus, obio_driver, obio_devclass, 0, 0); 487