2 * Copyright (c) 2007, Oleksandr Tymoshenko <gonzo@freebsd.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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
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.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
39 #include <sys/interrupt.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
43 #include <sys/malloc.h>
45 #include <machine/bus.h>
47 #include <mips/idt/idtreg.h>
48 #include <mips/idt/obiovar.h>
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)
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)
58 static int obio_activate_resource(device_t, device_t, int, int,
60 static device_t obio_add_child(device_t, u_int, const char *, int);
61 static struct resource *
62 obio_alloc_resource(device_t, device_t, int, int *, rman_res_t,
63 rman_res_t, rman_res_t, u_int);
64 static int obio_attach(device_t);
65 static int obio_deactivate_resource(device_t, device_t, int, int,
67 static struct resource_list *
68 obio_get_resource_list(device_t, device_t);
69 static void obio_hinted_child(device_t, const char *, int);
70 static int obio_intr(void *);
71 static int obio_probe(device_t);
72 static int obio_release_resource(device_t, device_t, int, int,
74 static int obio_setup_intr(device_t, device_t, struct resource *, int,
75 driver_filter_t *, driver_intr_t *, void *, void **);
76 static int obio_teardown_intr(device_t, device_t, struct resource *,
80 obio_mask_irq(void *arg)
82 unsigned int irq = (unsigned int)arg;
83 int ip_bit, mask, mask_register;
86 mask_register = ICU_IRQ_MASK_REG(irq);
87 ip_bit = ICU_IP_BIT(irq);
89 mask = ICU_REG_READ(mask_register);
90 ICU_REG_WRITE(mask_register, mask | ip_bit);
94 obio_unmask_irq(void *arg)
96 unsigned int irq = (unsigned int)arg;
97 int ip_bit, mask, mask_register;
100 mask_register = ICU_IRQ_MASK_REG(irq);
101 ip_bit = ICU_IP_BIT(irq);
103 mask = ICU_REG_READ(mask_register);
104 ICU_REG_WRITE(mask_register, mask & ~ip_bit);
108 obio_probe(device_t dev)
111 return (BUS_PROBE_NOWILDCARD);
115 obio_attach(device_t dev)
117 struct obio_softc *sc = device_get_softc(dev);
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");
127 sc->oba_irq_rman.rm_type = RMAN_ARRAY;
128 sc->oba_irq_rman.rm_descr = "OBIO IRQ";
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");
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");
142 if ((bus_setup_intr(dev, sc->sc_irq[irq], INTR_TYPE_MISC,
143 obio_intr, NULL, sc, &sc->sc_ih[irq]))) {
145 "WARNING: unable to register interrupt handler\n");
150 bus_generic_probe(dev);
151 bus_enumerate_hinted_children(dev);
152 bus_generic_attach(dev);
157 static struct resource *
158 obio_alloc_resource(device_t bus, device_t child, int type, int *rid,
159 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
161 struct obio_softc *sc = device_get_softc(bus);
162 struct obio_ivar *ivar = device_get_ivars(child);
164 struct resource_list_entry *rle;
166 int isdefault, needactivate, passthrough;
168 isdefault = (RMAN_IS_DEFAULT_RANGE(start, end));
169 needactivate = flags & RF_ACTIVE;
170 passthrough = (device_get_parent(child) != bus);
174 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type,
175 rid, start, end, count, flags));
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.
183 rle = resource_list_find(&ivar->resources, type, *rid);
186 if (rle->res != NULL) {
187 panic("%s: resource entry is busy", __func__);
196 rm = &sc->oba_irq_rman;
199 rm = &sc->oba_mem_rman;
202 printf("%s: unknown resource type %d\n", __func__, type);
206 rv = rman_reserve_resource(rm, start, end, count, flags, child);
208 printf("%s: could not reserve resource\n", __func__);
212 rman_set_rid(rv, *rid);
215 if (bus_activate_resource(child, type, *rid, rv)) {
216 printf("%s: could not activate resource\n", __func__);
217 rman_release_resource(rv);
226 obio_activate_resource(device_t bus, device_t child, int type, int rid,
230 /* XXX: should we mask/unmask IRQ here? */
231 return (BUS_ACTIVATE_RESOURCE(device_get_parent(bus), child,
236 obio_deactivate_resource(device_t bus, device_t child, int type, int rid,
240 /* XXX: should we mask/unmask IRQ here? */
241 return (BUS_DEACTIVATE_RESOURCE(device_get_parent(bus), child,
246 obio_release_resource(device_t dev, device_t child, int type,
247 int rid, struct resource *r)
249 struct resource_list *rl;
250 struct resource_list_entry *rle;
252 rl = obio_get_resource_list(dev, child);
255 rle = resource_list_find(rl, type, rid);
258 rman_release_resource(r);
265 obio_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)
269 struct obio_softc *sc = device_get_softc(dev);
270 struct intr_event *event;
271 int irq, ip_bit, error, mask, mask_register;
273 irq = rman_get_start(ires);
276 panic("%s: bad irq %d", __func__, irq);
278 event = sc->sc_eventstab[irq];
280 error = intr_event_create(&event, (void *)irq, 0, irq,
281 obio_mask_irq, obio_unmask_irq,
283 "obio intr%d:", irq);
285 sc->sc_eventstab[irq] = event;
288 intr_event_add_handler(event, device_get_nameunit(child), filt,
289 handler, arg, intr_priority(flags), flags, cookiep);
292 mask_register = ICU_IRQ_MASK_REG(irq);
293 ip_bit = ICU_IP_BIT(irq);
295 mask = ICU_REG_READ(mask_register);
296 ICU_REG_WRITE(mask_register, mask & ~ip_bit);
302 obio_teardown_intr(device_t dev, device_t child, struct resource *ires,
305 struct obio_softc *sc = device_get_softc(dev);
307 uint32_t mask_register, mask, ip_bit;
309 irq = rman_get_start(ires);
311 panic("%s: bad irq %d", __func__, irq);
313 if (sc->sc_eventstab[irq] == NULL)
314 panic("Trying to teardown unoccupied IRQ");
317 mask_register = ICU_IRQ_MASK_REG(irq);
318 ip_bit = ICU_IP_BIT(irq);
320 mask = ICU_REG_READ(mask_register);
321 ICU_REG_WRITE(mask_register, mask | ip_bit);
323 result = intr_event_remove_handler(cookie);
325 sc->sc_eventstab[irq] = NULL;
333 struct obio_softc *sc = arg;
334 struct intr_event *event;
335 uint32_t irqstat, ipend, imask, xpend;
336 int irq, thread, group, i;
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));
346 while ((i = fls(xpend)) != 0) {
347 xpend &= ~(1 << (i - 1));
348 irq = IP_IRQ(group, i - 1);
351 while ((i = fls(ipend)) != 0) {
352 ipend &= ~(1 << (i - 1));
353 irq = IP_IRQ(group, i - 1);
354 event = sc->sc_eventstab[irq];
356 if (!event || TAILQ_EMPTY(&event->ie_handlers)) {
357 /* TODO: Log stray IRQs */
361 /* TODO: frame instead of NULL? */
362 intr_event_handle(event, NULL);
363 /* XXX: Log stray IRQs */
367 ipend = ICU_REG_READ(ICU_IPEND2);
368 printf("ipend2 = %08x!\n", ipend);
370 ipend = ICU_REG_READ(ICU_IPEND3);
371 printf("ipend3 = %08x!\n", ipend);
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);
378 ipend = ICU_REG_READ(ICU_IPEND6);
379 printf("ipend6 = %08x!\n", ipend);
381 while (irqstat != 0) {
382 if ((irqstat & 1) == 1) {
389 return (FILTER_HANDLED);
393 obio_hinted_child(device_t bus, const char *dname, int dunit)
401 child = BUS_ADD_CHILD(bus, 0, dname, dunit);
404 * Set hard-wired resources for hinted child using
407 resource_long_value(dname, dunit, "maddr", &maddr);
408 resource_int_value(dname, dunit, "msize", &msize);
411 result = bus_set_resource(child, SYS_RES_MEMORY, 0,
414 device_printf(bus, "warning: bus_set_resource() failed\n");
416 if (resource_int_value(dname, dunit, "irq", &irq) == 0) {
417 result = bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
420 "warning: bus_set_resource() failed\n");
425 obio_add_child(device_t bus, u_int order, const char *name, int unit)
428 struct obio_ivar *ivar;
430 ivar = malloc(sizeof(struct obio_ivar), M_DEVBUF, M_WAITOK | M_ZERO);
431 resource_list_init(&ivar->resources);
433 child = device_add_child_ordered(bus, order, name, unit);
435 printf("Can't add child %s%d ordered\n", name, unit);
439 device_set_ivars(child, ivar);
445 * Helper routine for bus_generic_rl_get_resource/bus_generic_rl_set_resource
446 * Provides pointer to resource_list for these routines
448 static struct resource_list *
449 obio_get_resource_list(device_t dev, device_t child)
451 struct obio_ivar *ivar;
453 ivar = device_get_ivars(child);
454 return (&(ivar->resources));
457 static device_method_t obio_methods[] = {
458 DEVMETHOD(bus_activate_resource, obio_activate_resource),
459 DEVMETHOD(bus_add_child, obio_add_child),
460 DEVMETHOD(bus_alloc_resource, obio_alloc_resource),
461 DEVMETHOD(bus_deactivate_resource, obio_deactivate_resource),
462 DEVMETHOD(bus_get_resource_list, obio_get_resource_list),
463 DEVMETHOD(bus_hinted_child, obio_hinted_child),
464 DEVMETHOD(bus_release_resource, obio_release_resource),
465 DEVMETHOD(bus_setup_intr, obio_setup_intr),
466 DEVMETHOD(bus_teardown_intr, obio_teardown_intr),
467 DEVMETHOD(device_attach, obio_attach),
468 DEVMETHOD(device_probe, obio_probe),
469 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
470 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
475 static driver_t obio_driver = {
478 sizeof(struct obio_softc),
480 static devclass_t obio_devclass;
482 DRIVER_MODULE(obio, nexus, obio_driver, obio_devclass, 0, 0);