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 #define DEVTOAPB(dev) ((struct apb_ivar *) device_get_ivars(dev))
62 static int apb_activate_resource(device_t, device_t, int, int,
64 static device_t apb_add_child(device_t, u_int, const char *, int);
65 static struct resource *
66 apb_alloc_resource(device_t, device_t, int, int *, u_long,
67 u_long, u_long, u_int);
68 static int apb_attach(device_t);
69 static int apb_deactivate_resource(device_t, device_t, int, int,
71 static struct resource_list *
72 apb_get_resource_list(device_t, device_t);
73 static void apb_hinted_child(device_t, const char *, int);
74 static int apb_filter(void *);
75 static int apb_probe(device_t);
76 static int apb_release_resource(device_t, device_t, int, int,
78 static int apb_setup_intr(device_t, device_t, struct resource *, int,
79 driver_filter_t *, driver_intr_t *, void *, void **);
80 static int apb_teardown_intr(device_t, device_t, struct resource *,
84 apb_mask_irq(void *source)
86 unsigned int irq = (unsigned int)source;
89 reg = ATH_READ_REG(AR71XX_MISC_INTR_MASK);
90 ATH_WRITE_REG(AR71XX_MISC_INTR_MASK, reg & ~(1 << irq));
95 apb_unmask_irq(void *source)
98 unsigned int irq = (unsigned int)source;
100 reg = ATH_READ_REG(AR71XX_MISC_INTR_MASK);
101 ATH_WRITE_REG(AR71XX_MISC_INTR_MASK, reg | (1 << irq));
105 apb_probe(device_t dev)
108 return (BUS_PROBE_NOWILDCARD);
112 apb_attach(device_t dev)
114 struct apb_softc *sc = device_get_softc(dev);
117 device_set_desc(dev, "APB Bus bridge");
119 sc->apb_mem_rman.rm_type = RMAN_ARRAY;
120 sc->apb_mem_rman.rm_descr = "APB memory window";
122 if (rman_init(&sc->apb_mem_rman) != 0 ||
123 rman_manage_region(&sc->apb_mem_rman,
125 AR71XX_APB_BASE + AR71XX_APB_SIZE - 1) != 0)
126 panic("apb_attach: failed to set up memory rman");
128 sc->apb_irq_rman.rm_type = RMAN_ARRAY;
129 sc->apb_irq_rman.rm_descr = "APB IRQ";
131 if (rman_init(&sc->apb_irq_rman) != 0 ||
132 rman_manage_region(&sc->apb_irq_rman,
133 APB_IRQ_BASE, APB_IRQ_END) != 0)
134 panic("apb_attach: failed to set up IRQ rman");
136 if ((sc->sc_misc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
137 RF_SHAREABLE | RF_ACTIVE)) == NULL) {
138 device_printf(dev, "unable to allocate IRQ resource\n");
142 if ((bus_setup_intr(dev, sc->sc_misc_irq, INTR_TYPE_MISC,
143 apb_filter, NULL, sc, &sc->sc_misc_ih))) {
145 "WARNING: unable to register interrupt handler\n");
149 bus_generic_probe(dev);
150 bus_enumerate_hinted_children(dev);
151 bus_generic_attach(dev);
154 * Unmask performance counter IRQ
156 apb_unmask_irq((void*)APB_INTR_PMC);
157 sc->sc_intr_counter[APB_INTR_PMC] = mips_intrcnt_create("apb irq5: pmc");
162 static struct resource *
163 apb_alloc_resource(device_t bus, device_t child, int type, int *rid,
164 u_long start, u_long end, u_long count, u_int flags)
166 struct apb_softc *sc = device_get_softc(bus);
167 struct apb_ivar *ivar = device_get_ivars(child);
169 struct resource_list_entry *rle;
171 int isdefault, needactivate, passthrough;
173 isdefault = (start == 0UL && end == ~0UL);
174 needactivate = flags & RF_ACTIVE;
176 * Pass memory requests to nexus device
178 passthrough = (device_get_parent(child) != bus);
181 dprintf("%s: entry (%p, %p, %d, %d, %p, %p, %ld, %d)\n",
182 __func__, bus, child, type, *rid, (void *)(intptr_t)start,
183 (void *)(intptr_t)end, count, flags);
186 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type,
187 rid, start, end, count, flags));
190 * If this is an allocation of the "default" range for a given RID,
191 * and we know what the resources for this device are (ie. they aren't
192 * maintained by a child bus), then work out the start/end values.
196 rle = resource_list_find(&ivar->resources, type, *rid);
201 if (rle->res != NULL) {
202 panic("%s: resource entry is busy", __func__);
208 dprintf("%s: default resource (%p, %p, %ld)\n",
209 __func__, (void *)(intptr_t)start,
210 (void *)(intptr_t)end, count);
215 rm = &sc->apb_irq_rman;
218 rm = &sc->apb_mem_rman;
221 printf("%s: unknown resource type %d\n", __func__, type);
225 rv = rman_reserve_resource(rm, start, end, count, flags, child);
227 printf("%s: could not reserve resource\n", __func__);
231 rman_set_rid(rv, *rid);
234 if (bus_activate_resource(child, type, *rid, rv)) {
235 printf("%s: could not activate resource\n", __func__);
236 rman_release_resource(rv);
245 apb_activate_resource(device_t bus, device_t child, int type, int rid,
249 /* XXX: should we mask/unmask IRQ here? */
250 return (BUS_ACTIVATE_RESOURCE(device_get_parent(bus), child,
255 apb_deactivate_resource(device_t bus, device_t child, int type, int rid,
259 /* XXX: should we mask/unmask IRQ here? */
260 return (BUS_DEACTIVATE_RESOURCE(device_get_parent(bus), child,
265 apb_release_resource(device_t dev, device_t child, int type,
266 int rid, struct resource *r)
268 struct resource_list *rl;
269 struct resource_list_entry *rle;
271 rl = apb_get_resource_list(dev, child);
274 rle = resource_list_find(rl, type, rid);
277 rman_release_resource(r);
284 apb_setup_intr(device_t bus, device_t child, struct resource *ires,
285 int flags, driver_filter_t *filt, driver_intr_t *handler,
286 void *arg, void **cookiep)
288 struct apb_softc *sc = device_get_softc(bus);
289 struct intr_event *event;
292 irq = rman_get_start(ires);
294 if (irq > APB_IRQ_END)
295 panic("%s: bad irq %d", __func__, irq);
297 event = sc->sc_eventstab[irq];
299 error = intr_event_create(&event, (void *)irq, 0, irq,
300 apb_mask_irq, apb_unmask_irq,
305 sc->sc_eventstab[irq] = event;
306 sc->sc_intr_counter[irq] =
307 mips_intrcnt_create(event->ie_name);
313 intr_event_add_handler(event, device_get_nameunit(child), filt,
314 handler, arg, intr_priority(flags), flags, cookiep);
315 mips_intrcnt_setname(sc->sc_intr_counter[irq], event->ie_fullname);
317 apb_unmask_irq((void*)irq);
323 apb_teardown_intr(device_t dev, device_t child, struct resource *ires,
326 struct apb_softc *sc = device_get_softc(dev);
329 irq = rman_get_start(ires);
330 if (irq > APB_IRQ_END)
331 panic("%s: bad irq %d", __func__, irq);
333 if (sc->sc_eventstab[irq] == NULL)
334 panic("Trying to teardown unoccupied IRQ");
336 apb_mask_irq((void*)irq);
338 result = intr_event_remove_handler(cookie);
340 sc->sc_eventstab[irq] = NULL;
346 apb_filter(void *arg)
348 struct apb_softc *sc = arg;
349 struct intr_event *event;
352 struct trapframe *tf;
354 reg = ATH_READ_REG(AR71XX_MISC_INTR_STATUS);
355 for (irq = 0; irq < APB_NIRQS; irq++) {
356 if (reg & (1 << irq)) {
358 switch (ar71xx_soc) {
359 case AR71XX_SOC_AR7240:
360 case AR71XX_SOC_AR7241:
361 case AR71XX_SOC_AR7242:
362 case AR71XX_SOC_AR9330:
363 case AR71XX_SOC_AR9331:
364 case AR71XX_SOC_AR9341:
365 case AR71XX_SOC_AR9342:
366 case AR71XX_SOC_AR9344:
367 /* ACK/clear the given interrupt */
368 ATH_WRITE_REG(AR71XX_MISC_INTR_STATUS,
376 event = sc->sc_eventstab[irq];
377 if (!event || TAILQ_EMPTY(&event->ie_handlers)) {
378 if (irq == APB_INTR_PMC) {
379 td = PCPU_GET(curthread);
380 tf = td->td_intr_frame;
383 (*pmc_intr)(PCPU_GET(cpuid), tf);
385 mips_intrcnt_inc(sc->sc_intr_counter[irq]);
389 /* Ignore timer interrupts */
390 if (irq != 0 && irq != 8 && irq != 9 && irq != 10)
391 printf("Stray APB IRQ %d\n", irq);
395 intr_event_handle(event, PCPU_GET(curthread)->td_intr_frame);
396 mips_intrcnt_inc(sc->sc_intr_counter[irq]);
400 return (FILTER_HANDLED);
404 apb_hinted_child(device_t bus, const char *dname, int dunit)
413 child = BUS_ADD_CHILD(bus, 0, dname, dunit);
416 * Set hard-wired resources for hinted child using
420 if (resource_long_value(dname, dunit, "maddr", &maddr) == 0)
422 if (resource_int_value(dname, dunit, "msize", &msize) == 0)
425 /* check if all info for mem resource has been provided */
426 if ((mem_hints_count > 0) && (mem_hints_count < 2)) {
427 printf("Either maddr or msize hint is missing for %s%d\n",
429 } else if (mem_hints_count) {
430 result = bus_set_resource(child, SYS_RES_MEMORY, 0,
434 "warning: bus_set_resource() failed\n");
437 if (resource_int_value(dname, dunit, "irq", &irq) == 0) {
438 result = bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
441 "warning: bus_set_resource() failed\n");
446 apb_add_child(device_t bus, u_int order, const char *name, int unit)
449 struct apb_ivar *ivar;
451 ivar = malloc(sizeof(struct apb_ivar), M_DEVBUF, M_WAITOK | M_ZERO);
453 printf("Failed to allocate ivar\n");
456 resource_list_init(&ivar->resources);
458 child = device_add_child_ordered(bus, order, name, unit);
460 printf("Can't add child %s%d ordered\n", name, unit);
464 device_set_ivars(child, ivar);
470 * Helper routine for bus_generic_rl_get_resource/bus_generic_rl_set_resource
471 * Provides pointer to resource_list for these routines
473 static struct resource_list *
474 apb_get_resource_list(device_t dev, device_t child)
476 struct apb_ivar *ivar;
478 ivar = device_get_ivars(child);
479 return (&(ivar->resources));
483 apb_print_all_resources(device_t dev)
485 struct apb_ivar *ndev = DEVTOAPB(dev);
486 struct resource_list *rl = &ndev->resources;
489 if (STAILQ_FIRST(rl))
490 retval += printf(" at");
492 retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
493 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
499 apb_print_child(device_t bus, device_t child)
503 retval += bus_print_child_header(bus, child);
504 retval += apb_print_all_resources(child);
505 if (device_get_flags(child))
506 retval += printf(" flags %#x", device_get_flags(child));
507 retval += printf(" on %s\n", device_get_nameunit(bus));
513 static device_method_t apb_methods[] = {
514 DEVMETHOD(bus_activate_resource, apb_activate_resource),
515 DEVMETHOD(bus_add_child, apb_add_child),
516 DEVMETHOD(bus_alloc_resource, apb_alloc_resource),
517 DEVMETHOD(bus_deactivate_resource, apb_deactivate_resource),
518 DEVMETHOD(bus_get_resource_list, apb_get_resource_list),
519 DEVMETHOD(bus_hinted_child, apb_hinted_child),
520 DEVMETHOD(bus_release_resource, apb_release_resource),
521 DEVMETHOD(bus_setup_intr, apb_setup_intr),
522 DEVMETHOD(bus_teardown_intr, apb_teardown_intr),
523 DEVMETHOD(device_attach, apb_attach),
524 DEVMETHOD(device_probe, apb_probe),
525 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
526 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
527 DEVMETHOD(bus_print_child, apb_print_child),
532 static driver_t apb_driver = {
535 sizeof(struct apb_softc),
537 static devclass_t apb_devclass;
539 DRIVER_MODULE(apb, nexus, apb_driver, apb_devclass, 0, 0);