2 * Copyright (c) 2016, Hiroki Mori
3 * Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice unmodified, this list of conditions, and the following
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include "opt_platform.h"
30 #include "opt_ar531x.h"
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
38 #include <sys/interrupt.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
42 #include <sys/malloc.h>
46 #include <sys/pmckern.h>
48 #include <machine/bus.h>
50 #include <machine/intr.h>
52 #include <machine/intr_machdep.h>
58 #define PIC_INTR_ISRC(sc, irq) (&(sc)->pic_irqs[(irq)].isrc)
61 #include <mips/atheros/ar531x/apbvar.h>
62 #include <mips/atheros/ar531x/ar5315reg.h>
63 #include <mips/atheros/ar531x/ar5312reg.h>
64 #include <mips/atheros/ar531x/ar5315_setup.h>
66 #ifdef AR531X_APB_DEBUG
67 #define dprintf printf
69 #define dprintf(x, arg...)
70 #endif /* AR531X_APB_DEBUG */
72 static int apb_activate_resource(device_t, device_t, int, int,
74 static device_t apb_add_child(device_t, u_int, const char *, int);
75 static struct resource *
76 apb_alloc_resource(device_t, device_t, int, int *, rman_res_t,
77 rman_res_t, rman_res_t, u_int);
78 static int apb_attach(device_t);
79 static int apb_deactivate_resource(device_t, device_t, int, int,
81 static struct resource_list *
82 apb_get_resource_list(device_t, device_t);
83 static void apb_hinted_child(device_t, const char *, int);
84 static int apb_filter(void *);
85 static int apb_probe(device_t);
86 static int apb_release_resource(device_t, device_t, int, int,
89 static int apb_setup_intr(device_t, device_t, struct resource *, int,
90 driver_filter_t *, driver_intr_t *, void *, void **);
91 static int apb_teardown_intr(device_t, device_t, struct resource *,
96 apb_mask_irq(void *source)
98 unsigned int irq = (unsigned int)source;
101 if(ar531x_soc >= AR531X_SOC_AR5315) {
102 reg = ATH_READ_REG(AR5315_SYSREG_BASE +
103 AR5315_SYSREG_MISC_INTMASK);
104 ATH_WRITE_REG(AR5315_SYSREG_BASE
105 + AR5315_SYSREG_MISC_INTMASK, reg & ~(1 << irq));
107 reg = ATH_READ_REG(AR5312_SYSREG_BASE +
108 AR5312_SYSREG_MISC_INTMASK);
109 ATH_WRITE_REG(AR5312_SYSREG_BASE
110 + AR5312_SYSREG_MISC_INTMASK, reg & ~(1 << irq));
115 apb_unmask_irq(void *source)
118 unsigned int irq = (unsigned int)source;
120 if(ar531x_soc >= AR531X_SOC_AR5315) {
121 reg = ATH_READ_REG(AR5315_SYSREG_BASE +
122 AR5315_SYSREG_MISC_INTMASK);
123 ATH_WRITE_REG(AR5315_SYSREG_BASE +
124 AR5315_SYSREG_MISC_INTMASK, reg | (1 << irq));
126 reg = ATH_READ_REG(AR5312_SYSREG_BASE +
127 AR5312_SYSREG_MISC_INTMASK);
128 ATH_WRITE_REG(AR5312_SYSREG_BASE +
129 AR5312_SYSREG_MISC_INTMASK, reg | (1 << irq));
135 apb_pic_register_isrcs(struct apb_softc *sc)
139 struct intr_irqsrc *isrc;
142 name = device_get_nameunit(sc->apb_dev);
143 for (irq = 0; irq < APB_NIRQS; irq++) {
144 sc->pic_irqs[irq].irq = irq;
145 isrc = PIC_INTR_ISRC(sc, irq);
146 error = intr_isrc_register(isrc, sc->apb_dev, 0, "%s", name);
148 /* XXX call intr_isrc_deregister */
149 device_printf(sc->apb_dev, "%s failed", __func__);
157 static inline intptr_t
158 pic_xref(device_t dev)
165 apb_probe(device_t dev)
168 device_set_desc(dev, "APB Bus bridge INTRNG");
170 device_set_desc(dev, "APB Bus bridge");
177 apb_attach(device_t dev)
179 struct apb_softc *sc = device_get_softc(dev);
181 intptr_t xref = pic_xref(dev);
189 sc->apb_mem_rman.rm_type = RMAN_ARRAY;
190 sc->apb_mem_rman.rm_descr = "APB memory window";
192 if(ar531x_soc >= AR531X_SOC_AR5315) {
193 if (rman_init(&sc->apb_mem_rman) != 0 ||
194 rman_manage_region(&sc->apb_mem_rman,
196 AR5315_APB_BASE + AR5315_APB_SIZE - 1) != 0)
197 panic("apb_attach: failed to set up memory rman");
199 if (rman_init(&sc->apb_mem_rman) != 0 ||
200 rman_manage_region(&sc->apb_mem_rman,
202 AR5312_APB_BASE + AR5312_APB_SIZE - 1) != 0)
203 panic("apb_attach: failed to set up memory rman");
206 sc->apb_irq_rman.rm_type = RMAN_ARRAY;
207 sc->apb_irq_rman.rm_descr = "APB IRQ";
209 if (rman_init(&sc->apb_irq_rman) != 0 ||
210 rman_manage_region(&sc->apb_irq_rman,
211 APB_IRQ_BASE, APB_IRQ_END) != 0)
212 panic("apb_attach: failed to set up IRQ rman");
215 if ((sc->sc_misc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
216 RF_SHAREABLE | RF_ACTIVE)) == NULL) {
217 device_printf(dev, "unable to allocate IRQ resource\n");
221 if ((bus_setup_intr(dev, sc->sc_misc_irq, INTR_TYPE_MISC,
222 apb_filter, NULL, sc, &sc->sc_misc_ih))) {
224 "WARNING: unable to register interrupt handler\n");
228 /* Register the interrupts */
229 if (apb_pic_register_isrcs(sc) != 0) {
230 device_printf(dev, "could not register PIC ISRCs\n");
235 * Now, when everything is initialized, it's right time to
236 * register interrupt controller to interrupt framefork.
238 if (intr_pic_register(dev, xref) == NULL) {
239 device_printf(dev, "could not register PIC\n");
243 if(ar531x_soc >= AR531X_SOC_AR5315) {
244 miscirq = AR5315_CPU_IRQ_MISC;
246 miscirq = AR5312_IRQ_MISC;
248 cpu_establish_hardintr("aric", apb_filter, NULL, sc, miscirq,
249 INTR_TYPE_MISC, NULL);
252 /* mask all misc interrupt */
253 if(ar531x_soc >= AR531X_SOC_AR5315) {
254 ATH_WRITE_REG(AR5315_SYSREG_BASE
255 + AR5315_SYSREG_MISC_INTMASK, 0);
257 ATH_WRITE_REG(AR5312_SYSREG_BASE
258 + AR5312_SYSREG_MISC_INTMASK, 0);
261 bus_generic_probe(dev);
262 bus_enumerate_hinted_children(dev);
263 bus_generic_attach(dev);
268 static struct resource *
269 apb_alloc_resource(device_t bus, device_t child, int type, int *rid,
270 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
272 struct apb_softc *sc = device_get_softc(bus);
273 struct apb_ivar *ivar = device_get_ivars(child);
275 struct resource_list_entry *rle;
277 int isdefault, needactivate, passthrough;
279 isdefault = (RMAN_IS_DEFAULT_RANGE(start, end));
280 needactivate = flags & RF_ACTIVE;
282 * Pass memory requests to nexus device
284 passthrough = (device_get_parent(child) != bus);
287 dprintf("%s: entry (%p, %p, %d, %d, %p, %p, %jd, %d)\n",
288 __func__, bus, child, type, *rid, (void *)(intptr_t)start,
289 (void *)(intptr_t)end, count, flags);
292 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type,
293 rid, start, end, count, flags));
296 * If this is an allocation of the "default" range for a given RID,
297 * and we know what the resources for this device are (ie. they aren't
298 * maintained by a child bus), then work out the start/end values.
302 rle = resource_list_find(&ivar->resources, type, *rid);
307 if (rle->res != NULL) {
308 panic("%s: resource entry is busy", __func__);
314 dprintf("%s: default resource (%p, %p, %jd)\n",
315 __func__, (void *)(intptr_t)start,
316 (void *)(intptr_t)end, count);
321 rm = &sc->apb_irq_rman;
324 rm = &sc->apb_mem_rman;
327 printf("%s: unknown resource type %d\n", __func__, type);
331 rv = rman_reserve_resource(rm, start, end, count, flags, child);
333 printf("%s: could not reserve resource %d\n", __func__, type);
337 rman_set_rid(rv, *rid);
340 if (bus_activate_resource(child, type, *rid, rv)) {
341 printf("%s: could not activate resource\n", __func__);
342 rman_release_resource(rv);
351 apb_activate_resource(device_t bus, device_t child, int type, int rid,
355 /* XXX: should we mask/unmask IRQ here? */
356 return (BUS_ACTIVATE_RESOURCE(device_get_parent(bus), child,
361 apb_deactivate_resource(device_t bus, device_t child, int type, int rid,
365 /* XXX: should we mask/unmask IRQ here? */
366 return (BUS_DEACTIVATE_RESOURCE(device_get_parent(bus), child,
371 apb_release_resource(device_t dev, device_t child, int type,
372 int rid, struct resource *r)
374 struct resource_list *rl;
375 struct resource_list_entry *rle;
377 rl = apb_get_resource_list(dev, child);
380 rle = resource_list_find(rl, type, rid);
383 rman_release_resource(r);
391 apb_setup_intr(device_t bus, device_t child, struct resource *ires,
392 int flags, driver_filter_t *filt, driver_intr_t *handler,
393 void *arg, void **cookiep)
395 struct apb_softc *sc = device_get_softc(bus);
399 struct intr_event *event;
403 struct intr_irqsrc *isrc;
406 if ((rman_get_flags(ires) & RF_SHAREABLE) == 0)
409 irq = rman_get_start(ires);
410 isrc = PIC_INTR_ISRC(sc, irq);
411 if(isrc->isrc_event == 0) {
412 error = intr_event_create(&isrc->isrc_event, (void *)irq,
413 0, irq, apb_mask_irq, apb_unmask_irq,
414 NULL, NULL, "apb intr%d:", irq);
418 name = device_get_nameunit(child);
419 error = intr_event_add_handler(isrc->isrc_event, name, filt, handler,
420 arg, intr_priority(flags), flags, cookiep);
423 irq = rman_get_start(ires);
425 if (irq > APB_IRQ_END)
426 panic("%s: bad irq %d", __func__, irq);
428 event = sc->sc_eventstab[irq];
430 error = intr_event_create(&event, (void *)irq, 0, irq,
431 apb_mask_irq, apb_unmask_irq,
436 sc->sc_eventstab[irq] = event;
437 sc->sc_intr_counter[irq] =
438 mips_intrcnt_create(event->ie_name);
444 intr_event_add_handler(event, device_get_nameunit(child), filt,
445 handler, arg, intr_priority(flags), flags, cookiep);
446 mips_intrcnt_setname(sc->sc_intr_counter[irq], event->ie_fullname);
448 apb_unmask_irq((void*)irq);
456 apb_teardown_intr(device_t dev, device_t child, struct resource *ires,
460 return (intr_teardown_irq(child, ires, cookie));
462 struct apb_softc *sc = device_get_softc(dev);
465 irq = rman_get_start(ires);
466 if (irq > APB_IRQ_END)
467 panic("%s: bad irq %d", __func__, irq);
469 if (sc->sc_eventstab[irq] == NULL)
470 panic("Trying to teardown unoccupied IRQ");
472 apb_mask_irq((void*)irq);
474 result = intr_event_remove_handler(cookie);
476 sc->sc_eventstab[irq] = NULL;
484 apb_filter(void *arg)
486 struct apb_softc *sc = arg;
487 struct intr_event *event;
490 if(ar531x_soc >= AR531X_SOC_AR5315)
491 reg = ATH_READ_REG(AR5315_SYSREG_BASE +
492 AR5315_SYSREG_MISC_INTSTAT);
494 reg = ATH_READ_REG(AR5312_SYSREG_BASE +
495 AR5312_SYSREG_MISC_INTSTAT);
497 for (irq = 0; irq < APB_NIRQS; irq++) {
498 if (reg & (1 << irq)) {
500 if(ar531x_soc >= AR531X_SOC_AR5315) {
501 ATH_WRITE_REG(AR5315_SYSREG_BASE +
502 AR5315_SYSREG_MISC_INTSTAT,
505 ATH_WRITE_REG(AR5312_SYSREG_BASE +
506 AR5312_SYSREG_MISC_INTSTAT,
510 event = sc->sc_eventstab[irq];
511 if (!event || CK_SLIST_EMPTY(&event->ie_handlers)) {
512 if(irq == 1 && ar531x_soc < AR531X_SOC_AR5315) {
513 ATH_READ_REG(AR5312_SYSREG_BASE +
514 AR5312_SYSREG_AHBPERR);
515 ATH_READ_REG(AR5312_SYSREG_BASE +
516 AR5312_SYSREG_AHBDMAE);
518 /* Ignore non handle interrupts */
519 if (irq != 0 && irq != 6)
520 printf("Stray APB IRQ %d\n", irq);
525 intr_event_handle(event, PCPU_GET(curthread)->td_intr_frame);
526 mips_intrcnt_inc(sc->sc_intr_counter[irq]);
530 return (FILTER_HANDLED);
534 apb_filter(void *arg)
536 struct apb_softc *sc = arg;
541 /* Workaround: do not inflate intr nesting level */
542 td->td_intr_nesting_level--;
544 if(ar531x_soc >= AR531X_SOC_AR5315)
545 intr = ATH_READ_REG(AR5315_SYSREG_BASE +
546 AR5315_SYSREG_MISC_INTSTAT);
548 intr = ATH_READ_REG(AR5312_SYSREG_BASE +
549 AR5312_SYSREG_MISC_INTSTAT);
551 while ((i = fls(intr)) != 0) {
555 if(i == 1 && ar531x_soc < AR531X_SOC_AR5315) {
556 ATH_READ_REG(AR5312_SYSREG_BASE +
557 AR5312_SYSREG_AHBPERR);
558 ATH_READ_REG(AR5312_SYSREG_BASE +
559 AR5312_SYSREG_AHBDMAE);
562 if (intr_isrc_dispatch(PIC_INTR_ISRC(sc, i),
563 curthread->td_intr_frame) != 0) {
564 device_printf(sc->apb_dev,
565 "Stray interrupt %u detected\n", i);
566 apb_mask_irq((void*)i);
571 KASSERT(i == 0, ("all interrupts handled"));
573 td->td_intr_nesting_level++;
575 return (FILTER_HANDLED);
582 apb_hinted_child(device_t bus, const char *dname, int dunit)
591 child = BUS_ADD_CHILD(bus, 0, dname, dunit);
594 * Set hard-wired resources for hinted child using
598 if (resource_long_value(dname, dunit, "maddr", &maddr) == 0)
600 if (resource_int_value(dname, dunit, "msize", &msize) == 0)
603 /* check if all info for mem resource has been provided */
604 if ((mem_hints_count > 0) && (mem_hints_count < 2)) {
605 printf("Either maddr or msize hint is missing for %s%d\n",
607 } else if (mem_hints_count) {
608 result = bus_set_resource(child, SYS_RES_MEMORY, 0,
612 "warning: bus_set_resource() failed\n");
615 if (resource_int_value(dname, dunit, "irq", &irq) == 0) {
616 result = bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
619 "warning: bus_set_resource() failed\n");
624 apb_add_child(device_t bus, u_int order, const char *name, int unit)
627 struct apb_ivar *ivar;
629 ivar = malloc(sizeof(struct apb_ivar), M_DEVBUF, M_WAITOK | M_ZERO);
631 printf("Failed to allocate ivar\n");
634 resource_list_init(&ivar->resources);
636 child = device_add_child_ordered(bus, order, name, unit);
638 printf("Can't add child %s%d ordered\n", name, unit);
642 device_set_ivars(child, ivar);
648 * Helper routine for bus_generic_rl_get_resource/bus_generic_rl_set_resource
649 * Provides pointer to resource_list for these routines
651 static struct resource_list *
652 apb_get_resource_list(device_t dev, device_t child)
654 struct apb_ivar *ivar;
656 ivar = device_get_ivars(child);
657 return (&(ivar->resources));
662 apb_pic_enable_intr(device_t dev, struct intr_irqsrc *isrc)
666 irq = ((struct apb_pic_irqsrc *)isrc)->irq;
667 apb_unmask_irq((void*)irq);
671 apb_pic_disable_intr(device_t dev, struct intr_irqsrc *isrc)
675 irq = ((struct apb_pic_irqsrc *)isrc)->irq;
676 apb_mask_irq((void*)irq);
680 apb_pic_pre_ithread(device_t dev, struct intr_irqsrc *isrc)
682 apb_pic_disable_intr(dev, isrc);
686 apb_pic_post_ithread(device_t dev, struct intr_irqsrc *isrc)
688 apb_pic_enable_intr(dev, isrc);
692 apb_pic_post_filter(device_t dev, struct intr_irqsrc *isrc)
696 irq = ((struct apb_pic_irqsrc *)isrc)->irq;
697 if(ar531x_soc >= AR531X_SOC_AR5315) {
698 reg = ATH_READ_REG(AR5315_SYSREG_BASE +
699 AR5315_SYSREG_MISC_INTSTAT);
700 ATH_WRITE_REG(AR5315_SYSREG_BASE + AR5315_SYSREG_MISC_INTSTAT,
703 reg = ATH_READ_REG(AR5312_SYSREG_BASE +
704 AR5312_SYSREG_MISC_INTSTAT);
705 ATH_WRITE_REG(AR5312_SYSREG_BASE + AR5312_SYSREG_MISC_INTSTAT,
711 apb_pic_map_intr(device_t dev, struct intr_map_data *data,
712 struct intr_irqsrc **isrcp)
719 static device_method_t apb_methods[] = {
720 DEVMETHOD(bus_activate_resource, apb_activate_resource),
721 DEVMETHOD(bus_add_child, apb_add_child),
722 DEVMETHOD(bus_alloc_resource, apb_alloc_resource),
723 DEVMETHOD(bus_deactivate_resource, apb_deactivate_resource),
724 DEVMETHOD(bus_get_resource_list, apb_get_resource_list),
725 DEVMETHOD(bus_hinted_child, apb_hinted_child),
726 DEVMETHOD(bus_release_resource, apb_release_resource),
727 DEVMETHOD(device_attach, apb_attach),
728 DEVMETHOD(device_probe, apb_probe),
729 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
730 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
732 DEVMETHOD(pic_disable_intr, apb_pic_disable_intr),
733 DEVMETHOD(pic_enable_intr, apb_pic_enable_intr),
734 DEVMETHOD(pic_map_intr, apb_pic_map_intr),
735 DEVMETHOD(pic_post_filter, apb_pic_post_filter),
736 DEVMETHOD(pic_post_ithread, apb_pic_post_ithread),
737 DEVMETHOD(pic_pre_ithread, apb_pic_pre_ithread),
739 // DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
741 DEVMETHOD(bus_teardown_intr, apb_teardown_intr),
743 DEVMETHOD(bus_setup_intr, apb_setup_intr),
748 static driver_t apb_driver = {
751 sizeof(struct apb_softc),
753 static devclass_t apb_devclass;
755 EARLY_DRIVER_MODULE(apb, nexus, apb_driver, apb_devclass, 0, 0,
756 BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);