xref: /netbsd/sys/arch/atari/isa/isa_milan.c (revision 6550d01e)
1 /*	$NetBSD: isa_milan.c,v 1.14 2009/03/18 10:22:25 cegger Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Leo Weppelman.
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>
33 __KERNEL_RCSID(0, "$NetBSD: isa_milan.c,v 1.14 2009/03/18 10:22:25 cegger Exp $");
34 
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/device.h>
39 
40 #include <dev/isa/isavar.h>
41 #include <dev/isa/isareg.h>
42 
43 #include <machine/iomap.h>
44 
45 void	isa_bus_init(void);
46 
47 static void new_imask(void);
48 static void isa_callback(int);
49 
50 /*
51  * Bitmask of currently enabled isa interrupts. Used by new_imask().
52  */
53 static u_int16_t imask_enable = 0xffff;
54 
55 #define	IRQ_SLAVE		2	/* Slave at level 2		*/
56 #define MILAN_MAX_ISA_INTS	16	/* Max. number of vectors	*/
57 #define	ICU_OFFSET		0	/* Interrupt vector base	*/
58 
59 #define	WICU(icu, val)		*(volatile u_int8_t*)(icu) = val
60 
61 static isa_intr_info_t milan_isa_iinfo[MILAN_MAX_ISA_INTS];
62 
63 void
64 isa_bus_init(void)
65 {
66 	volatile u_int8_t	*icu;
67 
68 	/*
69 	 * Initialize both the icu's:
70 	 *   - enter Special Mask Mode
71 	 *   - Block all interrupts
72 	 */
73 	icu = (u_int8_t*)(AD_8259_MASTER);
74 
75 	icu[0] = 0x11;			/* reset; program device, four bytes */
76 	icu[1] = ICU_OFFSET;		/* starting at this vector index */
77 	icu[1] = (1 << IRQ_SLAVE);	/* slave on line 2 */
78 	icu[1] = 1;			/* 8086 mode */
79 	icu[1] = 0xff;			/* leave interrupts masked */
80 	icu[0] = 0x68;			/* special mask mode  */
81 	icu[0] = 0x0a;			/* Read IRR by default. */
82 
83 	icu = (u_int8_t*)(AD_8259_SLAVE);
84 
85 	icu[0] = 0x11;			/* reset; program device, four bytes */
86 	icu[1] = ICU_OFFSET + 8;	/* starting at this vector index */
87 	icu[1] = IRQ_SLAVE;		/* slave on line 2 */
88 	icu[1] = 1;			/* 8086 mode */
89 	icu[1] = 0xff;			/* leave interrupts masked */
90 	icu[0] = 0x68;			/* special mask mode  */
91 	icu[0] = 0x0a;			/* Read IRR by default. */
92 }
93 
94 /*
95  * Determine and activate new interrupt mask by scanning the milan_isa_iinfo
96  * array for enabled interrupts.
97  */
98 static void
99 new_imask(void)
100 {
101 	int		irq;
102 	u_int16_t	nmask = 0;
103 
104 	for (irq = 0; irq < MILAN_MAX_ISA_INTS; irq++) {
105 		if (milan_isa_iinfo[irq].ifunc != NULL)
106 			nmask |= 1 << irq;
107 		if (nmask >= 0x100)
108 			nmask |= 1 << IRQ_SLAVE;
109 	}
110 	imask_enable = ~nmask;
111 	WICU(AD_8259_MASTER+1, imask_enable & 0xff);
112 	WICU(AD_8259_SLAVE+1 , (imask_enable >> 8) & 0xff);
113 }
114 
115 static void
116 isa_callback(int vector)
117 {
118 	isa_intr_info_t	*iinfo_p;
119 	int		s;
120 
121 	iinfo_p = &milan_isa_iinfo[vector];
122 
123 	s = splx(iinfo_p->ipl);
124 	(void) (iinfo_p->ifunc)(iinfo_p->iarg);
125 	if (vector > 7)
126 		WICU(AD_8259_SLAVE, 0x60 | (vector & 7));
127 	else WICU(AD_8259_MASTER, 0x60 | (vector & 7));
128 	splx(s);
129 }
130 
131 void milan_isa_intr(int, int);
132 void
133 milan_isa_intr(int vector, int sr)
134 {
135 	isa_intr_info_t *iinfo_p;
136 	int		s;
137 
138 	if (vector >= MILAN_MAX_ISA_INTS) {
139 		printf("milan_isa_intr: Bogus vector %d\n", vector);
140 		return;
141 	}
142 
143 	/* Ack cascade 0x60 == Specific EOI		*/
144 	if (vector > 7)
145 		WICU(AD_8259_MASTER, 0x60|IRQ_SLAVE);
146 
147 	iinfo_p = &milan_isa_iinfo[vector];
148 	if (iinfo_p->ifunc == NULL) {
149 		printf("milan_isa_intr: Stray interrupt: %d (mask:%04x)\n",
150 				vector, imask_enable);
151 		return;
152 	}
153 	if ((sr & PSL_IPL) >= (iinfo_p->ipl & PSL_IPL)) {
154 		/*
155 		 * We're running at a too high priority now.
156 		 */
157 		add_sicallback((si_farg)isa_callback, (void*)vector, 0);
158 	}
159 	else {
160 		s = splx(iinfo_p->ipl);
161 		(void) (iinfo_p->ifunc)(iinfo_p->iarg);
162 		if (vector > 7)
163 			WICU(AD_8259_SLAVE, 0x60 | (vector & 7));
164 		else WICU(AD_8259_MASTER, 0x60 | (vector & 7));
165 		splx(s);
166 	}
167 }
168 
169 /*
170  * Try to allocate a free interrupt... On the Milan, we have available:
171  * 5, 9, 10, 11, 13. Or in a bitmask: 0x1720.
172  */
173 #define	MILAN_AVAIL_ISA_INTS	0x1720
174 
175 int
176 isa_intr_alloc(isa_chipset_tag_t ic, int mask, int type, int *irq)
177 {
178 	int	i;
179 
180 	/*
181 	 * Say no to impossible questions...
182 	 */
183 	if (!(mask &= MILAN_AVAIL_ISA_INTS))
184 		return 1;
185 
186 	for (i = 0; i < MILAN_MAX_ISA_INTS; i++) {
187 		if (mask & (1<<i)) {
188 		    if (milan_isa_iinfo[i].ifunc == NULL) {
189 			*irq = i;
190 			return 0;
191 		    }
192 		}
193 	}
194 	return (1);
195 }
196 
197 void *
198 isa_intr_establish(isa_chipset_tag_t ic, int irq, int type, int level, int (*ih_fun)(void *), void *ih_arg)
199 {
200 	isa_intr_info_t *iinfo_p;
201 
202 	iinfo_p = &milan_isa_iinfo[irq];
203 
204 	if (iinfo_p->ifunc != NULL) {
205 		printf("isa_intr_establish: interrupt %d was already "
206 			"established\n", irq);
207 		return NULL;
208 	}
209 
210 	iinfo_p->slot  = 0;	/* Unused on Milan */
211 	iinfo_p->ihand = NULL;	/* Unused on Milan */
212 	iinfo_p->ipl   = level;
213 	iinfo_p->ifunc = ih_fun;
214 	iinfo_p->iarg  = ih_arg;
215 
216 	new_imask();
217 	return(iinfo_p);
218 }
219 
220 void
221 isa_intr_disestablish(isa_chipset_tag_t ic, void *handler)
222 {
223 	isa_intr_info_t *iinfo_p = (isa_intr_info_t *)handler;
224 
225 	if (iinfo_p->ifunc == NULL)
226 	    panic("isa_intr_disestablish: interrupt was not established");
227 
228 	iinfo_p->ifunc = NULL;
229 	new_imask();
230 }
231