int.c revision 1.7 1 1.7 pooka /* $NetBSD: int.c,v 1.7 2004/04/11 12:05:37 pooka Exp $ */
2 1.1 sekiya
3 1.1 sekiya /*
4 1.1 sekiya * Copyright (c) 2004 Christopher SEKIYA
5 1.1 sekiya * All rights reserved.
6 1.1 sekiya *
7 1.1 sekiya * Redistribution and use in source and binary forms, with or without
8 1.1 sekiya * modification, are permitted provided that the following conditions
9 1.1 sekiya * are met:
10 1.1 sekiya * 1. Redistributions of source code must retain the above copyright
11 1.1 sekiya * notice, this list of conditions and the following disclaimer.
12 1.1 sekiya * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 sekiya * notice, this list of conditions and the following disclaimer in the
14 1.1 sekiya * documentation and/or other materials provided with the distribution.
15 1.1 sekiya * 3. The name of the author may not be used to endorse or promote products
16 1.1 sekiya * derived from this software without specific prior written permission.
17 1.1 sekiya *
18 1.1 sekiya * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 sekiya * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1 sekiya * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1 sekiya * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 sekiya * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 1.1 sekiya * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 1.1 sekiya * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 1.1 sekiya * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 1.1 sekiya * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 1.1 sekiya * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1 sekiya */
29 1.1 sekiya
30 1.1 sekiya /*
31 1.5 pooka * INT/INT2/INT3 interrupt controller (used in Indy's, Indigo's, etc..)
32 1.1 sekiya */
33 1.1 sekiya
34 1.1 sekiya #include <sys/cdefs.h>
35 1.7 pooka __KERNEL_RCSID(0, "$NetBSD: int.c,v 1.7 2004/04/11 12:05:37 pooka Exp $");
36 1.1 sekiya
37 1.1 sekiya #include "opt_cputype.h"
38 1.1 sekiya
39 1.1 sekiya #include <sys/param.h>
40 1.1 sekiya #include <sys/proc.h>
41 1.1 sekiya #include <sys/systm.h>
42 1.1 sekiya #include <sys/kernel.h>
43 1.1 sekiya #include <sys/device.h>
44 1.1 sekiya
45 1.1 sekiya #include <dev/ic/i8253reg.h>
46 1.1 sekiya #include <machine/sysconf.h>
47 1.1 sekiya #include <machine/machtype.h>
48 1.1 sekiya #include <machine/bus.h>
49 1.1 sekiya #include <mips/locore.h>
50 1.1 sekiya
51 1.1 sekiya #include <mips/cache.h>
52 1.1 sekiya
53 1.1 sekiya #include <sgimips/dev/int2reg.h>
54 1.3 sekiya #include <sgimips/dev/int2var.h>
55 1.1 sekiya
56 1.1 sekiya static bus_space_handle_t ioh;
57 1.1 sekiya static bus_space_tag_t iot;
58 1.1 sekiya
59 1.1 sekiya struct int_softc {
60 1.1 sekiya struct device sc_dev;
61 1.1 sekiya };
62 1.1 sekiya
63 1.1 sekiya
64 1.1 sekiya static int int_match(struct device *, struct cfdata *, void *);
65 1.1 sekiya static void int_attach(struct device *, struct device *, void *);
66 1.1 sekiya void int_local0_intr(u_int32_t, u_int32_t, u_int32_t, u_int32_t);
67 1.1 sekiya void int_local1_intr(u_int32_t, u_int32_t, u_int32_t, u_int32_t);
68 1.1 sekiya int int_mappable_intr(void *);
69 1.1 sekiya void *int_intr_establish(int, int, int (*)(void *), void *);
70 1.1 sekiya unsigned long int_cal_timer(void);
71 1.1 sekiya void int_8254_cal(void);
72 1.1 sekiya
73 1.1 sekiya CFATTACH_DECL(int, sizeof(struct int_softc),
74 1.1 sekiya int_match, int_attach, NULL, NULL);
75 1.1 sekiya
76 1.1 sekiya static int
77 1.1 sekiya int_match(struct device *parent, struct cfdata *match, void *aux)
78 1.1 sekiya {
79 1.6 pooka
80 1.6 pooka if ((mach_type == MACH_SGI_IP12) || (mach_type == MACH_SGI_IP20) ||
81 1.6 pooka (mach_type == MACH_SGI_IP22) )
82 1.1 sekiya return 1;
83 1.1 sekiya
84 1.1 sekiya return 0;
85 1.1 sekiya }
86 1.1 sekiya
87 1.1 sekiya static void
88 1.1 sekiya int_attach(struct device *parent, struct device *self, void *aux)
89 1.1 sekiya {
90 1.1 sekiya u_int32_t address;
91 1.1 sekiya
92 1.1 sekiya if (mach_type == MACH_SGI_IP12)
93 1.1 sekiya address = INT_IP12;
94 1.1 sekiya else if (mach_type == MACH_SGI_IP20)
95 1.1 sekiya address = INT_IP20;
96 1.1 sekiya else if (mach_type == MACH_SGI_IP22) {
97 1.1 sekiya if (mach_subtype == MACH_SGI_IP22_FULLHOUSE)
98 1.1 sekiya address = INT_IP22;
99 1.1 sekiya else
100 1.1 sekiya address = INT_IP24;
101 1.7 pooka } else
102 1.1 sekiya panic("\nint0: passed match, but failed attach?");
103 1.1 sekiya
104 1.1 sekiya printf(" addr 0x%x", address);
105 1.1 sekiya
106 1.1 sekiya bus_space_map(iot, address, 0, 0, &ioh);
107 1.1 sekiya iot = SGIMIPS_BUS_SPACE_NORMAL;
108 1.1 sekiya
109 1.1 sekiya /* Clean out interrupt masks */
110 1.1 sekiya bus_space_write_4(iot, ioh, INT2_LOCAL0_MASK, 0);
111 1.1 sekiya bus_space_write_4(iot, ioh, INT2_LOCAL1_MASK, 0);
112 1.1 sekiya bus_space_write_4(iot, ioh, INT2_MAP_MASK0, 0);
113 1.1 sekiya bus_space_write_4(iot, ioh, INT2_MAP_MASK1, 0);
114 1.1 sekiya
115 1.1 sekiya /* Reset timer interrupts */
116 1.1 sekiya bus_space_write_4(iot, ioh, INT2_TIMER_CLEAR, 0x03);
117 1.1 sekiya
118 1.1 sekiya switch (mach_type) {
119 1.1 sekiya case MACH_SGI_IP12:
120 1.1 sekiya platform.intr1 = int_local0_intr;
121 1.1 sekiya platform.intr2 = int_local1_intr;
122 1.1 sekiya int_8254_cal();
123 1.1 sekiya break;
124 1.4 pooka #ifdef MIPS3
125 1.1 sekiya case MACH_SGI_IP20:
126 1.1 sekiya case MACH_SGI_IP22:
127 1.4 pooka {
128 1.4 pooka int i;
129 1.4 pooka unsigned long cps;
130 1.4 pooka unsigned long ctrdiff[3];
131 1.4 pooka
132 1.1 sekiya platform.intr0 = int_local0_intr;
133 1.1 sekiya platform.intr1 = int_local1_intr;
134 1.1 sekiya
135 1.1 sekiya /* calibrate timer */
136 1.1 sekiya int_cal_timer();
137 1.1 sekiya
138 1.1 sekiya cps = 0;
139 1.6 pooka for (i = 0;
140 1.6 pooka i < sizeof(ctrdiff) / sizeof(ctrdiff[0]); i++) {
141 1.1 sekiya do {
142 1.1 sekiya ctrdiff[i] = int_cal_timer();
143 1.1 sekiya } while (ctrdiff[i] == 0);
144 1.1 sekiya
145 1.1 sekiya cps += ctrdiff[i];
146 1.1 sekiya }
147 1.1 sekiya
148 1.1 sekiya cps = cps / (sizeof(ctrdiff) / sizeof(ctrdiff[0]));
149 1.1 sekiya
150 1.6 pooka printf(": bus %luMHz, CPU %luMHz",
151 1.6 pooka cps / 10000, cps / 5000);
152 1.1 sekiya
153 1.1 sekiya /* R4k/R4400/R4600/R5k count at half CPU frequency */
154 1.1 sekiya curcpu()->ci_cpu_freq = 2 * cps * hz;
155 1.4 pooka }
156 1.4 pooka #endif /* MIPS3 */
157 1.1 sekiya
158 1.1 sekiya break;
159 1.1 sekiya default:
160 1.1 sekiya panic("int0: unsupported machine type %i\n", mach_type);
161 1.1 sekiya break;
162 1.1 sekiya }
163 1.1 sekiya
164 1.1 sekiya printf("\n");
165 1.1 sekiya
166 1.1 sekiya curcpu()->ci_cycles_per_hz = curcpu()->ci_cpu_freq / (2 * hz);
167 1.1 sekiya curcpu()->ci_divisor_delay = curcpu()->ci_cpu_freq / (2 * 1000000);
168 1.1 sekiya MIPS_SET_CI_RECIPRICAL(curcpu());
169 1.1 sekiya
170 1.1 sekiya if (mach_type == MACH_SGI_IP22) {
171 1.1 sekiya /* Wire interrupts 7, 11 to mappable interrupt 0,1 handlers */
172 1.1 sekiya intrtab[7].ih_fun = int_mappable_intr;
173 1.1 sekiya intrtab[7].ih_arg = (void*) 0;
174 1.1 sekiya
175 1.1 sekiya intrtab[11].ih_fun = int_mappable_intr;
176 1.1 sekiya intrtab[11].ih_arg = (void*) 1;
177 1.1 sekiya }
178 1.1 sekiya
179 1.1 sekiya platform.intr_establish = int_intr_establish;
180 1.1 sekiya }
181 1.1 sekiya
182 1.1 sekiya int
183 1.1 sekiya int_mappable_intr(void *arg)
184 1.1 sekiya {
185 1.1 sekiya int i;
186 1.1 sekiya int ret;
187 1.1 sekiya int intnum;
188 1.1 sekiya u_int32_t mstat;
189 1.1 sekiya u_int32_t mmask;
190 1.1 sekiya int which = (int)arg;
191 1.1 sekiya
192 1.1 sekiya ret = 0;
193 1.1 sekiya mstat = bus_space_read_4(iot, ioh, INT2_MAP_STATUS);
194 1.1 sekiya mmask = bus_space_read_4(iot, ioh, INT2_MAP_MASK0 + (which << 2));
195 1.1 sekiya
196 1.1 sekiya mstat &= mmask;
197 1.1 sekiya
198 1.1 sekiya for (i = 0; i < 8; i++) {
199 1.1 sekiya intnum = i + 16 + (which << 3);
200 1.1 sekiya if (mstat & (1 << i)) {
201 1.1 sekiya if (intrtab[intnum].ih_fun != NULL)
202 1.1 sekiya ret |= (intrtab[intnum].ih_fun)
203 1.1 sekiya (intrtab[intnum].ih_arg);
204 1.1 sekiya else
205 1.1 sekiya printf("int0: unexpected mapped interrupt %d\n",
206 1.1 sekiya intnum);
207 1.1 sekiya }
208 1.1 sekiya }
209 1.1 sekiya
210 1.1 sekiya return ret;
211 1.1 sekiya }
212 1.1 sekiya
213 1.1 sekiya void
214 1.6 pooka int_local0_intr(u_int32_t status, u_int32_t cause, u_int32_t pc,
215 1.6 pooka u_int32_t ipending)
216 1.1 sekiya {
217 1.1 sekiya int i;
218 1.1 sekiya int ret;
219 1.1 sekiya u_int32_t l0stat;
220 1.1 sekiya u_int32_t l0mask;
221 1.1 sekiya
222 1.1 sekiya ret = 0;
223 1.1 sekiya l0stat = bus_space_read_4(iot, ioh, INT2_LOCAL0_STATUS);
224 1.1 sekiya l0mask = bus_space_read_4(iot, ioh, INT2_LOCAL0_MASK);
225 1.1 sekiya
226 1.1 sekiya l0stat &= l0mask;
227 1.1 sekiya
228 1.1 sekiya for (i = 0; i < 8; i++) {
229 1.1 sekiya if (l0stat & (1 << i)) {
230 1.1 sekiya if (intrtab[i].ih_fun != NULL)
231 1.1 sekiya ret |= (intrtab[i].ih_fun)(intrtab[i].ih_arg);
232 1.1 sekiya else
233 1.6 pooka printf("int0: unexpected local0 interrupt %d\n",
234 1.6 pooka i);
235 1.1 sekiya }
236 1.1 sekiya }
237 1.1 sekiya }
238 1.1 sekiya
239 1.1 sekiya void
240 1.6 pooka int_local1_intr(u_int32_t status, u_int32_t cause, u_int32_t pc,
241 1.6 pooka u_int32_t ipending)
242 1.1 sekiya {
243 1.1 sekiya int i;
244 1.1 sekiya int ret;
245 1.1 sekiya u_int32_t l1stat;
246 1.1 sekiya u_int32_t l1mask;
247 1.1 sekiya
248 1.1 sekiya l1stat = bus_space_read_4(iot, ioh, INT2_LOCAL1_STATUS);
249 1.1 sekiya l1mask = bus_space_read_4(iot, ioh, INT2_LOCAL1_MASK);
250 1.1 sekiya
251 1.1 sekiya l1stat &= l1mask;
252 1.1 sekiya
253 1.1 sekiya ret = 0;
254 1.1 sekiya for (i = 0; i < 8; i++) {
255 1.1 sekiya if (l1stat & (1 << i)) {
256 1.1 sekiya if (intrtab[8 + i].ih_fun != NULL)
257 1.1 sekiya ret |= (intrtab[8 + i].ih_fun)
258 1.1 sekiya (intrtab[8 + i].ih_arg);
259 1.1 sekiya else
260 1.1 sekiya printf("int0: unexpected local1 interrupt %x\n",
261 1.6 pooka 8 + i);
262 1.1 sekiya }
263 1.1 sekiya }
264 1.1 sekiya }
265 1.1 sekiya
266 1.1 sekiya void *
267 1.1 sekiya int_intr_establish(int level, int ipl, int (*handler) (void *), void *arg)
268 1.1 sekiya {
269 1.1 sekiya u_int32_t mask;
270 1.1 sekiya
271 1.1 sekiya if (level < 0 || level >= NINTR)
272 1.1 sekiya panic("invalid interrupt level");
273 1.1 sekiya
274 1.1 sekiya if (intrtab[level].ih_fun != NULL)
275 1.1 sekiya {
276 1.1 sekiya printf("int0: cannot share interrupts yet.\n");
277 1.1 sekiya return (void *)NULL;
278 1.1 sekiya }
279 1.1 sekiya
280 1.1 sekiya intrtab[level].ih_fun = handler;
281 1.1 sekiya intrtab[level].ih_arg = arg;
282 1.1 sekiya
283 1.1 sekiya if (level < 8) {
284 1.1 sekiya mask = bus_space_read_4(iot, ioh, INT2_LOCAL0_MASK);
285 1.1 sekiya mask |= (1 << level);
286 1.1 sekiya bus_space_write_4(iot, ioh, INT2_LOCAL0_MASK, mask);
287 1.1 sekiya } else if (level < 16) {
288 1.1 sekiya mask = bus_space_read_4(iot, ioh, INT2_LOCAL1_MASK);
289 1.1 sekiya mask |= (1 << (level - 8));
290 1.1 sekiya bus_space_write_4(iot, ioh, INT2_LOCAL1_MASK, mask);
291 1.1 sekiya } else if (level < 24) {
292 1.1 sekiya /* Map0 interrupt maps to l0 bit 7, so turn that on too */
293 1.1 sekiya mask = bus_space_read_4(iot, ioh, INT2_LOCAL0_MASK);
294 1.1 sekiya mask |= (1 << 7);
295 1.1 sekiya bus_space_write_4(iot, ioh, INT2_LOCAL0_MASK, mask);
296 1.1 sekiya
297 1.1 sekiya mask = bus_space_read_4(iot, ioh, INT2_MAP_MASK0);
298 1.1 sekiya mask |= (1 << (level - 16));
299 1.1 sekiya bus_space_write_4(iot, ioh, INT2_MAP_MASK0, mask);
300 1.1 sekiya } else {
301 1.1 sekiya /* Map1 interrupt maps to l1 bit 3, so turn that on too */
302 1.1 sekiya mask = bus_space_read_4(iot, ioh, INT2_LOCAL1_MASK);
303 1.1 sekiya mask |= (1 << 3);
304 1.1 sekiya bus_space_write_4(iot, ioh, INT2_LOCAL1_MASK, mask);
305 1.1 sekiya
306 1.1 sekiya mask = bus_space_read_4(iot, ioh, INT2_MAP_MASK1);
307 1.1 sekiya mask |= (1 << (level - 24));
308 1.1 sekiya bus_space_write_4(iot, ioh, INT2_MAP_MASK1, mask);
309 1.1 sekiya }
310 1.1 sekiya
311 1.1 sekiya return (void *)NULL;
312 1.1 sekiya }
313 1.1 sekiya
314 1.4 pooka #ifdef MIPS3
315 1.1 sekiya unsigned long
316 1.1 sekiya int_cal_timer(void)
317 1.1 sekiya {
318 1.1 sekiya int s;
319 1.1 sekiya int roundtime;
320 1.1 sekiya int sampletime;
321 1.1 sekiya int startmsb, lsb, msb;
322 1.1 sekiya unsigned long startctr, endctr;
323 1.1 sekiya
324 1.1 sekiya /*
325 1.1 sekiya * NOTE: HZ must be greater than 15 for this to work, as otherwise
326 1.1 sekiya * we'll overflow the counter. We round the answer to hearest 1
327 1.1 sekiya * MHz of the master (2x) clock.
328 1.1 sekiya */
329 1.1 sekiya roundtime = (1000000 / hz) / 2;
330 1.1 sekiya sampletime = (1000000 / hz) + 0xff;
331 1.1 sekiya startmsb = (sampletime >> 8);
332 1.1 sekiya
333 1.1 sekiya s = splhigh();
334 1.1 sekiya
335 1.1 sekiya bus_space_write_4(iot, ioh, INT2_TIMER_CONTROL,
336 1.1 sekiya ( TIMER_SEL2 | TIMER_16BIT | TIMER_RATEGEN) );
337 1.1 sekiya bus_space_write_4(iot, ioh, INT2_TIMER_2, (sampletime & 0xff));
338 1.1 sekiya bus_space_write_4(iot, ioh, INT2_TIMER_2, (sampletime >> 8));
339 1.1 sekiya
340 1.1 sekiya startctr = mips3_cp0_count_read();
341 1.1 sekiya
342 1.1 sekiya /* Wait for the MSB to count down to zero */
343 1.1 sekiya do {
344 1.1 sekiya bus_space_write_4(iot, ioh, INT2_TIMER_CONTROL, TIMER_SEL2 );
345 1.1 sekiya lsb = bus_space_read_4(iot, ioh, INT2_TIMER_2) & 0xff;
346 1.1 sekiya msb = bus_space_read_4(iot, ioh, INT2_TIMER_2) & 0xff;
347 1.1 sekiya
348 1.1 sekiya endctr = mips3_cp0_count_read();
349 1.1 sekiya } while (msb);
350 1.1 sekiya
351 1.1 sekiya /* Turn off timer */
352 1.1 sekiya bus_space_write_4(iot, ioh, INT2_TIMER_CONTROL,
353 1.1 sekiya ( TIMER_SEL2 | TIMER_16BIT | TIMER_SWSTROBE) );
354 1.1 sekiya
355 1.1 sekiya splx(s);
356 1.1 sekiya
357 1.1 sekiya return (endctr - startctr) / roundtime * roundtime;
358 1.1 sekiya }
359 1.4 pooka #endif /* MIPS3 */
360 1.1 sekiya
361 1.1 sekiya void
362 1.1 sekiya int_8254_cal(void)
363 1.1 sekiya {
364 1.1 sekiya int s;
365 1.1 sekiya
366 1.1 sekiya s = splhigh();
367 1.1 sekiya
368 1.7 pooka bus_space_write_1(iot, ioh, INT2_TIMER_0 + 15,
369 1.1 sekiya TIMER_SEL0|TIMER_RATEGEN|TIMER_16BIT);
370 1.7 pooka bus_space_write_1(iot, ioh, INT2_TIMER_0 + 3, (20000 / hz) % 256);
371 1.1 sekiya wbflush();
372 1.1 sekiya delay(4);
373 1.7 pooka bus_space_write_1(iot, ioh, INT2_TIMER_0 + 3, (20000 / hz) / 256);
374 1.1 sekiya
375 1.7 pooka bus_space_write_1(iot, ioh, INT2_TIMER_0 + 15,
376 1.1 sekiya TIMER_SEL2|TIMER_RATEGEN|TIMER_16BIT);
377 1.7 pooka bus_space_write_1(iot, ioh, INT2_TIMER_0 + 11, 50);
378 1.1 sekiya wbflush();
379 1.1 sekiya delay(4);
380 1.7 pooka bus_space_write_1(iot, ioh, INT2_TIMER_0 + 11, 0);
381 1.1 sekiya splx(s);
382 1.1 sekiya }
383 1.3 sekiya
384 1.3 sekiya void
385 1.3 sekiya int2_wait_fifo(u_int32_t flag)
386 1.3 sekiya {
387 1.3 sekiya while (bus_space_read_4(iot, ioh, INT2_LOCAL0_STATUS) & flag)
388 1.3 sekiya ;
389 1.3 sekiya }
390