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