jcs's openbsd hax
openbsd
at jcs 145 lines 3.8 kB view raw
1/* $OpenBSD: mtintc.c,v 1.1 2025/01/30 00:26:44 hastings Exp $ */ 2/* 3 * Copyright (c) 2025 James Hastings <hastings@openbsd.org> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18#include <sys/param.h> 19#include <sys/device.h> 20#include <sys/malloc.h> 21#include <sys/systm.h> 22 23#include <machine/bus.h> 24#include <machine/fdt.h> 25#include <machine/intr.h> 26 27#include <dev/ofw/openfirm.h> 28#include <dev/ofw/fdt.h> 29 30struct mtintc_softc { 31 struct device sc_dev; 32 bus_space_tag_t sc_iot; 33 bus_space_handle_t sc_ioh; 34 bus_size_t sc_ios; 35 36 int sc_nirq; 37 uint32_t *sc_irq_cfg; 38 39 struct interrupt_controller sc_ic; 40}; 41 42int mtintc_match(struct device *, void *, void *); 43void mtintc_attach(struct device *, struct device *, void *); 44int mtintc_activate(struct device *, int); 45 46void *mtintc_establish_fdt(void *, int *, int, struct cpu_info *, 47 int (*)(void *), void *, char *); 48 49const struct cfattach mtintc_ca = { 50 sizeof(struct mtintc_softc), mtintc_match, mtintc_attach, NULL, 51 mtintc_activate 52}; 53 54struct cfdriver mtintc_cd = { 55 NULL, "mtintc", DV_DULL 56}; 57 58int 59mtintc_match(struct device *parent, void *match, void *aux) 60{ 61 struct fdt_attach_args *faa = aux; 62 63 return OF_is_compatible(faa->fa_node, "mediatek,mt6577-sysirq"); 64} 65 66void 67mtintc_attach(struct device *parent, struct device *self, void *aux) 68{ 69 struct mtintc_softc *sc = (struct mtintc_softc *)self; 70 struct fdt_attach_args *faa = aux; 71 72 if (faa->fa_nreg < 1) { 73 printf(": no registers\n"); 74 return; 75 } 76 77 sc->sc_iot = faa->fa_iot; 78 sc->sc_ios = faa->fa_reg[0].size; 79 if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr, 80 sc->sc_ios, 0, &sc->sc_ioh)) 81 panic("%s: bus_space_map failed!", __func__); 82 83 sc->sc_nirq = sc->sc_ios * 8; 84 sc->sc_irq_cfg = malloc(sc->sc_ios, M_DEVBUF, M_WAITOK); 85 86 sc->sc_ic.ic_node = faa->fa_node; 87 sc->sc_ic.ic_cookie = sc; 88 sc->sc_ic.ic_establish = mtintc_establish_fdt; 89 90 printf(" nirq %d\n", sc->sc_nirq); 91 92 fdt_intr_register(&sc->sc_ic); 93} 94 95int 96mtintc_activate(struct device *self, int act) 97{ 98 struct mtintc_softc *sc = (struct mtintc_softc *)self; 99 100 switch (act) { 101 case DVACT_SUSPEND: 102 bus_space_read_region_4(sc->sc_iot, sc->sc_ioh, 0, 103 sc->sc_irq_cfg, sc->sc_ios / sizeof(uint32_t)); 104 break; 105 case DVACT_RESUME: 106 bus_space_write_region_4(sc->sc_iot, sc->sc_ioh, 0, 107 sc->sc_irq_cfg, sc->sc_ios / sizeof(uint32_t)); 108 break; 109 } 110 111 return 0; 112} 113 114void 115mtintc_invert(struct mtintc_softc *sc, int irq) 116{ 117 int reg = (irq / 32) * 4; 118 int bit = (irq % 32); 119 120 bus_space_write_4(sc->sc_iot, sc->sc_ioh, reg, 121 bus_space_read_4(sc->sc_iot, sc->sc_ioh, reg) | 1U << bit); 122} 123 124void * 125mtintc_establish_fdt(void *cookie, int *cells, int level, 126 struct cpu_info *ci, int (*func)(void *), void *arg, char *name) 127{ 128 struct mtintc_softc *sc = cookie; 129 int irq = cells[1]; 130 int flags = cells[2]; 131 132 KASSERT(cells[0] == 0); 133 KASSERT(irq >= 0 && irq < sc->sc_nirq); 134 135#ifdef DEBUG_INTC 136 printf("%s: irq %d level %d flags 0x%x [%s]\n", __func__, irq, level, 137 flags, name); 138#endif 139 140 if (flags & 0xa) 141 mtintc_invert(sc, irq); 142 143 return fdt_intr_parent_establish(&sc->sc_ic, cells, level, ci, func, 144 arg, name); 145}