malta_intr.c revision 1.13 1 /* $NetBSD: malta_intr.c,v 1.13 2006/12/21 15:55:22 yamt 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.13 2006/12/21 15:55:22 yamt Exp $");
44
45 #include <sys/param.h>
46 #include <sys/device.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/systm.h>
50
51 #include <mips/locore.h>
52
53 #include <evbmips/malta/maltavar.h>
54 #include <evbmips/malta/pci/pcibvar.h>
55
56 #include <dev/ic/mc146818reg.h> /* for malta_cal_timer() */
57
58 #include <dev/isa/isavar.h>
59 #include <dev/pci/pciidereg.h>
60
61 /*
62 * This is a mask of bits to clear in the SR when we go to a
63 * given hardware interrupt priority level.
64 */
65 const uint32_t ipl_sr_bits[_IPL_N] = {
66 0, /* 0: IPL_NONE */
67
68 MIPS_SOFT_INT_MASK_0, /* 1: IPL_SOFT */
69
70 MIPS_SOFT_INT_MASK_0, /* 2: IPL_SOFTCLOCK */
71
72 MIPS_SOFT_INT_MASK_0|
73 MIPS_SOFT_INT_MASK_1, /* 3: IPL_SOFTNET */
74
75 MIPS_SOFT_INT_MASK_0|
76 MIPS_SOFT_INT_MASK_1, /* 4: IPL_SOFTSERIAL */
77
78 MIPS_SOFT_INT_MASK_0|
79 MIPS_SOFT_INT_MASK_1|
80 MIPS_INT_MASK_0, /* 5: IPL_BIO */
81
82 MIPS_SOFT_INT_MASK_0|
83 MIPS_SOFT_INT_MASK_1|
84 MIPS_INT_MASK_0, /* 6: IPL_NET */
85
86 MIPS_SOFT_INT_MASK_0|
87 MIPS_SOFT_INT_MASK_1|
88 MIPS_INT_MASK_0, /* 7: IPL_{TTY,SERIAL} */
89
90 MIPS_SOFT_INT_MASK_0|
91 MIPS_SOFT_INT_MASK_1|
92 MIPS_INT_MASK_0|
93 MIPS_INT_MASK_1|
94 MIPS_INT_MASK_2|
95 MIPS_INT_MASK_3|
96 MIPS_INT_MASK_4|
97 MIPS_INT_MASK_5, /* 8: IPL_{CLOCK,HIGH} */
98 };
99
100 /*
101 * This is a mask of bits to clear in the SR when we go to a
102 * given software interrupt priority level.
103 * Hardware ipls are port/board specific.
104 */
105 const uint32_t mips_ipl_si_to_sr[SI_NQUEUES] = {
106 [SI_SOFT] = MIPS_SOFT_INT_MASK_0,
107 [SI_SOFTCLOCK] = MIPS_SOFT_INT_MASK_0,
108 [SI_SOFTNET] = MIPS_SOFT_INT_MASK_1,
109 [SI_SOFTSERIAL] = MIPS_SOFT_INT_MASK_1,
110 };
111
112 struct malta_cpuintr {
113 LIST_HEAD(, evbmips_intrhand) cintr_list;
114 struct evcnt cintr_count;
115 };
116 #define NINTRS 5 /* MIPS INT0 - INT4 */
117
118 struct malta_cpuintr malta_cpuintrs[NINTRS];
119 const char *malta_cpuintrnames[NINTRS] = {
120 "int 0 (piix4)",
121 "int 1 (smi)",
122 "int 2 (uart)",
123 "int 3 (core hi/gt64120)",
124 "int 4 (core lo)",
125 };
126
127 static int malta_pci_intr_map(struct pci_attach_args *, pci_intr_handle_t *);
128 static const char
129 *malta_pci_intr_string(void *, pci_intr_handle_t);
130 static const struct evcnt
131 *malta_pci_intr_evcnt(void *, pci_intr_handle_t);
132 static void *malta_pci_intr_establish(void *, pci_intr_handle_t, int,
133 int (*)(void *), void *);
134 static void malta_pci_intr_disestablish(void *, void *);
135 static void malta_pci_conf_interrupt(void *, int, int, int, int, int *);
136 static void *malta_pciide_compat_intr_establish(void *, struct device *,
137 struct pci_attach_args *, int, int (*)(void *), void *);
138
139 void
140 evbmips_intr_init(void)
141 {
142 struct malta_config *mcp = &malta_configuration;
143 int i;
144
145 for (i = 0; i < NINTRS; i++) {
146 LIST_INIT(&malta_cpuintrs[i].cintr_list);
147 evcnt_attach_dynamic(&malta_cpuintrs[i].cintr_count,
148 EVCNT_TYPE_INTR, NULL, "mips", malta_cpuintrnames[i]);
149 }
150
151 mcp->mc_pc.pc_intr_v = NULL;
152 mcp->mc_pc.pc_intr_map = malta_pci_intr_map;
153 mcp->mc_pc.pc_intr_string = malta_pci_intr_string;
154 mcp->mc_pc.pc_intr_evcnt = malta_pci_intr_evcnt;
155 mcp->mc_pc.pc_intr_establish = malta_pci_intr_establish;
156 mcp->mc_pc.pc_intr_disestablish = malta_pci_intr_disestablish;
157 mcp->mc_pc.pc_conf_interrupt = malta_pci_conf_interrupt;
158 mcp->mc_pc.pc_pciide_compat_intr_establish =
159 malta_pciide_compat_intr_establish;
160 }
161
162 void
163 malta_cal_timer(bus_space_tag_t st, bus_space_handle_t sh)
164 {
165 uint32_t ctrdiff[4], startctr, endctr;
166 uint8_t regc;
167 int i;
168
169 /* Disable interrupts first. */
170 bus_space_write_1(st, sh, 0, MC_REGB);
171 bus_space_write_1(st, sh, 1, MC_REGB_SQWE | MC_REGB_BINARY |
172 MC_REGB_24HR);
173
174 /* Initialize for 16Hz. */
175 bus_space_write_1(st, sh, 0, MC_REGA);
176 bus_space_write_1(st, sh, 1, MC_BASE_32_KHz | MC_RATE_16_Hz);
177
178 /* Run the loop an extra time to prime the cache. */
179 for (i = 0; i < 4; i++) {
180 // led_display('h', 'z', '0' + i, ' ');
181
182 /* Enable the interrupt. */
183 bus_space_write_1(st, sh, 0, MC_REGB);
184 bus_space_write_1(st, sh, 1, MC_REGB_PIE | MC_REGB_SQWE |
185 MC_REGB_BINARY | MC_REGB_24HR);
186
187 /* Go to REGC. */
188 bus_space_write_1(st, sh, 0, MC_REGC);
189
190 /* Wait for it to happen. */
191 startctr = mips3_cp0_count_read();
192 do {
193 regc = bus_space_read_1(st, sh, 1);
194 endctr = mips3_cp0_count_read();
195 } while ((regc & MC_REGC_IRQF) == 0);
196
197 /* Already ACK'd. */
198
199 /* Disable. */
200 bus_space_write_1(st, sh, 0, MC_REGB);
201 bus_space_write_1(st, sh, 1, MC_REGB_SQWE | MC_REGB_BINARY |
202 MC_REGB_24HR);
203
204 ctrdiff[i] = endctr - startctr;
205 }
206
207 /* Compute the number of cycles per second. */
208 curcpu()->ci_cpu_freq = ((ctrdiff[2] + ctrdiff[3]) / 2) * 16/*Hz*/;
209
210 /* Compute the number of ticks for hz. */
211 curcpu()->ci_cycles_per_hz = (curcpu()->ci_cpu_freq + hz / 2) / hz;
212
213 /* Compute the delay divisor and reciprical. */
214 curcpu()->ci_divisor_delay =
215 ((curcpu()->ci_cpu_freq + 500000) / 1000000);
216 MIPS_SET_CI_RECIPRICAL(curcpu());
217
218 /*
219 * Get correct cpu frequency if the CPU runs at twice the
220 * external/cp0-count frequency.
221 */
222 if (mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT)
223 curcpu()->ci_cpu_freq *= 2;
224
225 #ifdef DEBUG
226 printf("Timer calibration: %lu cycles/sec [(%u, %u) * 16]\n",
227 curcpu()->ci_cpu_freq, ctrdiff[2], ctrdiff[3]);
228 #endif
229 }
230
231 void *
232 evbmips_intr_establish(int irq, int (*func)(void *), void *arg)
233 {
234 struct evbmips_intrhand *ih;
235 int s;
236
237 ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT);
238 if (ih == NULL)
239 return (NULL);
240
241 ih->ih_func = func;
242 ih->ih_arg = arg;
243
244 s = splhigh();
245
246 /*
247 * Link it into the tables.
248 */
249 LIST_INSERT_HEAD(&malta_cpuintrs[0].cintr_list, ih, ih_q);
250
251 /* XXX - should check that MIPS_INT_MASK_0 is set... */
252
253 splx(s);
254
255 return (ih);
256 }
257
258 void
259 evbmips_intr_disestablish(void *arg)
260 {
261 struct evbmips_intrhand *ih = arg;
262 int s;
263
264 s = splhigh();
265
266 /*
267 * First, remove it from the table.
268 */
269 LIST_REMOVE(ih, ih_q);
270
271 /* XXX - disable MIPS_INT_MASK_0 if list is empty? */
272
273 splx(s);
274
275 free(ih, M_DEVBUF);
276 }
277
278 void
279 evbmips_iointr(uint32_t status, uint32_t cause, uint32_t pc, uint32_t ipending)
280 {
281 struct evbmips_intrhand *ih;
282
283 /* Check for error interrupts (SMI, GT64120) */
284 if (ipending & (MIPS_INT_MASK_1 | MIPS_INT_MASK_3)) {
285 if (ipending & MIPS_INT_MASK_1)
286 panic("piix4 SMI interrupt");
287 if (ipending & MIPS_INT_MASK_3)
288 panic("gt64120 error interrupt");
289 }
290
291 /*
292 * Read the interrupt pending registers, mask them with the
293 * ones we have enabled, and service them in order of decreasing
294 * priority.
295 */
296 if (ipending & MIPS_INT_MASK_0) {
297 /* All interrupts are gated through MIPS HW interrupt 0 */
298 malta_cpuintrs[0].cintr_count.ev_count++;
299 LIST_FOREACH(ih, &malta_cpuintrs[0].cintr_list, ih_q)
300 (*ih->ih_func)(ih->ih_arg);
301 cause &= ~MIPS_INT_MASK_0;
302 }
303
304 /* Re-enable anything that we have processed. */
305 _splset(MIPS_SR_INT_IE | ((status & ~cause) & MIPS_HARD_INT_MASK));
306 }
307
308 /*
309 * YAMON configures pa_intrline correctly (so far), so we trust it to DTRT
310 * in the future...
311 */
312 #undef YAMON_IRQ_MAP_BAD
313
314 /*
315 * PCI interrupt support
316 */
317 static int
318 malta_pci_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
319 {
320 #ifdef YAMON_IRQ_MAP_BAD
321 static const int pciirqmap[12/*device*/][4/*pin*/] = {
322 { -1, -1, -1, 11 }, /* 10: USB */
323 { 10, -1, -1, -1 }, /* 11: Ethernet */
324 { 11, -1, -1, -1 }, /* 12: Audio */
325 { -1, -1, -1, -1 }, /* 13: not used */
326 { -1, -1, -1, -1 }, /* 14: not used */
327 { -1, -1, -1, -1 }, /* 15: not used */
328 { -1, -1, -1, -1 }, /* 16: not used */
329 { -1, -1, -1, -1 }, /* 17: Core card(?) */
330 { 10, 10, 11, 11 }, /* 18: PCI Slot 1 */
331 { 10, 11, 11, 10 }, /* 19: PCI Slot 2 */
332 { 11, 11, 10, 10 }, /* 20: PCI Slot 3 */
333 { 11, 10, 10, 11 }, /* 21: PCI Slot 4 */
334 };
335 int buspin, device, irq;
336 #else /* !YAMON_IRQ_MAP_BAD */
337 int buspin;
338 #endif /* !YAMON_IRQ_MAP_BAD */
339
340 buspin = pa->pa_intrpin;
341
342 if (buspin == 0) {
343 /* No IRQ used. */
344 return (1);
345 }
346
347 if (buspin > 4) {
348 printf("malta_pci_intr_map: bad interrupt pin %d\n", buspin);
349 return (1);
350 }
351
352 #ifdef YAMON_IRQ_MAP_BAD
353 pci_decompose_tag(pa->pa_pc, pa->pa_intrtag, NULL, &device, NULL);
354
355 if (device < 10 || device > 21) {
356 printf("malta_pci_intr_map: bad device %d\n", device);
357 return (1);
358 }
359
360 irq = pciirqmap[device - 10][buspin - 1];
361 if (irq == -1) {
362 printf("malta_pci_intr_map: no mapping for device %d pin %d\n",
363 device, buspin);
364 return (1);
365 }
366
367 *ihp = irq;
368 #else /* !YAMON_IRQ_MAP_BAD */
369 *ihp = pa->pa_intrline;
370 #endif /* !YAMON_IRQ_MAP_BAD */
371 return (0);
372 }
373
374 static const char *
375 malta_pci_intr_string(void *v, pci_intr_handle_t irq)
376 {
377
378 return (isa_intr_string(pcib_ic, irq));
379 }
380
381 static const struct evcnt *
382 malta_pci_intr_evcnt(void *v, pci_intr_handle_t irq)
383 {
384
385 return (isa_intr_evcnt(pcib_ic, irq));
386 }
387
388 static void *
389 malta_pci_intr_establish(void *v, pci_intr_handle_t irq, int level,
390 int (*func)(void *), void *arg)
391 {
392
393 return (isa_intr_establish(pcib_ic, irq, IST_LEVEL, level, func, arg));
394 }
395
396 static void
397 malta_pci_intr_disestablish(void *v, void *arg)
398 {
399
400 return (isa_intr_disestablish(pcib_ic, arg));
401 }
402
403 static void
404 malta_pci_conf_interrupt(void *v, int bus, int dev, int func, int swiz,
405 int *iline)
406 {
407
408 /*
409 * We actually don't need to do anything; everything is handled
410 * in pci_intr_map().
411 */
412 *iline = 0;
413 }
414
415 void *
416 malta_pciide_compat_intr_establish(void *v, struct device *dev,
417 struct pci_attach_args *pa, int chan, int (*func)(void *), void *arg)
418 {
419 pci_chipset_tag_t pc = pa->pa_pc;
420 void *cookie;
421 int bus, irq;
422
423 pci_decompose_tag(pc, pa->pa_tag, &bus, NULL, NULL);
424
425 /*
426 * If this isn't PCI bus #0, all bets are off.
427 */
428 if (bus != 0)
429 return (NULL);
430
431 irq = PCIIDE_COMPAT_IRQ(chan);
432 cookie = isa_intr_establish(pcib_ic, irq, IST_EDGE, IPL_BIO, func, arg);
433 if (cookie == NULL)
434 return (NULL);
435 printf("%s: %s channel interrupting at %s\n", dev->dv_xname,
436 PCIIDE_CHANNEL_NAME(chan), malta_pci_intr_string(v, irq));
437 return (cookie);
438 }
439