ti_omapintc.c revision 1.2 1 /* $NetBSD: ti_omapintc.c,v 1.2 2019/10/29 22:19:13 jmcneill 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.2 2019/10/29 22:19:13 jmcneill 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 #include <arm/atomic.h>
48
49 #include <dev/fdt/fdtvar.h>
50
51 #define INTC_CONTROL 0x048
52 #define INTC_CONTROL_NEWIRQAGR __BIT(0)
53 #define INTC_ITR 0x080
54 #define INTC_MIR 0x084
55 #define INTC_MIR_CLEAR 0x088
56 #define INTC_MIR_SET 0x08c
57 #define INTC_PENDING_IRQ 0x098
58
59 #define INTC_MAX_SOURCES 128
60
61 static const struct of_compat_data compat_data[] = {
62 /* compatible number of banks */
63 { "ti,omap3-intc", 3 },
64 { "ti,am33xx-intc", 4 },
65 { NULL }
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)
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(irq, ipl, IST_LEVEL | mpsafe, func, arg);
198 }
199
200 static void
201 omapintc_fdt_disestablish(device_t dev, void *ih)
202 {
203 intr_disestablish(ih);
204 }
205
206 static bool
207 omapintc_fdt_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen)
208 {
209 if (!specifier)
210 return false;
211
212 const u_int irq = be32toh(specifier[0]);
213 snprintf(buf, buflen, "INTC irq %d", irq);
214 return true;
215 }
216
217 static const struct fdtbus_interrupt_controller_func omapintc_fdt_funcs = {
218 .establish = omapintc_fdt_establish,
219 .disestablish = omapintc_fdt_disestablish,
220 .intrstr = omapintc_fdt_intrstr,
221 };
222
223 int
224 omap2icu_match(device_t parent, cfdata_t cf, void *aux)
225 {
226 struct fdt_attach_args * const faa = aux;
227
228 return of_match_compat_data(faa->faa_phandle, compat_data);
229 }
230
231 void
232 omap2icu_attach(device_t parent, device_t self, void *aux)
233 {
234 struct omap2icu_softc * const sc = device_private(self);
235 struct fdt_attach_args * const faa = aux;
236 const int phandle = faa->faa_phandle;
237 bus_addr_t addr;
238 bus_size_t size;
239 int error, n;
240
241 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
242 aprint_error(": couldn't get registers\n");
243 return;
244 }
245
246 sc->sc_dev = self;
247 sc->sc_memt = faa->faa_bst;
248 if (bus_space_map(sc->sc_memt, addr, size, 0, &sc->sc_memh) != 0) {
249 aprint_error(": couldn't map registers\n");
250 return;
251 }
252 sc->sc_nbank = of_search_compatible(phandle, compat_data)->data;
253 sc->sc_enabled_irqs =
254 kmem_zalloc(sizeof(*sc->sc_enabled_irqs) * sc->sc_nbank, KM_SLEEP);
255
256 aprint_naive("\n");
257 aprint_normal("\n");
258
259 for (n = 0; n < sc->sc_nbank; n++)
260 INTC_WRITE(sc, n, INTC_MIR_SET, 0xffffffff);
261
262 sc->sc_dev = self;
263 self->dv_private = sc;
264
265 sc->sc_pic.pic_ops = &omap2icu_picops;
266 sc->sc_pic.pic_maxsources = sc->sc_nbank * 32;
267 snprintf(sc->sc_pic.pic_name, sizeof(sc->sc_pic.pic_name), "intc");
268 pic_add(&sc->sc_pic, 0);
269 error = fdtbus_register_interrupt_controller(self, phandle,
270 &omapintc_fdt_funcs);
271 if (error) {
272 aprint_error_dev(self, "couldn't register with fdtbus: %d\n",
273 error);
274 return;
275 }
276
277 KASSERT(intc_softc == NULL);
278 intc_softc = sc;
279 arm_fdt_irq_set_handler(omap_irq_handler);
280 }
281
282 CFATTACH_DECL_NEW(omapintc,
283 sizeof(struct omap2icu_softc),
284 omap2icu_match, omap2icu_attach,
285 NULL, NULL);
286