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