exynos_combiner.c revision 1.11 1 /* $NetBSD: exynos_combiner.c,v 1.11 2019/10/18 06:13:38 skrll 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 "opt_multiprocessor.h"
35 #include "gpio.h"
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(1, "$NetBSD: exynos_combiner.c,v 1.11 2019/10/18 06:13:38 skrll Exp $");
39
40 #include <sys/param.h>
41 #include <sys/bus.h>
42 #include <sys/cpu.h>
43 #include <sys/device.h>
44 #include <sys/intr.h>
45 #include <sys/systm.h>
46 #include <sys/kmem.h>
47
48 #include <arm/cortex/gic_intr.h>
49
50 #include <arm/samsung/exynos_reg.h>
51 #include <arm/samsung/exynos_intr.h>
52
53 #include <dev/fdt/fdtvar.h>
54
55 #define COMBINER_GROUP_BASE(group) (((group) / 4) * 0x10)
56 #define COMBINER_GROUP_MASK(group) (0xff << (((group) % 4) * 8))
57
58 #define COMBINER_IESR_REG(group) (COMBINER_GROUP_BASE(group) + 0x00)
59 #define COMBINER_IECR_REG(group) (COMBINER_GROUP_BASE(group) + 0x04)
60 #define COMBINER_ISTR_REG(group) (COMBINER_GROUP_BASE(group) + 0x08)
61 #define COMBINER_IMSR_REG(group) (COMBINER_GROUP_BASE(group) + 0x0c)
62
63 struct exynos_combiner_softc;
64
65 struct exynos_combiner_irq_entry {
66 int irq_no;
67 int (*irq_handler)(void *);
68 void * irq_arg;
69 struct exynos_combiner_irq_entry *irq_next;
70 bool irq_mpsafe;
71 };
72
73 struct exynos_combiner_irq_group {
74 int irq_group_no;
75 struct exynos_combiner_softc *irq_sc;
76 struct exynos_combiner_irq_entry *irq_entries;
77 struct exynos_combiner_irq_group *irq_group_next;
78 void *irq_ih;
79 int irq_ipl;
80 };
81
82 struct exynos_combiner_softc {
83 device_t sc_dev;
84 bus_space_tag_t sc_bst;
85 bus_space_handle_t sc_bsh;
86 int sc_phandle;
87 struct exynos_combiner_irq_group *irq_groups;
88 };
89
90 static int exynos_combiner_match(device_t, cfdata_t, void *);
91 static void exynos_combiner_attach(device_t, device_t, void *);
92
93 static void * exynos_combiner_establish(device_t, u_int *, int, int,
94 int (*)(void *), void *);
95 static void exynos_combiner_disestablish(device_t, void *);
96 static bool exynos_combiner_intrstr(device_t, u_int *, char *,
97 size_t);
98
99 struct fdtbus_interrupt_controller_func exynos_combiner_funcs = {
100 .establish = exynos_combiner_establish,
101 .disestablish = exynos_combiner_disestablish,
102 .intrstr = exynos_combiner_intrstr
103 };
104
105 CFATTACH_DECL_NEW(exynos_intr, sizeof(struct exynos_combiner_softc),
106 exynos_combiner_match, exynos_combiner_attach, NULL, NULL);
107
108 static int
109 exynos_combiner_match(device_t parent, cfdata_t cf, void *aux)
110 {
111 const char * const compatible[] = { "samsung,exynos4210-combiner",
112 NULL };
113 struct fdt_attach_args * const faa = aux;
114 return of_match_compatible(faa->faa_phandle, compatible);
115 }
116
117 static void
118 exynos_combiner_attach(device_t parent, device_t self, void *aux)
119 {
120 struct exynos_combiner_softc * const sc = device_private(self);
121 struct fdt_attach_args * const faa = aux;
122 bus_addr_t addr;
123 bus_size_t size;
124 int error;
125
126 if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
127 aprint_error(": couldn't get registers\n");
128 return;
129 }
130
131 sc->sc_dev = self;
132 sc->sc_phandle = faa->faa_phandle;
133 sc->sc_bst = faa->faa_bst;
134
135 error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
136 if (error) {
137 aprint_error(": couldn't map %#" PRIxBUSADDR ": %d",
138 addr, error);
139 return;
140 }
141
142 error = fdtbus_register_interrupt_controller(self, faa->faa_phandle,
143 &exynos_combiner_funcs);
144 if (error) {
145 aprint_error(": couldn't register with fdtbus: %d\n", error);
146 return;
147 }
148
149 aprint_naive("\n");
150 aprint_normal(" @ 0x%08x: interrupt combiner\n", (uint)addr);
151
152 }
153
154 static struct exynos_combiner_irq_group *
155 exynos_combiner_new_group(struct exynos_combiner_softc *sc, int group_no)
156 {
157 struct exynos_combiner_irq_group *n = kmem_zalloc(sizeof(*n),
158 KM_SLEEP);
159 n->irq_group_no = group_no;
160 n->irq_group_next = sc->irq_groups;
161 n->irq_sc = sc;
162 sc->irq_groups = n;
163 return n;
164 }
165
166 static struct exynos_combiner_irq_group *
167 exynos_combiner_get_group(struct exynos_combiner_softc *sc, int group_no)
168 {
169 for (struct exynos_combiner_irq_group *g = sc->irq_groups;
170 g; g = g->irq_group_next) {
171 if (g->irq_group_no == group_no)
172 return g;
173 }
174 return NULL;
175 }
176
177 static struct exynos_combiner_irq_entry *
178 exynos_combiner_new_irq(struct exynos_combiner_irq_group *group,
179 int irq, bool mpsafe, int (*func)(void *), void *arg)
180 {
181 struct exynos_combiner_irq_entry * n = kmem_zalloc(sizeof(*n),
182 KM_SLEEP);
183 n->irq_no = irq;
184 n->irq_handler = func;
185 n->irq_next = group->irq_entries;
186 n->irq_arg = arg;
187 n->irq_mpsafe = mpsafe;
188 group->irq_entries = n;
189 return n;
190 }
191
192 static struct exynos_combiner_irq_entry *
193 exynos_combiner_get_irq(struct exynos_combiner_irq_group *g, int irq)
194 {
195 for (struct exynos_combiner_irq_entry *p = g->irq_entries; p;
196 p = p->irq_next) {
197 if (p->irq_no == irq)
198 return p;
199 }
200 return NULL;
201 }
202
203 static int
204 exynos_combiner_irq(void *cookie)
205 {
206 struct exynos_combiner_irq_group *groupp = cookie;
207 struct exynos_combiner_softc *sc = groupp->irq_sc;
208 int rv = 0;
209
210 const int group_no = groupp->irq_group_no;
211
212 const uint32_t istr =
213 bus_space_read_4(sc->sc_bst, sc->sc_bsh, COMBINER_ISTR_REG(group_no));
214 const uint32_t istatus = __SHIFTOUT(istr, COMBINER_GROUP_MASK(group_no));
215
216 for (int irq = 0; irq < 8; irq++) {
217 if (istatus & __BIT(irq)) {
218 struct exynos_combiner_irq_entry *e =
219 exynos_combiner_get_irq(groupp, irq);
220 if (e) {
221 if (!e->irq_mpsafe)
222 KERNEL_LOCK(1, curlwp);
223 rv += e->irq_handler(e->irq_arg);
224 if (!e->irq_mpsafe)
225 KERNEL_UNLOCK_ONE(curlwp);
226 } else
227 printf("%s: Unexpected irq (%d) on group %d\n", __func__,
228 irq, group_no);
229 }
230 }
231
232 return !!rv;
233 }
234
235 static void *
236 exynos_combiner_establish(device_t dev, u_int *specifier,
237 int ipl, int flags,
238 int (*func)(void *), void *arg)
239 {
240 struct exynos_combiner_softc * const sc = device_private(dev);
241 struct exynos_combiner_irq_group *groupp;
242 struct exynos_combiner_irq_entry *entryp;
243 const bool mpsafe = (flags & FDT_INTR_MPSAFE) != 0;
244 uint32_t iesr;
245
246 const u_int group = be32toh(specifier[0]);
247 const u_int intr = be32toh(specifier[1]);
248
249 groupp = exynos_combiner_get_group(sc, group);
250 if (!groupp) {
251 groupp = exynos_combiner_new_group(sc, group);
252 if (arg == NULL) {
253 groupp->irq_ih = fdtbus_intr_establish(sc->sc_phandle, group,
254 ipl /* XXX */, flags, func, NULL);
255 } else {
256 groupp->irq_ih = fdtbus_intr_establish(sc->sc_phandle, group,
257 ipl /* XXX */, FDT_INTR_MPSAFE, exynos_combiner_irq,
258 groupp);
259 }
260 KASSERT(groupp->irq_ih != NULL);
261 groupp->irq_ipl = ipl;
262 } else if (groupp->irq_ipl != ipl) {
263 aprint_error_dev(dev,
264 "interrupt combiner cannot share interrupts with different ipl\n");
265 return NULL;
266 }
267
268 if (exynos_combiner_get_irq(groupp, intr) != NULL)
269 return NULL;
270
271 entryp = exynos_combiner_new_irq(groupp, intr, mpsafe, func, arg);
272
273 iesr = bus_space_read_4(sc->sc_bst, sc->sc_bsh, COMBINER_IESR_REG(group));
274 iesr |= __SHIFTIN((1 << intr), COMBINER_GROUP_MASK(group));
275 bus_space_write_4(sc->sc_bst, sc->sc_bsh, COMBINER_IESR_REG(group), iesr);
276
277 return entryp;
278 }
279
280 static void
281 exynos_combiner_disestablish(device_t dev, void *ih)
282 {
283 /* MJF: Find the ih and disable the handler. */
284 panic("exynos_combiner_disestablish not implemented");
285 }
286
287 static bool
288 exynos_combiner_intrstr(device_t dev, u_int *specifier, char *buf,
289 size_t buflen)
290 {
291
292 /* 1st cell is the combiner group number */
293 /* 2nd cell is the interrupt number within the group */
294
295 const u_int group = be32toh(specifier[0]);
296 const u_int intr = be32toh(specifier[1]);
297
298 snprintf(buf, buflen, "interrupt combiner group %d intr %d", group, intr);
299
300 return true;
301 }
302