s3c2440_extint.c revision 1.5 1 /*-
2 * Copyright (c) 2012 The NetBSD Foundation, Inc.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Paul Fleischer <paul (at) xpg.dk>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 /* Derived from s3c2410_extint.c */
31
32 /*
33 * Copyright (c) 2003 Genetec corporation. All rights reserved.
34 * Written by Hiroyuki Bessho for Genetec corporation.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. The name of Genetec corporation may not be used to endorse
45 * or promote products derived from this software without specific prior
46 * written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY GENETEC CORP. ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
50 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
51 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENETEC CORP.
52 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
53 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
54 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
55 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
56 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
57 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
58 * POSSIBILITY OF SUCH DAMAGE.
59 */
60
61 /*
62 * device driver to handle cascaded external interrupts of S3C2440.
63 *
64 * EXTINT [8..23] are cascaded to IRQ #5
65 * EXTINT [4..7] are cascaded to IRQ #4
66 * EXTINT [0..3] are not cascaded and connected directly as IRQ #[0..3].
67 * EXTINT [0..3] are handled by main interrupt handler in s3c2440_intr.c.
68 */
69
70 #include <sys/cdefs.h>
71 __KERNEL_RCSID(0, "$NetBSD: s3c2440_extint.c,v 1.5 2021/08/07 16:18:45 thorpej Exp $");
72
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/malloc.h>
76 #include <uvm/uvm_extern.h>
77 #include <sys/bus.h>
78 #include <machine/intr.h>
79 #include <arm/cpufunc.h>
80
81 #include <arm/s3c2xx0/s3c2440reg.h>
82 #include <arm/s3c2xx0/s3c2440var.h>
83
84 #include "locators.h"
85 #include "opt_s3c2440.h" /* for S3C2410_EXTINT_MAX */
86
87 #ifndef S3C2440_EXTINT_MAX
88 #define S3C2440_EXTINT_MAX 23
89 #endif
90
91 #define EXTINT_CASCADE_MIN 4
92
93 #if S3C2440_EXTINT_MAX < EXTINT_CASCADE_MIN
94 #error "Don't enable ssextio if you don't use extint[4..23]."
95 #endif
96
97 #define N_EXTINT (S3C2440_EXTINT_MAX - EXTINT_CASCADE_MIN +1)
98
99 struct ssextio_softc {
100 device_t sc_dev;
101
102 bus_space_tag_t sc_iot;
103 bus_space_handle_t sc_ioh;
104
105 uint32_t sc_pending;
106 uint32_t sc_mask;
107
108 struct extint_handler {
109 int (* func)(void *);
110 void *arg;
111 void *sh; /* softintr handler */
112 int level; /* IPL */
113 } sc_handler[N_EXTINT];
114
115 };
116
117 static struct ssextio_softc *ssextio_softc = NULL;
118
119 /* cookies */
120 #define EXTINT_4_7 1
121 #define EXTINT_8_23 2
122
123 /* prototypes */
124 static int ssextio_match(device_t, cfdata_t, void *);
125 static void ssextio_attach(device_t, device_t, void *);
126 static int ssextio_search(device_t, cfdata_t, const int *, void *);
127 static int ssextio_print(void *, const char *);
128
129 static int ssextio_cascaded_intr(void *);
130 static void ssextio_softintr(void *);
131
132 static inline void
133 update_hw_mask(void)
134 {
135 bus_space_write_4(ssextio_softc->sc_iot, ssextio_softc->sc_ioh,
136 GPIO_EINTMASK, ssextio_softc->sc_mask | ssextio_softc->sc_pending);
137 }
138
139
140 /* attach structures */
141 CFATTACH_DECL_NEW(ssextio, sizeof(struct ssextio_softc), ssextio_match, ssextio_attach,
142 NULL, NULL);
143
144 static int
145 ssextio_print(void *aux, const char *name)
146 {
147 struct s3c2xx0_attach_args *sa = (struct s3c2xx0_attach_args*)aux;
148
149 if (sa->sa_addr != SSEXTIOCF_ADDR_DEFAULT)
150 aprint_normal(" addr 0x%lx", sa->sa_addr);
151 if (sa->sa_intr > 0)
152 aprint_normal(" intr %d", sa->sa_intr);
153 return (UNCONF);
154 }
155
156 int
157 ssextio_match(device_t parent, cfdata_t match, void *aux)
158 {
159 #if S3C2440_EXTINT_MAX < 4
160 /* better not configure this driver */
161 return 0;
162 #else
163 if (ssextio_softc != NULL)
164 return 0;
165
166 return 1;
167 #endif
168 }
169
170 void
171 ssextio_attach(device_t parent, device_t self, void *aux)
172 {
173 struct ssextio_softc *sc = device_private(self);
174 struct s3c24x0_softc *cpuc = ((struct s3c2xx0_attach_args *)aux)->sa_sc;
175
176 aprint_normal("\n");
177
178 ssextio_softc = sc;
179
180 sc->sc_iot = cpuc->sc_sx.sc_iot;
181 sc->sc_ioh = cpuc->sc_sx.sc_gpio_ioh;
182
183 sc->sc_pending = 0;
184 sc->sc_mask = ~0;
185
186 s3c24x0_intr_establish(S3C2440_INT_4_7, IPL_HIGH, IST_NONE,
187 ssextio_cascaded_intr, (void *)EXTINT_4_7);
188 #if S3C2440_EXTINT_MAX >= 8
189 s3c24x0_intr_establish(S3C2440_INT_8_23, IPL_HIGH, IST_NONE,
190 ssextio_cascaded_intr, (void *)EXTINT_8_23);
191 #endif
192
193 /*
194 * Attach each devices
195 */
196 config_search(self, NULL,
197 CFARGS(.search = ssextio_search));
198 }
199
200 static int
201 ssextio_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
202 {
203 struct ssextio_softc *sc = device_private(parent);
204 struct s3c24x0_softc *cpuc = device_private(device_parent(parent));
205 struct s3c2xx0_attach_args sa;
206
207 sa.sa_sc = sc;
208 sa.sa_iot = sc->sc_iot;
209 sa.sa_addr = cf->cf_loc[SSEXTIOCF_ADDR];
210 sa.sa_size = cf->cf_loc[SSEXTIOCF_SIZE];
211 sa.sa_intr = cf->cf_loc[SSEXTIOCF_INTR];
212 sa.sa_dmat = cpuc->sc_sx.sc_dmat;
213
214 if (config_probe(parent, cf, &sa))
215 config_attach(parent, cf, &sa, ssextio_print, CFARGS_NONE);
216
217 return 0;
218 }
219
220 void *
221 s3c2440_extint_establish(int extint, int ipl, int type,
222 int (*func)(void *), void *arg)
223 {
224 int save;
225 int idx;
226 int soft_level;
227
228 if (extint < 0 || N_EXTINT <= extint)
229 panic("Bad interrupt no for extio");
230
231 if (extint < EXTINT_CASCADE_MIN) {
232 /*
233 * EXINT[0..3] are not cascaded. they are handled by
234 * the main interrupt controller.
235 */
236 return s3c24x0_intr_establish(extint, ipl, type, func, arg);
237 }
238
239 soft_level = SOFTINT_SERIAL;
240
241 idx = extint - EXTINT_CASCADE_MIN;
242
243 save = disable_interrupts(I32_bit);
244
245 ssextio_softc->sc_handler[idx].func = func;
246 ssextio_softc->sc_handler[idx].arg = arg;
247 ssextio_softc->sc_handler[idx].level = ipl;
248
249 ssextio_softc->sc_handler[idx].sh = softint_establish(soft_level,
250 ssextio_softintr, &ssextio_softc->sc_handler[idx]);
251
252 s3c2440_setup_extint(extint, type);
253
254 bus_space_write_4(ssextio_softc->sc_iot, ssextio_softc->sc_ioh,
255 GPIO_EINTPEND, 1U << extint);
256 ssextio_softc->sc_mask &= ~(1U << extint);
257 update_hw_mask();
258
259 restore_interrupts(save);
260 return &ssextio_softc->sc_handler[idx];
261 }
262
263
264 static int
265 ssextio_cascaded_intr(void *cookie)
266 {
267 uint32_t pending_mask, pending;
268 int int_min;
269 bus_space_tag_t iot = ssextio_softc->sc_iot;
270 bus_space_handle_t ioh = ssextio_softc->sc_ioh;
271 int save, i;
272
273 switch((int)cookie) {
274 case EXTINT_4_7:
275 pending_mask = 0x000000f0;
276 int_min = 4;
277 break;
278
279 case EXTINT_8_23:
280 pending_mask = 0x00ffff00;
281 int_min = 8;
282 break;
283
284 default:
285 panic("Bad cookie for %s", __func__);
286 }
287
288
289 save = disable_interrupts(I32_bit);
290 pending = pending_mask & bus_space_read_4(iot, ioh, GPIO_EINTPEND);
291 pending &= ~ssextio_softc->sc_mask;
292 ssextio_softc->sc_pending |= pending;
293 /* disable the extint until the handler is called. */
294 update_hw_mask();
295 restore_interrupts(save);
296
297 for (i=int_min; pending; ++i) {
298 if (pending & (1<<i)) {
299 assert(ssextio_softc->sc_handler[i-EXTINT_CASCADE_MIN].sh != NULL);
300
301 softint_schedule(
302 ssextio_softc->sc_handler[i-EXTINT_CASCADE_MIN].sh);
303 pending &= ~ (1<<i);
304 }
305 }
306
307 return 1;
308 }
309
310 static void
311 ssextio_softintr(void *cookie)
312 {
313 struct extint_handler *h = cookie;
314 int extint = EXTINT_CASCADE_MIN + h - ssextio_softc->sc_handler;
315 int s, save;
316
317 save = disable_interrupts(I32_bit);
318 /* clear hardware pending bits */
319 bus_space_write_4(ssextio_softc->sc_iot, ssextio_softc->sc_ioh,
320 GPIO_EINTPEND, 1<<extint);
321 ssextio_softc->sc_pending &= ~(1<<extint);
322 update_hw_mask();
323 restore_interrupts(save);
324
325 s = _splraise(h->level);
326 h->func(h->arg);
327 splx(s);
328 }
329