malta_intr.c revision 1.20 1 /* $NetBSD: malta_intr.c,v 1.20 2011/02/20 07:48:34 matt Exp $ */
2
3 /*
4 * Copyright 2001, 2002 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Jason R. Thorpe and Simon Burge for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /*
39 * Platform-specific interrupt support for the MIPS Malta.
40 */
41
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: malta_intr.c,v 1.20 2011/02/20 07:48:34 matt Exp $");
44
45 #define __INTR_PRIVATE
46
47 #include <sys/param.h>
48 #include <sys/device.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/systm.h>
52 #include <sys/cpu.h>
53
54 #include <mips/locore.h>
55
56 #include <evbmips/malta/maltavar.h>
57 #include <evbmips/malta/pci/pcibvar.h>
58
59 #include <dev/ic/mc146818reg.h> /* for malta_cal_timer() */
60
61 #include <dev/isa/isavar.h>
62 #include <dev/pci/pciidereg.h>
63
64 /*
65 * This is a mask of bits to clear in the SR when we go to a
66 * given hardware interrupt priority level.
67 */
68 static const struct ipl_sr_map malta_ipl_sr_map = {
69 .sr_bits = {
70 [IPL_NONE] = 0,
71 [IPL_SOFTCLOCK] = MIPS_SOFT_INT_MASK_0,
72 [IPL_SOFTNET] = MIPS_SOFT_INT_MASK,
73 [IPL_VM] = MIPS_SOFT_INT_MASK | MIPS_INT_MASK_0,
74 [IPL_SCHED] = MIPS_SOFT_INT_MASK | MIPS_INT_MASK_0
75 | MIPS_INT_MASK_5,
76 [IPL_DDB] = MIPS_INT_MASK,
77 [IPL_HIGH] = MIPS_INT_MASK,
78 },
79 };
80
81 struct malta_cpuintr {
82 LIST_HEAD(, evbmips_intrhand) cintr_list;
83 struct evcnt cintr_count;
84 };
85 #define NINTRS 5 /* MIPS INT0 - INT4 */
86
87 struct malta_cpuintr malta_cpuintrs[NINTRS];
88 const char * const malta_cpuintrnames[NINTRS] = {
89 "int 0 (piix4)",
90 "int 1 (smi)",
91 "int 2 (uart)",
92 "int 3 (core hi/gt64120)",
93 "int 4 (core lo)",
94 };
95
96 static int malta_pci_intr_map(struct pci_attach_args *, pci_intr_handle_t *);
97 static const char
98 *malta_pci_intr_string(void *, pci_intr_handle_t);
99 static const struct evcnt
100 *malta_pci_intr_evcnt(void *, pci_intr_handle_t);
101 static void *malta_pci_intr_establish(void *, pci_intr_handle_t, int,
102 int (*)(void *), void *);
103 static void malta_pci_intr_disestablish(void *, void *);
104 static void malta_pci_conf_interrupt(void *, int, int, int, int, int *);
105 static void *malta_pciide_compat_intr_establish(void *, struct device *,
106 struct pci_attach_args *, int, int (*)(void *), void *);
107
108 void
109 evbmips_intr_init(void)
110 {
111 struct malta_config * const mcp = &malta_configuration;
112
113 ipl_sr_map = malta_ipl_sr_map;
114
115 for (size_t i = 0; i < NINTRS; i++) {
116 LIST_INIT(&malta_cpuintrs[i].cintr_list);
117 evcnt_attach_dynamic(&malta_cpuintrs[i].cintr_count,
118 EVCNT_TYPE_INTR, NULL, "mips", malta_cpuintrnames[i]);
119 }
120
121 mcp->mc_pc.pc_intr_v = NULL;
122 mcp->mc_pc.pc_intr_map = malta_pci_intr_map;
123 mcp->mc_pc.pc_intr_string = malta_pci_intr_string;
124 mcp->mc_pc.pc_intr_evcnt = malta_pci_intr_evcnt;
125 mcp->mc_pc.pc_intr_establish = malta_pci_intr_establish;
126 mcp->mc_pc.pc_intr_disestablish = malta_pci_intr_disestablish;
127 mcp->mc_pc.pc_conf_interrupt = malta_pci_conf_interrupt;
128 mcp->mc_pc.pc_pciide_compat_intr_establish =
129 malta_pciide_compat_intr_establish;
130 }
131
132 void
133 malta_cal_timer(bus_space_tag_t st, bus_space_handle_t sh)
134 {
135 struct cpu_info * const ci = curcpu();
136 uint32_t ctrdiff[4], startctr, endctr;
137 uint8_t regc;
138 int i;
139
140 /* Disable interrupts first. */
141 bus_space_write_1(st, sh, 0, MC_REGB);
142 bus_space_write_1(st, sh, 1, MC_REGB_SQWE | MC_REGB_BINARY |
143 MC_REGB_24HR);
144
145 /* Initialize for 16Hz. */
146 bus_space_write_1(st, sh, 0, MC_REGA);
147 bus_space_write_1(st, sh, 1, MC_BASE_32_KHz | MC_RATE_16_Hz);
148
149 /* Run the loop an extra time to prime the cache. */
150 for (i = 0; i < 4; i++) {
151 // led_display('h', 'z', '0' + i, ' ');
152
153 /* Enable the interrupt. */
154 bus_space_write_1(st, sh, 0, MC_REGB);
155 bus_space_write_1(st, sh, 1, MC_REGB_PIE | MC_REGB_SQWE |
156 MC_REGB_BINARY | MC_REGB_24HR);
157
158 /* Go to REGC. */
159 bus_space_write_1(st, sh, 0, MC_REGC);
160
161 /* Wait for it to happen. */
162 startctr = mips3_cp0_count_read();
163 do {
164 regc = bus_space_read_1(st, sh, 1);
165 endctr = mips3_cp0_count_read();
166 } while ((regc & MC_REGC_IRQF) == 0);
167
168 /* Already ACK'd. */
169
170 /* Disable. */
171 bus_space_write_1(st, sh, 0, MC_REGB);
172 bus_space_write_1(st, sh, 1, MC_REGB_SQWE | MC_REGB_BINARY |
173 MC_REGB_24HR);
174
175 ctrdiff[i] = endctr - startctr;
176 }
177
178 /* Compute the number of cycles per second. */
179 ci->ci_cpu_freq = ((ctrdiff[2] + ctrdiff[3]) / 2) * 16/*Hz*/;
180
181 /* Compute the number of ticks for hz. */
182 ci->ci_cycles_per_hz = (ci->ci_cpu_freq + hz / 2) / hz;
183
184 /* Compute the delay divisor. */
185 ci->ci_divisor_delay = ((ci->ci_cpu_freq + 500000) / 1000000);
186
187 /*
188 * Get correct cpu frequency if the CPU runs at twice the
189 * external/cp0-count frequency.
190 */
191 ci->ci_cctr_freq = ci->ci_cpu_freq;
192 if (mips_options.mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT)
193 ci->ci_cpu_freq *= 2;
194
195 #ifdef DEBUG
196 printf("Timer calibration: %lu cycles/sec [(%u, %u) * 16]\n",
197 ci->ci_cpu_freq, ctrdiff[2], ctrdiff[3]);
198 #endif
199 }
200
201 void *
202 evbmips_intr_establish(int irq, int (*func)(void *), void *arg)
203 {
204 struct evbmips_intrhand *ih;
205 int s;
206
207 ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT);
208 if (ih == NULL)
209 return (NULL);
210
211 ih->ih_func = func;
212 ih->ih_arg = arg;
213
214 s = splhigh();
215
216 /*
217 * Link it into the tables.
218 */
219 LIST_INSERT_HEAD(&malta_cpuintrs[0].cintr_list, ih, ih_q);
220
221 /* XXX - should check that MIPS_INT_MASK_0 is set... */
222
223 splx(s);
224
225 return (ih);
226 }
227
228 void
229 evbmips_intr_disestablish(void *arg)
230 {
231 struct evbmips_intrhand *ih = arg;
232 int s;
233
234 s = splhigh();
235
236 /*
237 * First, remove it from the table.
238 */
239 LIST_REMOVE(ih, ih_q);
240
241 /* XXX - disable MIPS_INT_MASK_0 if list is empty? */
242
243 splx(s);
244
245 free(ih, M_DEVBUF);
246 }
247
248 void
249 evbmips_iointr(int ipl, vaddr_t pc, uint32_t ipending)
250 {
251
252 /* Check for error interrupts (SMI, GT64120) */
253 if (ipending & (MIPS_INT_MASK_1 | MIPS_INT_MASK_3)) {
254 if (ipending & MIPS_INT_MASK_1)
255 panic("piix4 SMI interrupt");
256 if (ipending & MIPS_INT_MASK_3)
257 panic("gt64120 error interrupt");
258 }
259
260 /*
261 * Read the interrupt pending registers, mask them with the
262 * ones we have enabled, and service them in order of decreasing
263 * priority.
264 */
265 if (ipending & MIPS_INT_MASK_0) {
266 struct evbmips_intrhand *ih;
267 /* All interrupts are gated through MIPS HW interrupt 0 */
268 malta_cpuintrs[0].cintr_count.ev_count++;
269 LIST_FOREACH(ih, &malta_cpuintrs[0].cintr_list, ih_q)
270 (*ih->ih_func)(ih->ih_arg);
271 }
272 }
273
274 /*
275 * YAMON configures pa_intrline correctly (so far), so we trust it to DTRT
276 * in the future...
277 */
278 #undef YAMON_IRQ_MAP_BAD
279
280 /*
281 * PCI interrupt support
282 */
283 static int
284 malta_pci_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
285 {
286 #ifdef YAMON_IRQ_MAP_BAD
287 static const int pciirqmap[12/*device*/][4/*pin*/] = {
288 { -1, -1, -1, 11 }, /* 10: USB */
289 { 10, -1, -1, -1 }, /* 11: Ethernet */
290 { 11, -1, -1, -1 }, /* 12: Audio */
291 { -1, -1, -1, -1 }, /* 13: not used */
292 { -1, -1, -1, -1 }, /* 14: not used */
293 { -1, -1, -1, -1 }, /* 15: not used */
294 { -1, -1, -1, -1 }, /* 16: not used */
295 { -1, -1, -1, -1 }, /* 17: Core card(?) */
296 { 10, 10, 11, 11 }, /* 18: PCI Slot 1 */
297 { 10, 11, 11, 10 }, /* 19: PCI Slot 2 */
298 { 11, 11, 10, 10 }, /* 20: PCI Slot 3 */
299 { 11, 10, 10, 11 }, /* 21: PCI Slot 4 */
300 };
301 int buspin, device, irq;
302 #else /* !YAMON_IRQ_MAP_BAD */
303 int buspin;
304 #endif /* !YAMON_IRQ_MAP_BAD */
305
306 buspin = pa->pa_intrpin;
307
308 if (buspin == 0) {
309 /* No IRQ used. */
310 return (1);
311 }
312
313 if (buspin > 4) {
314 printf("malta_pci_intr_map: bad interrupt pin %d\n", buspin);
315 return (1);
316 }
317
318 #ifdef YAMON_IRQ_MAP_BAD
319 pci_decompose_tag(pa->pa_pc, pa->pa_intrtag, NULL, &device, NULL);
320
321 if (device < 10 || device > 21) {
322 printf("malta_pci_intr_map: bad device %d\n", device);
323 return (1);
324 }
325
326 irq = pciirqmap[device - 10][buspin - 1];
327 if (irq == -1) {
328 printf("malta_pci_intr_map: no mapping for device %d pin %d\n",
329 device, buspin);
330 return (1);
331 }
332
333 *ihp = irq;
334 #else /* !YAMON_IRQ_MAP_BAD */
335 *ihp = pa->pa_intrline;
336 #endif /* !YAMON_IRQ_MAP_BAD */
337 return (0);
338 }
339
340 static const char *
341 malta_pci_intr_string(void *v, pci_intr_handle_t irq)
342 {
343
344 return (isa_intr_string(pcib_ic, irq));
345 }
346
347 static const struct evcnt *
348 malta_pci_intr_evcnt(void *v, pci_intr_handle_t irq)
349 {
350
351 return (isa_intr_evcnt(pcib_ic, irq));
352 }
353
354 static void *
355 malta_pci_intr_establish(void *v, pci_intr_handle_t irq, int level,
356 int (*func)(void *), void *arg)
357 {
358
359 return (isa_intr_establish(pcib_ic, irq, IST_LEVEL, level, func, arg));
360 }
361
362 static void
363 malta_pci_intr_disestablish(void *v, void *arg)
364 {
365
366 return (isa_intr_disestablish(pcib_ic, arg));
367 }
368
369 static void
370 malta_pci_conf_interrupt(void *v, int bus, int dev, int func, int swiz,
371 int *iline)
372 {
373
374 /*
375 * We actually don't need to do anything; everything is handled
376 * in pci_intr_map().
377 */
378 *iline = 0;
379 }
380
381 void *
382 malta_pciide_compat_intr_establish(void *v, struct device *dev,
383 struct pci_attach_args *pa, int chan, int (*func)(void *), void *arg)
384 {
385 pci_chipset_tag_t pc = pa->pa_pc;
386 void *cookie;
387 int bus, irq;
388
389 pci_decompose_tag(pc, pa->pa_tag, &bus, NULL, NULL);
390
391 /*
392 * If this isn't PCI bus #0, all bets are off.
393 */
394 if (bus != 0)
395 return (NULL);
396
397 irq = PCIIDE_COMPAT_IRQ(chan);
398 cookie = isa_intr_establish(pcib_ic, irq, IST_EDGE, IPL_BIO, func, arg);
399 if (cookie == NULL)
400 return (NULL);
401 printf("%s: %s channel interrupting at %s\n", dev->dv_xname,
402 PCIIDE_CHANNEL_NAME(chan), malta_pci_intr_string(v, irq));
403 return (cookie);
404 }
405