2 * Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
34 #include <sys/interrupt.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
38 #include <sys/malloc.h>
42 #include <sys/pmckern.h>
44 #include <machine/bus.h>
45 #include <machine/intr_machdep.h>
47 #include <mips/atheros/apbvar.h>
48 #include <mips/atheros/ar71xxreg.h>
49 #include <mips/atheros/ar71xx_setup.h>
51 #define APB_INTR_PMC 5
55 #define dprintf printf
57 #define dprintf(x, arg...)
58 #endif /* APB_DEBUG */
60 static int apb_activate_resource(device_t, device_t, int, int,
62 static device_t apb_add_child(device_t, u_int, const char *, int);
63 static struct resource *
64 apb_alloc_resource(device_t, device_t, int, int *, u_long,
65 u_long, u_long, u_int);
66 static int apb_attach(device_t);
67 static int apb_deactivate_resource(device_t, device_t, int, int,
69 static struct resource_list *
70 apb_get_resource_list(device_t, device_t);
71 static void apb_hinted_child(device_t, const char *, int);
72 static int apb_filter(void *);
73 static int apb_probe(device_t);
74 static int apb_release_resource(device_t, device_t, int, int,
76 static int apb_setup_intr(device_t, device_t, struct resource *, int,
77 driver_filter_t *, driver_intr_t *, void *, void **);
78 static int apb_teardown_intr(device_t, device_t, struct resource *,
82 apb_mask_irq(void *source)
84 unsigned int irq = (unsigned int)source;
87 reg = ATH_READ_REG(AR71XX_MISC_INTR_MASK);
88 ATH_WRITE_REG(AR71XX_MISC_INTR_MASK, reg & ~(1 << irq));
93 apb_unmask_irq(void *source)
96 unsigned int irq = (unsigned int)source;
98 reg = ATH_READ_REG(AR71XX_MISC_INTR_MASK);
99 ATH_WRITE_REG(AR71XX_MISC_INTR_MASK, reg | (1 << irq));
103 apb_probe(device_t dev)
106 return (BUS_PROBE_NOWILDCARD);
110 apb_attach(device_t dev)
112 struct apb_softc *sc = device_get_softc(dev);
115 device_set_desc(dev, "APB Bus bridge");
117 sc->apb_mem_rman.rm_type = RMAN_ARRAY;
118 sc->apb_mem_rman.rm_descr = "APB memory window";
120 if (rman_init(&sc->apb_mem_rman) != 0 ||
121 rman_manage_region(&sc->apb_mem_rman,
123 AR71XX_APB_BASE + AR71XX_APB_SIZE - 1) != 0)
124 panic("apb_attach: failed to set up memory rman");
126 sc->apb_irq_rman.rm_type = RMAN_ARRAY;
127 sc->apb_irq_rman.rm_descr = "APB IRQ";
129 if (rman_init(&sc->apb_irq_rman) != 0 ||
130 rman_manage_region(&sc->apb_irq_rman,
131 APB_IRQ_BASE, APB_IRQ_END) != 0)
132 panic("apb_attach: failed to set up IRQ rman");
134 if ((sc->sc_misc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
135 RF_SHAREABLE | RF_ACTIVE)) == NULL) {
136 device_printf(dev, "unable to allocate IRQ resource\n");
140 if ((bus_setup_intr(dev, sc->sc_misc_irq, INTR_TYPE_MISC,
141 apb_filter, NULL, sc, &sc->sc_misc_ih))) {
143 "WARNING: unable to register interrupt handler\n");
147 bus_generic_probe(dev);
148 bus_enumerate_hinted_children(dev);
149 bus_generic_attach(dev);
152 * Unmask performance counter IRQ
154 apb_unmask_irq((void*)APB_INTR_PMC);
155 sc->sc_intr_counter[APB_INTR_PMC] = mips_intrcnt_create("apb irq5: pmc");
160 static struct resource *
161 apb_alloc_resource(device_t bus, device_t child, int type, int *rid,
162 u_long start, u_long end, u_long count, u_int flags)
164 struct apb_softc *sc = device_get_softc(bus);
165 struct apb_ivar *ivar = device_get_ivars(child);
167 struct resource_list_entry *rle;
169 int isdefault, needactivate, passthrough;
171 isdefault = (start == 0UL && end == ~0UL);
172 needactivate = flags & RF_ACTIVE;
174 * Pass memory requests to nexus device
176 passthrough = (device_get_parent(child) != bus);
179 dprintf("%s: entry (%p, %p, %d, %d, %p, %p, %ld, %d)\n",
180 __func__, bus, child, type, *rid, (void *)(intptr_t)start,
181 (void *)(intptr_t)end, count, flags);
184 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type,
185 rid, start, end, count, flags));
188 * If this is an allocation of the "default" range for a given RID,
189 * and we know what the resources for this device are (ie. they aren't
190 * maintained by a child bus), then work out the start/end values.
194 rle = resource_list_find(&ivar->resources, type, *rid);
199 if (rle->res != NULL) {
200 panic("%s: resource entry is busy", __func__);
206 dprintf("%s: default resource (%p, %p, %ld)\n",
207 __func__, (void *)(intptr_t)start,
208 (void *)(intptr_t)end, count);
213 rm = &sc->apb_irq_rman;
216 rm = &sc->apb_mem_rman;
219 printf("%s: unknown resource type %d\n", __func__, type);
223 rv = rman_reserve_resource(rm, start, end, count, flags, child);
225 printf("%s: could not reserve resource\n", __func__);
229 rman_set_rid(rv, *rid);
232 if (bus_activate_resource(child, type, *rid, rv)) {
233 printf("%s: could not activate resource\n", __func__);
234 rman_release_resource(rv);
243 apb_activate_resource(device_t bus, device_t child, int type, int rid,
247 /* XXX: should we mask/unmask IRQ here? */
248 return (BUS_ACTIVATE_RESOURCE(device_get_parent(bus), child,
253 apb_deactivate_resource(device_t bus, device_t child, int type, int rid,
257 /* XXX: should we mask/unmask IRQ here? */
258 return (BUS_DEACTIVATE_RESOURCE(device_get_parent(bus), child,
263 apb_release_resource(device_t dev, device_t child, int type,
264 int rid, struct resource *r)
266 struct resource_list *rl;
267 struct resource_list_entry *rle;
269 rl = apb_get_resource_list(dev, child);
272 rle = resource_list_find(rl, type, rid);
275 rman_release_resource(r);
282 apb_setup_intr(device_t bus, device_t child, struct resource *ires,
283 int flags, driver_filter_t *filt, driver_intr_t *handler,
284 void *arg, void **cookiep)
286 struct apb_softc *sc = device_get_softc(bus);
287 struct intr_event *event;
290 irq = rman_get_start(ires);
292 if (irq > APB_IRQ_END)
293 panic("%s: bad irq %d", __func__, irq);
295 event = sc->sc_eventstab[irq];
297 error = intr_event_create(&event, (void *)irq, 0, irq,
298 apb_mask_irq, apb_unmask_irq,
303 sc->sc_eventstab[irq] = event;
304 sc->sc_intr_counter[irq] =
305 mips_intrcnt_create(event->ie_name);
311 intr_event_add_handler(event, device_get_nameunit(child), filt,
312 handler, arg, intr_priority(flags), flags, cookiep);
313 mips_intrcnt_setname(sc->sc_intr_counter[irq], event->ie_fullname);
315 apb_unmask_irq((void*)irq);
321 apb_teardown_intr(device_t dev, device_t child, struct resource *ires,
324 struct apb_softc *sc = device_get_softc(dev);
327 irq = rman_get_start(ires);
328 if (irq > APB_IRQ_END)
329 panic("%s: bad irq %d", __func__, irq);
331 if (sc->sc_eventstab[irq] == NULL)
332 panic("Trying to teardown unoccupied IRQ");
334 apb_mask_irq((void*)irq);
336 result = intr_event_remove_handler(cookie);
338 sc->sc_eventstab[irq] = NULL;
344 apb_filter(void *arg)
346 struct apb_softc *sc = arg;
347 struct intr_event *event;
350 struct trapframe *tf;
352 reg = ATH_READ_REG(AR71XX_MISC_INTR_STATUS);
353 for (irq = 0; irq < APB_NIRQS; irq++) {
354 if (reg & (1 << irq)) {
356 switch (ar71xx_soc) {
357 case AR71XX_SOC_AR7240:
358 case AR71XX_SOC_AR7241:
359 case AR71XX_SOC_AR7242:
360 case AR71XX_SOC_AR9330:
361 case AR71XX_SOC_AR9331:
362 case AR71XX_SOC_AR9341:
363 case AR71XX_SOC_AR9342:
364 case AR71XX_SOC_AR9344:
365 /* Ack/clear the irq on status register for AR724x */
366 ATH_WRITE_REG(AR71XX_MISC_INTR_STATUS,
374 event = sc->sc_eventstab[irq];
375 if (!event || TAILQ_EMPTY(&event->ie_handlers)) {
376 if (irq == APB_INTR_PMC) {
377 td = PCPU_GET(curthread);
378 tf = td->td_intr_frame;
381 (*pmc_intr)(PCPU_GET(cpuid), tf);
383 mips_intrcnt_inc(sc->sc_intr_counter[irq]);
387 /* Ignore timer interrupts */
389 printf("Stray APB IRQ %d\n", irq);
393 intr_event_handle(event, PCPU_GET(curthread)->td_intr_frame);
394 mips_intrcnt_inc(sc->sc_intr_counter[irq]);
398 return (FILTER_HANDLED);
402 apb_hinted_child(device_t bus, const char *dname, int dunit)
411 child = BUS_ADD_CHILD(bus, 0, dname, dunit);
414 * Set hard-wired resources for hinted child using
418 if (resource_long_value(dname, dunit, "maddr", &maddr) == 0)
420 if (resource_int_value(dname, dunit, "msize", &msize) == 0)
423 /* check if all info for mem resource has been provided */
424 if ((mem_hints_count > 0) && (mem_hints_count < 2)) {
425 printf("Either maddr or msize hint is missing for %s%d\n",
427 } else if (mem_hints_count) {
428 result = bus_set_resource(child, SYS_RES_MEMORY, 0,
432 "warning: bus_set_resource() failed\n");
435 if (resource_int_value(dname, dunit, "irq", &irq) == 0) {
436 result = bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
439 "warning: bus_set_resource() failed\n");
444 apb_add_child(device_t bus, u_int order, const char *name, int unit)
447 struct apb_ivar *ivar;
449 ivar = malloc(sizeof(struct apb_ivar), M_DEVBUF, M_WAITOK | M_ZERO);
451 printf("Failed to allocate ivar\n");
454 resource_list_init(&ivar->resources);
456 child = device_add_child_ordered(bus, order, name, unit);
458 printf("Can't add child %s%d ordered\n", name, unit);
462 device_set_ivars(child, ivar);
468 * Helper routine for bus_generic_rl_get_resource/bus_generic_rl_set_resource
469 * Provides pointer to resource_list for these routines
471 static struct resource_list *
472 apb_get_resource_list(device_t dev, device_t child)
474 struct apb_ivar *ivar;
476 ivar = device_get_ivars(child);
477 return (&(ivar->resources));
480 static device_method_t apb_methods[] = {
481 DEVMETHOD(bus_activate_resource, apb_activate_resource),
482 DEVMETHOD(bus_add_child, apb_add_child),
483 DEVMETHOD(bus_alloc_resource, apb_alloc_resource),
484 DEVMETHOD(bus_deactivate_resource, apb_deactivate_resource),
485 DEVMETHOD(bus_get_resource_list, apb_get_resource_list),
486 DEVMETHOD(bus_hinted_child, apb_hinted_child),
487 DEVMETHOD(bus_release_resource, apb_release_resource),
488 DEVMETHOD(bus_setup_intr, apb_setup_intr),
489 DEVMETHOD(bus_teardown_intr, apb_teardown_intr),
490 DEVMETHOD(device_attach, apb_attach),
491 DEVMETHOD(device_probe, apb_probe),
492 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
493 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
498 static driver_t apb_driver = {
501 sizeof(struct apb_softc),
503 static devclass_t apb_devclass;
505 DRIVER_MODULE(apb, nexus, apb_driver, apb_devclass, 0, 0);