ti_omapintc.c revision 1.5 1 1.5 thorpej /* $NetBSD: ti_omapintc.c,v 1.5 2021/01/18 02:35:49 thorpej 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.5 thorpej __KERNEL_RCSID(0, "$NetBSD: ti_omapintc.c,v 1.5 2021/01/18 02:35:49 thorpej 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.5 thorpej
65 1.5 thorpej { 0 }
66 1.2 jmcneill };
67 1.1 jakllsch
68 1.1 jakllsch #define INTC_READ(sc, g, o) \
69 1.1 jakllsch bus_space_read_4((sc)->sc_memt, (sc)->sc_memh, (g) * 0x20 + (o))
70 1.1 jakllsch #define INTC_WRITE(sc, g, o, v) \
71 1.1 jakllsch bus_space_write_4((sc)->sc_memt, (sc)->sc_memh, (g) * 0x20 + (o), v)
72 1.1 jakllsch
73 1.1 jakllsch static int omap2icu_match(device_t, cfdata_t, void *);
74 1.1 jakllsch static void omap2icu_attach(device_t, device_t, void *);
75 1.1 jakllsch
76 1.1 jakllsch static void omap2icu_unblock_irqs(struct pic_softc *, size_t, uint32_t);
77 1.1 jakllsch static void omap2icu_block_irqs(struct pic_softc *, size_t, uint32_t);
78 1.1 jakllsch static void omap2icu_establish_irq(struct pic_softc *, struct intrsource *);
79 1.1 jakllsch static void omap2icu_set_priority(struct pic_softc *, int);
80 1.1 jakllsch #if 0
81 1.1 jakllsch static void omap2icu_source_name(struct pic_softc *, int, char *, size_t);
82 1.1 jakllsch #endif
83 1.1 jakllsch
84 1.1 jakllsch static const struct pic_ops omap2icu_picops = {
85 1.1 jakllsch .pic_unblock_irqs = omap2icu_unblock_irqs,
86 1.1 jakllsch .pic_block_irqs = omap2icu_block_irqs,
87 1.1 jakllsch .pic_establish_irq = omap2icu_establish_irq,
88 1.1 jakllsch .pic_set_priority = omap2icu_set_priority,
89 1.1 jakllsch #if 0
90 1.1 jakllsch .pic_source_name = omap2icu_source_name,
91 1.1 jakllsch #endif
92 1.1 jakllsch };
93 1.1 jakllsch
94 1.1 jakllsch #define PICTOSOFTC(pic) \
95 1.1 jakllsch ((struct omap2icu_softc *)((uintptr_t)(pic) - offsetof(struct omap2icu_softc, sc_pic)))
96 1.1 jakllsch
97 1.1 jakllsch struct omap2icu_softc {
98 1.1 jakllsch device_t sc_dev;
99 1.1 jakllsch bus_space_tag_t sc_memt;
100 1.1 jakllsch bus_space_handle_t sc_memh;
101 1.1 jakllsch struct pic_softc sc_pic;
102 1.2 jmcneill uint32_t *sc_enabled_irqs;
103 1.2 jmcneill u_int sc_nbank;
104 1.1 jakllsch };
105 1.1 jakllsch
106 1.1 jakllsch static struct omap2icu_softc *intc_softc;
107 1.1 jakllsch
108 1.1 jakllsch static void
109 1.1 jakllsch omap2icu_unblock_irqs(struct pic_softc *pic, size_t irqbase, uint32_t irq_mask)
110 1.1 jakllsch {
111 1.1 jakllsch struct omap2icu_softc * const sc = PICTOSOFTC(pic);
112 1.1 jakllsch const size_t group = irqbase / 32;
113 1.1 jakllsch KASSERT((irq_mask & sc->sc_enabled_irqs[group]) == 0);
114 1.1 jakllsch sc->sc_enabled_irqs[group] |= irq_mask;
115 1.1 jakllsch INTC_WRITE(sc, group, INTC_MIR_CLEAR, irq_mask);
116 1.1 jakllsch
117 1.1 jakllsch /* Force INTC to recompute IRQ availability */
118 1.1 jakllsch INTC_WRITE(sc, 0, INTC_CONTROL, INTC_CONTROL_NEWIRQAGR);
119 1.1 jakllsch }
120 1.1 jakllsch
121 1.1 jakllsch static void
122 1.1 jakllsch omap2icu_block_irqs(struct pic_softc *pic, size_t irqbase, uint32_t irq_mask)
123 1.1 jakllsch {
124 1.1 jakllsch struct omap2icu_softc * const sc = PICTOSOFTC(pic);
125 1.1 jakllsch const size_t group = irqbase / 32;
126 1.1 jakllsch
127 1.1 jakllsch INTC_WRITE(sc, group, INTC_MIR_SET, irq_mask);
128 1.1 jakllsch sc->sc_enabled_irqs[group] &= ~irq_mask;
129 1.1 jakllsch }
130 1.1 jakllsch
131 1.1 jakllsch /*
132 1.1 jakllsch * Called with interrupts disabled
133 1.1 jakllsch */
134 1.1 jakllsch static int
135 1.1 jakllsch find_pending_irqs(struct omap2icu_softc *sc, size_t group)
136 1.1 jakllsch {
137 1.1 jakllsch uint32_t pending = INTC_READ(sc, group, INTC_PENDING_IRQ);
138 1.1 jakllsch
139 1.1 jakllsch KASSERT((sc->sc_enabled_irqs[group] & pending) == pending);
140 1.1 jakllsch
141 1.1 jakllsch if (pending == 0)
142 1.1 jakllsch return 0;
143 1.1 jakllsch
144 1.1 jakllsch return pic_mark_pending_sources(&sc->sc_pic, group * 32, pending);
145 1.1 jakllsch }
146 1.1 jakllsch
147 1.1 jakllsch static void
148 1.1 jakllsch omap_irq_handler(void *frame)
149 1.1 jakllsch {
150 1.1 jakllsch struct cpu_info * const ci = curcpu();
151 1.1 jakllsch struct omap2icu_softc * const sc = intc_softc;
152 1.1 jakllsch const int oldipl = ci->ci_cpl;
153 1.1 jakllsch const uint32_t oldipl_mask = __BIT(oldipl);
154 1.2 jmcneill int ipl_mask = 0, n;
155 1.1 jakllsch
156 1.1 jakllsch ci->ci_data.cpu_nintr++;
157 1.1 jakllsch
158 1.2 jmcneill for (n = 0; n < sc->sc_nbank; n++) {
159 1.2 jmcneill if (sc->sc_enabled_irqs[n])
160 1.2 jmcneill ipl_mask |= find_pending_irqs(sc, n);
161 1.2 jmcneill }
162 1.1 jakllsch
163 1.2 jmcneill /* force INTC to recompute IRQ */
164 1.1 jakllsch INTC_WRITE(sc, 0, INTC_CONTROL, INTC_CONTROL_NEWIRQAGR);
165 1.1 jakllsch
166 1.1 jakllsch /*
167 1.1 jakllsch * Record the pending_ipls and deliver them if we can.
168 1.1 jakllsch */
169 1.1 jakllsch if ((ipl_mask & ~oldipl_mask) > oldipl_mask)
170 1.1 jakllsch pic_do_pending_ints(I32_bit, oldipl, frame);
171 1.1 jakllsch }
172 1.1 jakllsch
173 1.1 jakllsch void
174 1.1 jakllsch omap2icu_establish_irq(struct pic_softc *pic, struct intrsource *is)
175 1.1 jakllsch {
176 1.1 jakllsch KASSERT(is->is_irq < PICTOSOFTC(pic)->sc_pic.pic_maxsources);
177 1.1 jakllsch KASSERT(is->is_type == IST_LEVEL);
178 1.1 jakllsch }
179 1.1 jakllsch
180 1.1 jakllsch static void
181 1.1 jakllsch omap2icu_set_priority(struct pic_softc *pic, int ipl)
182 1.1 jakllsch {
183 1.1 jakllsch curcpu()->ci_cpl = ipl;
184 1.1 jakllsch }
185 1.1 jakllsch
186 1.1 jakllsch static void *
187 1.1 jakllsch omapintc_fdt_establish(device_t dev, u_int *specifier, int ipl, int flags,
188 1.4 jmcneill int (*func)(void *), void *arg, const char *xname)
189 1.1 jakllsch {
190 1.1 jakllsch const u_int irq = be32toh(specifier[0]);
191 1.1 jakllsch if (irq >= INTC_MAX_SOURCES) {
192 1.1 jakllsch device_printf(dev, "IRQ %u is invalid\n", irq);
193 1.1 jakllsch return NULL;
194 1.1 jakllsch }
195 1.1 jakllsch
196 1.1 jakllsch const u_int mpsafe = (flags & FDT_INTR_MPSAFE) ? IST_MPSAFE : 0;
197 1.4 jmcneill return intr_establish_xname(irq, ipl, IST_LEVEL | mpsafe, func, arg,
198 1.4 jmcneill xname);
199 1.1 jakllsch }
200 1.1 jakllsch
201 1.1 jakllsch static void
202 1.1 jakllsch omapintc_fdt_disestablish(device_t dev, void *ih)
203 1.1 jakllsch {
204 1.1 jakllsch intr_disestablish(ih);
205 1.1 jakllsch }
206 1.1 jakllsch
207 1.1 jakllsch static bool
208 1.1 jakllsch omapintc_fdt_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen)
209 1.1 jakllsch {
210 1.1 jakllsch if (!specifier)
211 1.1 jakllsch return false;
212 1.1 jakllsch
213 1.1 jakllsch const u_int irq = be32toh(specifier[0]);
214 1.1 jakllsch snprintf(buf, buflen, "INTC irq %d", irq);
215 1.1 jakllsch return true;
216 1.1 jakllsch }
217 1.1 jakllsch
218 1.1 jakllsch static const struct fdtbus_interrupt_controller_func omapintc_fdt_funcs = {
219 1.1 jakllsch .establish = omapintc_fdt_establish,
220 1.1 jakllsch .disestablish = omapintc_fdt_disestablish,
221 1.1 jakllsch .intrstr = omapintc_fdt_intrstr,
222 1.1 jakllsch };
223 1.1 jakllsch
224 1.1 jakllsch int
225 1.1 jakllsch omap2icu_match(device_t parent, cfdata_t cf, void *aux)
226 1.1 jakllsch {
227 1.1 jakllsch struct fdt_attach_args * const faa = aux;
228 1.1 jakllsch
229 1.2 jmcneill return of_match_compat_data(faa->faa_phandle, compat_data);
230 1.1 jakllsch }
231 1.1 jakllsch
232 1.1 jakllsch void
233 1.1 jakllsch omap2icu_attach(device_t parent, device_t self, void *aux)
234 1.1 jakllsch {
235 1.1 jakllsch struct omap2icu_softc * const sc = device_private(self);
236 1.1 jakllsch struct fdt_attach_args * const faa = aux;
237 1.1 jakllsch const int phandle = faa->faa_phandle;
238 1.1 jakllsch bus_addr_t addr;
239 1.1 jakllsch bus_size_t size;
240 1.2 jmcneill int error, n;
241 1.1 jakllsch
242 1.1 jakllsch if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
243 1.1 jakllsch aprint_error(": couldn't get registers\n");
244 1.1 jakllsch return;
245 1.1 jakllsch }
246 1.1 jakllsch
247 1.1 jakllsch sc->sc_dev = self;
248 1.1 jakllsch sc->sc_memt = faa->faa_bst;
249 1.1 jakllsch if (bus_space_map(sc->sc_memt, addr, size, 0, &sc->sc_memh) != 0) {
250 1.1 jakllsch aprint_error(": couldn't map registers\n");
251 1.1 jakllsch return;
252 1.1 jakllsch }
253 1.5 thorpej sc->sc_nbank = of_search_compatible(phandle, compat_data)->value;
254 1.2 jmcneill sc->sc_enabled_irqs =
255 1.2 jmcneill kmem_zalloc(sizeof(*sc->sc_enabled_irqs) * sc->sc_nbank, KM_SLEEP);
256 1.1 jakllsch
257 1.1 jakllsch aprint_naive("\n");
258 1.1 jakllsch aprint_normal("\n");
259 1.1 jakllsch
260 1.2 jmcneill for (n = 0; n < sc->sc_nbank; n++)
261 1.2 jmcneill INTC_WRITE(sc, n, INTC_MIR_SET, 0xffffffff);
262 1.1 jakllsch
263 1.1 jakllsch sc->sc_dev = self;
264 1.1 jakllsch self->dv_private = sc;
265 1.1 jakllsch
266 1.1 jakllsch sc->sc_pic.pic_ops = &omap2icu_picops;
267 1.2 jmcneill sc->sc_pic.pic_maxsources = sc->sc_nbank * 32;
268 1.1 jakllsch snprintf(sc->sc_pic.pic_name, sizeof(sc->sc_pic.pic_name), "intc");
269 1.1 jakllsch pic_add(&sc->sc_pic, 0);
270 1.1 jakllsch error = fdtbus_register_interrupt_controller(self, phandle,
271 1.1 jakllsch &omapintc_fdt_funcs);
272 1.1 jakllsch if (error) {
273 1.1 jakllsch aprint_error_dev(self, "couldn't register with fdtbus: %d\n",
274 1.1 jakllsch error);
275 1.1 jakllsch return;
276 1.1 jakllsch }
277 1.1 jakllsch
278 1.1 jakllsch KASSERT(intc_softc == NULL);
279 1.1 jakllsch intc_softc = sc;
280 1.1 jakllsch arm_fdt_irq_set_handler(omap_irq_handler);
281 1.1 jakllsch }
282 1.1 jakllsch
283 1.1 jakllsch CFATTACH_DECL_NEW(omapintc,
284 1.1 jakllsch sizeof(struct omap2icu_softc),
285 1.1 jakllsch omap2icu_match, omap2icu_attach,
286 1.1 jakllsch NULL, NULL);
287