sunxi_intc.c revision 1.8 1 1.8 jmcneill /* $NetBSD: sunxi_intc.c,v 1.8 2022/06/25 12:41:56 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2017 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.8 jmcneill __KERNEL_RCSID(0, "$NetBSD: sunxi_intc.c,v 1.8 2022/06/25 12:41:56 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.4 skrll #include <sys/kernel.h>
39 1.5 skrll #include <sys/lwp.h>
40 1.1 jmcneill #include <sys/systm.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 #define INTC_MAX_SOURCES 96
49 1.1 jmcneill #define INTC_MAX_GROUPS 3
50 1.1 jmcneill
51 1.1 jmcneill #define INTC_VECTOR_REG 0x00
52 1.1 jmcneill #define INTC_BASE_ADDR_REG 0x04
53 1.1 jmcneill #define INTC_PROTECT_REG 0x08
54 1.1 jmcneill #define INTC_PROTECT_EN __BIT(0)
55 1.1 jmcneill #define INTC_NMII_CTRL_REG 0x0c
56 1.1 jmcneill #define INTC_IRQ_PEND_REG(n) (0x10 + ((n) * 4))
57 1.1 jmcneill #define INTC_FIQ_PEND_REG(n) (0x20 + ((n) * 4))
58 1.1 jmcneill #define INTC_SEL_REG(n) (0x30 + ((n) * 4))
59 1.1 jmcneill #define INTC_EN_REG(n) (0x40 + ((n) * 4))
60 1.1 jmcneill #define INTC_MASK_REG(n) (0x50 + ((n) * 4))
61 1.1 jmcneill #define INTC_RESP_REG(n) (0x60 + ((n) * 4))
62 1.1 jmcneill #define INTC_FORCE_REG(n) (0x70 + ((n) * 4))
63 1.1 jmcneill #define INTC_SRC_PRIO_REG(n) (0x80 + ((n) * 4))
64 1.1 jmcneill
65 1.7 thorpej static const struct device_compatible_entry compat_data[] = {
66 1.7 thorpej { .compat = "allwinner,sun4i-a10-ic" },
67 1.7 thorpej DEVICE_COMPAT_EOL
68 1.1 jmcneill };
69 1.1 jmcneill
70 1.1 jmcneill struct sunxi_intc_softc {
71 1.1 jmcneill device_t sc_dev;
72 1.1 jmcneill bus_space_tag_t sc_bst;
73 1.1 jmcneill bus_space_handle_t sc_bsh;
74 1.1 jmcneill int sc_phandle;
75 1.1 jmcneill
76 1.1 jmcneill uint32_t sc_enabled_irqs[INTC_MAX_GROUPS];
77 1.1 jmcneill
78 1.1 jmcneill struct pic_softc sc_pic;
79 1.1 jmcneill };
80 1.1 jmcneill
81 1.1 jmcneill static struct sunxi_intc_softc *intc_softc;
82 1.1 jmcneill
83 1.1 jmcneill #define PICTOSOFTC(pic) \
84 1.1 jmcneill ((void *)((uintptr_t)(pic) - offsetof(struct sunxi_intc_softc, sc_pic)))
85 1.1 jmcneill
86 1.1 jmcneill #define INTC_READ(sc, reg) \
87 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
88 1.1 jmcneill #define INTC_WRITE(sc, reg, val) \
89 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
90 1.1 jmcneill
91 1.1 jmcneill static void
92 1.1 jmcneill sunxi_intc_unblock_irqs(struct pic_softc *pic, size_t irqbase, uint32_t mask)
93 1.1 jmcneill {
94 1.1 jmcneill struct sunxi_intc_softc * const sc = PICTOSOFTC(pic);
95 1.1 jmcneill const u_int group = irqbase / 32;
96 1.1 jmcneill
97 1.1 jmcneill KASSERT((mask & sc->sc_enabled_irqs[group]) == 0);
98 1.1 jmcneill sc->sc_enabled_irqs[group] |= mask;
99 1.1 jmcneill INTC_WRITE(sc, INTC_EN_REG(group), sc->sc_enabled_irqs[group]);
100 1.3 jmcneill INTC_WRITE(sc, INTC_MASK_REG(group), ~sc->sc_enabled_irqs[group]);
101 1.1 jmcneill }
102 1.1 jmcneill
103 1.1 jmcneill static void
104 1.1 jmcneill sunxi_intc_block_irqs(struct pic_softc *pic, size_t irqbase, uint32_t mask)
105 1.1 jmcneill {
106 1.1 jmcneill struct sunxi_intc_softc * const sc = PICTOSOFTC(pic);
107 1.1 jmcneill const u_int group = irqbase / 32;
108 1.1 jmcneill
109 1.1 jmcneill sc->sc_enabled_irqs[group] &= ~mask;
110 1.1 jmcneill INTC_WRITE(sc, INTC_EN_REG(group), sc->sc_enabled_irqs[group]);
111 1.3 jmcneill INTC_WRITE(sc, INTC_MASK_REG(group), ~sc->sc_enabled_irqs[group]);
112 1.1 jmcneill }
113 1.1 jmcneill
114 1.1 jmcneill static void
115 1.1 jmcneill sunxi_intc_establish_irq(struct pic_softc *pic, struct intrsource *is)
116 1.1 jmcneill {
117 1.1 jmcneill KASSERT(is->is_irq < INTC_MAX_SOURCES);
118 1.1 jmcneill KASSERT(is->is_type == IST_LEVEL);
119 1.1 jmcneill }
120 1.1 jmcneill
121 1.1 jmcneill static void
122 1.1 jmcneill sunxi_intc_set_priority(struct pic_softc *pic, int ipl)
123 1.1 jmcneill {
124 1.8 jmcneill curcpu()->ci_cpl = ipl;
125 1.1 jmcneill }
126 1.1 jmcneill
127 1.1 jmcneill static const struct pic_ops sunxi_intc_picops = {
128 1.1 jmcneill .pic_unblock_irqs = sunxi_intc_unblock_irqs,
129 1.1 jmcneill .pic_block_irqs = sunxi_intc_block_irqs,
130 1.1 jmcneill .pic_establish_irq = sunxi_intc_establish_irq,
131 1.1 jmcneill .pic_set_priority = sunxi_intc_set_priority,
132 1.1 jmcneill };
133 1.1 jmcneill
134 1.1 jmcneill static void *
135 1.1 jmcneill sunxi_intc_fdt_establish(device_t dev, u_int *specifier, int ipl, int flags,
136 1.6 jmcneill int (*func)(void *), void *arg, const char *xname)
137 1.1 jmcneill {
138 1.1 jmcneill /* 1st cell is the interrupt number */
139 1.1 jmcneill const u_int irq = be32toh(specifier[0]);
140 1.1 jmcneill
141 1.1 jmcneill if (irq >= INTC_MAX_SOURCES) {
142 1.1 jmcneill #ifdef DIAGNOSTIC
143 1.1 jmcneill device_printf(dev, "IRQ %u is invalid\n", irq);
144 1.1 jmcneill #endif
145 1.1 jmcneill return NULL;
146 1.1 jmcneill }
147 1.1 jmcneill
148 1.1 jmcneill const u_int mpsafe = (flags & FDT_INTR_MPSAFE) ? IST_MPSAFE : 0;
149 1.1 jmcneill
150 1.6 jmcneill return intr_establish_xname(irq, ipl, IST_LEVEL | mpsafe, func, arg,
151 1.6 jmcneill xname);
152 1.1 jmcneill }
153 1.1 jmcneill
154 1.1 jmcneill static void
155 1.1 jmcneill sunxi_intc_fdt_disestablish(device_t dev, void *ih)
156 1.1 jmcneill {
157 1.1 jmcneill intr_disestablish(ih);
158 1.1 jmcneill }
159 1.1 jmcneill
160 1.1 jmcneill static bool
161 1.1 jmcneill sunxi_intc_fdt_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen)
162 1.1 jmcneill {
163 1.1 jmcneill /* 1st cell is the interrupt number */
164 1.1 jmcneill if (!specifier)
165 1.1 jmcneill return false;
166 1.1 jmcneill const u_int irq = be32toh(specifier[0]);
167 1.1 jmcneill
168 1.1 jmcneill snprintf(buf, buflen, "INTC irq %d", irq);
169 1.1 jmcneill
170 1.1 jmcneill return true;
171 1.1 jmcneill }
172 1.1 jmcneill
173 1.1 jmcneill static const struct fdtbus_interrupt_controller_func sunxi_intc_fdt_funcs = {
174 1.1 jmcneill .establish = sunxi_intc_fdt_establish,
175 1.1 jmcneill .disestablish = sunxi_intc_fdt_disestablish,
176 1.1 jmcneill .intrstr = sunxi_intc_fdt_intrstr,
177 1.1 jmcneill };
178 1.1 jmcneill
179 1.1 jmcneill static int
180 1.1 jmcneill sunxi_intc_find_pending_irqs(struct sunxi_intc_softc *sc, u_int group)
181 1.1 jmcneill {
182 1.1 jmcneill uint32_t pend;
183 1.1 jmcneill
184 1.1 jmcneill pend = INTC_READ(sc, INTC_IRQ_PEND_REG(group));
185 1.2 jmcneill pend &= sc->sc_enabled_irqs[group];
186 1.1 jmcneill
187 1.1 jmcneill if (pend == 0)
188 1.1 jmcneill return 0;
189 1.1 jmcneill
190 1.2 jmcneill INTC_WRITE(sc, INTC_IRQ_PEND_REG(group), pend);
191 1.2 jmcneill
192 1.1 jmcneill return pic_mark_pending_sources(&sc->sc_pic, group * 32, pend);
193 1.1 jmcneill }
194 1.1 jmcneill
195 1.1 jmcneill static void
196 1.1 jmcneill sunxi_intc_irq_handler(void *frame)
197 1.1 jmcneill {
198 1.1 jmcneill struct cpu_info * const ci = curcpu();
199 1.1 jmcneill struct sunxi_intc_softc * const sc = intc_softc;
200 1.1 jmcneill const int oldipl = ci->ci_cpl;
201 1.1 jmcneill const uint32_t oldipl_mask = __BIT(oldipl);
202 1.1 jmcneill int ipl_mask = 0;
203 1.1 jmcneill
204 1.1 jmcneill ci->ci_data.cpu_nintr++;
205 1.1 jmcneill
206 1.1 jmcneill if (sc->sc_enabled_irqs[0])
207 1.1 jmcneill ipl_mask |= sunxi_intc_find_pending_irqs(sc, 0);
208 1.1 jmcneill if (sc->sc_enabled_irqs[1])
209 1.1 jmcneill ipl_mask |= sunxi_intc_find_pending_irqs(sc, 1);
210 1.1 jmcneill if (sc->sc_enabled_irqs[2])
211 1.1 jmcneill ipl_mask |= sunxi_intc_find_pending_irqs(sc, 2);
212 1.1 jmcneill
213 1.1 jmcneill if ((ipl_mask & ~oldipl_mask) > oldipl_mask)
214 1.1 jmcneill pic_do_pending_ints(I32_bit, oldipl, frame);
215 1.1 jmcneill }
216 1.1 jmcneill
217 1.1 jmcneill static int
218 1.1 jmcneill sunxi_intc_match(device_t parent, cfdata_t cf, void *aux)
219 1.1 jmcneill {
220 1.1 jmcneill struct fdt_attach_args * const faa = aux;
221 1.1 jmcneill
222 1.7 thorpej return of_compatible_match(faa->faa_phandle, compat_data);
223 1.1 jmcneill }
224 1.1 jmcneill
225 1.1 jmcneill static void
226 1.1 jmcneill sunxi_intc_attach(device_t parent, device_t self, void *aux)
227 1.1 jmcneill {
228 1.1 jmcneill struct sunxi_intc_softc * const sc = device_private(self);
229 1.1 jmcneill struct fdt_attach_args * const faa = aux;
230 1.1 jmcneill const int phandle = faa->faa_phandle;
231 1.1 jmcneill bus_addr_t addr;
232 1.1 jmcneill bus_size_t size;
233 1.1 jmcneill int error, i;
234 1.1 jmcneill
235 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
236 1.1 jmcneill aprint_error(": couldn't get registers\n");
237 1.1 jmcneill return;
238 1.1 jmcneill }
239 1.1 jmcneill
240 1.1 jmcneill sc->sc_dev = self;
241 1.1 jmcneill sc->sc_phandle = phandle;
242 1.1 jmcneill sc->sc_bst = faa->faa_bst;
243 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
244 1.1 jmcneill aprint_error(": couldn't map registers\n");
245 1.1 jmcneill return;
246 1.1 jmcneill }
247 1.1 jmcneill
248 1.1 jmcneill aprint_naive("\n");
249 1.1 jmcneill aprint_normal(": Interrupt Controller\n");
250 1.1 jmcneill
251 1.1 jmcneill /* Disable IRQs */
252 1.1 jmcneill for (i = 0; i < INTC_MAX_GROUPS; i++) {
253 1.1 jmcneill INTC_WRITE(sc, INTC_EN_REG(i), 0);
254 1.3 jmcneill INTC_WRITE(sc, INTC_MASK_REG(i), ~0U);
255 1.2 jmcneill INTC_WRITE(sc, INTC_IRQ_PEND_REG(i),
256 1.2 jmcneill INTC_READ(sc, INTC_IRQ_PEND_REG(i)));
257 1.1 jmcneill }
258 1.1 jmcneill /* Disable user mode access to intc registers */
259 1.1 jmcneill INTC_WRITE(sc, INTC_PROTECT_REG, INTC_PROTECT_EN);
260 1.1 jmcneill
261 1.1 jmcneill sc->sc_pic.pic_ops = &sunxi_intc_picops;
262 1.1 jmcneill sc->sc_pic.pic_maxsources = INTC_MAX_SOURCES;
263 1.1 jmcneill snprintf(sc->sc_pic.pic_name, sizeof(sc->sc_pic.pic_name), "intc");
264 1.1 jmcneill pic_add(&sc->sc_pic, 0);
265 1.1 jmcneill
266 1.1 jmcneill error = fdtbus_register_interrupt_controller(self, phandle,
267 1.1 jmcneill &sunxi_intc_fdt_funcs);
268 1.1 jmcneill if (error) {
269 1.1 jmcneill aprint_error_dev(self, "couldn't register with fdtbus: %d\n",
270 1.1 jmcneill error);
271 1.1 jmcneill return;
272 1.1 jmcneill }
273 1.1 jmcneill
274 1.1 jmcneill KASSERT(intc_softc == NULL);
275 1.1 jmcneill intc_softc = sc;
276 1.1 jmcneill arm_fdt_irq_set_handler(sunxi_intc_irq_handler);
277 1.1 jmcneill }
278 1.1 jmcneill
279 1.1 jmcneill CFATTACH_DECL_NEW(sunxi_intc, sizeof(struct sunxi_intc_softc),
280 1.1 jmcneill sunxi_intc_match, sunxi_intc_attach, NULL, NULL);
281