xref: /netbsd/sys/arch/alpha/jensenio/jensenio_intr.c (revision c4a72b64)
1 /* $NetBSD: jensenio_intr.c,v 1.4 2002/09/27 15:35:36 provos 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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
40 
41 __KERNEL_RCSID(0, "$NetBSD: jensenio_intr.c,v 1.4 2002/09/27 15:35:36 provos Exp $");
42 
43 #include <sys/types.h>
44 #include <sys/param.h>
45 #include <sys/time.h>
46 #include <sys/systm.h>
47 #include <sys/errno.h>
48 #include <sys/malloc.h>
49 #include <sys/device.h>
50 #include <sys/syslog.h>
51 
52 #include <machine/autoconf.h>
53 
54 #include <dev/eisa/eisavar.h>
55 
56 #include <dev/isa/isareg.h>
57 #include <dev/isa/isavar.h>
58 
59 #include <alpha/jensenio/jenseniovar.h>
60 
61 static bus_space_tag_t pic_iot;
62 static bus_space_handle_t pic_ioh[2];
63 static bus_space_handle_t pic_elcr_ioh;
64 
65 int	jensenio_eisa_intr_map(void *, u_int, eisa_intr_handle_t *);
66 const char *jensenio_eisa_intr_string(void *, int);
67 const struct evcnt *jensenio_eisa_intr_evcnt(void *, int);
68 void	*jensenio_eisa_intr_establish(void *, int, int, int,
69 	    int (*)(void *), void *);
70 void	jensenio_eisa_intr_disestablish(void *, void *);
71 int	jensenio_eisa_intr_alloc(void *, int, int, int *);
72 
73 #define	JENSEN_MAX_IRQ		16
74 
75 struct alpha_shared_intr *jensenio_eisa_intr;
76 
77 void	jensenio_iointr(void *, u_long);
78 
79 void	jensenio_enable_intr(int, int);
80 void	jensenio_setlevel(int, int);
81 void	jensenio_pic_init(void);
82 
83 const int jensenio_intr_deftype[JENSEN_MAX_IRQ] = {
84 	IST_EDGE,		/*  0: interval timer 0 output */
85 	IST_EDGE,		/*  1: line printer */
86 	IST_UNUSABLE,		/*  2: (cascade) */
87 	IST_NONE,		/*  3: EISA pin B25 */
88 	IST_NONE,		/*  4: EISA pin B24 */
89 	IST_NONE,		/*  5: EISA pin B23 */
90 	IST_NONE,		/*  6: EISA pin B22 (floppy) */
91 	IST_NONE,		/*  7: EISA pin B21 */
92 	IST_EDGE,		/*  8: RTC */
93 	IST_NONE,		/*  9: EISA pin B04 */
94 	IST_NONE,		/* 10: EISA pin D03 */
95 	IST_NONE,		/* 11: EISA pin D04 */
96 	IST_NONE,		/* 12: EISA pin D05 */
97 	IST_UNUSABLE,		/* 13: not connected */
98 	IST_NONE,		/* 14: EISA pin D07 (SCSI) */
99 	IST_NONE,		/* 15: EISA pin D06 */
100 };
101 
102 static __inline void
103 jensenio_specific_eoi(int irq)
104 {
105 
106 	if (irq > 7)
107 		bus_space_write_1(pic_iot, pic_ioh[1],
108 		    0, 0x20 | (irq & 0x07));
109 	bus_space_write_1(pic_iot, pic_ioh[0],
110 	    0, 0x20 | (irq > 7 ? 2 : irq));
111 }
112 
113 void
114 jensenio_intr_init(struct jensenio_config *jcp)
115 {
116 	eisa_chipset_tag_t ec = &jcp->jc_ec;
117 	isa_chipset_tag_t ic = &jcp->jc_ic;
118 	char *cp;
119 	int i;
120 
121 	pic_iot = &jcp->jc_eisa_iot;
122 
123 	jensenio_pic_init();
124 
125 	jensenio_eisa_intr = alpha_shared_intr_alloc(JENSEN_MAX_IRQ, 16);
126 	for (i = 0; i < JENSEN_MAX_IRQ; i++) {
127 		alpha_shared_intr_set_dfltsharetype(jensenio_eisa_intr,
128 		    i, jensenio_intr_deftype[i]);
129 		/* Don't bother with stray interrupts. */
130 		alpha_shared_intr_set_maxstrays(jensenio_eisa_intr,
131 		    i, 0);
132 
133 		cp = alpha_shared_intr_string(jensenio_eisa_intr, i);
134 		sprintf(cp, "irq %d", i);
135 		evcnt_attach_dynamic(alpha_shared_intr_evcnt(
136 		    jensenio_eisa_intr, i), EVCNT_TYPE_INTR,
137 		    NULL, "eisa", cp);
138 	}
139 
140 	/*
141 	 * The cascasde interrupt must be edge triggered and always enabled.
142 	 */
143 	jensenio_setlevel(2, 0);
144 	jensenio_enable_intr(2, 1);
145 
146 	/*
147 	 * Initialize the EISA chipset.
148 	 */
149 	ec->ec_v = jcp;
150 	ec->ec_intr_map = jensenio_eisa_intr_map;
151 	ec->ec_intr_string = jensenio_eisa_intr_string;
152 	ec->ec_intr_evcnt = jensenio_eisa_intr_evcnt;
153 	ec->ec_intr_establish = jensenio_eisa_intr_establish;
154 	ec->ec_intr_disestablish = jensenio_eisa_intr_disestablish;
155 
156 	/*
157 	 * Initialize the ISA chipset.
158 	 */
159 	ic->ic_v = jcp;
160 	ic->ic_intr_establish = jensenio_eisa_intr_establish;
161 	ic->ic_intr_disestablish = jensenio_eisa_intr_disestablish;
162 	ic->ic_intr_alloc = jensenio_eisa_intr_alloc;
163 	ic->ic_intr_evcnt = jensenio_eisa_intr_evcnt;
164 }
165 
166 int
167 jensenio_eisa_intr_map(void *v, u_int eirq, eisa_intr_handle_t *ihp)
168 {
169 
170 	if (*ihp >= JENSEN_MAX_IRQ)
171 		panic("jensenio_eisa_intr_map: bogus IRQ %d", *ihp);
172 
173 	if (jensenio_intr_deftype[eirq] == IST_UNUSABLE) {
174 		printf("jensenio_eisa_intr_map: unusable irq %d\n",
175 		    eirq);
176 		*ihp = -1;
177 		return (1);
178 	}
179 
180 	*ihp = eirq;
181 	return (0);
182 }
183 
184 const char *
185 jensenio_eisa_intr_string(void *v, int eirq)
186 {
187 	static char irqstr[64];
188 
189 	if (eirq >= JENSEN_MAX_IRQ)
190 		panic("jensenio_eisa_intr_string: bogus IRQ %d", eirq);
191 
192 	sprintf(irqstr, "eisa irq %d", eirq);
193 
194 	return (irqstr);
195 }
196 
197 const struct evcnt *
198 jensenio_eisa_intr_evcnt(void *v, int eirq)
199 {
200 
201 	if (eirq >= JENSEN_MAX_IRQ)
202 		panic("jensenio_eisa_intr_evcnt: bogus IRQ %d", eirq);
203 
204 	return (alpha_shared_intr_evcnt(jensenio_eisa_intr, eirq));
205 }
206 
207 void *
208 jensenio_eisa_intr_establish(void *v, int irq, int type, int level,
209     int (*fn)(void *), void *arg)
210 {
211 	void *cookie;
212 
213 	if (irq >= JENSEN_MAX_IRQ || type == IST_NONE)
214 		panic("jensenio_eisa_intr_establish: bogus irq or type");
215 
216 	if (jensenio_intr_deftype[irq] == IST_UNUSABLE) {
217 		printf("jensenio_eisa_intr_establish: IRQ %d not usable\n",
218 		    irq);
219 		return (NULL);
220 	}
221 
222 	cookie = alpha_shared_intr_establish(jensenio_eisa_intr, irq,
223 	    type, level, fn, arg, "eisa irq");
224 
225 	if (cookie != NULL &&
226 	    alpha_shared_intr_firstactive(jensenio_eisa_intr, irq)) {
227 		scb_set(0x800 + SCB_IDXTOVEC(irq), jensenio_iointr, NULL);
228 		jensenio_setlevel(irq,
229 		    alpha_shared_intr_get_sharetype(jensenio_eisa_intr,
230 						    irq) == IST_LEVEL);
231 		jensenio_enable_intr(irq, 1);
232 	}
233 
234 	return (cookie);
235 }
236 
237 void
238 jensenio_eisa_intr_disestablish(void *v, void *cookie)
239 {
240 	struct alpha_shared_intrhand *ih = cookie;
241 	int s, irq = ih->ih_num;
242 
243 	s = splhigh();
244 
245 	/* Remove it from the link. */
246 	alpha_shared_intr_disestablish(jensenio_eisa_intr, cookie,
247 	    "eisa irq");
248 
249 	if (alpha_shared_intr_isactive(jensenio_eisa_intr, irq) == 0) {
250 		jensenio_enable_intr(irq, 0);
251 		alpha_shared_intr_set_dfltsharetype(jensenio_eisa_intr,
252 		    irq, jensenio_intr_deftype[irq]);
253 		scb_free(0x800 + SCB_IDXTOVEC(irq));
254 	}
255 
256 	splx(s);
257 }
258 
259 int
260 jensenio_eisa_intr_alloc(void *v, int mask, int type, int *rqp)
261 {
262 
263 	/* XXX Not supported right now. */
264 	return (1);
265 }
266 
267 void
268 jensenio_iointr(void *framep, u_long vec)
269 {
270 	int irq;
271 
272 	irq = SCB_VECTOIDX(vec - 0x800);
273 
274 	if (!alpha_shared_intr_dispatch(jensenio_eisa_intr, irq))
275 		alpha_shared_intr_stray(jensenio_eisa_intr, irq, "eisa irq");
276 
277 	jensenio_specific_eoi(irq);
278 }
279 
280 void
281 jensenio_enable_intr(int irq, int onoff)
282 {
283 	int pic;
284 	u_int8_t bit, mask;
285 
286 	pic = irq >> 3;
287 	bit = 1 << (irq & 0x7);
288 
289 	mask = bus_space_read_1(pic_iot, pic_ioh[pic], 1);
290 	if (onoff)
291 		mask &= ~bit;
292 	else
293 		mask |= bit;
294 	bus_space_write_1(pic_iot, pic_ioh[pic], 1, mask);
295 }
296 
297 void
298 jensenio_setlevel(int irq, int level)
299 {
300 	int elcr;
301 	u_int8_t bit, mask;
302 
303 	elcr = irq >> 3;
304 	bit = 1 << (irq & 0x7);
305 
306 	mask = bus_space_read_1(pic_iot, pic_elcr_ioh, elcr);
307 	if (level)
308 		mask |= bit;
309 	else
310 		mask &= ~bit;
311 	bus_space_write_1(pic_iot, pic_elcr_ioh, elcr, mask);
312 }
313 
314 void
315 jensenio_pic_init(void)
316 {
317 	static const int picaddr[2] = { IO_ICU1, IO_ICU2 };
318 	int pic;
319 
320 	/*
321 	 * Map the PICs and mask off the interrupts on them.
322 	 */
323 	for (pic = 0; pic < 2; pic++) {
324 		if (bus_space_map(pic_iot, picaddr[pic], 2, 0, &pic_ioh[pic]))
325 			panic("jensenio_init_intr: unable to map PIC %d", pic);
326 		bus_space_write_1(pic_iot, pic_ioh[pic], 1, 0xff);
327 	}
328 
329 	/*
330 	 * Map the ELCR registers.
331 	 */
332 	if (bus_space_map(pic_iot, 0x4d0, 2, 0, &pic_elcr_ioh))
333 		panic("jensenio_init_intr: unable to map ELCR registers");
334 }
335