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, int, const char *, int);
61 static struct resource *
62 obio_alloc_resource(device_t, device_t, int, int *, u_long,
63 u_long, u_long, 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 *,
79 static void obio_mask_irq(unsigned int irq)
81 int ip_bit, mask, mask_register;
84 mask_register = ICU_IRQ_MASK_REG(irq);
85 ip_bit = ICU_IP_BIT(irq);
87 mask = ICU_REG_READ(mask_register);
88 ICU_REG_WRITE(mask_register, mask | ip_bit);
91 static void obio_unmask_irq(unsigned int irq)
93 int ip_bit, mask, mask_register;
96 mask_register = ICU_IRQ_MASK_REG(irq);
97 ip_bit = ICU_IP_BIT(irq);
99 mask = ICU_REG_READ(mask_register);
100 ICU_REG_WRITE(mask_register, mask & ~ip_bit);
104 obio_probe(device_t dev)
111 obio_attach(device_t dev)
113 struct obio_softc *sc = device_get_softc(dev);
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");
123 sc->oba_irq_rman.rm_type = RMAN_ARRAY;
124 sc->oba_irq_rman.rm_descr = "OBIO IRQ";
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");
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");
138 if ((bus_setup_intr(dev, sc->sc_irq[irq], INTR_TYPE_MISC,
139 obio_intr, NULL, sc, &sc->sc_ih[irq]))) {
141 "WARNING: unable to register interrupt handler\n");
146 bus_generic_probe(dev);
147 bus_enumerate_hinted_children(dev);
148 bus_generic_attach(dev);
153 static struct resource *
154 obio_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)
157 struct obio_softc *sc = device_get_softc(bus);
158 struct obio_ivar *ivar = device_get_ivars(child);
160 struct resource_list_entry *rle;
162 int isdefault, needactivate, passthrough;
164 isdefault = (start == 0UL && end == ~0UL);
165 needactivate = flags & RF_ACTIVE;
166 passthrough = (device_get_parent(child) != bus);
170 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type,
171 rid, start, end, count, flags));
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.
179 rle = resource_list_find(&ivar->resources, type, *rid);
182 if (rle->res != NULL) {
183 panic("%s: resource entry is busy", __func__);
192 rm = &sc->oba_irq_rman;
195 rm = &sc->oba_mem_rman;
198 printf("%s: unknown resource type %d\n", __func__, type);
202 rv = rman_reserve_resource(rm, start, end, count, flags, child);
204 printf("%s: could not reserve resource\n", __func__);
208 rman_set_rid(rv, *rid);
211 if (bus_activate_resource(child, type, *rid, rv)) {
212 printf("%s: could not activate resource\n", __func__);
213 rman_release_resource(rv);
222 obio_activate_resource(device_t bus, device_t child, int type, int rid,
226 /* XXX: should we mask/unmask IRQ here? */
227 return (BUS_ACTIVATE_RESOURCE(device_get_parent(bus), child,
232 obio_deactivate_resource(device_t bus, device_t child, int type, int rid,
236 /* XXX: should we mask/unmask IRQ here? */
237 return (BUS_DEACTIVATE_RESOURCE(device_get_parent(bus), child,
242 obio_release_resource(device_t dev, device_t child, int type,
243 int rid, struct resource *r)
245 struct resource_list *rl;
246 struct resource_list_entry *rle;
248 rl = obio_get_resource_list(dev, child);
251 rle = resource_list_find(rl, type, rid);
254 rman_release_resource(r);
261 obio_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)
265 struct obio_softc *sc = device_get_softc(dev);
266 struct intr_event *event;
267 int irq, ip_bit, error, mask, mask_register;
269 irq = rman_get_start(ires);
272 panic("%s: bad irq %d", __func__, irq);
274 event = sc->sc_eventstab[irq];
276 error = intr_event_create(&event, (void *)irq, 0, irq,
277 (mask_fn)obio_mask_irq, (mask_fn)obio_unmask_irq,
279 "obio intr%d:", irq);
281 sc->sc_eventstab[irq] = event;
284 intr_event_add_handler(event, device_get_nameunit(child), filt,
285 handler, arg, intr_priority(flags), flags, cookiep);
288 mask_register = ICU_IRQ_MASK_REG(irq);
289 ip_bit = ICU_IP_BIT(irq);
291 mask = ICU_REG_READ(mask_register);
292 ICU_REG_WRITE(mask_register, mask & ~ip_bit);
298 obio_teardown_intr(device_t dev, device_t child, struct resource *ires,
301 struct obio_softc *sc = device_get_softc(dev);
303 uint32_t mask_register, mask, ip_bit;
305 irq = rman_get_start(ires);
307 panic("%s: bad irq %d", __func__, irq);
309 if (sc->sc_eventstab[irq] == NULL)
310 panic("Trying to teardown unoccupied IRQ");
313 mask_register = ICU_IRQ_MASK_REG(irq);
314 ip_bit = ICU_IP_BIT(irq);
316 mask = ICU_REG_READ(mask_register);
317 ICU_REG_WRITE(mask_register, mask | ip_bit);
319 result = intr_event_remove_handler(cookie);
321 sc->sc_eventstab[irq] = NULL;
329 struct obio_softc *sc = arg;
330 struct intr_event *event;
331 uint32_t irqstat, ipend, imask, xpend;
332 int irq, thread, group, i;
336 for (group = 2; group <= 6; group++) {
337 ipend = ICU_REG_READ(ICU_GROUP_IPEND_REG(group));
338 imask = ICU_REG_READ(ICU_GROUP_MASK_REG(group));
342 while ((i = fls(xpend)) != 0) {
343 xpend &= ~(1 << (i - 1));
344 irq = IP_IRQ(group, i - 1);
347 while ((i = fls(ipend)) != 0) {
348 ipend &= ~(1 << (i - 1));
349 irq = IP_IRQ(group, i - 1);
350 event = sc->sc_eventstab[irq];
352 if (!event || TAILQ_EMPTY(&event->ie_handlers)) {
353 /* TODO: Log stray IRQs */
357 /* TODO: frame instead of NULL? */
358 intr_event_handle(event, NULL);
359 /* XXX: Log stray IRQs */
363 ipend = ICU_REG_READ(ICU_IPEND2);
364 printf("ipend2 = %08x!\n", ipend);
366 ipend = ICU_REG_READ(ICU_IPEND3);
367 printf("ipend3 = %08x!\n", ipend);
369 ipend = ICU_REG_READ(ICU_IPEND4);
370 printf("ipend4 = %08x!\n", ipend);
371 ipend = ICU_REG_READ(ICU_IPEND5);
372 printf("ipend5 = %08x!\n", ipend);
374 ipend = ICU_REG_READ(ICU_IPEND6);
375 printf("ipend6 = %08x!\n", ipend);
377 while (irqstat != 0) {
378 if ((irqstat & 1) == 1) {
385 return (FILTER_HANDLED);
389 obio_hinted_child(device_t bus, const char *dname, int dunit)
397 child = BUS_ADD_CHILD(bus, 0, dname, dunit);
400 * Set hard-wired resources for hinted child using
403 resource_long_value(dname, dunit, "maddr", &maddr);
404 resource_int_value(dname, dunit, "msize", &msize);
407 result = bus_set_resource(child, SYS_RES_MEMORY, 0,
410 device_printf(bus, "warning: bus_set_resource() failed\n");
412 if (resource_int_value(dname, dunit, "irq", &irq) == 0) {
413 result = bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
416 "warning: bus_set_resource() failed\n");
421 obio_add_child(device_t bus, int order, const char *name, int unit)
424 struct obio_ivar *ivar;
426 ivar = malloc(sizeof(struct obio_ivar), M_DEVBUF, M_WAITOK | M_ZERO);
428 printf("Failed to allocate ivar\n");
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);