sunxi_nmi.c revision 1.4 1 1.4 skrll /* $NetBSD: sunxi_nmi.c,v 1.4 2020/01/07 10:20:07 skrll 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.4 skrll __KERNEL_RCSID(0, "$NetBSD: sunxi_nmi.c,v 1.4 2020/01/07 10:20:07 skrll 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.4 skrll #include <sys/lwp.h>
41 1.1 jmcneill
42 1.1 jmcneill #include <dev/fdt/fdtvar.h>
43 1.1 jmcneill
44 1.1 jmcneill #include <arm/cpu.h>
45 1.1 jmcneill #include <arm/pic/picvar.h>
46 1.1 jmcneill #include <arm/fdt/arm_fdtvar.h>
47 1.1 jmcneill
48 1.1 jmcneill /* ctrl_reg */
49 1.1 jmcneill #define NMI_CTRL_IRQ_LOW_LEVEL 0
50 1.1 jmcneill #define NMI_CTRL_IRQ_LOW_EDGE 1
51 1.1 jmcneill #define NMI_CTRL_IRQ_HIGH_LEVEL 2
52 1.1 jmcneill #define NMI_CTRL_IRQ_HIGH_EDGE 3
53 1.1 jmcneill #define NMI_CTRL_IRQ_TYPE __BITS(1,0)
54 1.1 jmcneill
55 1.1 jmcneill /* pend_reg */
56 1.1 jmcneill #define NMI_PEND_IRQ_ACK __BIT(0)
57 1.1 jmcneill
58 1.1 jmcneill /* enable_reg */
59 1.1 jmcneill #define NMI_ENABLE_IRQEN __BIT(0)
60 1.1 jmcneill
61 1.1 jmcneill struct sunxi_nmi_config {
62 1.1 jmcneill const char * name;
63 1.1 jmcneill bus_size_t ctrl_reg;
64 1.1 jmcneill bus_size_t pend_reg;
65 1.1 jmcneill bus_size_t enable_reg;
66 1.1 jmcneill };
67 1.1 jmcneill
68 1.1 jmcneill static const struct sunxi_nmi_config sun7i_a20_sc_nmi_config = {
69 1.1 jmcneill .name = "NMI",
70 1.1 jmcneill .ctrl_reg = 0x00,
71 1.1 jmcneill .pend_reg = 0x04,
72 1.1 jmcneill .enable_reg = 0x08,
73 1.1 jmcneill };
74 1.1 jmcneill
75 1.1 jmcneill static const struct sunxi_nmi_config sun6i_a31_r_intc_config = {
76 1.1 jmcneill .name = "R_INTC",
77 1.1 jmcneill .ctrl_reg = 0x0c,
78 1.1 jmcneill .pend_reg = 0x10,
79 1.1 jmcneill .enable_reg = 0x40,
80 1.1 jmcneill };
81 1.1 jmcneill
82 1.2 jmcneill static const struct sunxi_nmi_config sun9i_a80_nmi_config = {
83 1.2 jmcneill .name = "NMI",
84 1.2 jmcneill .ctrl_reg = 0x00,
85 1.2 jmcneill .pend_reg = 0x04,
86 1.2 jmcneill .enable_reg = 0x08,
87 1.2 jmcneill };
88 1.2 jmcneill
89 1.1 jmcneill static const struct of_compat_data compat_data[] = {
90 1.1 jmcneill { "allwinner,sun7i-a20-sc-nmi", (uintptr_t)&sun7i_a20_sc_nmi_config },
91 1.1 jmcneill { "allwinner,sun6i-a31-r-intc", (uintptr_t)&sun6i_a31_r_intc_config },
92 1.2 jmcneill { "allwinner,sun9i-a80-nmi", (uintptr_t)&sun9i_a80_nmi_config },
93 1.1 jmcneill { NULL }
94 1.1 jmcneill };
95 1.1 jmcneill
96 1.1 jmcneill struct sunxi_nmi_softc {
97 1.1 jmcneill device_t sc_dev;
98 1.1 jmcneill bus_space_tag_t sc_bst;
99 1.1 jmcneill bus_space_handle_t sc_bsh;
100 1.1 jmcneill int sc_phandle;
101 1.1 jmcneill
102 1.1 jmcneill const struct sunxi_nmi_config *sc_config;
103 1.1 jmcneill
104 1.1 jmcneill int (*sc_func)(void *);
105 1.1 jmcneill void *sc_arg;
106 1.1 jmcneill };
107 1.1 jmcneill
108 1.1 jmcneill #define NMI_READ(sc, reg) \
109 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
110 1.1 jmcneill #define NMI_WRITE(sc, reg, val) \
111 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
112 1.1 jmcneill
113 1.1 jmcneill static void
114 1.1 jmcneill sunxi_nmi_irq_ack(struct sunxi_nmi_softc *sc)
115 1.1 jmcneill {
116 1.1 jmcneill uint32_t val;
117 1.1 jmcneill
118 1.1 jmcneill val = NMI_READ(sc, sc->sc_config->pend_reg);
119 1.1 jmcneill val |= NMI_PEND_IRQ_ACK;
120 1.1 jmcneill NMI_WRITE(sc, sc->sc_config->pend_reg, val);
121 1.1 jmcneill }
122 1.1 jmcneill
123 1.1 jmcneill static void
124 1.1 jmcneill sunxi_nmi_irq_enable(struct sunxi_nmi_softc *sc, bool on)
125 1.1 jmcneill {
126 1.1 jmcneill uint32_t val;
127 1.1 jmcneill
128 1.1 jmcneill val = NMI_READ(sc, sc->sc_config->enable_reg);
129 1.1 jmcneill if (on)
130 1.1 jmcneill val |= NMI_ENABLE_IRQEN;
131 1.1 jmcneill else
132 1.1 jmcneill val &= ~NMI_ENABLE_IRQEN;
133 1.1 jmcneill NMI_WRITE(sc, sc->sc_config->enable_reg, val);
134 1.1 jmcneill }
135 1.1 jmcneill
136 1.1 jmcneill static void
137 1.1 jmcneill sunxi_nmi_irq_set_type(struct sunxi_nmi_softc *sc, u_int irq_type)
138 1.1 jmcneill {
139 1.1 jmcneill uint32_t val;
140 1.1 jmcneill
141 1.1 jmcneill val = NMI_READ(sc, sc->sc_config->ctrl_reg);
142 1.1 jmcneill val &= ~NMI_CTRL_IRQ_TYPE;
143 1.1 jmcneill val |= __SHIFTIN(irq_type, NMI_CTRL_IRQ_TYPE);
144 1.1 jmcneill NMI_WRITE(sc, sc->sc_config->ctrl_reg, val);
145 1.1 jmcneill }
146 1.1 jmcneill
147 1.1 jmcneill static int
148 1.1 jmcneill sunxi_nmi_intr(void *priv)
149 1.1 jmcneill {
150 1.1 jmcneill struct sunxi_nmi_softc * const sc = priv;
151 1.1 jmcneill int rv = 0;
152 1.1 jmcneill
153 1.1 jmcneill if (sc->sc_func)
154 1.1 jmcneill rv = sc->sc_func(sc->sc_arg);
155 1.1 jmcneill
156 1.1 jmcneill sunxi_nmi_irq_ack(sc);
157 1.1 jmcneill
158 1.1 jmcneill return rv;
159 1.1 jmcneill }
160 1.1 jmcneill
161 1.1 jmcneill static void *
162 1.1 jmcneill sunxi_nmi_fdt_establish(device_t dev, u_int *specifier, int ipl, int flags,
163 1.1 jmcneill int (*func)(void *), void *arg)
164 1.1 jmcneill {
165 1.1 jmcneill struct sunxi_nmi_softc * const sc = device_private(dev);
166 1.1 jmcneill u_int irq_type;
167 1.1 jmcneill
168 1.1 jmcneill /* 1st cell is the interrupt number */
169 1.1 jmcneill const u_int irq = be32toh(specifier[0]);
170 1.1 jmcneill /* 2nd cell is polarity */
171 1.1 jmcneill const u_int pol = be32toh(specifier[1]);
172 1.1 jmcneill
173 1.1 jmcneill if (sc->sc_func != NULL) {
174 1.1 jmcneill #ifdef DIAGNOSTIC
175 1.1 jmcneill device_printf(dev, "%s in use\n", sc->sc_config->name);
176 1.1 jmcneill #endif
177 1.1 jmcneill return NULL;
178 1.1 jmcneill }
179 1.1 jmcneill
180 1.1 jmcneill if (irq != 0) {
181 1.1 jmcneill #ifdef DIAGNOSTIC
182 1.1 jmcneill device_printf(dev, "IRQ %u is invalid\n", irq);
183 1.1 jmcneill #endif
184 1.1 jmcneill return NULL;
185 1.1 jmcneill }
186 1.1 jmcneill
187 1.1 jmcneill switch (pol & 0x7) {
188 1.1 jmcneill case 1: /* IRQ_TYPE_EDGE_RISING */
189 1.1 jmcneill irq_type = NMI_CTRL_IRQ_HIGH_EDGE;
190 1.1 jmcneill break;
191 1.1 jmcneill case 2: /* IRQ_TYPE_EDGE_FALLING */
192 1.1 jmcneill irq_type = NMI_CTRL_IRQ_LOW_EDGE;
193 1.1 jmcneill break;
194 1.1 jmcneill case 3: /* IRQ_TYPE_LEVEL_HIGH */
195 1.1 jmcneill irq_type = NMI_CTRL_IRQ_HIGH_LEVEL;
196 1.1 jmcneill break;
197 1.1 jmcneill case 4: /* IRQ_TYPE_LEVEL_LOW */
198 1.1 jmcneill irq_type = NMI_CTRL_IRQ_LOW_LEVEL;
199 1.1 jmcneill break;
200 1.1 jmcneill default:
201 1.1 jmcneill irq_type = NMI_CTRL_IRQ_LOW_LEVEL;
202 1.1 jmcneill break;
203 1.1 jmcneill }
204 1.1 jmcneill
205 1.1 jmcneill sc->sc_func = func;
206 1.1 jmcneill sc->sc_arg = arg;
207 1.1 jmcneill
208 1.1 jmcneill sunxi_nmi_irq_set_type(sc, irq_type);
209 1.1 jmcneill sunxi_nmi_irq_enable(sc, true);
210 1.1 jmcneill
211 1.1 jmcneill return fdtbus_intr_establish(sc->sc_phandle, 0, ipl, flags,
212 1.1 jmcneill sunxi_nmi_intr, sc);
213 1.1 jmcneill }
214 1.1 jmcneill
215 1.1 jmcneill static void
216 1.1 jmcneill sunxi_nmi_fdt_disestablish(device_t dev, void *ih)
217 1.1 jmcneill {
218 1.1 jmcneill struct sunxi_nmi_softc * const sc = device_private(dev);
219 1.1 jmcneill
220 1.1 jmcneill sunxi_nmi_irq_enable(sc, false);
221 1.1 jmcneill
222 1.1 jmcneill fdtbus_intr_disestablish(sc->sc_phandle, ih);
223 1.1 jmcneill
224 1.1 jmcneill sc->sc_func = NULL;
225 1.1 jmcneill sc->sc_arg = NULL;
226 1.1 jmcneill }
227 1.1 jmcneill
228 1.1 jmcneill static bool
229 1.1 jmcneill sunxi_nmi_fdt_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen)
230 1.1 jmcneill {
231 1.1 jmcneill struct sunxi_nmi_softc * const sc = device_private(dev);
232 1.1 jmcneill
233 1.1 jmcneill snprintf(buf, buflen, "%s", sc->sc_config->name);
234 1.1 jmcneill
235 1.1 jmcneill return true;
236 1.1 jmcneill }
237 1.1 jmcneill
238 1.1 jmcneill static const struct fdtbus_interrupt_controller_func sunxi_nmi_fdt_funcs = {
239 1.1 jmcneill .establish = sunxi_nmi_fdt_establish,
240 1.1 jmcneill .disestablish = sunxi_nmi_fdt_disestablish,
241 1.1 jmcneill .intrstr = sunxi_nmi_fdt_intrstr,
242 1.1 jmcneill };
243 1.1 jmcneill
244 1.1 jmcneill static int
245 1.1 jmcneill sunxi_nmi_match(device_t parent, cfdata_t cf, void *aux)
246 1.1 jmcneill {
247 1.1 jmcneill struct fdt_attach_args * const faa = aux;
248 1.1 jmcneill
249 1.1 jmcneill return of_match_compat_data(faa->faa_phandle, compat_data);
250 1.1 jmcneill }
251 1.1 jmcneill
252 1.1 jmcneill static void
253 1.1 jmcneill sunxi_nmi_attach(device_t parent, device_t self, void *aux)
254 1.1 jmcneill {
255 1.1 jmcneill struct sunxi_nmi_softc * const sc = device_private(self);
256 1.1 jmcneill struct fdt_attach_args * const faa = aux;
257 1.1 jmcneill const int phandle = faa->faa_phandle;
258 1.1 jmcneill bus_addr_t addr;
259 1.1 jmcneill bus_size_t size;
260 1.1 jmcneill int error;
261 1.1 jmcneill
262 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
263 1.1 jmcneill aprint_error(": couldn't get registers\n");
264 1.1 jmcneill return;
265 1.1 jmcneill }
266 1.1 jmcneill
267 1.1 jmcneill sc->sc_dev = self;
268 1.1 jmcneill sc->sc_phandle = phandle;
269 1.1 jmcneill sc->sc_config = (void *)of_search_compatible(phandle, compat_data)->data;
270 1.1 jmcneill sc->sc_bst = faa->faa_bst;
271 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
272 1.1 jmcneill aprint_error(": couldn't map registers\n");
273 1.1 jmcneill return;
274 1.1 jmcneill }
275 1.1 jmcneill
276 1.1 jmcneill aprint_naive("\n");
277 1.1 jmcneill aprint_normal(": %s\n", sc->sc_config->name);
278 1.1 jmcneill
279 1.1 jmcneill sunxi_nmi_irq_enable(sc, false);
280 1.1 jmcneill sunxi_nmi_irq_ack(sc);
281 1.1 jmcneill
282 1.1 jmcneill error = fdtbus_register_interrupt_controller(self, phandle,
283 1.1 jmcneill &sunxi_nmi_fdt_funcs);
284 1.1 jmcneill if (error) {
285 1.1 jmcneill aprint_error_dev(self, "couldn't register with fdtbus: %d\n",
286 1.1 jmcneill error);
287 1.1 jmcneill return;
288 1.1 jmcneill }
289 1.1 jmcneill }
290 1.1 jmcneill
291 1.1 jmcneill CFATTACH_DECL_NEW(sunxi_nmi, sizeof(struct sunxi_nmi_softc),
292 1.1 jmcneill sunxi_nmi_match, sunxi_nmi_attach, NULL, NULL);
293