adm5120_intr.c revision 1.4 1 1.4 matt /* $NetBSD: adm5120_intr.c,v 1.4 2011/02/20 07:48:35 matt Exp $ */
2 1.1 dyoung
3 1.1 dyoung /*-
4 1.1 dyoung * Copyright (c) 2007 Ruslan Ermilov and Vsevolod Lobko.
5 1.1 dyoung * All rights reserved.
6 1.1 dyoung *
7 1.1 dyoung * Redistribution and use in source and binary forms, with or
8 1.1 dyoung * without modification, are permitted provided that the following
9 1.1 dyoung * conditions are met:
10 1.1 dyoung * 1. Redistributions of source code must retain the above copyright
11 1.1 dyoung * notice, this list of conditions and the following disclaimer.
12 1.1 dyoung * 2. Redistributions in binary form must reproduce the above
13 1.1 dyoung * copyright notice, this list of conditions and the following
14 1.1 dyoung * disclaimer in the documentation and/or other materials provided
15 1.1 dyoung * with the distribution.
16 1.1 dyoung * 3. The names of the authors may not be used to endorse or promote
17 1.1 dyoung * products derived from this software without specific prior
18 1.1 dyoung * written permission.
19 1.1 dyoung *
20 1.1 dyoung * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY
21 1.1 dyoung * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 1.1 dyoung * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 1.1 dyoung * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS
24 1.1 dyoung * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
25 1.1 dyoung * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 1.1 dyoung * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
27 1.1 dyoung * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 dyoung * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
29 1.1 dyoung * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 1.1 dyoung * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31 1.1 dyoung * OF SUCH DAMAGE.
32 1.1 dyoung */
33 1.1 dyoung /*-
34 1.1 dyoung * Copyright (c) 2001 The NetBSD Foundation, Inc.
35 1.1 dyoung * All rights reserved.
36 1.1 dyoung *
37 1.1 dyoung * This code is derived from software contributed to The NetBSD Foundation
38 1.1 dyoung * by Jason R. Thorpe.
39 1.1 dyoung *
40 1.1 dyoung * Redistribution and use in source and binary forms, with or without
41 1.1 dyoung * modification, are permitted provided that the following conditions
42 1.1 dyoung * are met:
43 1.1 dyoung * 1. Redistributions of source code must retain the above copyright
44 1.1 dyoung * notice, this list of conditions and the following disclaimer.
45 1.1 dyoung * 2. Redistributions in binary form must reproduce the above copyright
46 1.1 dyoung * notice, this list of conditions and the following disclaimer in the
47 1.1 dyoung * documentation and/or other materials provided with the distribution.
48 1.1 dyoung *
49 1.1 dyoung * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
50 1.1 dyoung * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
51 1.1 dyoung * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
52 1.1 dyoung * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
53 1.1 dyoung * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
54 1.1 dyoung * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
55 1.1 dyoung * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
56 1.1 dyoung * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
57 1.1 dyoung * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
58 1.1 dyoung * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
59 1.1 dyoung * POSSIBILITY OF SUCH DAMAGE.
60 1.1 dyoung */
61 1.1 dyoung
62 1.1 dyoung /*
63 1.1 dyoung * Platform-specific interrupt support for the Alchemy Semiconductor Pb1000.
64 1.1 dyoung *
65 1.1 dyoung * The Alchemy Semiconductor Pb1000's interrupts are wired to two internal
66 1.1 dyoung * interrupt controllers.
67 1.1 dyoung */
68 1.1 dyoung
69 1.1 dyoung #include <sys/cdefs.h>
70 1.4 matt __KERNEL_RCSID(0, "$NetBSD: adm5120_intr.c,v 1.4 2011/02/20 07:48:35 matt Exp $");
71 1.1 dyoung
72 1.1 dyoung #include "opt_ddb.h"
73 1.4 matt #define __INTR_PRIVATE
74 1.1 dyoung
75 1.1 dyoung #include <sys/param.h>
76 1.1 dyoung #include <sys/queue.h>
77 1.1 dyoung #include <sys/malloc.h>
78 1.1 dyoung #include <sys/systm.h>
79 1.1 dyoung #include <sys/device.h>
80 1.1 dyoung #include <sys/kernel.h>
81 1.1 dyoung
82 1.1 dyoung #include <machine/bus.h>
83 1.1 dyoung #include <machine/intr.h>
84 1.1 dyoung
85 1.1 dyoung #include <mips/locore.h>
86 1.1 dyoung #include <mips/adm5120/include/adm5120reg.h>
87 1.1 dyoung #include <mips/adm5120/include/adm5120var.h>
88 1.1 dyoung
89 1.1 dyoung #include <dev/pci/pcireg.h>
90 1.1 dyoung #include <dev/pci/pcivar.h>
91 1.1 dyoung
92 1.1 dyoung /*
93 1.1 dyoung * This is a mask of bits to clear in the SR when we go to a
94 1.1 dyoung * given hardware interrupt priority level.
95 1.1 dyoung */
96 1.4 matt static const struct ipl_sr_map adm5120_ipl_sr_map = {
97 1.4 matt .sr_bits = {
98 1.4 matt [IPL_NONE] = 0,
99 1.4 matt [IPL_SOFTCLOCK] = MIPS_SOFT_INT_MASK_0,
100 1.4 matt [IPL_SOFTBIO] = MIPS_SOFT_INT_MASK_0,
101 1.4 matt [IPL_SOFTNET] = MIPS_SOFT_INT_MASK,
102 1.4 matt [IPL_SOFTSERIAL] = MIPS_SOFT_INT_MASK,
103 1.4 matt [IPL_VM] = MIPS_SOFT_INT_MASK|MIPS_INT_MASK_0,
104 1.4 matt [IPL_SCHED] = MIPS_INT_MASK,
105 1.4 matt [IPL_HIGH] = MIPS_INT_MASK,
106 1.4 matt },
107 1.1 dyoung };
108 1.1 dyoung
109 1.1 dyoung #define NIRQS 32
110 1.4 matt const char * const adm5120_intrnames[NIRQS] = {
111 1.1 dyoung "timer", /* 0 */
112 1.1 dyoung "uart0", /* 1 */
113 1.1 dyoung "uart1", /* 2 */
114 1.1 dyoung "usb", /* 3 */
115 1.1 dyoung "intx0/gpio2", /* 4 */
116 1.1 dyoung "intx1/gpio4", /* 5 */
117 1.1 dyoung "pci0", /* 6 */
118 1.1 dyoung "pci1", /* 7 */
119 1.1 dyoung "pci2", /* 8 */
120 1.1 dyoung "switch",/* 9 */
121 1.1 dyoung "res10", /* 10 */
122 1.1 dyoung "res11", /* 11 */
123 1.1 dyoung "res12", /* 12 */
124 1.1 dyoung "res13", /* 13 */
125 1.1 dyoung "res14", /* 14 */
126 1.1 dyoung "res15", /* 15 */
127 1.1 dyoung "res16", /* 16 */
128 1.1 dyoung "res17", /* 17 */
129 1.1 dyoung "res18", /* 18 */
130 1.1 dyoung "res19", /* 19 */
131 1.1 dyoung "res20", /* 20 */
132 1.1 dyoung "res21", /* 21 */
133 1.1 dyoung "res22", /* 22 */
134 1.1 dyoung "res23", /* 23 */
135 1.1 dyoung "res24", /* 24 */
136 1.1 dyoung "res25", /* 25 */
137 1.1 dyoung "res26", /* 26 */
138 1.1 dyoung "res27", /* 27 */
139 1.1 dyoung "res28", /* 28 */
140 1.1 dyoung "res29", /* 29 */
141 1.1 dyoung "res30", /* 30 */
142 1.1 dyoung "res31", /* 31 */
143 1.1 dyoung };
144 1.1 dyoung
145 1.1 dyoung struct adm5120_intrhead {
146 1.1 dyoung struct evcnt intr_count;
147 1.1 dyoung int intr_refcnt;
148 1.1 dyoung };
149 1.1 dyoung struct adm5120_intrhead adm5120_intrtab[NIRQS];
150 1.1 dyoung
151 1.1 dyoung
152 1.1 dyoung #define NINTRS 2 /* MIPS INT0 - INT1 */
153 1.1 dyoung struct adm5120_cpuintr {
154 1.1 dyoung LIST_HEAD(, evbmips_intrhand) cintr_list;
155 1.1 dyoung struct evcnt cintr_count;
156 1.1 dyoung };
157 1.1 dyoung struct adm5120_cpuintr adm5120_cpuintrs[NINTRS];
158 1.1 dyoung
159 1.4 matt const char * const adm5120_cpuintrnames[NINTRS] = {
160 1.1 dyoung "int 0 (irq)",
161 1.1 dyoung "int 1 (fiq)",
162 1.1 dyoung };
163 1.1 dyoung
164 1.1 dyoung #define REG_READ(o) *((volatile uint32_t *)MIPS_PHYS_TO_KSEG1(ADM5120_BASE_ICU + (o)))
165 1.1 dyoung #define REG_WRITE(o,v) (REG_READ(o)) = (v)
166 1.1 dyoung
167 1.1 dyoung void
168 1.1 dyoung evbmips_intr_init(void)
169 1.1 dyoung {
170 1.4 matt ipl_sr_map = adm5120_ipl_sr_map;
171 1.1 dyoung
172 1.4 matt for (size_t i = 0; i < NINTRS; i++) {
173 1.1 dyoung LIST_INIT(&adm5120_cpuintrs[i].cintr_list);
174 1.1 dyoung evcnt_attach_dynamic(&adm5120_cpuintrs[i].cintr_count,
175 1.1 dyoung EVCNT_TYPE_INTR, NULL, "mips", adm5120_cpuintrnames[i]);
176 1.1 dyoung }
177 1.1 dyoung
178 1.4 matt for (size_t i = 0; i < NIRQS; i++) {
179 1.1 dyoung /* XXX steering - use an irqmap array? */
180 1.1 dyoung
181 1.1 dyoung adm5120_intrtab[i].intr_refcnt = 0;
182 1.1 dyoung evcnt_attach_dynamic(&adm5120_intrtab[i].intr_count,
183 1.1 dyoung EVCNT_TYPE_INTR, NULL, "adm5120", adm5120_intrnames[i]);
184 1.1 dyoung }
185 1.1 dyoung
186 1.1 dyoung /* disable all interrupts */
187 1.1 dyoung REG_WRITE(ICU_DISABLE_REG, ICU_INT_MASK);
188 1.1 dyoung }
189 1.1 dyoung
190 1.1 dyoung void *
191 1.1 dyoung adm5120_intr_establish(int irq, int priority, int (*func)(void *), void *arg)
192 1.1 dyoung {
193 1.1 dyoung struct evbmips_intrhand *ih;
194 1.1 dyoung uint32_t irqmask;
195 1.1 dyoung int cpu_int, s;
196 1.1 dyoung
197 1.1 dyoung if (irq < 0 || irq >= NIRQS)
198 1.1 dyoung panic("adm5120_intr_establish: bogus IRQ %d", irq);
199 1.1 dyoung
200 1.1 dyoung ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT);
201 1.1 dyoung if (ih == NULL)
202 1.1 dyoung return NULL;
203 1.1 dyoung
204 1.1 dyoung ih->ih_func = func;
205 1.1 dyoung ih->ih_arg = arg;
206 1.1 dyoung ih->ih_irq = irq;
207 1.1 dyoung
208 1.1 dyoung s = splhigh();
209 1.1 dyoung
210 1.1 dyoung /*
211 1.1 dyoung * First, link it into the tables.
212 1.1 dyoung * XXX do we want a separate list (really, should only be one item, not
213 1.1 dyoung * a list anyway) per irq, not per CPU interrupt?
214 1.1 dyoung */
215 1.1 dyoung
216 1.1 dyoung cpu_int = (priority == INTR_FIQ) ? 1 : 0;
217 1.1 dyoung
218 1.1 dyoung LIST_INSERT_HEAD(&adm5120_cpuintrs[cpu_int].cintr_list, ih, ih_q);
219 1.1 dyoung
220 1.1 dyoung /*
221 1.1 dyoung * Now enable it.
222 1.1 dyoung */
223 1.1 dyoung if (adm5120_intrtab[irq].intr_refcnt++ == 0) {
224 1.1 dyoung irqmask = 1 << irq;
225 1.1 dyoung
226 1.1 dyoung /* configure as IRQ or FIQ */
227 1.1 dyoung if (priority == INTR_FIQ) {
228 1.1 dyoung REG_WRITE(ICU_MODE_REG,
229 1.1 dyoung REG_READ(ICU_MODE_REG) | irqmask);
230 1.1 dyoung } else {
231 1.1 dyoung REG_WRITE(ICU_MODE_REG,
232 1.1 dyoung REG_READ(ICU_MODE_REG) & ~irqmask);
233 1.1 dyoung }
234 1.1 dyoung /* enable */
235 1.1 dyoung REG_WRITE(ICU_ENABLE_REG, irqmask);
236 1.1 dyoung }
237 1.1 dyoung splx(s);
238 1.1 dyoung
239 1.1 dyoung return ih;
240 1.1 dyoung }
241 1.1 dyoung
242 1.1 dyoung void
243 1.1 dyoung adm5120_intr_disestablish(void *cookie)
244 1.1 dyoung {
245 1.1 dyoung struct evbmips_intrhand *ih = cookie;
246 1.1 dyoung int irq, s;
247 1.1 dyoung uint32_t irqmask;
248 1.1 dyoung
249 1.1 dyoung irq = ih->ih_irq;
250 1.1 dyoung
251 1.1 dyoung s = splhigh();
252 1.1 dyoung
253 1.1 dyoung /*
254 1.1 dyoung * First, remove it from the table.
255 1.1 dyoung */
256 1.1 dyoung LIST_REMOVE(ih, ih_q);
257 1.1 dyoung
258 1.1 dyoung /*
259 1.1 dyoung * Now, disable it, if there is nothing remaining on the
260 1.1 dyoung * list.
261 1.1 dyoung */
262 1.1 dyoung if (adm5120_intrtab[irq].intr_refcnt-- == 1) {
263 1.1 dyoung irqmask = 1 << irq; /* only used as a mask from here on */
264 1.1 dyoung
265 1.1 dyoung /* disable this irq in HW */
266 1.1 dyoung REG_WRITE(ICU_DISABLE_REG, irqmask);
267 1.1 dyoung }
268 1.1 dyoung
269 1.1 dyoung splx(s);
270 1.1 dyoung
271 1.1 dyoung free(ih, M_DEVBUF);
272 1.1 dyoung }
273 1.1 dyoung void
274 1.4 matt evbmips_iointr(int ipl, uint32_t pc, uint32_t ipending)
275 1.1 dyoung {
276 1.1 dyoung struct evbmips_intrhand *ih;
277 1.1 dyoung uint32_t irqmask, irqstat;
278 1.1 dyoung
279 1.4 matt for (int level = NINTRS - 1; level >= 0; level--) {
280 1.1 dyoung if ((ipending & (MIPS_INT_MASK_0 << level)) == 0)
281 1.1 dyoung continue;
282 1.1 dyoung
283 1.1 dyoung if (level)
284 1.1 dyoung irqstat = REG_READ(ICU_FIQ_STATUS_REG);
285 1.1 dyoung else
286 1.1 dyoung irqstat = REG_READ(ICU_STATUS_REG);
287 1.1 dyoung
288 1.1 dyoung adm5120_cpuintrs[level].cintr_count.ev_count++;
289 1.1 dyoung LIST_FOREACH(ih, &adm5120_cpuintrs[level].cintr_list, ih_q) {
290 1.1 dyoung irqmask = 1 << ih->ih_irq;
291 1.1 dyoung if (irqmask & irqstat) {
292 1.1 dyoung adm5120_intrtab[ih->ih_irq].intr_count.ev_count++;
293 1.1 dyoung (*ih->ih_func)(ih->ih_arg);
294 1.1 dyoung }
295 1.1 dyoung }
296 1.1 dyoung }
297 1.1 dyoung }
298