xref: /netbsd-src/sys/arch/hp300/dev/frodo.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: frodo.c,v 1.5 1999/07/31 21:15:20 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 #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 		/*
204 		 * Skip the first serial port if we're not a 425e;
205 		 * it's mapped to the DCA at select code 9 on all
206 		 * other models.
207 		 */
208 		if (frodo_subdevs[i].fa_offset == FRODO_APCI_OFFSET(1) &&
209 		    mmuid != MMUID_425_E)
210 			continue;
211 		config_found_sm(self, &frodo_subdevs[i],
212 		    frodoprint, frodosubmatch);
213 	}
214 }
215 
216 int
217 frodosubmatch(parent, cf, aux)
218 	struct device *parent;
219 	struct cfdata *cf;
220 	void *aux;
221 {
222 	struct frodo_attach_args *fa = aux;
223 
224 	if (cf->frodocf_offset != FRODO_UNKNOWN_OFFSET &&
225 	    cf->frodocf_offset != fa->fa_offset)
226 		return (0);
227 
228 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
229 }
230 
231 int
232 frodoprint(aux, pnp)
233 	void *aux;
234 	const char *pnp;
235 {
236 	struct frodo_attach_args *fa = aux;
237 
238 	if (pnp)
239 		printf("%s at %s", fa->fa_name, pnp);
240 	printf(" offset 0x%x", fa->fa_offset);
241 	return (UNCONF);
242 }
243 
244 void
245 frodo_intr_establish(frdev, func, arg, line, priority)
246 	struct device *frdev;
247 	int (*func) __P((void *));
248 	void *arg;
249 	int line;
250 	int priority;
251 {
252 	struct frodo_softc *sc = (struct frodo_softc *)frdev;
253 	struct isr *isr = sc->sc_ih;
254 
255 	if (line < 0 || line >= FRODO_NINTR) {
256 		printf("%s: bad interrupt line %d\n",
257 		    sc->sc_dev.dv_xname, line);
258 		goto lose;
259 	}
260 	if (sc->sc_intr[line].isr_func != NULL) {
261 		printf("%s: interrupt line %d already used\n",
262 		    sc->sc_dev.dv_xname, line);
263 		goto lose;
264 	}
265 
266 	/* Install the handler. */
267 	sc->sc_intr[line].isr_func = func;
268 	sc->sc_intr[line].isr_arg = arg;
269 	sc->sc_intr[line].isr_priority = priority;
270 
271 	/*
272 	 * If this is the first one, establish the frodo
273 	 * interrupt handler.  If not, reestablish at a
274 	 * higher priority if necessary.
275 	 */
276 	if (isr == NULL || isr->isr_priority < priority) {
277 		if (isr != NULL)
278 			intr_disestablish(isr);
279 		sc->sc_ih = intr_establish(frodointr, sc, 5, priority);
280 	}
281 
282 	sc->sc_refcnt++;
283 
284 	/* Enable the interrupt line. */
285 	frodo_imask(sc, (1 << line), 0);
286 	return;
287  lose:
288 	panic("frodo_intr_establish");
289 }
290 
291 void
292 frodo_intr_disestablish(frdev, line)
293 	struct device *frdev;
294 	int line;
295 {
296 	struct frodo_softc *sc = (struct frodo_softc *)frdev;
297 	struct isr *isr = sc->sc_ih;
298 	int newpri;
299 
300 	if (sc->sc_intr[line].isr_func == NULL) {
301 		printf("%s: no handler for line %d\n",
302 		    sc->sc_dev.dv_xname, line);
303 		panic("frodo_intr_disestablish");
304 	}
305 
306 	sc->sc_intr[line].isr_func = NULL;
307 	frodo_imask(sc, 0, (1 << line));
308 
309 	/* If this was the last, unhook ourselves. */
310 	if (sc->sc_refcnt-- == 1) {
311 		intr_disestablish(isr);
312 		return;
313 	}
314 
315 	/* Lower our priority, if appropriate. */
316 	for (newpri = 0, line = 0; line < FRODO_NINTR; line++)
317 		if (sc->sc_intr[line].isr_func != NULL &&
318 		    sc->sc_intr[line].isr_priority > newpri)
319 			newpri = sc->sc_intr[line].isr_priority;
320 
321 	if (newpri != isr->isr_priority) {
322 		intr_disestablish(isr);
323 		sc->sc_ih = intr_establish(frodointr, sc, 5, newpri);
324 	}
325 }
326 
327 int
328 frodointr(arg)
329 	void *arg;
330 {
331 	struct frodo_softc *sc = arg;
332 	struct frodo_isr *fisr;
333 	int line, taken = 0;
334 
335 	/* Any interrupts pending? */
336 	if (FRODO_GETPEND(sc) == 0)
337 		return (0);
338 
339 	do {
340 		/*
341 		 * Get pending interrupt; this also clears it for us.
342 		 */
343 		line = FRODO_IPEND(sc);
344 		fisr = &sc->sc_intr[line];
345 		if (fisr->isr_func == NULL ||
346 		    (*fisr->isr_func)(fisr->isr_arg) == 0)
347 			printf("%s: spurious interrupt on line %d\n",
348 			    sc->sc_dev.dv_xname, line);
349 		if (taken++ > 100)
350 			panic("frodointr: looping!");
351 	} while (FRODO_GETPEND(sc) != 0);
352 
353 	return (1);
354 }
355 
356 void
357 frodo_imask(sc, set, clear)
358 	struct frodo_softc *sc;
359 	u_int16_t set, clear;
360 {
361 	u_int16_t imask;
362 
363 	imask = FRODO_GETMASK(sc);
364 
365 	imask |= set;
366 	imask &= ~clear;
367 
368 	FRODO_SETMASK(sc, imask);
369 }
370