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