exynos_combiner.c revision 1.7 1 /* $NetBSD: exynos_combiner.c,v 1.7 2017/06/11 16:19:27 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2015 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Marty Fouts
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "opt_exynos.h"
33 #include "opt_arm_debug.h"
34 #include "gpio.h"
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(1, "$NetBSD: exynos_combiner.c,v 1.7 2017/06/11 16:19:27 jmcneill Exp $");
38
39 #include <sys/param.h>
40 #include <sys/bus.h>
41 #include <sys/device.h>
42 #include <sys/intr.h>
43 #include <sys/systm.h>
44 #include <sys/kmem.h>
45
46 #include <arm/cortex/gic_intr.h>
47
48 #include <arm/samsung/exynos_reg.h>
49 #include <arm/samsung/exynos_intr.h>
50
51 #include <dev/fdt/fdtvar.h>
52
53 #define COMBINER_IESR_OFFSET 0x00
54 #define COMBINER_IECR_OFFSET 0x04
55 #define COMBINER_ISTR_OFFSET 0x08
56 #define COMBINER_IMSR_OFFSET 0x0C
57 #define COMBINER_GROUP_SIZE 0x10
58 #define COMBINER_IRQS_PER_BLOCK 8
59 #define COMBINER_BLOCKS_PER_GROUP 4
60 #define COMBINER_N_BLOCKS 32
61
62 struct exynos_combiner_softc;
63
64 struct exynos_combiner_irq_entry {
65 int irq_no;
66 int (*irq_handler)(void *);
67 void * irq_arg;
68 struct exynos_combiner_irq_entry *irq_next;
69 bool irq_mpsafe;
70 };
71
72 struct exynos_combiner_irq_block {
73 int irq_block_no;
74 struct exynos_combiner_softc *irq_sc;
75 struct exynos_combiner_irq_entry *irq_entries;
76 struct exynos_combiner_irq_block *irq_block_next;
77 void *irq_ih;
78 };
79
80 struct exynos_combiner_softc {
81 device_t sc_dev;
82 bus_space_tag_t sc_bst;
83 bus_space_handle_t sc_bsh;
84 int sc_phandle;
85 struct exynos_combiner_irq_block *irq_blocks;
86 };
87
88 static int exynos_combiner_match(device_t, cfdata_t, void *);
89 static void exynos_combiner_attach(device_t, device_t, void *);
90
91 static void * exynos_combiner_establish(device_t, u_int *, int, int,
92 int (*)(void *), void *);
93 static void exynos_combiner_disestablish(device_t, void *);
94 static bool exynos_combiner_intrstr(device_t, u_int *, char *,
95 size_t);
96
97 struct fdtbus_interrupt_controller_func exynos_combiner_funcs = {
98 .establish = exynos_combiner_establish,
99 .disestablish = exynos_combiner_disestablish,
100 .intrstr = exynos_combiner_intrstr
101 };
102
103 CFATTACH_DECL_NEW(exynos_intr, sizeof(struct exynos_combiner_softc),
104 exynos_combiner_match, exynos_combiner_attach, NULL, NULL);
105
106 static int
107 exynos_combiner_match(device_t parent, cfdata_t cf, void *aux)
108 {
109 const char * const compatible[] = { "samsung,exynos4210-combiner",
110 NULL };
111 struct fdt_attach_args * const faa = aux;
112 return of_match_compatible(faa->faa_phandle, compatible);
113 }
114
115 static void
116 exynos_combiner_attach(device_t parent, device_t self, void *aux)
117 {
118 struct exynos_combiner_softc * const sc = device_private(self);
119 struct fdt_attach_args * const faa = aux;
120 bus_addr_t addr;
121 bus_size_t size;
122 int error;
123
124 if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
125 aprint_error(": couldn't get registers\n");
126 return;
127 }
128
129 sc->sc_dev = self;
130 sc->sc_phandle = faa->faa_phandle;
131 sc->sc_bst = faa->faa_bst;
132 sc->irq_blocks = NULL;
133
134 error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
135 if (error) {
136 aprint_error(": couldn't map %#llx: %d",
137 (uint64_t)addr, error);
138 return;
139 }
140
141 error = fdtbus_register_interrupt_controller(self, faa->faa_phandle,
142 &exynos_combiner_funcs);
143 if (error) {
144 aprint_error(": couldn't register with fdtbus: %d\n", error);
145 return;
146 }
147
148 aprint_normal(" @ 0x%08x: interrupt combiner", (uint)addr);
149 aprint_naive("\n");
150 aprint_normal("\n");
151
152 }
153
154 static struct exynos_combiner_irq_block *
155 exynos_combiner_new_block(struct exynos_combiner_softc *sc, int block_no)
156 {
157 struct exynos_combiner_irq_block *n = kmem_zalloc(sizeof(*n),
158 KM_SLEEP);
159 n->irq_block_no = block_no;
160 n->irq_block_next = sc->irq_blocks;
161 sc->irq_blocks = n;
162 return n;
163 }
164
165 static struct exynos_combiner_irq_block *
166 exynos_combiner_get_block(struct exynos_combiner_softc *sc, int block)
167 {
168 for (struct exynos_combiner_irq_block *b = sc->irq_blocks;
169 b; b = b->irq_block_next) {
170 if (b->irq_block_no == block)
171 return b;
172 }
173 return NULL;
174 }
175
176 static struct exynos_combiner_irq_entry *
177 exynos_combiner_new_irq(struct exynos_combiner_irq_block *block,
178 int irq, bool mpsafe, int (*func)(void *), void *arg)
179 {
180 struct exynos_combiner_irq_entry * n = kmem_zalloc(sizeof(*n),
181 KM_SLEEP);
182 n->irq_no = irq;
183 n->irq_handler = func;
184 n->irq_next = block->irq_entries;
185 n->irq_arg = arg;
186 n->irq_mpsafe = mpsafe;
187 block->irq_entries = n;
188 return n;
189 }
190
191 static struct exynos_combiner_irq_entry *
192 exynos_combiner_get_irq(struct exynos_combiner_irq_block *b, int irq)
193 {
194 for (struct exynos_combiner_irq_entry *p = b->irq_entries; p;
195 p = p->irq_next) {
196 if (p->irq_no == irq)
197 return p;
198 }
199 return NULL;
200 }
201
202 static int
203 exynos_combiner_irq(void *cookie)
204 {
205 struct exynos_combiner_irq_block *blockp = cookie;
206 struct exynos_combiner_softc *sc = blockp->irq_sc;
207 int intr = blockp->irq_block_no;
208 int iblock =
209 intr / COMBINER_BLOCKS_PER_GROUP * COMBINER_GROUP_SIZE
210 + COMBINER_IESR_OFFSET;
211 int istatus =
212 bus_space_read_4(sc->sc_bst, sc->sc_bsh, iblock);
213 istatus >>= (intr % 4) *8;
214 for (int irq = 0; irq < 8; irq++) {
215 if (istatus & 1 << irq) {
216 struct exynos_combiner_irq_entry *e =
217 exynos_combiner_get_irq(blockp, irq);
218 if (e) {
219 if (!e->irq_mpsafe)
220 KERNEL_LOCK(1, curlwp);
221 e->irq_handler(e->irq_arg);
222 if (!e->irq_mpsafe)
223 KERNEL_UNLOCK_ONE(curlwp);
224 } else
225 printf("%s: Unexpected irq %d, %d\n", __func__,
226 intr, irq);
227 }
228 }
229 return 0;
230 }
231
232 static void *
233 exynos_combiner_establish(device_t dev, u_int *specifier,
234 int ipl, int flags,
235 int (*func)(void *), void *arg)
236 {
237 struct exynos_combiner_softc * const sc = device_private(dev);
238 struct exynos_combiner_irq_block *blockp;
239 struct exynos_combiner_irq_entry *entryp;
240 const bool mpsafe = (flags & FDT_INTR_MPSAFE) != 0;
241
242 const u_int intr = be32toh(specifier[0]);
243 const u_int irq = be32toh(specifier[1]);
244
245 int iblock =
246 intr / COMBINER_BLOCKS_PER_GROUP * COMBINER_GROUP_SIZE
247 + COMBINER_IESR_OFFSET;
248
249 blockp = exynos_combiner_get_block(sc, intr);
250 if (!blockp) {
251 blockp = exynos_combiner_new_block(sc, intr);
252 KASSERT(blockp);
253 blockp->irq_ih = fdtbus_intr_establish(sc->sc_phandle, intr,
254 IPL_VM /* XXX */, FDT_INTR_MPSAFE, exynos_combiner_irq,
255 blockp);
256 }
257
258 entryp = exynos_combiner_get_irq(blockp, irq);
259 if (entryp)
260 return NULL;
261 entryp = exynos_combiner_new_irq(blockp, irq, mpsafe, func, arg);
262 KASSERT(entryp);
263
264 int istatus =
265 bus_space_read_4(sc->sc_bst, sc->sc_bsh, iblock);
266 istatus |= 1 << (irq + ((intr % 4) * 8));
267 bus_space_write_4(sc->sc_bst, sc->sc_bsh, iblock, istatus);
268 return (void *)istatus;
269 }
270
271 static void
272 exynos_combiner_disestablish(device_t dev, void *ih)
273 {
274 /* MJF: Find the ih and disable the handler. */
275 panic("exynos_combiner_disestablish not implemented");
276 }
277
278 static bool
279 exynos_combiner_intrstr(device_t dev, u_int *specifier, char *buf,
280 size_t buflen)
281 {
282
283 /* 1st cell is the interrupt block */
284 /* 2nd cell is the interrupt number */
285
286 const u_int intr = be32toh(specifier[0]);
287 const u_int irq = be32toh(specifier[1]);
288
289 snprintf(buf, buflen, "combiner intr %d irq %d", intr, irq);
290
291 return true;
292 }
293