sunxi_nmi.c revision 1.6 1 1.6 jmcneill /* $NetBSD: sunxi_nmi.c,v 1.6 2021/01/15 00:38:23 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #define _INTR_PRIVATE
30 1.1 jmcneill
31 1.1 jmcneill #include <sys/cdefs.h>
32 1.6 jmcneill __KERNEL_RCSID(0, "$NetBSD: sunxi_nmi.c,v 1.6 2021/01/15 00:38:23 jmcneill Exp $");
33 1.1 jmcneill
34 1.1 jmcneill #include <sys/param.h>
35 1.1 jmcneill #include <sys/bus.h>
36 1.1 jmcneill #include <sys/device.h>
37 1.1 jmcneill #include <sys/intr.h>
38 1.3 skrll #include <sys/kernel.h>
39 1.1 jmcneill #include <sys/systm.h>
40 1.5 thorpej #include <sys/atomic.h>
41 1.5 thorpej #include <sys/mutex.h>
42 1.4 skrll #include <sys/lwp.h>
43 1.1 jmcneill
44 1.1 jmcneill #include <dev/fdt/fdtvar.h>
45 1.1 jmcneill
46 1.1 jmcneill #include <arm/cpu.h>
47 1.1 jmcneill #include <arm/pic/picvar.h>
48 1.1 jmcneill #include <arm/fdt/arm_fdtvar.h>
49 1.1 jmcneill
50 1.1 jmcneill /* ctrl_reg */
51 1.1 jmcneill #define NMI_CTRL_IRQ_LOW_LEVEL 0
52 1.1 jmcneill #define NMI_CTRL_IRQ_LOW_EDGE 1
53 1.1 jmcneill #define NMI_CTRL_IRQ_HIGH_LEVEL 2
54 1.1 jmcneill #define NMI_CTRL_IRQ_HIGH_EDGE 3
55 1.1 jmcneill #define NMI_CTRL_IRQ_TYPE __BITS(1,0)
56 1.1 jmcneill
57 1.1 jmcneill /* pend_reg */
58 1.1 jmcneill #define NMI_PEND_IRQ_ACK __BIT(0)
59 1.1 jmcneill
60 1.1 jmcneill /* enable_reg */
61 1.1 jmcneill #define NMI_ENABLE_IRQEN __BIT(0)
62 1.1 jmcneill
63 1.1 jmcneill struct sunxi_nmi_config {
64 1.1 jmcneill const char * name;
65 1.1 jmcneill bus_size_t ctrl_reg;
66 1.1 jmcneill bus_size_t pend_reg;
67 1.1 jmcneill bus_size_t enable_reg;
68 1.1 jmcneill };
69 1.1 jmcneill
70 1.1 jmcneill static const struct sunxi_nmi_config sun7i_a20_sc_nmi_config = {
71 1.1 jmcneill .name = "NMI",
72 1.1 jmcneill .ctrl_reg = 0x00,
73 1.1 jmcneill .pend_reg = 0x04,
74 1.1 jmcneill .enable_reg = 0x08,
75 1.1 jmcneill };
76 1.1 jmcneill
77 1.1 jmcneill static const struct sunxi_nmi_config sun6i_a31_r_intc_config = {
78 1.1 jmcneill .name = "R_INTC",
79 1.1 jmcneill .ctrl_reg = 0x0c,
80 1.1 jmcneill .pend_reg = 0x10,
81 1.1 jmcneill .enable_reg = 0x40,
82 1.1 jmcneill };
83 1.1 jmcneill
84 1.2 jmcneill static const struct sunxi_nmi_config sun9i_a80_nmi_config = {
85 1.2 jmcneill .name = "NMI",
86 1.2 jmcneill .ctrl_reg = 0x00,
87 1.2 jmcneill .pend_reg = 0x04,
88 1.2 jmcneill .enable_reg = 0x08,
89 1.2 jmcneill };
90 1.2 jmcneill
91 1.1 jmcneill static const struct of_compat_data compat_data[] = {
92 1.1 jmcneill { "allwinner,sun7i-a20-sc-nmi", (uintptr_t)&sun7i_a20_sc_nmi_config },
93 1.1 jmcneill { "allwinner,sun6i-a31-r-intc", (uintptr_t)&sun6i_a31_r_intc_config },
94 1.2 jmcneill { "allwinner,sun9i-a80-nmi", (uintptr_t)&sun9i_a80_nmi_config },
95 1.1 jmcneill { NULL }
96 1.1 jmcneill };
97 1.1 jmcneill
98 1.1 jmcneill struct sunxi_nmi_softc {
99 1.1 jmcneill device_t sc_dev;
100 1.1 jmcneill bus_space_tag_t sc_bst;
101 1.1 jmcneill bus_space_handle_t sc_bsh;
102 1.1 jmcneill int sc_phandle;
103 1.1 jmcneill
104 1.5 thorpej kmutex_t sc_intr_lock;
105 1.5 thorpej
106 1.1 jmcneill const struct sunxi_nmi_config *sc_config;
107 1.1 jmcneill
108 1.5 thorpej struct intrsource sc_is;
109 1.5 thorpej void *sc_ih;
110 1.1 jmcneill };
111 1.1 jmcneill
112 1.1 jmcneill #define NMI_READ(sc, reg) \
113 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
114 1.1 jmcneill #define NMI_WRITE(sc, reg, val) \
115 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
116 1.1 jmcneill
117 1.1 jmcneill static void
118 1.1 jmcneill sunxi_nmi_irq_ack(struct sunxi_nmi_softc *sc)
119 1.1 jmcneill {
120 1.1 jmcneill uint32_t val;
121 1.1 jmcneill
122 1.1 jmcneill val = NMI_READ(sc, sc->sc_config->pend_reg);
123 1.1 jmcneill val |= NMI_PEND_IRQ_ACK;
124 1.1 jmcneill NMI_WRITE(sc, sc->sc_config->pend_reg, val);
125 1.1 jmcneill }
126 1.1 jmcneill
127 1.1 jmcneill static void
128 1.1 jmcneill sunxi_nmi_irq_enable(struct sunxi_nmi_softc *sc, bool on)
129 1.1 jmcneill {
130 1.1 jmcneill uint32_t val;
131 1.1 jmcneill
132 1.1 jmcneill val = NMI_READ(sc, sc->sc_config->enable_reg);
133 1.1 jmcneill if (on)
134 1.1 jmcneill val |= NMI_ENABLE_IRQEN;
135 1.1 jmcneill else
136 1.1 jmcneill val &= ~NMI_ENABLE_IRQEN;
137 1.1 jmcneill NMI_WRITE(sc, sc->sc_config->enable_reg, val);
138 1.1 jmcneill }
139 1.1 jmcneill
140 1.1 jmcneill static void
141 1.1 jmcneill sunxi_nmi_irq_set_type(struct sunxi_nmi_softc *sc, u_int irq_type)
142 1.1 jmcneill {
143 1.1 jmcneill uint32_t val;
144 1.1 jmcneill
145 1.1 jmcneill val = NMI_READ(sc, sc->sc_config->ctrl_reg);
146 1.1 jmcneill val &= ~NMI_CTRL_IRQ_TYPE;
147 1.1 jmcneill val |= __SHIFTIN(irq_type, NMI_CTRL_IRQ_TYPE);
148 1.1 jmcneill NMI_WRITE(sc, sc->sc_config->ctrl_reg, val);
149 1.1 jmcneill }
150 1.1 jmcneill
151 1.1 jmcneill static int
152 1.1 jmcneill sunxi_nmi_intr(void *priv)
153 1.1 jmcneill {
154 1.1 jmcneill struct sunxi_nmi_softc * const sc = priv;
155 1.5 thorpej int (*func)(void *);
156 1.1 jmcneill int rv = 0;
157 1.1 jmcneill
158 1.5 thorpej func = atomic_load_acquire(&sc->sc_is.is_func);
159 1.5 thorpej if (func)
160 1.5 thorpej rv = func(sc->sc_is.is_arg);
161 1.5 thorpej
162 1.5 thorpej /*
163 1.5 thorpej * We don't serialize access to this register because we're the
164 1.5 thorpej * only thing fiddling wth it.
165 1.5 thorpej */
166 1.1 jmcneill sunxi_nmi_irq_ack(sc);
167 1.1 jmcneill
168 1.1 jmcneill return rv;
169 1.1 jmcneill }
170 1.1 jmcneill
171 1.1 jmcneill static void *
172 1.1 jmcneill sunxi_nmi_fdt_establish(device_t dev, u_int *specifier, int ipl, int flags,
173 1.6 jmcneill int (*func)(void *), void *arg, const char *xname)
174 1.1 jmcneill {
175 1.1 jmcneill struct sunxi_nmi_softc * const sc = device_private(dev);
176 1.1 jmcneill u_int irq_type;
177 1.5 thorpej int ist;
178 1.1 jmcneill
179 1.1 jmcneill /* 1st cell is the interrupt number */
180 1.1 jmcneill const u_int irq = be32toh(specifier[0]);
181 1.1 jmcneill /* 2nd cell is polarity */
182 1.1 jmcneill const u_int pol = be32toh(specifier[1]);
183 1.1 jmcneill
184 1.1 jmcneill if (irq != 0) {
185 1.1 jmcneill #ifdef DIAGNOSTIC
186 1.1 jmcneill device_printf(dev, "IRQ %u is invalid\n", irq);
187 1.1 jmcneill #endif
188 1.1 jmcneill return NULL;
189 1.1 jmcneill }
190 1.1 jmcneill
191 1.1 jmcneill switch (pol & 0x7) {
192 1.1 jmcneill case 1: /* IRQ_TYPE_EDGE_RISING */
193 1.1 jmcneill irq_type = NMI_CTRL_IRQ_HIGH_EDGE;
194 1.5 thorpej ist = IST_EDGE;
195 1.1 jmcneill break;
196 1.1 jmcneill case 2: /* IRQ_TYPE_EDGE_FALLING */
197 1.1 jmcneill irq_type = NMI_CTRL_IRQ_LOW_EDGE;
198 1.5 thorpej ist = IST_EDGE;
199 1.1 jmcneill break;
200 1.1 jmcneill case 3: /* IRQ_TYPE_LEVEL_HIGH */
201 1.1 jmcneill irq_type = NMI_CTRL_IRQ_HIGH_LEVEL;
202 1.5 thorpej ist = IST_LEVEL;
203 1.1 jmcneill break;
204 1.1 jmcneill case 4: /* IRQ_TYPE_LEVEL_LOW */
205 1.1 jmcneill irq_type = NMI_CTRL_IRQ_LOW_LEVEL;
206 1.5 thorpej ist = IST_LEVEL;
207 1.1 jmcneill break;
208 1.1 jmcneill default:
209 1.1 jmcneill irq_type = NMI_CTRL_IRQ_LOW_LEVEL;
210 1.5 thorpej ist = IST_LEVEL;
211 1.1 jmcneill break;
212 1.1 jmcneill }
213 1.1 jmcneill
214 1.5 thorpej mutex_enter(&sc->sc_intr_lock);
215 1.5 thorpej
216 1.5 thorpej if (atomic_load_relaxed(&sc->sc_is.is_func) != NULL) {
217 1.5 thorpej mutex_exit(&sc->sc_intr_lock);
218 1.5 thorpej #ifdef DIAGNOSTIC
219 1.5 thorpej device_printf(dev, "%s in use\n", sc->sc_config->name);
220 1.5 thorpej #endif
221 1.5 thorpej return NULL;
222 1.5 thorpej }
223 1.5 thorpej
224 1.5 thorpej sc->sc_is.is_arg = arg;
225 1.5 thorpej atomic_store_release(&sc->sc_is.is_func, func);
226 1.5 thorpej
227 1.5 thorpej sc->sc_is.is_type = ist;
228 1.5 thorpej sc->sc_is.is_ipl = ipl;
229 1.5 thorpej sc->sc_is.is_mpsafe = (flags & FDT_INTR_MPSAFE) ? true : false;
230 1.5 thorpej
231 1.5 thorpej mutex_exit(&sc->sc_intr_lock);
232 1.5 thorpej
233 1.5 thorpej sc->sc_ih = fdtbus_intr_establish(sc->sc_phandle, 0, ipl, flags,
234 1.5 thorpej sunxi_nmi_intr, sc);
235 1.1 jmcneill
236 1.5 thorpej mutex_enter(&sc->sc_intr_lock);
237 1.1 jmcneill sunxi_nmi_irq_set_type(sc, irq_type);
238 1.1 jmcneill sunxi_nmi_irq_enable(sc, true);
239 1.5 thorpej mutex_exit(&sc->sc_intr_lock);
240 1.1 jmcneill
241 1.5 thorpej return &sc->sc_is;
242 1.5 thorpej }
243 1.5 thorpej
244 1.5 thorpej static void
245 1.5 thorpej sunxi_nmi_fdt_mask(device_t dev, void *ih __unused)
246 1.5 thorpej {
247 1.5 thorpej struct sunxi_nmi_softc * const sc = device_private(dev);
248 1.5 thorpej
249 1.5 thorpej mutex_enter(&sc->sc_intr_lock);
250 1.5 thorpej if (sc->sc_is.is_mask_count++ == 0) {
251 1.5 thorpej sunxi_nmi_irq_enable(sc, false);
252 1.5 thorpej }
253 1.5 thorpej mutex_exit(&sc->sc_intr_lock);
254 1.5 thorpej }
255 1.5 thorpej
256 1.5 thorpej static void
257 1.5 thorpej sunxi_nmi_fdt_unmask(device_t dev, void *ih __unused)
258 1.5 thorpej {
259 1.5 thorpej struct sunxi_nmi_softc * const sc = device_private(dev);
260 1.5 thorpej
261 1.5 thorpej mutex_enter(&sc->sc_intr_lock);
262 1.5 thorpej if (sc->sc_is.is_mask_count-- == 1) {
263 1.5 thorpej sunxi_nmi_irq_enable(sc, true);
264 1.5 thorpej }
265 1.5 thorpej mutex_exit(&sc->sc_intr_lock);
266 1.1 jmcneill }
267 1.1 jmcneill
268 1.1 jmcneill static void
269 1.1 jmcneill sunxi_nmi_fdt_disestablish(device_t dev, void *ih)
270 1.1 jmcneill {
271 1.1 jmcneill struct sunxi_nmi_softc * const sc = device_private(dev);
272 1.5 thorpej struct intrsource * const is = ih;
273 1.5 thorpej
274 1.5 thorpej KASSERT(is == &sc->sc_is);
275 1.1 jmcneill
276 1.5 thorpej mutex_enter(&sc->sc_intr_lock);
277 1.1 jmcneill sunxi_nmi_irq_enable(sc, false);
278 1.5 thorpej is->is_mask_count = 0;
279 1.5 thorpej mutex_exit(&sc->sc_intr_lock);
280 1.1 jmcneill
281 1.5 thorpej fdtbus_intr_disestablish(sc->sc_phandle, sc->sc_ih);
282 1.5 thorpej sc->sc_ih = NULL;
283 1.1 jmcneill
284 1.5 thorpej mutex_enter(&sc->sc_intr_lock);
285 1.5 thorpej is->is_arg = NULL;
286 1.5 thorpej is->is_func = NULL;
287 1.5 thorpej mutex_exit(&sc->sc_intr_lock);
288 1.1 jmcneill }
289 1.1 jmcneill
290 1.1 jmcneill static bool
291 1.1 jmcneill sunxi_nmi_fdt_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen)
292 1.1 jmcneill {
293 1.1 jmcneill struct sunxi_nmi_softc * const sc = device_private(dev);
294 1.1 jmcneill
295 1.1 jmcneill snprintf(buf, buflen, "%s", sc->sc_config->name);
296 1.1 jmcneill
297 1.1 jmcneill return true;
298 1.1 jmcneill }
299 1.1 jmcneill
300 1.1 jmcneill static const struct fdtbus_interrupt_controller_func sunxi_nmi_fdt_funcs = {
301 1.1 jmcneill .establish = sunxi_nmi_fdt_establish,
302 1.1 jmcneill .disestablish = sunxi_nmi_fdt_disestablish,
303 1.1 jmcneill .intrstr = sunxi_nmi_fdt_intrstr,
304 1.5 thorpej .mask = sunxi_nmi_fdt_mask,
305 1.5 thorpej .unmask = sunxi_nmi_fdt_unmask,
306 1.1 jmcneill };
307 1.1 jmcneill
308 1.1 jmcneill static int
309 1.1 jmcneill sunxi_nmi_match(device_t parent, cfdata_t cf, void *aux)
310 1.1 jmcneill {
311 1.1 jmcneill struct fdt_attach_args * const faa = aux;
312 1.1 jmcneill
313 1.1 jmcneill return of_match_compat_data(faa->faa_phandle, compat_data);
314 1.1 jmcneill }
315 1.1 jmcneill
316 1.1 jmcneill static void
317 1.1 jmcneill sunxi_nmi_attach(device_t parent, device_t self, void *aux)
318 1.1 jmcneill {
319 1.1 jmcneill struct sunxi_nmi_softc * const sc = device_private(self);
320 1.1 jmcneill struct fdt_attach_args * const faa = aux;
321 1.1 jmcneill const int phandle = faa->faa_phandle;
322 1.1 jmcneill bus_addr_t addr;
323 1.1 jmcneill bus_size_t size;
324 1.1 jmcneill int error;
325 1.1 jmcneill
326 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
327 1.1 jmcneill aprint_error(": couldn't get registers\n");
328 1.1 jmcneill return;
329 1.1 jmcneill }
330 1.1 jmcneill
331 1.1 jmcneill sc->sc_dev = self;
332 1.1 jmcneill sc->sc_phandle = phandle;
333 1.1 jmcneill sc->sc_config = (void *)of_search_compatible(phandle, compat_data)->data;
334 1.1 jmcneill sc->sc_bst = faa->faa_bst;
335 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
336 1.1 jmcneill aprint_error(": couldn't map registers\n");
337 1.1 jmcneill return;
338 1.1 jmcneill }
339 1.1 jmcneill
340 1.1 jmcneill aprint_naive("\n");
341 1.1 jmcneill aprint_normal(": %s\n", sc->sc_config->name);
342 1.1 jmcneill
343 1.5 thorpej mutex_init(&sc->sc_intr_lock, MUTEX_SPIN, IPL_HIGH);
344 1.5 thorpej
345 1.5 thorpej /*
346 1.5 thorpej * Normally it's assumed that an intrsource can be passed to
347 1.5 thorpej * interrupt_distribute(). We're providing our own that's
348 1.5 thorpej * independent of our parent PIC, but because we will leave
349 1.5 thorpej * the intrsource::is_pic field NULL, the right thing
350 1.5 thorpej * (i.e. nothing) will happen in interrupt_distribute().
351 1.5 thorpej */
352 1.5 thorpej snprintf(sc->sc_is.is_source, sizeof(sc->sc_is.is_source),
353 1.5 thorpej "%s", sc->sc_config->name);
354 1.5 thorpej
355 1.1 jmcneill sunxi_nmi_irq_enable(sc, false);
356 1.1 jmcneill sunxi_nmi_irq_ack(sc);
357 1.1 jmcneill
358 1.1 jmcneill error = fdtbus_register_interrupt_controller(self, phandle,
359 1.1 jmcneill &sunxi_nmi_fdt_funcs);
360 1.1 jmcneill if (error) {
361 1.1 jmcneill aprint_error_dev(self, "couldn't register with fdtbus: %d\n",
362 1.1 jmcneill error);
363 1.1 jmcneill return;
364 1.1 jmcneill }
365 1.1 jmcneill }
366 1.1 jmcneill
367 1.1 jmcneill CFATTACH_DECL_NEW(sunxi_nmi, sizeof(struct sunxi_nmi_softc),
368 1.1 jmcneill sunxi_nmi_match, sunxi_nmi_attach, NULL, NULL);
369