xref: /netbsd-src/sys/arch/hp300/dev/frodo.c (revision e5548b402ae4c44fb816de42c7bba9581ce23ef5)
1 /*	$NetBSD: frodo.c,v 1.22 2005/12/11 12:17:13 christos 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.22 2005/12/11 12:17:13 christos 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 uint8_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 *, const int *, void *);
114 
115 static int	frodointr(void *);
116 
117 static void	frodo_imask(struct frodo_softc *, uint16_t, uint16_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 uint8_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_loc(self, "frodo", NULL, &fa, frodoprint,
211 				    frodosubmatch);
212 	}
213 }
214 
215 static int
216 frodosubmatch(struct device *parent, struct cfdata *cf,
217 	      const int *ldesc, void *aux)
218 {
219 	struct frodo_attach_args *fa = aux;
220 
221 	if (cf->frodocf_offset != FRODO_UNKNOWN_OFFSET &&
222 	    cf->frodocf_offset != fa->fa_offset)
223 		return (0);
224 
225 	return (config_match(parent, cf, aux));
226 }
227 
228 static int
229 frodoprint(void *aux, const char *pnp)
230 {
231 	struct frodo_attach_args *fa = aux;
232 
233 	if (pnp)
234 		aprint_normal("%s at %s", fa->fa_name, pnp);
235 	aprint_normal(" offset 0x%x", fa->fa_offset);
236 	return (UNCONF);
237 }
238 
239 void
240 frodo_intr_establish(struct device *frdev, int (*func)(void *), void *arg,
241     int line, int priority)
242 {
243 	struct frodo_softc *sc = (struct frodo_softc *)frdev;
244 	struct hp300_intrhand *ih = sc->sc_ih;
245 
246 	if (line < 0 || line >= FRODO_NINTR) {
247 		printf("%s: bad interrupt line %d\n",
248 		    sc->sc_dev.dv_xname, line);
249 		goto lose;
250 	}
251 	if (sc->sc_intr[line].ih_fn != NULL) {
252 		printf("%s: interrupt line %d already used\n",
253 		    sc->sc_dev.dv_xname, line);
254 		goto lose;
255 	}
256 
257 	/* Install the handler. */
258 	sc->sc_intr[line].ih_fn = func;
259 	sc->sc_intr[line].ih_arg = arg;
260 	sc->sc_intr[line].ih_priority = priority;
261 
262 	/*
263 	 * If this is the first one, establish the frodo
264 	 * interrupt handler.  If not, reestablish at a
265 	 * higher priority if necessary.
266 	 */
267 	if (ih == NULL || ih->ih_priority < priority) {
268 		if (ih != NULL)
269 			intr_disestablish(ih);
270 		sc->sc_ih = intr_establish(frodointr, sc, sc->sc_ipl, priority);
271 	}
272 
273 	sc->sc_refcnt++;
274 
275 	/* Enable the interrupt line. */
276 	frodo_imask(sc, (1 << line), 0);
277 	return;
278  lose:
279 	panic("frodo_intr_establish");
280 }
281 
282 void
283 frodo_intr_disestablish(struct device *frdev, int line)
284 {
285 	struct frodo_softc *sc = (struct frodo_softc *)frdev;
286 	struct hp300_intrhand *ih = sc->sc_ih;
287 	int newpri;
288 
289 	if (sc->sc_intr[line].ih_fn == NULL) {
290 		printf("%s: no handler for line %d\n",
291 		    sc->sc_dev.dv_xname, line);
292 		panic("frodo_intr_disestablish");
293 	}
294 
295 	sc->sc_intr[line].ih_fn = NULL;
296 	frodo_imask(sc, 0, (1 << line));
297 
298 	/* If this was the last, unhook ourselves. */
299 	if (sc->sc_refcnt-- == 1) {
300 		intr_disestablish(ih);
301 		return;
302 	}
303 
304 	/* Lower our priority, if appropriate. */
305 	for (newpri = 0, line = 0; line < FRODO_NINTR; line++)
306 		if (sc->sc_intr[line].ih_fn != NULL &&
307 		    sc->sc_intr[line].ih_priority > newpri)
308 			newpri = sc->sc_intr[line].ih_priority;
309 
310 	if (newpri != ih->ih_priority) {
311 		intr_disestablish(ih);
312 		sc->sc_ih = intr_establish(frodointr, sc, sc->sc_ipl, newpri);
313 	}
314 }
315 
316 static int
317 frodointr(void *arg)
318 {
319 	struct frodo_softc *sc = arg;
320 	struct frodo_interhand *fih;
321 	int line, taken = 0;
322 
323 	/* Any interrupts pending? */
324 	if (FRODO_GETPEND(sc) == 0)
325 		return (0);
326 
327 	do {
328 		/*
329 		 * Get pending interrupt; this also clears it for us.
330 		 */
331 		line = FRODO_IPEND(sc);
332 		fih = &sc->sc_intr[line];
333 		if (fih->ih_fn == NULL ||
334 		    (*fih->ih_fn)(fih->ih_arg) == 0)
335 			printf("%s: spurious interrupt on line %d\n",
336 			    sc->sc_dev.dv_xname, line);
337 		if (taken++ > 100)
338 			panic("frodointr: looping!");
339 	} while (FRODO_GETPEND(sc) != 0);
340 
341 	return (1);
342 }
343 
344 static void
345 frodo_imask(struct frodo_softc *sc, uint16_t set, uint16_t clear)
346 {
347 	uint16_t imask;
348 
349 	imask = FRODO_GETMASK(sc);
350 
351 	imask |= set;
352 	imask &= ~clear;
353 
354 	FRODO_SETMASK(sc, imask);
355 }
356 
357 /*
358  * frodo chip specific bus_space(9) support functions.
359  */
360 static uint8_t frodo_bus_space_read_sparse_1(bus_space_tag_t,
361     bus_space_handle_t, bus_size_t);
362 static void frodo_bus_space_write_sparse_1(bus_space_tag_t,
363     bus_space_handle_t, bus_size_t, uint8_t);
364 
365 static void frodo_bus_space_read_multi_sparse_1(bus_space_tag_t,
366     bus_space_handle_t, bus_size_t, uint8_t *, bus_size_t);
367 static void frodo_bus_space_write_multi_sparse_1(bus_space_tag_t,
368     bus_space_handle_t, bus_size_t, const uint8_t *, bus_size_t);
369 
370 static void frodo_bus_space_read_region_sparse_1(bus_space_tag_t,
371     bus_space_handle_t, bus_size_t, uint8_t *, bus_size_t);
372 static void frodo_bus_space_write_region_sparse_1(bus_space_tag_t,
373     bus_space_handle_t, bus_size_t, const uint8_t *, bus_size_t);
374 
375 static void frodo_bus_space_set_multi_sparse_1(bus_space_tag_t,
376     bus_space_handle_t, bus_size_t, uint8_t, bus_size_t);
377 
378 static void frodo_bus_space_set_region_sparse_1(bus_space_tag_t,
379     bus_space_handle_t, bus_size_t, uint8_t, bus_size_t);
380 
381 /*
382  * frodo_init_bus_space():
383  *	Initialize bus_space functions in bus_space_tag_t
384  *	for frodo devices which have sparse address space.
385  */
386 void
387 frodo_init_bus_space(bus_space_tag_t bst)
388 {
389 
390 	memset(bst, 0, sizeof(struct bus_space_tag));
391 	bst->bustype = HP300_BUS_SPACE_INTIO;
392 
393 	bst->bsr1 = frodo_bus_space_read_sparse_1;
394 	bst->bsw1 = frodo_bus_space_write_sparse_1;
395 
396 	bst->bsrm1 = frodo_bus_space_read_multi_sparse_1;
397 	bst->bswm1 = frodo_bus_space_write_multi_sparse_1;
398 
399 	bst->bsrr1 = frodo_bus_space_read_region_sparse_1;
400 	bst->bswr1 = frodo_bus_space_write_region_sparse_1;
401 
402 	bst->bssm1 = frodo_bus_space_set_multi_sparse_1;
403 
404 	bst->bssr1 = frodo_bus_space_set_region_sparse_1;
405 }
406 
407 static uint8_t
408 frodo_bus_space_read_sparse_1(bus_space_tag_t bst, bus_space_handle_t bsh,
409     bus_size_t offset)
410 {
411 
412 	return *(volatile uint8_t *)(bsh + (offset << 2));
413 }
414 
415 static void
416 frodo_bus_space_write_sparse_1(bus_space_tag_t bst, bus_space_handle_t bsh,
417     bus_size_t offset, uint8_t val)
418 {
419 
420 	*(volatile uint8_t *)(bsh + (offset << 2)) = val;
421 }
422 
423 static void
424 frodo_bus_space_read_multi_sparse_1(bus_space_tag_t bst, bus_space_handle_t bsh,
425     bus_size_t offset, uint8_t *addr, bus_size_t len)
426 {
427 
428 	__asm __volatile (
429 	"	movl	%0,%%a0		;\n"
430 	"	movl	%1,%%a1		;\n"
431 	"	movl	%2,%%d0		;\n"
432 	"1:	movb	%%a0@,%%a1@+	;\n"
433 	"	subql	#1,%%d0		;\n"
434 	"	jne	1b"
435 	    :
436 	    : "r" (bsh + (offset << 2)), "g" (addr), "g" (len)
437 	    : "%a0","%a1","%d0");
438 }
439 
440 static void
441 frodo_bus_space_write_multi_sparse_1(bus_space_tag_t bst,
442     bus_space_handle_t bsh, bus_size_t offset, const uint8_t *addr,
443     bus_size_t len)
444 {
445 
446 	__asm __volatile (
447 	"	movl	%0,%%a0		;\n"
448 	"	movl	%1,%%a1		;\n"
449 	"	movl	%2,%%d0		;\n"
450 	"1:	movb	%%a1@+,%%a0@	;\n"
451 	"	subql	#1,%%d0		;\n"
452 	"	jne	1b"
453 	    :
454 	    : "r" (bsh + (offset << 2)), "g" (addr), "g" (len)
455 	    : "%a0","%a1","%d0");
456 }
457 
458 static void
459 frodo_bus_space_read_region_sparse_1(bus_space_tag_t bst,
460     bus_space_handle_t bsh, bus_size_t offset, uint8_t *addr, bus_size_t len)
461 {
462 	__asm __volatile (
463 	"	movl	%0,%%a0		;\n"
464 	"	movl	%1,%%a1		;\n"
465 	"	movl	%2,%%d0		;\n"
466 	"1:	movb	%%a0@,%%a1@+	;\n"
467 	"	addql	#4,%%a0		;\n"
468 	"	subql	#1,%%d0		;\n"
469 	"	jne	1b"
470 	    :
471 	    : "r" (bsh + (offset << 2)), "g" (addr), "g" (len)
472 	    : "%a0","%a1","%d0");
473 }
474 
475 static void
476 frodo_bus_space_write_region_sparse_1(bus_space_tag_t bst,
477     bus_space_handle_t bsh, bus_size_t offset, const uint8_t *addr,
478     bus_size_t len)
479 {
480 
481 	__asm __volatile (
482 	"	movl	%0,%%a0		;\n"
483 	"	movl	%1,%%a1		;\n"
484 	"	movl	%2,%%d0		;\n"
485 	"1:	movb	%%a1@+,%%a0@	;\n"
486 	"	addql	#4,%%a0		;\n"
487 	"	subql	#1,%%d0		;\n"
488 	"	jne	1b"
489 	    :
490 	    : "r" (bsh + (offset << 2)), "g" (addr), "g" (len)
491 	    : "%a0","%a1","%d0");
492 }
493 
494 static void
495 frodo_bus_space_set_multi_sparse_1(bus_space_tag_t bst, bus_space_handle_t bsh,
496     bus_size_t offset, uint8_t val, bus_size_t count)
497 {
498 	__asm __volatile (
499 	"	movl	%0,%%a0		;\n"
500 	"	movl	%1,%%d1		;\n"
501 	"	movl	%2,%%d0		;\n"
502 	"1:	movb	%%d1,%%a0@	;\n"
503 	"	subql	#1,%%d0		;\n"
504 	"	jne	1b"
505 	    :
506 	    : "r" (bsh + (offset << 2)), "g" (val), "g" (count)
507 	    : "%a0","%d0","%d1");
508 }
509 
510 static void
511 frodo_bus_space_set_region_sparse_1(bus_space_tag_t bst, bus_space_handle_t bsh,
512     bus_size_t offset, uint8_t val, bus_size_t count)
513 {
514 
515 	__asm __volatile (
516 	"	movl	%0,%%a0		;\n"
517 	"	movl	%1,%%d1		;\n"
518 	"	movl	%2,%%d0		;\n"
519 	"1:	movb	%%d1,%%a0@	;\n"
520 	"	addql	#4,%%a0		;\n"
521 	"	subql	#1,%%d0		;\n"
522 	"	jne	1b"
523 	    :
524 	    : "r" (bsh + (offset << 2)), "g" (val), "g" (count)
525 	    : "%a0","%d0","%d1");
526 }
527