1 /* $NetBSD: obio.c,v 1.11 2003/07/15 00:25:05 lukem Exp $ */
4 * Copyright (c) 2001, 2002, 2003 Wasabi Systems, Inc.
7 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/interrupt.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
41 #include <sys/malloc.h>
43 #include <machine/bus.h>
45 #include <mips/rt305x/rt305xreg.h>
46 #include <mips/rt305x/obiovar.h>
47 #include <mips/rt305x/rt305x_icvar.h>
49 /* MIPS HW interrupts of IRQ/FIQ respectively */
51 #define RT305X_FAST_INTR 1
53 /* Interrupt levels */
58 int irq_priorities[NIRQS] = {
59 INTR_IRQ, /* SYSCTL */
60 INTR_FIQ, /* TIMER0 */
61 INTR_FIQ, /* WDTIMER */
62 INTR_IRQ, /* Illegal Access */
68 INTR_IRQ, /* Perfomance Counter */
70 INTR_IRQ, /* unknown */
71 INTR_IRQ, /* UARTLITE */
72 INTR_IRQ, /* unknown */
73 INTR_IRQ, /* unknown */
74 INTR_IRQ, /* unknown */
75 INTR_IRQ, /* unknown */
76 INTR_IRQ, /* EtherNet Switch */
78 INTR_IRQ, /* unknown */
79 INTR_IRQ, /* unknown */
80 INTR_IRQ, /* unknown */
81 INTR_IRQ, /* unknown */
82 INTR_IRQ, /* unknown */
83 INTR_IRQ, /* unknown */
84 INTR_IRQ, /* unknown */
85 INTR_IRQ, /* unknown */
86 INTR_IRQ, /* unknown */
87 INTR_IRQ, /* unknown */
88 INTR_IRQ, /* unknown */
89 INTR_IRQ, /* unknown */
90 INTR_IRQ, /* unknown */
94 #define REG_READ(o) *((volatile uint32_t *)MIPS_PHYS_TO_KSEG1(INTCTL_BASE + (o)))
95 #define REG_WRITE(o,v) (REG_READ(o)) = (v)
97 static int obio_activate_resource(device_t, device_t, int, int,
99 static device_t obio_add_child(device_t, u_int, const char *, int);
100 static struct resource *
101 obio_alloc_resource(device_t, device_t, int, int *, u_long,
102 u_long, u_long, u_int);
103 static int obio_attach(device_t);
104 static int obio_deactivate_resource(device_t, device_t, int, int,
106 static struct resource_list *
107 obio_get_resource_list(device_t, device_t);
108 static void obio_add_res_child(device_t, const char *, int, long, int, int);
109 static void obio_hinted_child(device_t, const char *, int);
110 static int obio_intr(void *);
111 static int obio_probe(device_t);
112 static int obio_release_resource(device_t, device_t, int, int,
114 static int obio_setup_intr(device_t, device_t, struct resource *, int,
115 driver_filter_t *, driver_intr_t *, void *, void **);
116 static int obio_teardown_intr(device_t, device_t, struct resource *,
120 obio_mask_irq(void *source)
129 rt305x_ic_set(IC_INT_DIS, irqmask);
133 obio_unmask_irq(void *source)
142 rt305x_ic_set(IC_INT_ENA, irqmask);
148 obio_probe(device_t dev)
151 return (BUS_PROBE_NOWILDCARD);
155 obio_attach(device_t dev)
157 struct obio_softc *sc = device_get_softc(dev);
160 sc->oba_mem_rman.rm_type = RMAN_ARRAY;
161 sc->oba_mem_rman.rm_descr = "OBIO memory";
162 if (rman_init(&sc->oba_mem_rman) != 0 ||
163 rman_manage_region(&sc->oba_mem_rman, OBIO_MEM_START,
165 panic("obio_attach: failed to set up I/O rman");
167 sc->oba_irq_rman.rm_type = RMAN_ARRAY;
168 sc->oba_irq_rman.rm_descr = "OBIO IRQ";
169 if (rman_init(&sc->oba_irq_rman) != 0 ||
170 rman_manage_region(&sc->oba_irq_rman, 0, NIRQS-1) != 0)
171 panic("obio_attach: failed to set up IRQ rman");
173 /* Hook up our interrupt handler. */
174 if ((sc->sc_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
175 RT305X_INTR, RT305X_INTR, 1,
176 RF_SHAREABLE | RF_ACTIVE)) == NULL) {
177 device_printf(dev, "unable to allocate IRQ resource\n");
181 if ((bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_MISC, obio_intr, NULL,
184 "WARNING: unable to register interrupt handler\n");
188 /* Hook up our FAST interrupt handler. */
189 if ((sc->sc_fast_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
190 RT305X_FAST_INTR, RT305X_FAST_INTR, 1,
191 RF_SHAREABLE | RF_ACTIVE)) == NULL) {
192 device_printf(dev, "unable to allocate IRQ resource\n");
196 if ((bus_setup_intr(dev, sc->sc_fast_irq, INTR_TYPE_MISC, obio_intr,
197 NULL, sc, &sc->sc_fast_ih))) {
199 "WARNING: unable to register interrupt handler\n");
203 /* disable all interrupts */
204 rt305x_ic_set(IC_INT_DIS, IC_INT_MASK|IC_LINE_GLOBAL);
206 bus_generic_probe(dev);
208 obio_add_res_child(dev, "rt305x_sysctl", 0,
209 SYSCTL_BASE, (SYSCTL_END - SYSCTL_BASE + 1),
211 obio_add_res_child(dev, "rt305x_ic", 0,
212 INTCTL_BASE, (INTCTL_END - INTCTL_BASE + 1),
215 obio_add_res_child(dev, "timer",0,
216 TIMER_BASE, (TIMER_END - TIMER_BASE + 1),
218 obio_add_res_child(dev, "rt305x_memc", 0,
219 MEMCTRL_BASE, (MEMCTRL_END - MEMCTRL_BASE + 1),
221 obio_add_res_child(dev, "pcm", 0,
222 PCM_BASE, (PCM_END - PCM_BASE + 1),
224 obio_add_res_child(dev, "uart", 0,
225 UART_BASE, (UART_END - UART_BASE + 1),
228 obio_add_res_child(dev, "gpio", 0,
229 PIO_BASE, (PIO_END - PIO_BASE + 1),
232 obio_add_res_child(dev, "rt305x_dma", 0,
233 GDMA_BASE, (GDMA_END - GDMA_BASE + 1),
235 obio_add_res_child(dev, "rt305x_nandc", 0,
236 NANDFC_BASE, (NANDFC_END - NANDFC_BASE + 1),
238 obio_add_res_child(dev, "i2c", 0,
239 I2C_BASE, (I2C_END - I2C_BASE + 1),
241 obio_add_res_child(dev, "i2s", 0,
242 I2S_BASE, (I2S_END - I2S_BASE + 1),
244 obio_add_res_child(dev, "spi", 0,
245 SPI_BASE, (SPI_END - SPI_BASE + 1),
248 obio_add_res_child(dev, "uart", 1,
249 UARTLITE_BASE, (UARTLITE_END - UARTLITE_BASE + 1),
251 obio_add_res_child(dev, "cfi", 0,
252 FLASH_BASE, (FLASH_END - FLASH_BASE + 1),
254 obio_add_res_child(dev, "dotg", 0,
255 USB_OTG_BASE, (USB_OTG_END - USB_OTG_BASE + 1),
257 obio_add_res_child(dev, "switch", 0,
258 ETHSW_BASE, (ETHSW_END - ETHSW_BASE + 1),
261 bus_enumerate_hinted_children(dev);
262 bus_generic_attach(dev);
265 rt305x_ic_set(IC_INT_ENA, IC_LINE_GLOBAL);
270 static struct resource *
271 obio_alloc_resource(device_t bus, device_t child, int type, int *rid,
272 u_long start, u_long end, u_long count, u_int flags)
274 struct obio_softc *sc = device_get_softc(bus);
275 struct obio_ivar *ivar = device_get_ivars(child);
277 struct resource_list_entry *rle;
279 int isdefault, needactivate, passthrough;
281 isdefault = (start == 0UL && end == ~0UL && count == 1);
282 needactivate = flags & RF_ACTIVE;
283 passthrough = (device_get_parent(child) != bus);
287 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type,
288 rid, start, end, count, flags));
291 * If this is an allocation of the "default" range for a given RID,
292 * and we know what the resources for this device are (ie. they aren't
293 * maintained by a child bus), then work out the start/end values.
296 rle = resource_list_find(&ivar->resources, type, *rid);
299 if (rle->res != NULL) {
300 panic("%s: resource entry is busy", __func__);
309 rm = &sc->oba_irq_rman;
312 rm = &sc->oba_mem_rman;
315 printf("%s: unknown resource type %d\n", __func__, type);
319 rv = rman_reserve_resource(rm, start, end, count, flags, child);
321 printf("%s: could not reserve resource\n", __func__);
325 rman_set_rid(rv, *rid);
328 if (bus_activate_resource(child, type, *rid, rv)) {
329 printf("%s: could not activate resource\n", __func__);
330 rman_release_resource(rv);
339 obio_activate_resource(device_t bus, device_t child, int type, int rid,
344 * If this is a memory resource, track the direct mapping
345 * in the uncached MIPS KSEG1 segment.
347 if (type == SYS_RES_MEMORY) {
350 vaddr = (void *)MIPS_PHYS_TO_KSEG1((intptr_t)rman_get_start(r));
351 rman_set_virtual(r, vaddr);
352 rman_set_bustag(r, mips_bus_space_generic);
353 rman_set_bushandle(r, (bus_space_handle_t)vaddr);
356 return (rman_activate_resource(r));
360 obio_deactivate_resource(device_t bus, device_t child, int type, int rid,
364 return (rman_deactivate_resource(r));
368 obio_release_resource(device_t dev, device_t child, int type,
369 int rid, struct resource *r)
371 struct resource_list *rl;
372 struct resource_list_entry *rle;
374 rl = obio_get_resource_list(dev, child);
377 rle = resource_list_find(rl, type, rid);
380 rman_release_resource(r);
387 obio_setup_intr(device_t dev, device_t child, struct resource *ires,
388 int flags, driver_filter_t *filt, driver_intr_t *handler,
389 void *arg, void **cookiep)
391 struct obio_softc *sc = device_get_softc(dev);
392 struct intr_event *event;
393 int irq, error, priority;
396 irq = rman_get_start(ires);
399 panic("%s: bad irq %d", __func__, irq);
401 event = sc->sc_eventstab[irq];
403 error = intr_event_create(&event, (void *)irq, 0, irq,
404 obio_mask_irq, obio_unmask_irq,
405 NULL, NULL, "obio intr%d:", irq);
407 sc->sc_eventstab[irq] = event;
410 panic("obio: Can't share IRQs");
412 intr_event_add_handler(event, device_get_nameunit(child), filt,
413 handler, arg, intr_priority(flags), flags, cookiep);
416 priority = irq_priorities[irq];
418 if (priority == INTR_FIQ)
419 rt305x_ic_set(IC_INTTYPE, rt305x_ic_get(IC_INTTYPE) | irqmask);
421 rt305x_ic_set(IC_INTTYPE, rt305x_ic_get(IC_INTTYPE) & ~irqmask);
424 obio_unmask_irq((void*)irq);
430 obio_teardown_intr(device_t dev, device_t child, struct resource *ires,
433 struct obio_softc *sc = device_get_softc(dev);
434 int irq, result, priority;
437 irq = rman_get_start(ires);
439 panic("%s: bad irq %d", __func__, irq);
441 if (sc->sc_eventstab[irq] == NULL)
442 panic("Trying to teardown unoccupied IRQ");
444 irqmask = (1 << irq);
445 priority = irq_priorities[irq];
447 if (priority == INTR_FIQ)
448 rt305x_ic_set(IC_INTTYPE, rt305x_ic_get(IC_INTTYPE) & ~irqmask);
450 rt305x_ic_set(IC_INTTYPE, rt305x_ic_get(IC_INTTYPE) | irqmask);
453 obio_mask_irq((void*)irq);
455 result = intr_event_remove_handler(cookie);
457 sc->sc_eventstab[irq] = NULL;
466 struct obio_softc *sc = arg;
467 struct intr_event *event;
471 irqstat = rt305x_ic_get(IC_IRQ0STAT);
472 irqstat |= rt305x_ic_get(IC_IRQ1STAT);
475 while (irqstat != 0) {
476 if ((irqstat & 1) == 1) {
477 event = sc->sc_eventstab[irq];
478 if (!event || TAILQ_EMPTY(&event->ie_handlers))
481 /* TODO: pass frame as an argument*/
482 /* TODO: log stray interrupt */
483 intr_event_handle(event, NULL);
489 return (FILTER_HANDLED);
493 obio_add_res_child(device_t bus, const char *dname, int dunit,
494 long maddr, int msize, int irq)
499 child = BUS_ADD_CHILD(bus, 0, dname, dunit);
501 result = bus_set_resource(child, SYS_RES_MEMORY, 0,
504 device_printf(bus, "warning: bus_set_resource() failed\n");
507 result = bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
510 "warning: bus_set_resource() failed\n");
515 obio_hinted_child(device_t bus, const char *dname, int dunit)
522 * Set hard-wired resources for hinted child using
525 resource_long_value(dname, dunit, "maddr", &maddr);
526 resource_int_value(dname, dunit, "msize", &msize);
529 if (resource_int_value(dname, dunit, "irq", &irq) == 0) irq = -1;
531 obio_add_res_child(bus, dname, dunit, maddr, msize, irq);
535 obio_add_child(device_t bus, u_int order, const char *name, int unit)
538 struct obio_ivar *ivar;
540 ivar = malloc(sizeof(struct obio_ivar), M_DEVBUF, M_WAITOK | M_ZERO);
542 printf("Failed to allocate ivar\n");
545 resource_list_init(&ivar->resources);
547 child = device_add_child_ordered(bus, order, name, unit);
549 printf("Can't add child %s%d ordered\n", name, unit);
553 device_set_ivars(child, ivar);
559 * Helper routine for bus_generic_rl_get_resource/bus_generic_rl_set_resource
560 * Provides pointer to resource_list for these routines
562 static struct resource_list *
563 obio_get_resource_list(device_t dev, device_t child)
565 struct obio_ivar *ivar;
567 ivar = device_get_ivars(child);
568 return (&(ivar->resources));
572 obio_print_all_resources(device_t dev)
574 struct obio_ivar *ivar = device_get_ivars(dev);
575 struct resource_list *rl = &ivar->resources;
578 if (STAILQ_FIRST(rl))
579 retval += printf(" at");
581 retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
582 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
588 obio_print_child(device_t bus, device_t child)
592 retval += bus_print_child_header(bus, child);
593 retval += obio_print_all_resources(child);
594 if (device_get_flags(child))
595 retval += printf(" flags %#x", device_get_flags(child));
596 retval += printf(" on %s\n", device_get_nameunit(bus));
601 static device_method_t obio_methods[] = {
602 DEVMETHOD(bus_activate_resource, obio_activate_resource),
603 DEVMETHOD(bus_add_child, obio_add_child),
604 DEVMETHOD(bus_alloc_resource, obio_alloc_resource),
605 DEVMETHOD(bus_deactivate_resource, obio_deactivate_resource),
606 DEVMETHOD(bus_get_resource_list, obio_get_resource_list),
607 DEVMETHOD(bus_hinted_child, obio_hinted_child),
608 DEVMETHOD(bus_print_child, obio_print_child),
609 DEVMETHOD(bus_release_resource, obio_release_resource),
610 DEVMETHOD(bus_setup_intr, obio_setup_intr),
611 DEVMETHOD(bus_teardown_intr, obio_teardown_intr),
612 DEVMETHOD(device_attach, obio_attach),
613 DEVMETHOD(device_probe, obio_probe),
614 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
615 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
620 static driver_t obio_driver = {
623 sizeof(struct obio_softc),
625 static devclass_t obio_devclass;
627 DRIVER_MODULE(obio, nexus, obio_driver, obio_devclass, 0, 0);