2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/interrupt.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
40 #include <sys/malloc.h>
44 #include <sys/pmckern.h>
46 #include <machine/bus.h>
47 #include <machine/intr_machdep.h>
49 #include <mips/atheros/apbvar.h>
50 #include <mips/atheros/ar71xxreg.h>
51 #include <mips/atheros/ar71xx_setup.h>
53 #define APB_INTR_PMC 5
57 #define dprintf printf
59 #define dprintf(x, arg...)
60 #endif /* APB_DEBUG */
62 #define DEVTOAPB(dev) ((struct apb_ivar *) device_get_ivars(dev))
64 static int apb_activate_resource(device_t, device_t, int, int,
66 static device_t apb_add_child(device_t, u_int, const char *, int);
67 static struct resource *
68 apb_alloc_resource(device_t, device_t, int, int *, rman_res_t,
69 rman_res_t, rman_res_t, u_int);
70 static int apb_attach(device_t);
71 static int apb_deactivate_resource(device_t, device_t, int, int,
73 static struct resource_list *
74 apb_get_resource_list(device_t, device_t);
75 static void apb_hinted_child(device_t, const char *, int);
76 static int apb_filter(void *);
77 static int apb_probe(device_t);
78 static int apb_release_resource(device_t, device_t, int, int,
80 static int apb_setup_intr(device_t, device_t, struct resource *, int,
81 driver_filter_t *, driver_intr_t *, void *, void **);
82 static int apb_teardown_intr(device_t, device_t, struct resource *,
86 apb_mask_irq(void *source)
88 unsigned int irq = (unsigned int)source;
91 reg = ATH_READ_REG(AR71XX_MISC_INTR_MASK);
92 ATH_WRITE_REG(AR71XX_MISC_INTR_MASK, reg & ~(1 << irq));
97 apb_unmask_irq(void *source)
100 unsigned int irq = (unsigned int)source;
102 reg = ATH_READ_REG(AR71XX_MISC_INTR_MASK);
103 ATH_WRITE_REG(AR71XX_MISC_INTR_MASK, reg | (1 << irq));
107 apb_probe(device_t dev)
110 return (BUS_PROBE_NOWILDCARD);
114 apb_attach(device_t dev)
116 struct apb_softc *sc = device_get_softc(dev);
119 device_set_desc(dev, "APB Bus bridge");
121 sc->apb_mem_rman.rm_type = RMAN_ARRAY;
122 sc->apb_mem_rman.rm_descr = "APB memory window";
124 if (rman_init(&sc->apb_mem_rman) != 0 ||
125 rman_manage_region(&sc->apb_mem_rman,
127 AR71XX_APB_BASE + AR71XX_APB_SIZE - 1) != 0)
128 panic("apb_attach: failed to set up memory rman");
130 sc->apb_irq_rman.rm_type = RMAN_ARRAY;
131 sc->apb_irq_rman.rm_descr = "APB IRQ";
133 if (rman_init(&sc->apb_irq_rman) != 0 ||
134 rman_manage_region(&sc->apb_irq_rman,
135 APB_IRQ_BASE, APB_IRQ_END) != 0)
136 panic("apb_attach: failed to set up IRQ rman");
138 if ((sc->sc_misc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
139 RF_SHAREABLE | RF_ACTIVE)) == NULL) {
140 device_printf(dev, "unable to allocate IRQ resource\n");
144 if ((bus_setup_intr(dev, sc->sc_misc_irq, INTR_TYPE_MISC,
145 apb_filter, NULL, sc, &sc->sc_misc_ih))) {
147 "WARNING: unable to register interrupt handler\n");
151 bus_generic_probe(dev);
152 bus_enumerate_hinted_children(dev);
153 bus_generic_attach(dev);
156 * Unmask performance counter IRQ
158 apb_unmask_irq((void*)APB_INTR_PMC);
159 sc->sc_intr_counter[APB_INTR_PMC] = mips_intrcnt_create("apb irq5: pmc");
164 static struct resource *
165 apb_alloc_resource(device_t bus, device_t child, int type, int *rid,
166 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
168 struct apb_softc *sc = device_get_softc(bus);
169 struct apb_ivar *ivar = device_get_ivars(child);
171 struct resource_list_entry *rle;
173 int isdefault, needactivate, passthrough;
175 isdefault = (RMAN_IS_DEFAULT_RANGE(start, end));
176 needactivate = flags & RF_ACTIVE;
178 * Pass memory requests to nexus device
180 passthrough = (device_get_parent(child) != bus);
183 dprintf("%s: entry (%p, %p, %d, %d, %p, %p, %jd, %d)\n",
184 __func__, bus, child, type, *rid, (void *)(intptr_t)start,
185 (void *)(intptr_t)end, count, flags);
188 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type,
189 rid, start, end, count, flags));
192 * If this is an allocation of the "default" range for a given RID,
193 * and we know what the resources for this device are (ie. they aren't
194 * maintained by a child bus), then work out the start/end values.
198 rle = resource_list_find(&ivar->resources, type, *rid);
203 if (rle->res != NULL) {
204 panic("%s: resource entry is busy", __func__);
210 dprintf("%s: default resource (%p, %p, %ld)\n",
211 __func__, (void *)(intptr_t)start,
212 (void *)(intptr_t)end, count);
217 rm = &sc->apb_irq_rman;
220 rm = &sc->apb_mem_rman;
223 printf("%s: unknown resource type %d\n", __func__, type);
227 rv = rman_reserve_resource(rm, start, end, count, flags, child);
229 printf("%s: could not reserve resource\n", __func__);
233 rman_set_rid(rv, *rid);
236 if (bus_activate_resource(child, type, *rid, rv)) {
237 printf("%s: could not activate resource\n", __func__);
238 rman_release_resource(rv);
247 apb_activate_resource(device_t bus, device_t child, int type, int rid,
251 /* XXX: should we mask/unmask IRQ here? */
252 return (BUS_ACTIVATE_RESOURCE(device_get_parent(bus), child,
257 apb_deactivate_resource(device_t bus, device_t child, int type, int rid,
261 /* XXX: should we mask/unmask IRQ here? */
262 return (BUS_DEACTIVATE_RESOURCE(device_get_parent(bus), child,
267 apb_release_resource(device_t dev, device_t child, int type,
268 int rid, struct resource *r)
270 struct resource_list *rl;
271 struct resource_list_entry *rle;
273 rl = apb_get_resource_list(dev, child);
276 rle = resource_list_find(rl, type, rid);
279 rman_release_resource(r);
286 apb_setup_intr(device_t bus, device_t child, struct resource *ires,
287 int flags, driver_filter_t *filt, driver_intr_t *handler,
288 void *arg, void **cookiep)
290 struct apb_softc *sc = device_get_softc(bus);
291 struct intr_event *event;
294 irq = rman_get_start(ires);
296 if (irq > APB_IRQ_END)
297 panic("%s: bad irq %d", __func__, irq);
299 event = sc->sc_eventstab[irq];
301 error = intr_event_create(&event, (void *)irq, 0, irq,
302 apb_mask_irq, apb_unmask_irq,
307 sc->sc_eventstab[irq] = event;
308 sc->sc_intr_counter[irq] =
309 mips_intrcnt_create(event->ie_name);
315 intr_event_add_handler(event, device_get_nameunit(child), filt,
316 handler, arg, intr_priority(flags), flags, cookiep);
317 mips_intrcnt_setname(sc->sc_intr_counter[irq], event->ie_fullname);
319 apb_unmask_irq((void*)irq);
325 apb_teardown_intr(device_t dev, device_t child, struct resource *ires,
328 struct apb_softc *sc = device_get_softc(dev);
331 irq = rman_get_start(ires);
332 if (irq > APB_IRQ_END)
333 panic("%s: bad irq %d", __func__, irq);
335 if (sc->sc_eventstab[irq] == NULL)
336 panic("Trying to teardown unoccupied IRQ");
338 apb_mask_irq((void*)irq);
340 result = intr_event_remove_handler(cookie);
342 sc->sc_eventstab[irq] = NULL;
348 apb_filter(void *arg)
350 struct apb_softc *sc = arg;
351 struct intr_event *event;
354 struct trapframe *tf;
356 reg = ATH_READ_REG(AR71XX_MISC_INTR_STATUS);
357 for (irq = 0; irq < APB_NIRQS; irq++) {
358 if (reg & (1 << irq)) {
360 switch (ar71xx_soc) {
361 case AR71XX_SOC_AR7240:
362 case AR71XX_SOC_AR7241:
363 case AR71XX_SOC_AR7242:
364 case AR71XX_SOC_AR9330:
365 case AR71XX_SOC_AR9331:
366 case AR71XX_SOC_AR9341:
367 case AR71XX_SOC_AR9342:
368 case AR71XX_SOC_AR9344:
369 case AR71XX_SOC_QCA9533:
370 case AR71XX_SOC_QCA9533_V2:
371 case AR71XX_SOC_QCA9556:
372 case AR71XX_SOC_QCA9558:
373 /* ACK/clear the given interrupt */
374 ATH_WRITE_REG(AR71XX_MISC_INTR_STATUS,
382 event = sc->sc_eventstab[irq];
383 /* always count interrupts; spurious or otherwise */
384 mips_intrcnt_inc(sc->sc_intr_counter[irq]);
385 if (!event || TAILQ_EMPTY(&event->ie_handlers)) {
386 if (irq == APB_INTR_PMC) {
387 td = PCPU_GET(curthread);
388 tf = td->td_intr_frame;
391 (*pmc_intr)(PCPU_GET(cpuid), tf);
394 /* Ignore timer interrupts */
395 if (irq != 0 && irq != 8 && irq != 9 && irq != 10)
396 printf("Stray APB IRQ %d\n", irq);
400 intr_event_handle(event, PCPU_GET(curthread)->td_intr_frame);
404 return (FILTER_HANDLED);
408 apb_hinted_child(device_t bus, const char *dname, int dunit)
417 child = BUS_ADD_CHILD(bus, 0, dname, dunit);
420 * Set hard-wired resources for hinted child using
424 if (resource_long_value(dname, dunit, "maddr", &maddr) == 0)
426 if (resource_int_value(dname, dunit, "msize", &msize) == 0)
429 /* check if all info for mem resource has been provided */
430 if ((mem_hints_count > 0) && (mem_hints_count < 2)) {
431 printf("Either maddr or msize hint is missing for %s%d\n",
433 } else if (mem_hints_count) {
434 result = bus_set_resource(child, SYS_RES_MEMORY, 0,
438 "warning: bus_set_resource() failed\n");
441 if (resource_int_value(dname, dunit, "irq", &irq) == 0) {
442 result = bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
445 "warning: bus_set_resource() failed\n");
450 apb_add_child(device_t bus, u_int order, const char *name, int unit)
453 struct apb_ivar *ivar;
455 ivar = malloc(sizeof(struct apb_ivar), M_DEVBUF, M_WAITOK | M_ZERO);
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, "%#jx");
493 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd");
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);