ti_omapintc.c revision 1.9 1 1.9 riastrad /* $NetBSD: ti_omapintc.c,v 1.9 2022/02/11 23:48:33 riastradh Exp $ */
2 1.1 jakllsch /*
3 1.1 jakllsch * Define the SDP2430 specific information and then include the generic OMAP
4 1.1 jakllsch * interrupt header.
5 1.1 jakllsch */
6 1.1 jakllsch
7 1.1 jakllsch /*
8 1.1 jakllsch * Redistribution and use in source and binary forms, with or without
9 1.1 jakllsch * modification, are permitted provided that the following conditions
10 1.1 jakllsch * are met:
11 1.1 jakllsch * 1. Redistributions of source code must retain this list of conditions
12 1.1 jakllsch * and the following disclaimer.
13 1.1 jakllsch * 2. Redistributions in binary form must reproduce this list of conditions
14 1.1 jakllsch * and the following disclaimer in the documentation and/or other materials
15 1.1 jakllsch * provided with the distribution.
16 1.1 jakllsch *
17 1.1 jakllsch * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 1.1 jakllsch * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
19 1.1 jakllsch * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANY
20 1.1 jakllsch * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 1.1 jakllsch * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 1.1 jakllsch * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 1.1 jakllsch * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 1.1 jakllsch * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 1.1 jakllsch * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 1.1 jakllsch * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 1.1 jakllsch */
28 1.1 jakllsch
29 1.1 jakllsch #define _INTR_PRIVATE
30 1.1 jakllsch
31 1.1 jakllsch #include <sys/cdefs.h>
32 1.9 riastrad __KERNEL_RCSID(0, "$NetBSD: ti_omapintc.c,v 1.9 2022/02/11 23:48:33 riastradh Exp $");
33 1.1 jakllsch
34 1.1 jakllsch #include <sys/param.h>
35 1.1 jakllsch #include <sys/evcnt.h>
36 1.1 jakllsch #include <sys/device.h>
37 1.2 jmcneill #include <sys/kmem.h>
38 1.1 jakllsch
39 1.1 jakllsch #include <uvm/uvm_extern.h>
40 1.1 jakllsch
41 1.1 jakllsch #include <machine/intr.h>
42 1.1 jakllsch #include <sys/bus.h>
43 1.1 jakllsch
44 1.1 jakllsch #include <arm/cpu.h>
45 1.1 jakllsch #include <arm/armreg.h>
46 1.1 jakllsch #include <arm/cpufunc.h>
47 1.1 jakllsch
48 1.1 jakllsch #include <dev/fdt/fdtvar.h>
49 1.1 jakllsch
50 1.1 jakllsch #define INTC_CONTROL 0x048
51 1.1 jakllsch #define INTC_CONTROL_NEWIRQAGR __BIT(0)
52 1.1 jakllsch #define INTC_ITR 0x080
53 1.1 jakllsch #define INTC_MIR 0x084
54 1.1 jakllsch #define INTC_MIR_CLEAR 0x088
55 1.1 jakllsch #define INTC_MIR_SET 0x08c
56 1.1 jakllsch #define INTC_PENDING_IRQ 0x098
57 1.1 jakllsch
58 1.1 jakllsch #define INTC_MAX_SOURCES 128
59 1.1 jakllsch
60 1.5 thorpej static const struct device_compatible_entry compat_data[] = {
61 1.2 jmcneill /* compatible number of banks */
62 1.5 thorpej { .compat = "ti,omap3-intc", .value = 3 },
63 1.5 thorpej { .compat = "ti,am33xx-intc", .value = 4 },
64 1.7 thorpej DEVICE_COMPAT_EOL
65 1.2 jmcneill };
66 1.1 jakllsch
67 1.1 jakllsch #define INTC_READ(sc, g, o) \
68 1.1 jakllsch bus_space_read_4((sc)->sc_memt, (sc)->sc_memh, (g) * 0x20 + (o))
69 1.1 jakllsch #define INTC_WRITE(sc, g, o, v) \
70 1.1 jakllsch bus_space_write_4((sc)->sc_memt, (sc)->sc_memh, (g) * 0x20 + (o), v)
71 1.1 jakllsch
72 1.1 jakllsch static int omap2icu_match(device_t, cfdata_t, void *);
73 1.1 jakllsch static void omap2icu_attach(device_t, device_t, void *);
74 1.1 jakllsch
75 1.1 jakllsch static void omap2icu_unblock_irqs(struct pic_softc *, size_t, uint32_t);
76 1.1 jakllsch static void omap2icu_block_irqs(struct pic_softc *, size_t, uint32_t);
77 1.1 jakllsch static void omap2icu_establish_irq(struct pic_softc *, struct intrsource *);
78 1.1 jakllsch static void omap2icu_set_priority(struct pic_softc *, int);
79 1.1 jakllsch #if 0
80 1.1 jakllsch static void omap2icu_source_name(struct pic_softc *, int, char *, size_t);
81 1.1 jakllsch #endif
82 1.1 jakllsch
83 1.1 jakllsch static const struct pic_ops omap2icu_picops = {
84 1.1 jakllsch .pic_unblock_irqs = omap2icu_unblock_irqs,
85 1.1 jakllsch .pic_block_irqs = omap2icu_block_irqs,
86 1.1 jakllsch .pic_establish_irq = omap2icu_establish_irq,
87 1.1 jakllsch .pic_set_priority = omap2icu_set_priority,
88 1.1 jakllsch #if 0
89 1.1 jakllsch .pic_source_name = omap2icu_source_name,
90 1.1 jakllsch #endif
91 1.1 jakllsch };
92 1.1 jakllsch
93 1.1 jakllsch #define PICTOSOFTC(pic) \
94 1.1 jakllsch ((struct omap2icu_softc *)((uintptr_t)(pic) - offsetof(struct omap2icu_softc, sc_pic)))
95 1.1 jakllsch
96 1.1 jakllsch struct omap2icu_softc {
97 1.1 jakllsch device_t sc_dev;
98 1.1 jakllsch bus_space_tag_t sc_memt;
99 1.1 jakllsch bus_space_handle_t sc_memh;
100 1.1 jakllsch struct pic_softc sc_pic;
101 1.2 jmcneill uint32_t *sc_enabled_irqs;
102 1.2 jmcneill u_int sc_nbank;
103 1.1 jakllsch };
104 1.1 jakllsch
105 1.1 jakllsch static struct omap2icu_softc *intc_softc;
106 1.1 jakllsch
107 1.1 jakllsch static void
108 1.1 jakllsch omap2icu_unblock_irqs(struct pic_softc *pic, size_t irqbase, uint32_t irq_mask)
109 1.1 jakllsch {
110 1.1 jakllsch struct omap2icu_softc * const sc = PICTOSOFTC(pic);
111 1.1 jakllsch const size_t group = irqbase / 32;
112 1.1 jakllsch KASSERT((irq_mask & sc->sc_enabled_irqs[group]) == 0);
113 1.1 jakllsch sc->sc_enabled_irqs[group] |= irq_mask;
114 1.1 jakllsch INTC_WRITE(sc, group, INTC_MIR_CLEAR, irq_mask);
115 1.1 jakllsch
116 1.1 jakllsch /* Force INTC to recompute IRQ availability */
117 1.1 jakllsch INTC_WRITE(sc, 0, INTC_CONTROL, INTC_CONTROL_NEWIRQAGR);
118 1.1 jakllsch }
119 1.1 jakllsch
120 1.1 jakllsch static void
121 1.1 jakllsch omap2icu_block_irqs(struct pic_softc *pic, size_t irqbase, uint32_t irq_mask)
122 1.1 jakllsch {
123 1.1 jakllsch struct omap2icu_softc * const sc = PICTOSOFTC(pic);
124 1.1 jakllsch const size_t group = irqbase / 32;
125 1.1 jakllsch
126 1.1 jakllsch INTC_WRITE(sc, group, INTC_MIR_SET, irq_mask);
127 1.1 jakllsch sc->sc_enabled_irqs[group] &= ~irq_mask;
128 1.1 jakllsch }
129 1.1 jakllsch
130 1.1 jakllsch /*
131 1.1 jakllsch * Called with interrupts disabled
132 1.1 jakllsch */
133 1.1 jakllsch static int
134 1.1 jakllsch find_pending_irqs(struct omap2icu_softc *sc, size_t group)
135 1.1 jakllsch {
136 1.1 jakllsch uint32_t pending = INTC_READ(sc, group, INTC_PENDING_IRQ);
137 1.1 jakllsch
138 1.1 jakllsch KASSERT((sc->sc_enabled_irqs[group] & pending) == pending);
139 1.1 jakllsch
140 1.1 jakllsch if (pending == 0)
141 1.1 jakllsch return 0;
142 1.1 jakllsch
143 1.1 jakllsch return pic_mark_pending_sources(&sc->sc_pic, group * 32, pending);
144 1.1 jakllsch }
145 1.1 jakllsch
146 1.1 jakllsch static void
147 1.1 jakllsch omap_irq_handler(void *frame)
148 1.1 jakllsch {
149 1.1 jakllsch struct cpu_info * const ci = curcpu();
150 1.1 jakllsch struct omap2icu_softc * const sc = intc_softc;
151 1.1 jakllsch const int oldipl = ci->ci_cpl;
152 1.1 jakllsch const uint32_t oldipl_mask = __BIT(oldipl);
153 1.2 jmcneill int ipl_mask = 0, n;
154 1.1 jakllsch
155 1.1 jakllsch ci->ci_data.cpu_nintr++;
156 1.1 jakllsch
157 1.2 jmcneill for (n = 0; n < sc->sc_nbank; n++) {
158 1.2 jmcneill if (sc->sc_enabled_irqs[n])
159 1.2 jmcneill ipl_mask |= find_pending_irqs(sc, n);
160 1.2 jmcneill }
161 1.1 jakllsch
162 1.2 jmcneill /* force INTC to recompute IRQ */
163 1.1 jakllsch INTC_WRITE(sc, 0, INTC_CONTROL, INTC_CONTROL_NEWIRQAGR);
164 1.1 jakllsch
165 1.1 jakllsch /*
166 1.1 jakllsch * Record the pending_ipls and deliver them if we can.
167 1.1 jakllsch */
168 1.1 jakllsch if ((ipl_mask & ~oldipl_mask) > oldipl_mask)
169 1.1 jakllsch pic_do_pending_ints(I32_bit, oldipl, frame);
170 1.1 jakllsch }
171 1.1 jakllsch
172 1.1 jakllsch void
173 1.1 jakllsch omap2icu_establish_irq(struct pic_softc *pic, struct intrsource *is)
174 1.1 jakllsch {
175 1.1 jakllsch KASSERT(is->is_irq < PICTOSOFTC(pic)->sc_pic.pic_maxsources);
176 1.1 jakllsch KASSERT(is->is_type == IST_LEVEL);
177 1.1 jakllsch }
178 1.1 jakllsch
179 1.1 jakllsch static void
180 1.1 jakllsch omap2icu_set_priority(struct pic_softc *pic, int ipl)
181 1.1 jakllsch {
182 1.1 jakllsch curcpu()->ci_cpl = ipl;
183 1.1 jakllsch }
184 1.1 jakllsch
185 1.1 jakllsch static void *
186 1.1 jakllsch omapintc_fdt_establish(device_t dev, u_int *specifier, int ipl, int flags,
187 1.4 jmcneill int (*func)(void *), void *arg, const char *xname)
188 1.1 jakllsch {
189 1.1 jakllsch const u_int irq = be32toh(specifier[0]);
190 1.1 jakllsch if (irq >= INTC_MAX_SOURCES) {
191 1.1 jakllsch device_printf(dev, "IRQ %u is invalid\n", irq);
192 1.1 jakllsch return NULL;
193 1.1 jakllsch }
194 1.1 jakllsch
195 1.1 jakllsch const u_int mpsafe = (flags & FDT_INTR_MPSAFE) ? IST_MPSAFE : 0;
196 1.4 jmcneill return intr_establish_xname(irq, ipl, IST_LEVEL | mpsafe, func, arg,
197 1.4 jmcneill xname);
198 1.1 jakllsch }
199 1.1 jakllsch
200 1.1 jakllsch static void
201 1.1 jakllsch omapintc_fdt_disestablish(device_t dev, void *ih)
202 1.1 jakllsch {
203 1.1 jakllsch intr_disestablish(ih);
204 1.1 jakllsch }
205 1.1 jakllsch
206 1.1 jakllsch static bool
207 1.1 jakllsch omapintc_fdt_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen)
208 1.1 jakllsch {
209 1.1 jakllsch if (!specifier)
210 1.1 jakllsch return false;
211 1.1 jakllsch
212 1.1 jakllsch const u_int irq = be32toh(specifier[0]);
213 1.1 jakllsch snprintf(buf, buflen, "INTC irq %d", irq);
214 1.1 jakllsch return true;
215 1.1 jakllsch }
216 1.1 jakllsch
217 1.1 jakllsch static const struct fdtbus_interrupt_controller_func omapintc_fdt_funcs = {
218 1.1 jakllsch .establish = omapintc_fdt_establish,
219 1.1 jakllsch .disestablish = omapintc_fdt_disestablish,
220 1.1 jakllsch .intrstr = omapintc_fdt_intrstr,
221 1.1 jakllsch };
222 1.1 jakllsch
223 1.1 jakllsch int
224 1.1 jakllsch omap2icu_match(device_t parent, cfdata_t cf, void *aux)
225 1.1 jakllsch {
226 1.1 jakllsch struct fdt_attach_args * const faa = aux;
227 1.1 jakllsch
228 1.8 thorpej return of_compatible_match(faa->faa_phandle, compat_data);
229 1.1 jakllsch }
230 1.1 jakllsch
231 1.1 jakllsch void
232 1.1 jakllsch omap2icu_attach(device_t parent, device_t self, void *aux)
233 1.1 jakllsch {
234 1.1 jakllsch struct omap2icu_softc * const sc = device_private(self);
235 1.1 jakllsch struct fdt_attach_args * const faa = aux;
236 1.1 jakllsch const int phandle = faa->faa_phandle;
237 1.1 jakllsch bus_addr_t addr;
238 1.1 jakllsch bus_size_t size;
239 1.2 jmcneill int error, n;
240 1.1 jakllsch
241 1.1 jakllsch if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
242 1.1 jakllsch aprint_error(": couldn't get registers\n");
243 1.1 jakllsch return;
244 1.1 jakllsch }
245 1.1 jakllsch
246 1.1 jakllsch sc->sc_dev = self;
247 1.1 jakllsch sc->sc_memt = faa->faa_bst;
248 1.1 jakllsch if (bus_space_map(sc->sc_memt, addr, size, 0, &sc->sc_memh) != 0) {
249 1.1 jakllsch aprint_error(": couldn't map registers\n");
250 1.1 jakllsch return;
251 1.1 jakllsch }
252 1.8 thorpej sc->sc_nbank = of_compatible_lookup(phandle, compat_data)->value;
253 1.2 jmcneill sc->sc_enabled_irqs =
254 1.2 jmcneill kmem_zalloc(sizeof(*sc->sc_enabled_irqs) * sc->sc_nbank, KM_SLEEP);
255 1.1 jakllsch
256 1.1 jakllsch aprint_naive("\n");
257 1.1 jakllsch aprint_normal("\n");
258 1.1 jakllsch
259 1.2 jmcneill for (n = 0; n < sc->sc_nbank; n++)
260 1.2 jmcneill INTC_WRITE(sc, n, INTC_MIR_SET, 0xffffffff);
261 1.1 jakllsch
262 1.1 jakllsch sc->sc_dev = self;
263 1.1 jakllsch
264 1.1 jakllsch sc->sc_pic.pic_ops = &omap2icu_picops;
265 1.2 jmcneill sc->sc_pic.pic_maxsources = sc->sc_nbank * 32;
266 1.1 jakllsch snprintf(sc->sc_pic.pic_name, sizeof(sc->sc_pic.pic_name), "intc");
267 1.1 jakllsch pic_add(&sc->sc_pic, 0);
268 1.1 jakllsch error = fdtbus_register_interrupt_controller(self, phandle,
269 1.1 jakllsch &omapintc_fdt_funcs);
270 1.1 jakllsch if (error) {
271 1.1 jakllsch aprint_error_dev(self, "couldn't register with fdtbus: %d\n",
272 1.1 jakllsch error);
273 1.1 jakllsch return;
274 1.1 jakllsch }
275 1.1 jakllsch
276 1.1 jakllsch KASSERT(intc_softc == NULL);
277 1.1 jakllsch intc_softc = sc;
278 1.1 jakllsch arm_fdt_irq_set_handler(omap_irq_handler);
279 1.1 jakllsch }
280 1.1 jakllsch
281 1.1 jakllsch CFATTACH_DECL_NEW(omapintc,
282 1.1 jakllsch sizeof(struct omap2icu_softc),
283 1.1 jakllsch omap2icu_match, omap2icu_attach,
284 1.1 jakllsch NULL, NULL);
285