frodo.c revision 1.4 1 /* $NetBSD: frodo.c,v 1.4 1998/01/12 18:30:52 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998 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 /*
40 * Copyright (c) 1997 Michael Smith. All rights reserved.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 */
63
64 /*
65 * Support for the "Frodo" (a.k.a. "Apollo Utility") chip found
66 * in HP Apollo 9000/4xx workstations.
67 */
68
69 #define _HP300_INTR_H_PRIVATE
70
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/kernel.h>
74 #include <sys/syslog.h>
75 #include <sys/device.h>
76
77 #include <machine/cpu.h>
78 #include <machine/intr.h>
79 #include <machine/hp300spu.h>
80
81 #include <hp300/dev/intiovar.h>
82
83 #include <hp300/dev/frodoreg.h>
84 #include <hp300/dev/frodovar.h>
85
86 /*
87 * Description of a Frodo interrupt handler.
88 */
89 struct frodo_isr {
90 int (*isr_func) __P((void *));
91 void *isr_arg;
92 int isr_priority;
93 };
94
95 struct frodo_softc {
96 struct device sc_dev; /* generic device glue */
97 volatile u_int8_t *sc_regs; /* register base */
98 struct frodo_isr sc_intr[FRODO_NINTR]; /* interrupt handlers */
99 void *sc_ih; /* out interrupt cookie */
100 int sc_refcnt; /* number of interrupt refs */
101 };
102
103 int frodomatch __P((struct device *, struct cfdata *, void *));
104 void frodoattach __P((struct device *, struct device *, void *));
105
106 int frodoprint __P((void *, const char *));
107 int frodosubmatch __P((struct device *, struct cfdata *, void *));
108
109 int frodointr __P((void *));
110
111 void frodo_imask __P((struct frodo_softc *, u_int16_t, u_int16_t));
112
113 struct cfattach frodo_ca = {
114 sizeof(struct frodo_softc), frodomatch, frodoattach
115 };
116
117 struct frodo_attach_args frodo_subdevs[] = {
118 { "dnkbd", FRODO_APCI_OFFSET(0), FRODO_INTR_APCI0 },
119 { "apci", FRODO_APCI_OFFSET(1), FRODO_INTR_APCI1 },
120 { "apci", FRODO_APCI_OFFSET(2), FRODO_INTR_APCI2 },
121 { "apci", FRODO_APCI_OFFSET(3), FRODO_INTR_APCI3 },
122 { NULL, 0, 0 },
123 };
124
125 int
126 frodomatch(parent, match, aux)
127 struct device *parent;
128 struct cfdata *match;
129 void *aux;
130 {
131 struct intio_attach_args *ia = aux;
132 caddr_t va;
133 static int frodo_matched = 0;
134
135 /* only allow one instance */
136 if (frodo_matched)
137 return (0);
138
139 /* only 4xx workstations can have this */
140 switch (machineid) {
141 case HP_400:
142 case HP_425:
143 case HP_433:
144 break;
145
146 default:
147 return (0);
148 }
149
150 /* make sure the hardware is there in any case */
151 va = (caddr_t)IIOV(FRODO_BASE);
152 if (badaddr(va))
153 return (0);
154
155 frodo_matched = 1;
156 ia->ia_addr = FRODO_BASE;
157 return (1);
158 }
159
160 void
161 frodoattach(parent, self, aux)
162 struct device *parent, *self;
163 void *aux;
164 {
165 struct frodo_softc *sc = (struct frodo_softc *)self;
166 struct intio_attach_args *ia = aux;
167 int i;
168
169 sc->sc_regs = (volatile u_int8_t *)IIOV(ia->ia_addr);
170
171 if ((FRODO_READ(sc, FRODO_IISR) & FRODO_IISR_SERVICE) == 0)
172 printf(": service mode enabled");
173 printf("\n");
174
175 /* Clear all of the interrupt handlers. */
176 bzero(sc->sc_intr, sizeof(sc->sc_intr));
177 sc->sc_refcnt = 0;
178
179 /*
180 * Disable all of the interrupt lines; we reenable them
181 * as subdevices attach.
182 */
183 frodo_imask(sc, 0, 0xffff);
184
185 /* Clear any pending interrupts. */
186 FRODO_WRITE(sc, FRODO_PIC_PU, 0xff);
187 FRODO_WRITE(sc, FRODO_PIC_PL, 0xff);
188
189 /* Set interrupt polarities. */
190 FRODO_WRITE(sc, FRODO_PIO_IPR, 0x10);
191
192 /* ...and configure for edge triggering. */
193 FRODO_WRITE(sc, FRODO_PIO_IELR, 0xcf);
194
195 /*
196 * We defer hooking up our interrupt handler until
197 * a subdevice hooks up theirs.
198 */
199 sc->sc_ih = NULL;
200
201 /* ... and attach subdevices. */
202 for (i = 0; frodo_subdevs[i].fa_name != NULL; i++)
203 config_found_sm(self, &frodo_subdevs[i],
204 frodoprint, frodosubmatch);
205 }
206
207 int
208 frodosubmatch(parent, cf, aux)
209 struct device *parent;
210 struct cfdata *cf;
211 void *aux;
212 {
213 struct frodo_attach_args *fa = aux;
214
215 if (cf->frodocf_offset != FRODO_UNKNOWN_OFFSET &&
216 cf->frodocf_offset != fa->fa_offset)
217 return (0);
218
219 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
220 }
221
222 int
223 frodoprint(aux, pnp)
224 void *aux;
225 const char *pnp;
226 {
227 struct frodo_attach_args *fa = aux;
228
229 if (pnp)
230 printf("%s at %s", fa->fa_name, pnp);
231 printf(" offset 0x%x", fa->fa_offset);
232 return (UNCONF);
233 }
234
235 void
236 frodo_intr_establish(frdev, func, arg, line, priority)
237 struct device *frdev;
238 int (*func) __P((void *));
239 void *arg;
240 int line;
241 int priority;
242 {
243 struct frodo_softc *sc = (struct frodo_softc *)frdev;
244 struct isr *isr = sc->sc_ih;
245
246 if (line < 0 || line >= FRODO_NINTR) {
247 printf("%s: bad interrupt line %d\n",
248 sc->sc_dev.dv_xname, line);
249 goto lose;
250 }
251 if (sc->sc_intr[line].isr_func != NULL) {
252 printf("%s: interrupt line %d already used\n",
253 sc->sc_dev.dv_xname, line);
254 goto lose;
255 }
256
257 /* Install the handler. */
258 sc->sc_intr[line].isr_func = func;
259 sc->sc_intr[line].isr_arg = arg;
260 sc->sc_intr[line].isr_priority = priority;
261
262 /*
263 * If this is the first one, establish the frodo
264 * interrupt handler. If not, reestablish at a
265 * higher priority if necessary.
266 */
267 if (isr == NULL || isr->isr_priority < priority) {
268 if (isr != NULL)
269 intr_disestablish(isr);
270 sc->sc_ih = intr_establish(frodointr, sc, 5, priority);
271 }
272
273 sc->sc_refcnt++;
274
275 /* Enable the interrupt line. */
276 frodo_imask(sc, (1 << line), 0);
277 return;
278 lose:
279 panic("frodo_intr_establish");
280 }
281
282 void
283 frodo_intr_disestablish(frdev, line)
284 struct device *frdev;
285 int line;
286 {
287 struct frodo_softc *sc = (struct frodo_softc *)frdev;
288 struct isr *isr = sc->sc_ih;
289 int newpri;
290
291 if (sc->sc_intr[line].isr_func == NULL) {
292 printf("%s: no handler for line %d\n",
293 sc->sc_dev.dv_xname, line);
294 panic("frodo_intr_disestablish");
295 }
296
297 sc->sc_intr[line].isr_func = NULL;
298 frodo_imask(sc, 0, (1 << line));
299
300 /* If this was the last, unhook ourselves. */
301 if (sc->sc_refcnt-- == 1) {
302 intr_disestablish(isr);
303 return;
304 }
305
306 /* Lower our priority, if appropriate. */
307 for (newpri = 0, line = 0; line < FRODO_NINTR; line++)
308 if (sc->sc_intr[line].isr_func != NULL &&
309 sc->sc_intr[line].isr_priority > newpri)
310 newpri = sc->sc_intr[line].isr_priority;
311
312 if (newpri != isr->isr_priority) {
313 intr_disestablish(isr);
314 sc->sc_ih = intr_establish(frodointr, sc, 5, newpri);
315 }
316 }
317
318 int
319 frodointr(arg)
320 void *arg;
321 {
322 struct frodo_softc *sc = arg;
323 struct frodo_isr *fisr;
324 int line, taken = 0;
325
326 /* Any interrupts pending? */
327 if (FRODO_GETPEND(sc) == 0)
328 return (0);
329
330 do {
331 /*
332 * Get pending interrupt; this also clears it for us.
333 */
334 line = FRODO_IPEND(sc);
335 fisr = &sc->sc_intr[line];
336 if (fisr->isr_func == NULL ||
337 (*fisr->isr_func)(fisr->isr_arg) == 0)
338 printf("%s: spurious interrupt on line %d\n",
339 sc->sc_dev.dv_xname, line);
340 if (taken++ > 100)
341 panic("frodointr: looping!");
342 } while (FRODO_GETPEND(sc) != 0);
343
344 return (1);
345 }
346
347 void
348 frodo_imask(sc, set, clear)
349 struct frodo_softc *sc;
350 u_int16_t set, clear;
351 {
352 u_int16_t imask;
353
354 imask = FRODO_GETMASK(sc);
355
356 imask |= set;
357 imask &= ~clear;
358
359 FRODO_SETMASK(sc, imask);
360 }
361