frodo.c revision 1.12 1 /* $NetBSD: frodo.c,v 1.12 2002/09/27 20:31:45 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998, 1999 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 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: frodo.c,v 1.12 2002/09/27 20:31:45 thorpej Exp $");
71
72 #define _HP300_INTR_H_PRIVATE
73
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/kernel.h>
77 #include <sys/syslog.h>
78 #include <sys/device.h>
79
80 #include <machine/cpu.h>
81 #include <machine/intr.h>
82 #include <machine/hp300spu.h>
83
84 #include <hp300/dev/intiovar.h>
85
86 #include <hp300/dev/frodoreg.h>
87 #include <hp300/dev/frodovar.h>
88
89 /*
90 * Description of a Frodo interrupt handler.
91 */
92 struct frodo_interhand {
93 int (*ih_fn) __P((void *));
94 void *ih_arg;
95 int ih_priority;
96 };
97
98 struct frodo_softc {
99 struct device sc_dev; /* generic device glue */
100 volatile u_int8_t *sc_regs; /* register base */
101 struct frodo_interhand sc_intr[FRODO_NINTR]; /* interrupt handlers */
102 int sc_ipl;
103 void *sc_ih; /* out interrupt cookie */
104 int sc_refcnt; /* number of interrupt refs */
105 };
106
107 int frodomatch __P((struct device *, struct cfdata *, void *));
108 void frodoattach __P((struct device *, struct device *, void *));
109
110 int frodoprint __P((void *, const char *));
111 int frodosubmatch __P((struct device *, struct cfdata *, void *));
112
113 int frodointr __P((void *));
114
115 void frodo_imask __P((struct frodo_softc *, u_int16_t, u_int16_t));
116
117 const struct cfattach frodo_ca = {
118 sizeof(struct frodo_softc), frodomatch, frodoattach
119 };
120
121 struct frodo_attach_args frodo_subdevs[] = {
122 { "dnkbd", FRODO_APCI_OFFSET(0), FRODO_INTR_APCI0 },
123 { "apci", FRODO_APCI_OFFSET(1), FRODO_INTR_APCI1 },
124 { "apci", FRODO_APCI_OFFSET(2), FRODO_INTR_APCI2 },
125 { "apci", FRODO_APCI_OFFSET(3), FRODO_INTR_APCI3 },
126 { NULL, 0, 0 },
127 };
128
129 int
130 frodomatch(parent, match, aux)
131 struct device *parent;
132 struct cfdata *match;
133 void *aux;
134 {
135 struct intio_attach_args *ia = aux;
136 static int frodo_matched = 0;
137
138 /* only allow one instance */
139 if (frodo_matched)
140 return (0);
141
142 if (strcmp(ia->ia_modname, "frodo") != 0)
143 return (0);
144
145 if (badaddr((caddr_t)ia->ia_addr))
146 return (0);
147
148 frodo_matched = 1;
149 return (1);
150 }
151
152 void
153 frodoattach(parent, self, aux)
154 struct device *parent, *self;
155 void *aux;
156 {
157 struct frodo_softc *sc = (struct frodo_softc *)self;
158 struct intio_attach_args *ia = aux;
159 int i;
160
161 sc->sc_regs = (volatile u_int8_t *)ia->ia_addr;
162 sc->sc_ipl = ia->ia_ipl;
163
164 if ((FRODO_READ(sc, FRODO_IISR) & FRODO_IISR_SERVICE) == 0)
165 printf(": service mode enabled");
166 printf("\n");
167
168 /* Clear all of the interrupt handlers. */
169 memset(sc->sc_intr, 0, sizeof(sc->sc_intr));
170 sc->sc_refcnt = 0;
171
172 /*
173 * Disable all of the interrupt lines; we reenable them
174 * as subdevices attach.
175 */
176 frodo_imask(sc, 0, 0xffff);
177
178 /* Clear any pending interrupts. */
179 FRODO_WRITE(sc, FRODO_PIC_PU, 0xff);
180 FRODO_WRITE(sc, FRODO_PIC_PL, 0xff);
181
182 /* Set interrupt polarities. */
183 FRODO_WRITE(sc, FRODO_PIO_IPR, 0x10);
184
185 /* ...and configure for edge triggering. */
186 FRODO_WRITE(sc, FRODO_PIO_IELR, 0xcf);
187
188 /*
189 * We defer hooking up our interrupt handler until
190 * a subdevice hooks up theirs.
191 */
192 sc->sc_ih = NULL;
193
194 /* ... and attach subdevices. */
195 for (i = 0; frodo_subdevs[i].fa_name != NULL; i++) {
196 /*
197 * Skip the first serial port if we're not a 425e;
198 * it's mapped to the DCA at select code 9 on all
199 * other models.
200 */
201 if (frodo_subdevs[i].fa_offset == FRODO_APCI_OFFSET(1) &&
202 mmuid != MMUID_425_E)
203 continue;
204 config_found_sm(self, &frodo_subdevs[i],
205 frodoprint, frodosubmatch);
206 }
207 }
208
209 int
210 frodosubmatch(parent, cf, aux)
211 struct device *parent;
212 struct cfdata *cf;
213 void *aux;
214 {
215 struct frodo_attach_args *fa = aux;
216
217 if (cf->frodocf_offset != FRODO_UNKNOWN_OFFSET &&
218 cf->frodocf_offset != fa->fa_offset)
219 return (0);
220
221 return (config_match(parent, cf, aux));
222 }
223
224 int
225 frodoprint(aux, pnp)
226 void *aux;
227 const char *pnp;
228 {
229 struct frodo_attach_args *fa = aux;
230
231 if (pnp)
232 printf("%s at %s", fa->fa_name, pnp);
233 printf(" offset 0x%x", fa->fa_offset);
234 return (UNCONF);
235 }
236
237 void
238 frodo_intr_establish(frdev, func, arg, line, priority)
239 struct device *frdev;
240 int (*func) __P((void *));
241 void *arg;
242 int line;
243 int priority;
244 {
245 struct frodo_softc *sc = (struct frodo_softc *)frdev;
246 struct hp300_intrhand *ih = sc->sc_ih;
247
248 if (line < 0 || line >= FRODO_NINTR) {
249 printf("%s: bad interrupt line %d\n",
250 sc->sc_dev.dv_xname, line);
251 goto lose;
252 }
253 if (sc->sc_intr[line].ih_fn != NULL) {
254 printf("%s: interrupt line %d already used\n",
255 sc->sc_dev.dv_xname, line);
256 goto lose;
257 }
258
259 /* Install the handler. */
260 sc->sc_intr[line].ih_fn = func;
261 sc->sc_intr[line].ih_arg = arg;
262 sc->sc_intr[line].ih_priority = priority;
263
264 /*
265 * If this is the first one, establish the frodo
266 * interrupt handler. If not, reestablish at a
267 * higher priority if necessary.
268 */
269 if (ih == NULL || ih->ih_priority < priority) {
270 if (ih != NULL)
271 intr_disestablish(ih);
272 sc->sc_ih = intr_establish(frodointr, sc, sc->sc_ipl, priority);
273 }
274
275 sc->sc_refcnt++;
276
277 /* Enable the interrupt line. */
278 frodo_imask(sc, (1 << line), 0);
279 return;
280 lose:
281 panic("frodo_intr_establish");
282 }
283
284 void
285 frodo_intr_disestablish(frdev, line)
286 struct device *frdev;
287 int line;
288 {
289 struct frodo_softc *sc = (struct frodo_softc *)frdev;
290 struct hp300_intrhand *ih = sc->sc_ih;
291 int newpri;
292
293 if (sc->sc_intr[line].ih_fn == NULL) {
294 printf("%s: no handler for line %d\n",
295 sc->sc_dev.dv_xname, line);
296 panic("frodo_intr_disestablish");
297 }
298
299 sc->sc_intr[line].ih_fn = NULL;
300 frodo_imask(sc, 0, (1 << line));
301
302 /* If this was the last, unhook ourselves. */
303 if (sc->sc_refcnt-- == 1) {
304 intr_disestablish(ih);
305 return;
306 }
307
308 /* Lower our priority, if appropriate. */
309 for (newpri = 0, line = 0; line < FRODO_NINTR; line++)
310 if (sc->sc_intr[line].ih_fn != NULL &&
311 sc->sc_intr[line].ih_priority > newpri)
312 newpri = sc->sc_intr[line].ih_priority;
313
314 if (newpri != ih->ih_priority) {
315 intr_disestablish(ih);
316 sc->sc_ih = intr_establish(frodointr, sc, sc->sc_ipl, newpri);
317 }
318 }
319
320 int
321 frodointr(arg)
322 void *arg;
323 {
324 struct frodo_softc *sc = arg;
325 struct frodo_interhand *fih;
326 int line, taken = 0;
327
328 /* Any interrupts pending? */
329 if (FRODO_GETPEND(sc) == 0)
330 return (0);
331
332 do {
333 /*
334 * Get pending interrupt; this also clears it for us.
335 */
336 line = FRODO_IPEND(sc);
337 fih = &sc->sc_intr[line];
338 if (fih->ih_fn == NULL ||
339 (*fih->ih_fn)(fih->ih_arg) == 0)
340 printf("%s: spurious interrupt on line %d\n",
341 sc->sc_dev.dv_xname, line);
342 if (taken++ > 100)
343 panic("frodointr: looping!");
344 } while (FRODO_GETPEND(sc) != 0);
345
346 return (1);
347 }
348
349 void
350 frodo_imask(sc, set, clear)
351 struct frodo_softc *sc;
352 u_int16_t set, clear;
353 {
354 u_int16_t imask;
355
356 imask = FRODO_GETMASK(sc);
357
358 imask |= set;
359 imask &= ~clear;
360
361 FRODO_SETMASK(sc, imask);
362 }
363