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