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