jensenio_intr.c revision 1.16 1 /* $NetBSD: jensenio_intr.c,v 1.16 2021/07/04 22:36:43 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.16 2021/07/04 22:36:43 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
70 static struct alpha_shared_intr *jensenio_eisa_intr;
71
72 static void jensenio_iointr(void *, u_long);
73
74 static void jensenio_enable_intr(int, int);
75 static void jensenio_setlevel(int, int);
76 static void jensenio_pic_init(void);
77
78 static const int jensenio_intr_deftype[JENSEN_MAX_IRQ] = {
79 IST_EDGE, /* 0: interval timer 0 output */
80 IST_EDGE, /* 1: line printer */
81 IST_UNUSABLE, /* 2: (cascade) */
82 IST_NONE, /* 3: EISA pin B25 */
83 IST_NONE, /* 4: EISA pin B24 */
84 IST_NONE, /* 5: EISA pin B23 */
85 IST_NONE, /* 6: EISA pin B22 (floppy) */
86 IST_NONE, /* 7: EISA pin B21 */
87 IST_EDGE, /* 8: RTC */
88 IST_NONE, /* 9: EISA pin B04 */
89 IST_NONE, /* 10: EISA pin D03 */
90 IST_NONE, /* 11: EISA pin D04 */
91 IST_NONE, /* 12: EISA pin D05 */
92 IST_UNUSABLE, /* 13: not connected */
93 IST_NONE, /* 14: EISA pin D07 (SCSI) */
94 IST_NONE, /* 15: EISA pin D06 */
95 };
96
97 static inline void
98 jensenio_specific_eoi(int irq)
99 {
100
101 if (irq > 7)
102 bus_space_write_1(pic_iot, pic_ioh[1],
103 0, 0x20 | (irq & 0x07));
104 bus_space_write_1(pic_iot, pic_ioh[0],
105 0, 0x20 | (irq > 7 ? 2 : irq));
106 }
107
108 void
109 jensenio_intr_init(struct jensenio_config *jcp)
110 {
111 eisa_chipset_tag_t ec = &jcp->jc_ec;
112 isa_chipset_tag_t ic = &jcp->jc_ic;
113 struct evcnt *ev;
114 const 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 for (i = 0; i < JENSEN_MAX_IRQ; i++) {
123 alpha_shared_intr_set_dfltsharetype(jensenio_eisa_intr,
124 i, jensenio_intr_deftype[i]);
125
126 ev = alpha_shared_intr_evcnt(jensenio_eisa_intr, i);
127 cp = alpha_shared_intr_string(jensenio_eisa_intr, i);
128
129 evcnt_attach_dynamic(ev, EVCNT_TYPE_INTR, NULL, "eisa", cp);
130 }
131
132 /*
133 * The cascasde interrupt must be edge triggered and always enabled.
134 */
135 jensenio_setlevel(2, 0);
136 jensenio_enable_intr(2, 1);
137
138 /*
139 * Initialize the EISA chipset.
140 */
141 ec->ec_v = jcp;
142 ec->ec_intr_map = jensenio_eisa_intr_map;
143 ec->ec_intr_string = jensenio_eisa_intr_string;
144 ec->ec_intr_evcnt = jensenio_eisa_intr_evcnt;
145 ec->ec_intr_establish = jensenio_eisa_intr_establish;
146 ec->ec_intr_disestablish = jensenio_eisa_intr_disestablish;
147
148 /*
149 * Initialize the ISA chipset.
150 */
151 ic->ic_v = jcp;
152 ic->ic_intr_establish = jensenio_eisa_intr_establish;
153 ic->ic_intr_disestablish = jensenio_eisa_intr_disestablish;
154 ic->ic_intr_alloc = jensenio_eisa_intr_alloc;
155 ic->ic_intr_evcnt = jensenio_eisa_intr_evcnt;
156 }
157
158 static void
159 jensenio_intr_dispatch(void *arg, unsigned long vec)
160 {
161 struct jensenio_scb_intrhand *jih = arg;
162
163 jih->jih_evcnt.ev_count++;
164 (void) jih->jih_func(jih->jih_arg);
165 }
166
167 static void
168 jensenio_intr_dispatch_wrapped(void *arg, unsigned long vec)
169 {
170 KERNEL_LOCK(1, NULL);
171 jensenio_intr_dispatch(arg, vec);
172 KERNEL_UNLOCK_ONE(NULL);
173 }
174
175 void
176 jensenio_intr_establish(struct jensenio_scb_intrhand *jih,
177 unsigned long vec, int flags, int (*func)(void *), void *arg)
178 {
179 void (*scb_func)(void *, unsigned long);
180
181 /*
182 * Jensen systems are all uniprocessors, but we still do all
183 * of the KERNEL_LOCK handling as a formality to keep assertions
184 * valid in MI code.
185 */
186 KASSERT(CPU_IS_PRIMARY(curcpu()));
187 KASSERT(ncpu == 1);
188 if (flags & ALPHA_INTR_MPSAFE)
189 scb_func = jensenio_intr_dispatch;
190 else
191 scb_func = jensenio_intr_dispatch_wrapped;
192
193 jih->jih_func = func;
194 jih->jih_arg = arg;
195 jih->jih_vec = vec;
196
197 snprintf(jih->jih_vecstr, sizeof(jih->jih_vecstr), "0x%lx",
198 jih->jih_vec);
199 evcnt_attach_dynamic(&jih->jih_evcnt, EVCNT_TYPE_INTR,
200 NULL, "vector", jih->jih_vecstr);
201
202 mutex_enter(&cpu_lock);
203
204 scb_set(vec, scb_func, jih);
205 curcpu()->ci_nintrhand++;
206
207 mutex_exit(&cpu_lock);
208 }
209
210 static int
211 jensenio_eisa_intr_map(void *v, u_int eirq, eisa_intr_handle_t *ihp)
212 {
213
214 if (eirq >= JENSEN_MAX_IRQ) {
215 printf("jensenio_eisa_intr_map: bogus IRQ %d", eirq);
216 *ihp = -1;
217 return (1);
218 }
219
220 if (jensenio_intr_deftype[eirq] == IST_UNUSABLE) {
221 printf("jensenio_eisa_intr_map: unusable irq %d\n",
222 eirq);
223 *ihp = -1;
224 return (1);
225 }
226
227 *ihp = eirq;
228 return (0);
229 }
230
231 static const char *
232 jensenio_eisa_intr_string(void *v, int eirq, char *buf, size_t len)
233 {
234 if (eirq >= JENSEN_MAX_IRQ)
235 panic("%s: bogus IRQ %d", __func__, eirq);
236
237 snprintf(buf, len, "eisa irq %d", eirq);
238 return buf;
239 }
240
241 static const struct evcnt *
242 jensenio_eisa_intr_evcnt(void *v, int eirq)
243 {
244
245 if (eirq >= JENSEN_MAX_IRQ)
246 panic("%s: bogus IRQ %d", __func__, eirq);
247
248 return (alpha_shared_intr_evcnt(jensenio_eisa_intr, eirq));
249 }
250
251 static void *
252 jensenio_eisa_intr_establish(void *v, int irq, int type, int level,
253 int (*fn)(void *), void *arg)
254 {
255 void *cookie;
256
257 if (irq >= JENSEN_MAX_IRQ || type == IST_NONE)
258 panic("jensenio_eisa_intr_establish: bogus irq or type");
259
260 if (jensenio_intr_deftype[irq] == IST_UNUSABLE) {
261 printf("jensenio_eisa_intr_establish: IRQ %d not usable\n",
262 irq);
263 return NULL;
264 }
265
266 cookie = alpha_shared_intr_alloc_intrhand(jensenio_eisa_intr, irq,
267 type, level, 0, fn, arg, "eisa");
268
269 if (cookie == NULL)
270 return NULL;
271
272 mutex_enter(&cpu_lock);
273
274 if (! alpha_shared_intr_link(jensenio_eisa_intr, cookie, "eisa")) {
275 mutex_exit(&cpu_lock);
276 alpha_shared_intr_free_intrhand(cookie);
277 return NULL;
278 }
279
280 if (alpha_shared_intr_firstactive(jensenio_eisa_intr, irq)) {
281 scb_set(0x800 + SCB_IDXTOVEC(irq), jensenio_iointr, NULL);
282 jensenio_setlevel(irq,
283 alpha_shared_intr_get_sharetype(jensenio_eisa_intr,
284 irq) == IST_LEVEL);
285 jensenio_enable_intr(irq, 1);
286 }
287
288 mutex_exit(&cpu_lock);
289
290 return cookie;
291 }
292
293 static void
294 jensenio_eisa_intr_disestablish(void *v, void *cookie)
295 {
296 struct alpha_shared_intrhand *ih = cookie;
297 int irq = ih->ih_num;
298
299 mutex_enter(&cpu_lock);
300
301 if (alpha_shared_intr_firstactive(jensenio_eisa_intr, irq)) {
302 jensenio_enable_intr(irq, 0);
303 alpha_shared_intr_set_dfltsharetype(jensenio_eisa_intr,
304 irq, jensenio_intr_deftype[irq]);
305 scb_free(0x800 + SCB_IDXTOVEC(irq));
306 }
307
308 alpha_shared_intr_unlink(jensenio_eisa_intr, cookie, "eisa");
309
310 mutex_exit(&cpu_lock);
311
312 alpha_shared_intr_free_intrhand(cookie);
313 }
314
315 static int
316 jensenio_eisa_intr_alloc(void *v, int mask, int type, int *rqp)
317 {
318
319 /* XXX Not supported right now. */
320 return (1);
321 }
322
323 static void
324 jensenio_iointr(void *framep, u_long vec)
325 {
326 int irq;
327
328 irq = SCB_VECTOIDX(vec - 0x800);
329
330 if (!alpha_shared_intr_dispatch(jensenio_eisa_intr, irq))
331 alpha_shared_intr_stray(jensenio_eisa_intr, irq, "eisa");
332
333 jensenio_specific_eoi(irq);
334 }
335
336 static void
337 jensenio_enable_intr(int irq, int onoff)
338 {
339 int pic;
340 uint8_t bit, mask;
341
342 pic = irq >> 3;
343 bit = 1 << (irq & 0x7);
344
345 mask = bus_space_read_1(pic_iot, pic_ioh[pic], 1);
346 if (onoff)
347 mask &= ~bit;
348 else
349 mask |= bit;
350 bus_space_write_1(pic_iot, pic_ioh[pic], 1, mask);
351 }
352
353 void
354 jensenio_setlevel(int irq, int level)
355 {
356 int elcr;
357 uint8_t bit, mask;
358
359 elcr = irq >> 3;
360 bit = 1 << (irq & 0x7);
361
362 mask = bus_space_read_1(pic_iot, pic_elcr_ioh, elcr);
363 if (level)
364 mask |= bit;
365 else
366 mask &= ~bit;
367 bus_space_write_1(pic_iot, pic_elcr_ioh, elcr, mask);
368 }
369
370 static void
371 jensenio_pic_init(void)
372 {
373 static const int picaddr[2] = { IO_ICU1, IO_ICU2 };
374 int pic;
375
376 /*
377 * Map the PICs and mask off the interrupts on them.
378 */
379 for (pic = 0; pic < 2; pic++) {
380 if (bus_space_map(pic_iot, picaddr[pic], 2, 0, &pic_ioh[pic]))
381 panic("jensenio_init_intr: unable to map PIC %d", pic);
382 bus_space_write_1(pic_iot, pic_ioh[pic], 1, 0xff);
383 }
384
385 /*
386 * Map the ELCR registers.
387 */
388 if (bus_space_map(pic_iot, 0x4d0, 2, 0, &pic_elcr_ioh))
389 panic("jensenio_init_intr: unable to map ELCR registers");
390 }
391