jensenio_intr.c revision 1.14 1 /* $NetBSD: jensenio_intr.c,v 1.14 2021/05/07 16:58:34 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND 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 THE FOUNDATION OR CONTRIBUTORS
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 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
33
34 __KERNEL_RCSID(0, "$NetBSD: jensenio_intr.c,v 1.14 2021/05/07 16:58:34 thorpej Exp $");
35
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/time.h>
39 #include <sys/systm.h>
40 #include <sys/errno.h>
41 #include <sys/malloc.h>
42 #include <sys/device.h>
43 #include <sys/cpu.h>
44 #include <sys/syslog.h>
45
46 #include <machine/autoconf.h>
47
48 #include <dev/eisa/eisavar.h>
49
50 #include <dev/isa/isareg.h>
51 #include <dev/isa/isavar.h>
52
53 #include <alpha/jensenio/jenseniovar.h>
54
55 static bus_space_tag_t pic_iot;
56 static bus_space_handle_t pic_ioh[2];
57 static bus_space_handle_t pic_elcr_ioh;
58
59 static int jensenio_eisa_intr_map(void *, u_int,
60 eisa_intr_handle_t *);
61 static const char * jensenio_eisa_intr_string(void *, int, char *, size_t);
62 static const struct evcnt *jensenio_eisa_intr_evcnt(void *, int);
63 static void * jensenio_eisa_intr_establish(void *, int, int, int,
64 int (*)(void *), void *);
65 static void jensenio_eisa_intr_disestablish(void *, void *);
66 static int jensenio_eisa_intr_alloc(void *, int, int, int *);
67
68 #define JENSEN_MAX_IRQ 16
69 #define JENSEN_MAX_IRQ_STR 16
70
71 static struct alpha_shared_intr *jensenio_eisa_intr;
72
73 static void jensenio_iointr(void *, u_long);
74
75 static void jensenio_enable_intr(int, int);
76 static void jensenio_setlevel(int, int);
77 static void jensenio_pic_init(void);
78
79 static const int jensenio_intr_deftype[JENSEN_MAX_IRQ] = {
80 IST_EDGE, /* 0: interval timer 0 output */
81 IST_EDGE, /* 1: line printer */
82 IST_UNUSABLE, /* 2: (cascade) */
83 IST_NONE, /* 3: EISA pin B25 */
84 IST_NONE, /* 4: EISA pin B24 */
85 IST_NONE, /* 5: EISA pin B23 */
86 IST_NONE, /* 6: EISA pin B22 (floppy) */
87 IST_NONE, /* 7: EISA pin B21 */
88 IST_EDGE, /* 8: RTC */
89 IST_NONE, /* 9: EISA pin B04 */
90 IST_NONE, /* 10: EISA pin D03 */
91 IST_NONE, /* 11: EISA pin D04 */
92 IST_NONE, /* 12: EISA pin D05 */
93 IST_UNUSABLE, /* 13: not connected */
94 IST_NONE, /* 14: EISA pin D07 (SCSI) */
95 IST_NONE, /* 15: EISA pin D06 */
96 };
97
98 static inline void
99 jensenio_specific_eoi(int irq)
100 {
101
102 if (irq > 7)
103 bus_space_write_1(pic_iot, pic_ioh[1],
104 0, 0x20 | (irq & 0x07));
105 bus_space_write_1(pic_iot, pic_ioh[0],
106 0, 0x20 | (irq > 7 ? 2 : irq));
107 }
108
109 void
110 jensenio_intr_init(struct jensenio_config *jcp)
111 {
112 eisa_chipset_tag_t ec = &jcp->jc_ec;
113 isa_chipset_tag_t ic = &jcp->jc_ic;
114 char *cp;
115 int i;
116
117 pic_iot = &jcp->jc_eisa_iot;
118
119 jensenio_pic_init();
120
121 jensenio_eisa_intr = alpha_shared_intr_alloc(JENSEN_MAX_IRQ,
122 JENSEN_MAX_IRQ_STR);
123 for (i = 0; i < JENSEN_MAX_IRQ; i++) {
124 alpha_shared_intr_set_dfltsharetype(jensenio_eisa_intr,
125 i, jensenio_intr_deftype[i]);
126 /* Don't bother with stray interrupts. */
127 alpha_shared_intr_set_maxstrays(jensenio_eisa_intr,
128 i, 0);
129
130 cp = alpha_shared_intr_string(jensenio_eisa_intr, i);
131 snprintf(cp, JENSEN_MAX_IRQ_STR, "irq %d", i);
132 evcnt_attach_dynamic(alpha_shared_intr_evcnt(
133 jensenio_eisa_intr, i), EVCNT_TYPE_INTR,
134 NULL, "eisa", cp);
135 }
136
137 /*
138 * The cascasde interrupt must be edge triggered and always enabled.
139 */
140 jensenio_setlevel(2, 0);
141 jensenio_enable_intr(2, 1);
142
143 /*
144 * Initialize the EISA chipset.
145 */
146 ec->ec_v = jcp;
147 ec->ec_intr_map = jensenio_eisa_intr_map;
148 ec->ec_intr_string = jensenio_eisa_intr_string;
149 ec->ec_intr_evcnt = jensenio_eisa_intr_evcnt;
150 ec->ec_intr_establish = jensenio_eisa_intr_establish;
151 ec->ec_intr_disestablish = jensenio_eisa_intr_disestablish;
152
153 /*
154 * Initialize the ISA chipset.
155 */
156 ic->ic_v = jcp;
157 ic->ic_intr_establish = jensenio_eisa_intr_establish;
158 ic->ic_intr_disestablish = jensenio_eisa_intr_disestablish;
159 ic->ic_intr_alloc = jensenio_eisa_intr_alloc;
160 ic->ic_intr_evcnt = jensenio_eisa_intr_evcnt;
161 }
162
163 static void
164 jensenio_intr_dispatch(void *arg, unsigned long vec)
165 {
166 struct jensenio_scb_intrhand *jih = arg;
167
168 jih->jih_evcnt.ev_count++;
169 (void) jih->jih_func(jih->jih_arg);
170 }
171
172 static void
173 jensenio_intr_dispatch_wrapped(void *arg, unsigned long vec)
174 {
175 KERNEL_LOCK(1, NULL);
176 jensenio_intr_dispatch(arg, vec);
177 KERNEL_UNLOCK_ONE(NULL);
178 }
179
180 void
181 jensenio_intr_establish(struct jensenio_scb_intrhand *jih,
182 unsigned long vec, int flags, int (*func)(void *), void *arg)
183 {
184 void (*scb_func)(void *, unsigned long);
185
186 /*
187 * Jensen systems are all uniprocessors, but we still do all
188 * of the KERNEL_LOCK handling as a formality to keep assertions
189 * valid in MI code.
190 */
191 KASSERT(CPU_IS_PRIMARY(curcpu()));
192 KASSERT(ncpu == 1);
193 if (flags & ALPHA_INTR_MPSAFE)
194 scb_func = jensenio_intr_dispatch;
195 else
196 scb_func = jensenio_intr_dispatch_wrapped;
197
198 jih->jih_func = func;
199 jih->jih_arg = arg;
200 jih->jih_vec = vec;
201
202 snprintf(jih->jih_vecstr, sizeof(jih->jih_vecstr), "0x%lx",
203 jih->jih_vec);
204 evcnt_attach_dynamic(&jih->jih_evcnt, EVCNT_TYPE_INTR,
205 NULL, "vector", jih->jih_vecstr);
206
207 mutex_enter(&cpu_lock);
208
209 scb_set(vec, scb_func, jih);
210 curcpu()->ci_nintrhand++;
211
212 mutex_exit(&cpu_lock);
213 }
214
215 static int
216 jensenio_eisa_intr_map(void *v, u_int eirq, eisa_intr_handle_t *ihp)
217 {
218
219 if (eirq >= JENSEN_MAX_IRQ) {
220 printf("jensenio_eisa_intr_map: bogus IRQ %d", eirq);
221 *ihp = -1;
222 return (1);
223 }
224
225 if (jensenio_intr_deftype[eirq] == IST_UNUSABLE) {
226 printf("jensenio_eisa_intr_map: unusable irq %d\n",
227 eirq);
228 *ihp = -1;
229 return (1);
230 }
231
232 *ihp = eirq;
233 return (0);
234 }
235
236 static const char *
237 jensenio_eisa_intr_string(void *v, int eirq, char *buf, size_t len)
238 {
239 if (eirq >= JENSEN_MAX_IRQ)
240 panic("%s: bogus IRQ %d", __func__, eirq);
241
242 snprintf(buf, len, "eisa irq %d", eirq);
243 return buf;
244 }
245
246 static const struct evcnt *
247 jensenio_eisa_intr_evcnt(void *v, int eirq)
248 {
249
250 if (eirq >= JENSEN_MAX_IRQ)
251 panic("%s: bogus IRQ %d", __func__, eirq);
252
253 return (alpha_shared_intr_evcnt(jensenio_eisa_intr, eirq));
254 }
255
256 static void *
257 jensenio_eisa_intr_establish(void *v, int irq, int type, int level,
258 int (*fn)(void *), void *arg)
259 {
260 void *cookie;
261
262 if (irq >= JENSEN_MAX_IRQ || type == IST_NONE)
263 panic("jensenio_eisa_intr_establish: bogus irq or type");
264
265 if (jensenio_intr_deftype[irq] == IST_UNUSABLE) {
266 printf("jensenio_eisa_intr_establish: IRQ %d not usable\n",
267 irq);
268 return NULL;
269 }
270
271 cookie = alpha_shared_intr_alloc_intrhand(jensenio_eisa_intr, irq,
272 type, level, 0, fn, arg, "eisa irq");
273
274 if (cookie == NULL)
275 return NULL;
276
277 mutex_enter(&cpu_lock);
278
279 if (! alpha_shared_intr_link(jensenio_eisa_intr, cookie, "eisa irq")) {
280 mutex_exit(&cpu_lock);
281 alpha_shared_intr_free_intrhand(cookie);
282 return NULL;
283 }
284
285 if (alpha_shared_intr_firstactive(jensenio_eisa_intr, irq)) {
286 scb_set(0x800 + SCB_IDXTOVEC(irq), jensenio_iointr, NULL);
287 jensenio_setlevel(irq,
288 alpha_shared_intr_get_sharetype(jensenio_eisa_intr,
289 irq) == IST_LEVEL);
290 jensenio_enable_intr(irq, 1);
291 }
292
293 mutex_exit(&cpu_lock);
294
295 return cookie;
296 }
297
298 static void
299 jensenio_eisa_intr_disestablish(void *v, void *cookie)
300 {
301 struct alpha_shared_intrhand *ih = cookie;
302 int irq = ih->ih_num;
303
304 mutex_enter(&cpu_lock);
305
306 if (alpha_shared_intr_firstactive(jensenio_eisa_intr, irq)) {
307 jensenio_enable_intr(irq, 0);
308 alpha_shared_intr_set_dfltsharetype(jensenio_eisa_intr,
309 irq, jensenio_intr_deftype[irq]);
310 scb_free(0x800 + SCB_IDXTOVEC(irq));
311 }
312
313 alpha_shared_intr_unlink(jensenio_eisa_intr, cookie, "eisa irq");
314
315 mutex_exit(&cpu_lock);
316
317 alpha_shared_intr_free_intrhand(cookie);
318 }
319
320 static int
321 jensenio_eisa_intr_alloc(void *v, int mask, int type, int *rqp)
322 {
323
324 /* XXX Not supported right now. */
325 return (1);
326 }
327
328 static void
329 jensenio_iointr(void *framep, u_long vec)
330 {
331 int irq;
332
333 irq = SCB_VECTOIDX(vec - 0x800);
334
335 if (!alpha_shared_intr_dispatch(jensenio_eisa_intr, irq))
336 alpha_shared_intr_stray(jensenio_eisa_intr, irq, "eisa irq");
337
338 jensenio_specific_eoi(irq);
339 }
340
341 static void
342 jensenio_enable_intr(int irq, int onoff)
343 {
344 int pic;
345 uint8_t bit, mask;
346
347 pic = irq >> 3;
348 bit = 1 << (irq & 0x7);
349
350 mask = bus_space_read_1(pic_iot, pic_ioh[pic], 1);
351 if (onoff)
352 mask &= ~bit;
353 else
354 mask |= bit;
355 bus_space_write_1(pic_iot, pic_ioh[pic], 1, mask);
356 }
357
358 void
359 jensenio_setlevel(int irq, int level)
360 {
361 int elcr;
362 uint8_t bit, mask;
363
364 elcr = irq >> 3;
365 bit = 1 << (irq & 0x7);
366
367 mask = bus_space_read_1(pic_iot, pic_elcr_ioh, elcr);
368 if (level)
369 mask |= bit;
370 else
371 mask &= ~bit;
372 bus_space_write_1(pic_iot, pic_elcr_ioh, elcr, mask);
373 }
374
375 static void
376 jensenio_pic_init(void)
377 {
378 static const int picaddr[2] = { IO_ICU1, IO_ICU2 };
379 int pic;
380
381 /*
382 * Map the PICs and mask off the interrupts on them.
383 */
384 for (pic = 0; pic < 2; pic++) {
385 if (bus_space_map(pic_iot, picaddr[pic], 2, 0, &pic_ioh[pic]))
386 panic("jensenio_init_intr: unable to map PIC %d", pic);
387 bus_space_write_1(pic_iot, pic_ioh[pic], 1, 0xff);
388 }
389
390 /*
391 * Map the ELCR registers.
392 */
393 if (bus_space_map(pic_iot, 0x4d0, 2, 0, &pic_elcr_ioh))
394 panic("jensenio_init_intr: unable to map ELCR registers");
395 }
396