s3c2410_intr.c revision 1.6.52.1 1 /* $NetBSD: s3c2410_intr.c,v 1.6.52.1 2008/01/09 01:45:22 matt Exp $ */
2
3 /*
4 * Copyright (c) 2003 Genetec corporation. All rights reserved.
5 * Written by Hiroyuki Bessho for Genetec corporation.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of Genetec corporation may not be used to endorse
16 * or promote products derived from this software without specific prior
17 * written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY GENETEC CORP. ``AS IS'' AND
20 * 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 GENETEC CORP.
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 /*
33 * IRQ handler for Samsung S3C2410 processor.
34 * It has integrated interrupt controller.
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: s3c2410_intr.c,v 1.6.52.1 2008/01/09 01:45:22 matt Exp $");
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <uvm/uvm_extern.h>
44 #include <machine/bus.h>
45 #include <machine/intr.h>
46 #include <arm/cpufunc.h>
47
48 #include <arm/s3c2xx0/s3c2410reg.h>
49 #include <arm/s3c2xx0/s3c2410var.h>
50
51 /*
52 * interrupt dispatch table.
53 */
54
55 struct s3c2xx0_intr_dispatch handler[ICU_LEN];
56
57
58 volatile int intr_mask;
59 #ifdef __HAVE_FAST_SOFTINTS
60 volatile int softint_pending;
61 volatile int soft_intr_mask;
62 #endif
63 volatile int global_intr_mask = 0; /* mask some interrupts at all spl level */
64
65 /* interrupt masks for each level */
66 int s3c2xx0_imask[NIPL];
67 int s3c2xx0_ilevel[ICU_LEN];
68 #ifdef __HAVE_FAST_SOFTINTS
69 int s3c24x0_soft_imask[NIPL];
70 #endif
71
72 vaddr_t intctl_base; /* interrupt controller registers */
73 #define icreg(offset) \
74 (*(volatile uint32_t *)(intctl_base+(offset)))
75
76 #ifdef __HAVE_FAST_SOFTINTS
77 /*
78 * Map a software interrupt queue to an interrupt priority level.
79 */
80 static const int si_to_ipl[] = {
81 [SI_SOFTBIO] = IPL_SOFTBIO,
82 [SI_SOFTCLOCK] = IPL_SOFTCLOCK,
83 [SI_SOFTNET] = IPL_SOFTNET,
84 [SI_SOFTSERIAL] = IPL_SOFTSERIAL,
85 };
86 #endif
87
88 #define PENDING_CLEAR_MASK (~0)
89
90 /*
91 * called from irq_entry.
92 */
93 void s3c2410_irq_handler(struct clockframe *);
94 void
95 s3c2410_irq_handler(struct clockframe *frame)
96 {
97 uint32_t irqbits;
98 int irqno;
99 int saved_spl_level;
100
101 saved_spl_level = curcpl();
102
103 #ifdef DIAGNOSTIC
104 if (curcpu()->ci_intr_depth > 10)
105 panic("nested intr too deep");
106 #endif
107
108 while ((irqbits = icreg(INTCTL_INTPND)) != 0) {
109
110 /* Note: Only one bit in INTPND register is set */
111
112 irqno = icreg(INTCTL_INTOFFSET);
113
114 #ifdef DIAGNOSTIC
115 if (__predict_false((irqbits & (1<<irqno)) == 0)) {
116 /* This shouldn't happen */
117 printf("INTOFFSET=%d, INTPND=%x\n", irqno, irqbits);
118 break;
119 }
120 #endif
121 /* raise spl to stop interrupts of lower priorities */
122 if (saved_spl_level < handler[irqno].level)
123 s3c2xx0_setipl(handler[irqno].level);
124
125 /* clear pending bit */
126 icreg(INTCTL_SRCPND) = PENDING_CLEAR_MASK & (1 << irqno);
127 icreg(INTCTL_INTPND) = PENDING_CLEAR_MASK & (1 << irqno);
128
129 enable_interrupts(I32_bit); /* allow nested interrupts */
130
131 (*handler[irqno].func) (
132 handler[irqno].cookie == 0
133 ? frame : handler[irqno].cookie);
134
135 disable_interrupts(I32_bit);
136
137 /* restore spl to that was when this interrupt happen */
138 s3c2xx0_setipl(saved_spl_level);
139
140 }
141
142 #ifdef __HAVE_FAST_SOFTINTS
143 if (get_pending_softint())
144 s3c2xx0_do_pending(1);
145 #endif
146 }
147
148 /*
149 * Handler for main IRQ of cascaded interrupts.
150 */
151 static int
152 cascade_irq_handler(void *cookie)
153 {
154 int index = (int)cookie - 1;
155 uint32_t irqbits;
156 int irqno, i;
157 int save = disable_interrupts(I32_bit);
158
159 KASSERT(0 <= index && index <= 3);
160
161 irqbits = icreg(INTCTL_SUBSRCPND) &
162 ~icreg(INTCTL_INTSUBMSK) & (0x07 << (3*index));
163
164 for (irqno = 3*index; irqbits; ++irqno) {
165 if ((irqbits & (1<<irqno)) == 0)
166 continue;
167
168 /* clear pending bit */
169 irqbits &= ~(1<<irqno);
170 icreg(INTCTL_SUBSRCPND) = (1 << irqno);
171
172 /* allow nested interrupts. SPL is already set
173 * correctly by main handler. */
174 restore_interrupts(save);
175
176 i = S3C2410_SUBIRQ_MIN + irqno;
177 (* handler[i].func)(handler[i].cookie);
178
179 disable_interrupts(I32_bit);
180 }
181
182 return 1;
183 }
184
185
186 static const uint8_t subirq_to_main[] = {
187 S3C2410_INT_UART0,
188 S3C2410_INT_UART0,
189 S3C2410_INT_UART0,
190 S3C2410_INT_UART1,
191 S3C2410_INT_UART1,
192 S3C2410_INT_UART1,
193 S3C2410_INT_UART2,
194 S3C2410_INT_UART2,
195 S3C2410_INT_UART2,
196 S3C24X0_INT_ADCTC,
197 S3C24X0_INT_ADCTC,
198 };
199
200 void *
201 s3c24x0_intr_establish(int irqno, int level, int type,
202 int (* func) (void *), void *cookie)
203 {
204 int save;
205
206 if (irqno < 0 || irqno >= ICU_LEN ||
207 type < IST_NONE || IST_EDGE_BOTH < type)
208 panic("intr_establish: bogus irq or type");
209
210 save = disable_interrupts(I32_bit);
211
212 handler[irqno].cookie = cookie;
213 handler[irqno].func = func;
214 handler[irqno].level = level;
215
216 if (irqno >= S3C2410_SUBIRQ_MIN) {
217 /* cascaded interrupts. */
218 int main_irqno;
219 int i = (irqno - S3C2410_SUBIRQ_MIN);
220
221 main_irqno = subirq_to_main[i];
222
223 /* establish main irq if first time
224 * be careful that cookie shouldn't be 0 */
225 if (handler[main_irqno].func != cascade_irq_handler)
226 s3c24x0_intr_establish(main_irqno, level, type,
227 cascade_irq_handler, (void *)((i/3) + 1));
228
229 /* unmask it in submask register */
230 icreg(INTCTL_INTSUBMSK) &= ~(1<<i);
231
232 restore_interrupts(save);
233 return &handler[irqno];
234 }
235
236 s3c2xx0_update_intr_masks(irqno, level);
237
238 /*
239 * set trigger type for external interrupts 0..3
240 */
241 if (irqno <= S3C24X0_INT_EXT(3)) {
242 /*
243 * Update external interrupt control
244 */
245 s3c2410_setup_extint(irqno, type);
246 }
247
248 s3c2xx0_setipl(curcpl());
249
250 restore_interrupts(save);
251
252 return &handler[irqno];
253 }
254
255
256 static void
257 init_interrupt_masks(void)
258 {
259 int i;
260
261 for (i=0; i < NIPL; ++i)
262 s3c2xx0_imask[i] = 0;
263
264 #ifdef __HAVE_FAST_SOFTINTS
265 s3c24x0_soft_imask[IPL_NONE] = SI_TO_IRQBIT(SI_SOFTSERIAL) |
266 SI_TO_IRQBIT(SI_SOFTNET) | SI_TO_IRQBIT(SI_SOFTCLOCK) |
267 SI_TO_IRQBIT(SI_SOFT);
268
269 s3c24x0_soft_imask[IPL_SOFT] = SI_TO_IRQBIT(SI_SOFTSERIAL) |
270 SI_TO_IRQBIT(SI_SOFTNET) | SI_TO_IRQBIT(SI_SOFTCLOCK);
271
272 /*
273 * splsoftclock() is the only interface that users of the
274 * generic software interrupt facility have to block their
275 * soft intrs, so splsoftclock() must also block IPL_SOFT.
276 */
277 s3c24x0_soft_imask[IPL_SOFTCLOCK] = SI_TO_IRQBIT(SI_SOFTSERIAL) |
278 SI_TO_IRQBIT(SI_SOFTNET);
279
280 /*
281 * splsoftnet() must also block splsoftclock(), since we don't
282 * want timer-driven network events to occur while we're
283 * processing incoming packets.
284 */
285 s3c24x0_soft_imask[IPL_SOFTNET] = SI_TO_IRQBIT(SI_SOFTSERIAL);
286
287 for (i = IPL_BIO; i < IPL_SOFTSERIAL; ++i)
288 s3c24x0_soft_imask[i] = SI_TO_IRQBIT(SI_SOFTSERIAL);
289 #endif
290 }
291
292 void
293 s3c2410_intr_init(struct s3c24x0_softc *sc)
294 {
295 intctl_base = (vaddr_t) bus_space_vaddr(sc->sc_sx.sc_iot,
296 sc->sc_sx.sc_intctl_ioh);
297
298 s3c2xx0_intr_mask_reg = (uint32_t *)(intctl_base + INTCTL_INTMSK);
299
300 /* clear all pending interrupt */
301 icreg(INTCTL_SRCPND) = ~0;
302 icreg(INTCTL_INTPND) = ~0;
303
304 /* mask all sub interrupts */
305 icreg(INTCTL_INTSUBMSK) = 0x7ff;
306
307 init_interrupt_masks();
308
309 s3c2xx0_intr_init(handler, ICU_LEN);
310
311 }
312
313
314 /*
315 * mask/unmask sub interrupts
316 */
317 void
318 s3c2410_mask_subinterrupts(int bits)
319 {
320 atomic_set_bit((uint32_t *)__UNVOLATILE(&icreg(INTCTL_INTSUBMSK)),
321 bits);
322 }
323
324 void
325 s3c2410_unmask_subinterrupts(int bits)
326 {
327 atomic_clear_bit((uint32_t *)__UNVOLATILE(&icreg(INTCTL_INTSUBMSK)),
328 bits);
329 }
330
331 /*
332 * Update external interrupt control
333 */
334 static const u_char s3c24x0_ist[] = {
335 EXTINTR_LOW, /* NONE */
336 EXTINTR_FALLING, /* PULSE */
337 EXTINTR_FALLING, /* EDGE */
338 EXTINTR_LOW, /* LEVEL */
339 EXTINTR_HIGH,
340 EXTINTR_RISING,
341 EXTINTR_BOTH,
342 };
343
344 void
345 s3c2410_setup_extint(int extint, int type)
346 {
347 uint32_t reg;
348 u_int trig;
349 int i = extint % 8;
350 int regidx = extint/8; /* GPIO_EXTINT[0:2] */
351 int save;
352
353 trig = s3c24x0_ist[type];
354
355 save = disable_interrupts(I32_bit);
356
357 reg = bus_space_read_4(s3c2xx0_softc->sc_iot,
358 s3c2xx0_softc->sc_gpio_ioh,
359 GPIO_EXTINT(regidx));
360
361 reg = reg & ~(0x07 << (4*i));
362 reg |= trig << (4*i);
363
364 bus_space_write_4(s3c2xx0_softc->sc_iot, s3c2xx0_softc->sc_gpio_ioh,
365 GPIO_EXTINT(regidx), reg);
366
367 restore_interrupts(save);
368 }
369