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