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