xref: /netbsd-src/sys/arch/hp300/dev/frodo.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: frodo.c,v 1.18 2004/08/28 17:37:00 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.18 2004/08/28 17:37:00 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/bus.h>
81 #include <machine/cpu.h>
82 #include <machine/intr.h>
83 #include <machine/hp300spu.h>
84 
85 #include <hp300/dev/intiovar.h>
86 
87 #include <hp300/dev/frodoreg.h>
88 #include <hp300/dev/frodovar.h>
89 
90 /*
91  * Description of a Frodo interrupt handler.
92  */
93 struct frodo_interhand {
94 	int	(*ih_fn)(void *);
95 	void	*ih_arg;
96 	int	ih_priority;
97 };
98 
99 struct frodo_softc {
100 	struct device	sc_dev;		/* generic device glue */
101 	volatile u_int8_t *sc_regs;	/* register base */
102 	struct frodo_interhand sc_intr[FRODO_NINTR]; /* interrupt handlers */
103 	int		sc_ipl;
104 	void		*sc_ih;		/* out interrupt cookie */
105 	int		sc_refcnt;	/* number of interrupt refs */
106 	struct bus_space_tag sc_tag;	/* bus space tag */
107 };
108 
109 static int	frodomatch(struct device *, struct cfdata *, void *);
110 static void	frodoattach(struct device *, struct device *, void *);
111 
112 static int	frodoprint(void *, const char *);
113 static int	frodosubmatch(struct device *, struct cfdata *, void *);
114 
115 static int	frodointr(void *);
116 
117 static void	frodo_imask(struct frodo_softc *, u_int16_t, u_int16_t);
118 
119 CFATTACH_DECL(frodo, sizeof(struct frodo_softc),
120     frodomatch, frodoattach, NULL, NULL);
121 
122 static const struct frodo_device frodo_subdevs[] = {
123 	{ "dnkbd",	FRODO_APCI_OFFSET(0),	FRODO_INTR_APCI0 },
124 	{ "com",	FRODO_APCI_OFFSET(1),	FRODO_INTR_APCI1 },
125 	{ "com",	FRODO_APCI_OFFSET(2),	FRODO_INTR_APCI2 },
126 	{ "com",	FRODO_APCI_OFFSET(3),	FRODO_INTR_APCI3 },
127 	{ NULL,		0,			0 },
128 };
129 
130 static int
131 frodomatch(struct device *parent, struct cfdata *match, void *aux)
132 {
133 	struct intio_attach_args *ia = aux;
134 	static int frodo_matched = 0;
135 
136 	/* only allow one instance */
137 	if (frodo_matched)
138 		return (0);
139 
140 	if (strcmp(ia->ia_modname, "frodo") != 0)
141 		return (0);
142 
143 	if (badaddr((caddr_t)ia->ia_addr))
144 		return (0);
145 
146 	frodo_matched = 1;
147 	return (1);
148 }
149 
150 static void
151 frodoattach(struct device *parent, struct device *self, void *aux)
152 {
153 	struct frodo_softc *sc = (struct frodo_softc *)self;
154 	struct intio_attach_args *ia = aux;
155 	bus_space_tag_t bst = &sc->sc_tag;
156 	const struct frodo_device *fd;
157 	struct frodo_attach_args fa;
158 
159 	sc->sc_regs = (volatile u_int8_t *)ia->ia_addr;
160 	sc->sc_ipl = ia->ia_ipl;
161 
162 	if ((FRODO_READ(sc, FRODO_IISR) & FRODO_IISR_SERVICE) == 0)
163 		printf(": service mode enabled");
164 	printf("\n");
165 
166 	/* Initialize bus_space_tag_t for frodo */
167 	frodo_init_bus_space(bst);
168 
169 	/* Clear all of the interrupt handlers. */
170 	memset(sc->sc_intr, 0, sizeof(sc->sc_intr));
171 	sc->sc_refcnt = 0;
172 
173 	/*
174 	 * Disable all of the interrupt lines; we reenable them
175 	 * as subdevices attach.
176 	 */
177 	frodo_imask(sc, 0, 0xffff);
178 
179 	/* Clear any pending interrupts. */
180 	FRODO_WRITE(sc, FRODO_PIC_PU, 0xff);
181 	FRODO_WRITE(sc, FRODO_PIC_PL, 0xff);
182 
183 	/* Set interrupt polarities. */
184 	FRODO_WRITE(sc, FRODO_PIO_IPR, 0x10);
185 
186 	/* ...and configure for edge triggering. */
187 	FRODO_WRITE(sc, FRODO_PIO_IELR, 0xcf);
188 
189 	/*
190 	 * We defer hooking up our interrupt handler until
191 	 * a subdevice hooks up theirs.
192 	 */
193 	sc->sc_ih = NULL;
194 
195 	/* ... and attach subdevices. */
196 	for (fd = frodo_subdevs; fd->fd_name != NULL; fd++) {
197 		/*
198 		 * Skip the first serial port if we're not a 425e;
199 		 * it's mapped to the DCA at select code 9 on all
200 		 * other models.
201 		 */
202 		if (fd->fd_offset == FRODO_APCI_OFFSET(1) &&
203 		    mmuid != MMUID_425_E)
204 			continue;
205 		fa.fa_name = fd->fd_name;
206 		fa.fa_bst = bst;
207 		fa.fa_base = FRODO_BASE;
208 		fa.fa_offset = fd->fd_offset;
209 		fa.fa_line = fd->fd_line;
210 		config_found_sm(self, &fa, frodoprint, frodosubmatch);
211 	}
212 }
213 
214 static int
215 frodosubmatch(struct device *parent, struct cfdata *cf, void *aux)
216 {
217 	struct frodo_attach_args *fa = aux;
218 
219 	if (cf->frodocf_offset != FRODO_UNKNOWN_OFFSET &&
220 	    cf->frodocf_offset != fa->fa_offset)
221 		return (0);
222 
223 	return (config_match(parent, cf, aux));
224 }
225 
226 static int
227 frodoprint(void *aux, const char *pnp)
228 {
229 	struct frodo_attach_args *fa = aux;
230 
231 	if (pnp)
232 		aprint_normal("%s at %s", fa->fa_name, pnp);
233 	aprint_normal(" offset 0x%x", fa->fa_offset);
234 	return (UNCONF);
235 }
236 
237 void
238 frodo_intr_establish(struct device *frdev, int (*func)(void *), void *arg,
239     int line, int priority)
240 {
241 	struct frodo_softc *sc = (struct frodo_softc *)frdev;
242 	struct hp300_intrhand *ih = sc->sc_ih;
243 
244 	if (line < 0 || line >= FRODO_NINTR) {
245 		printf("%s: bad interrupt line %d\n",
246 		    sc->sc_dev.dv_xname, line);
247 		goto lose;
248 	}
249 	if (sc->sc_intr[line].ih_fn != NULL) {
250 		printf("%s: interrupt line %d already used\n",
251 		    sc->sc_dev.dv_xname, line);
252 		goto lose;
253 	}
254 
255 	/* Install the handler. */
256 	sc->sc_intr[line].ih_fn = func;
257 	sc->sc_intr[line].ih_arg = arg;
258 	sc->sc_intr[line].ih_priority = priority;
259 
260 	/*
261 	 * If this is the first one, establish the frodo
262 	 * interrupt handler.  If not, reestablish at a
263 	 * higher priority if necessary.
264 	 */
265 	if (ih == NULL || ih->ih_priority < priority) {
266 		if (ih != NULL)
267 			intr_disestablish(ih);
268 		sc->sc_ih = intr_establish(frodointr, sc, sc->sc_ipl, priority);
269 	}
270 
271 	sc->sc_refcnt++;
272 
273 	/* Enable the interrupt line. */
274 	frodo_imask(sc, (1 << line), 0);
275 	return;
276  lose:
277 	panic("frodo_intr_establish");
278 }
279 
280 void
281 frodo_intr_disestablish(struct device *frdev, int line)
282 {
283 	struct frodo_softc *sc = (struct frodo_softc *)frdev;
284 	struct hp300_intrhand *ih = sc->sc_ih;
285 	int newpri;
286 
287 	if (sc->sc_intr[line].ih_fn == NULL) {
288 		printf("%s: no handler for line %d\n",
289 		    sc->sc_dev.dv_xname, line);
290 		panic("frodo_intr_disestablish");
291 	}
292 
293 	sc->sc_intr[line].ih_fn = NULL;
294 	frodo_imask(sc, 0, (1 << line));
295 
296 	/* If this was the last, unhook ourselves. */
297 	if (sc->sc_refcnt-- == 1) {
298 		intr_disestablish(ih);
299 		return;
300 	}
301 
302 	/* Lower our priority, if appropriate. */
303 	for (newpri = 0, line = 0; line < FRODO_NINTR; line++)
304 		if (sc->sc_intr[line].ih_fn != NULL &&
305 		    sc->sc_intr[line].ih_priority > newpri)
306 			newpri = sc->sc_intr[line].ih_priority;
307 
308 	if (newpri != ih->ih_priority) {
309 		intr_disestablish(ih);
310 		sc->sc_ih = intr_establish(frodointr, sc, sc->sc_ipl, newpri);
311 	}
312 }
313 
314 static int
315 frodointr(void *arg)
316 {
317 	struct frodo_softc *sc = arg;
318 	struct frodo_interhand *fih;
319 	int line, taken = 0;
320 
321 	/* Any interrupts pending? */
322 	if (FRODO_GETPEND(sc) == 0)
323 		return (0);
324 
325 	do {
326 		/*
327 		 * Get pending interrupt; this also clears it for us.
328 		 */
329 		line = FRODO_IPEND(sc);
330 		fih = &sc->sc_intr[line];
331 		if (fih->ih_fn == NULL ||
332 		    (*fih->ih_fn)(fih->ih_arg) == 0)
333 			printf("%s: spurious interrupt on line %d\n",
334 			    sc->sc_dev.dv_xname, line);
335 		if (taken++ > 100)
336 			panic("frodointr: looping!");
337 	} while (FRODO_GETPEND(sc) != 0);
338 
339 	return (1);
340 }
341 
342 static void
343 frodo_imask(struct frodo_softc *sc, u_int16_t set, u_int16_t clear)
344 {
345 	u_int16_t imask;
346 
347 	imask = FRODO_GETMASK(sc);
348 
349 	imask |= set;
350 	imask &= ~clear;
351 
352 	FRODO_SETMASK(sc, imask);
353 }
354 
355 /*
356  * frodo chip specific bus_space(9) support functions.
357  */
358 static u_int8_t frodo_bus_space_read_sparse_1(bus_space_tag_t,
359     bus_space_handle_t, bus_size_t);
360 static void frodo_bus_space_write_sparse_1(bus_space_tag_t,
361     bus_space_handle_t, bus_size_t, u_int8_t);
362 
363 static void frodo_bus_space_read_multi_sparse_1(bus_space_tag_t,
364     bus_space_handle_t, bus_size_t, u_int8_t *, bus_size_t);
365 static void frodo_bus_space_write_multi_sparse_1(bus_space_tag_t,
366     bus_space_handle_t, bus_size_t, const u_int8_t *, bus_size_t);
367 
368 static void frodo_bus_space_read_region_sparse_1(bus_space_tag_t,
369     bus_space_handle_t, bus_size_t, u_int8_t *, bus_size_t);
370 static void frodo_bus_space_write_region_sparse_1(bus_space_tag_t,
371     bus_space_handle_t, bus_size_t, const u_int8_t *, bus_size_t);
372 
373 static void frodo_bus_space_set_multi_sparse_1(bus_space_tag_t,
374     bus_space_handle_t, bus_size_t, u_int8_t, bus_size_t);
375 
376 static void frodo_bus_space_set_region_sparse_1(bus_space_tag_t,
377     bus_space_handle_t, bus_size_t, u_int8_t, bus_size_t);
378 
379 /*
380  * frodo_init_bus_space():
381  *	Initialize bus_space functions in bus_space_tag_t
382  *	for frodo devices which have sparse address space.
383  */
384 void
385 frodo_init_bus_space(bus_space_tag_t bst)
386 {
387 
388 	memset(bst, 0, sizeof(struct bus_space_tag));
389 	bst->bustype = HP300_BUS_SPACE_INTIO;
390 
391 	bst->bsr1 = frodo_bus_space_read_sparse_1;
392 	bst->bsw1 = frodo_bus_space_write_sparse_1;
393 
394 	bst->bsrm1 = frodo_bus_space_read_multi_sparse_1;
395 	bst->bswm1 = frodo_bus_space_write_multi_sparse_1;
396 
397 	bst->bsrr1 = frodo_bus_space_read_region_sparse_1;
398 	bst->bswr1 = frodo_bus_space_write_region_sparse_1;
399 
400 	bst->bssm1 = frodo_bus_space_set_multi_sparse_1;
401 
402 	bst->bssr1 = frodo_bus_space_set_region_sparse_1;
403 }
404 
405 static u_int8_t
406 frodo_bus_space_read_sparse_1(bus_space_tag_t bst, bus_space_handle_t bsh,
407     bus_size_t offset)
408 {
409 
410 	return *(volatile u_int8_t *)(bsh + (offset << 2));
411 }
412 
413 static void
414 frodo_bus_space_write_sparse_1(bus_space_tag_t bst, bus_space_handle_t bsh,
415     bus_size_t offset, u_int8_t val)
416 {
417 
418 	*(volatile u_int8_t *)(bsh + (offset << 2)) = val;
419 }
420 
421 static void
422 frodo_bus_space_read_multi_sparse_1(bus_space_tag_t bst, bus_space_handle_t bsh,
423     bus_size_t offset, u_int8_t *addr, bus_size_t len)
424 {
425 
426 	__asm __volatile (
427 	"	movl	%0,%%a0		;\n"
428 	"	movl	%1,%%a1		;\n"
429 	"	movl	%2,%%d0		;\n"
430 	"1:	movb	%%a0@,%%a1@+	;\n"
431 	"	subql	#1,%%d0		;\n"
432 	"	jne	1b"
433 	    :
434 	    : "r" (bsh + (offset << 2)), "g" (addr), "g" (len)
435 	    : "%a0","%a1","%d0");
436 }
437 
438 static void
439 frodo_bus_space_write_multi_sparse_1(bus_space_tag_t bst,
440     bus_space_handle_t bsh, bus_size_t offset, const u_int8_t *addr,
441     bus_size_t len)
442 {
443 
444 	__asm __volatile (
445 	"	movl	%0,%%a0		;\n"
446 	"	movl	%1,%%a1		;\n"
447 	"	movl	%2,%%d0		;\n"
448 	"1:	movb	%%a1@+,%%a0@	;\n"
449 	"	subql	#1,%%d0		;\n"
450 	"	jne	1b"
451 	    :
452 	    : "r" (bsh + (offset << 2)), "g" (addr), "g" (len)
453 	    : "%a0","%a1","%d0");
454 }
455 
456 static void
457 frodo_bus_space_read_region_sparse_1(bus_space_tag_t bst,
458     bus_space_handle_t bsh, bus_size_t offset, u_int8_t *addr, bus_size_t len)
459 {
460 	__asm __volatile (
461 	"	movl	%0,%%a0		;\n"
462 	"	movl	%1,%%a1		;\n"
463 	"	movl	%2,%%d0		;\n"
464 	"1:	movb	%%a0@,%%a1@+	;\n"
465 	"	addql	#4,%%a0		;\n"
466 	"	subql	#1,%%d0		;\n"
467 	"	jne	1b"
468 	    :
469 	    : "r" (bsh + (offset << 2)), "g" (addr), "g" (len)
470 	    : "%a0","%a1","%d0");
471 }
472 
473 static void
474 frodo_bus_space_write_region_sparse_1(bus_space_tag_t bst,
475     bus_space_handle_t bsh, bus_size_t offset, const u_int8_t *addr,
476     bus_size_t len)
477 {
478 
479 	__asm __volatile (
480 	"	movl	%0,%%a0		;\n"
481 	"	movl	%1,%%a1		;\n"
482 	"	movl	%2,%%d0		;\n"
483 	"1:	movb	%%a1@+,%%a0@	;\n"
484 	"	addql	#4,%%a0		;\n"
485 	"	subql	#1,%%d0		;\n"
486 	"	jne	1b"
487 	    :
488 	    : "r" (bsh + (offset << 2)), "g" (addr), "g" (len)
489 	    : "%a0","%a1","%d0");
490 }
491 
492 static void
493 frodo_bus_space_set_multi_sparse_1(bus_space_tag_t bst, bus_space_handle_t bsh,
494     bus_size_t offset, u_int8_t val, bus_size_t count)
495 {
496 	__asm __volatile (
497 	"	movl	%0,%%a0		;\n"
498 	"	movl	%1,%%d1		;\n"
499 	"	movl	%2,%%d0		;\n"
500 	"1:	movb	%%d1,%%a0@	;\n"
501 	"	subql	#1,%%d0		;\n"
502 	"	jne	1b"
503 	    :
504 	    : "r" (bsh + (offset << 2)), "g" (val), "g" (count)
505 	    : "%a0","%d0","%d1");
506 }
507 
508 static void
509 frodo_bus_space_set_region_sparse_1(bus_space_tag_t bst, bus_space_handle_t bsh,
510     bus_size_t offset, u_int8_t val, bus_size_t count)
511 {
512 
513 	__asm __volatile (
514 	"	movl	%0,%%a0		;\n"
515 	"	movl	%1,%%d1		;\n"
516 	"	movl	%2,%%d0		;\n"
517 	"1:	movb	%%d1,%%a0@	;\n"
518 	"	addql	#4,%%a0		;\n"
519 	"	subql	#1,%%d0		;\n"
520 	"	jne	1b"
521 	    :
522 	    : "r" (bsh + (offset << 2)), "g" (val), "g" (count)
523 	    : "%a0","%d0","%d1");
524 }
525