Home | History | Annotate | Line # | Download | only in dev
frodo.c revision 1.14
      1 /*	$NetBSD: frodo.c,v 1.14 2003/01/01 01:34:45 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.14 2003/01/01 01:34:45 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/cpu.h>
     81 #include <machine/intr.h>
     82 #include <machine/hp300spu.h>
     83 
     84 #include <hp300/dev/intiovar.h>
     85 
     86 #include <hp300/dev/frodoreg.h>
     87 #include <hp300/dev/frodovar.h>
     88 
     89 /*
     90  * Description of a Frodo interrupt handler.
     91  */
     92 struct frodo_interhand {
     93 	int	(*ih_fn) __P((void *));
     94 	void	*ih_arg;
     95 	int	ih_priority;
     96 };
     97 
     98 struct frodo_softc {
     99 	struct device	sc_dev;		/* generic device glue */
    100 	volatile u_int8_t *sc_regs;	/* register base */
    101 	struct frodo_interhand sc_intr[FRODO_NINTR]; /* interrupt handlers */
    102 	int		sc_ipl;
    103 	void		*sc_ih;		/* out interrupt cookie */
    104 	int		sc_refcnt;	/* number of interrupt refs */
    105 };
    106 
    107 int	frodomatch __P((struct device *, struct cfdata *, void *));
    108 void	frodoattach __P((struct device *, struct device *, void *));
    109 
    110 int	frodoprint __P((void *, const char *));
    111 int	frodosubmatch __P((struct device *, struct cfdata *, void *));
    112 
    113 int	frodointr __P((void *));
    114 
    115 void	frodo_imask __P((struct frodo_softc *, u_int16_t, u_int16_t));
    116 
    117 CFATTACH_DECL(frodo, sizeof(struct frodo_softc),
    118     frodomatch, frodoattach, NULL, NULL);
    119 
    120 struct frodo_attach_args frodo_subdevs[] = {
    121 	{ "dnkbd",	FRODO_APCI_OFFSET(0),	FRODO_INTR_APCI0 },
    122 	{ "apci",	FRODO_APCI_OFFSET(1),	FRODO_INTR_APCI1 },
    123 	{ "apci",	FRODO_APCI_OFFSET(2),	FRODO_INTR_APCI2 },
    124 	{ "apci",	FRODO_APCI_OFFSET(3),	FRODO_INTR_APCI3 },
    125 	{ NULL,		0,			0 },
    126 };
    127 
    128 int
    129 frodomatch(parent, match, aux)
    130 	struct device *parent;
    131 	struct cfdata *match;
    132 	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((caddr_t)ia->ia_addr))
    145 		return (0);
    146 
    147 	frodo_matched = 1;
    148 	return (1);
    149 }
    150 
    151 void
    152 frodoattach(parent, self, aux)
    153 	struct device *parent, *self;
    154 	void *aux;
    155 {
    156 	struct frodo_softc *sc = (struct frodo_softc *)self;
    157 	struct intio_attach_args *ia = aux;
    158 	int i;
    159 
    160 	sc->sc_regs = (volatile u_int8_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 	/* Clear all of the interrupt handlers. */
    168 	memset(sc->sc_intr, 0, sizeof(sc->sc_intr));
    169 	sc->sc_refcnt = 0;
    170 
    171 	/*
    172 	 * Disable all of the interrupt lines; we reenable them
    173 	 * as subdevices attach.
    174 	 */
    175 	frodo_imask(sc, 0, 0xffff);
    176 
    177 	/* Clear any pending interrupts. */
    178 	FRODO_WRITE(sc, FRODO_PIC_PU, 0xff);
    179 	FRODO_WRITE(sc, FRODO_PIC_PL, 0xff);
    180 
    181 	/* Set interrupt polarities. */
    182 	FRODO_WRITE(sc, FRODO_PIO_IPR, 0x10);
    183 
    184 	/* ...and configure for edge triggering. */
    185 	FRODO_WRITE(sc, FRODO_PIO_IELR, 0xcf);
    186 
    187 	/*
    188 	 * We defer hooking up our interrupt handler until
    189 	 * a subdevice hooks up theirs.
    190 	 */
    191 	sc->sc_ih = NULL;
    192 
    193 	/* ... and attach subdevices. */
    194 	for (i = 0; frodo_subdevs[i].fa_name != NULL; i++) {
    195 		/*
    196 		 * Skip the first serial port if we're not a 425e;
    197 		 * it's mapped to the DCA at select code 9 on all
    198 		 * other models.
    199 		 */
    200 		if (frodo_subdevs[i].fa_offset == FRODO_APCI_OFFSET(1) &&
    201 		    mmuid != MMUID_425_E)
    202 			continue;
    203 		config_found_sm(self, &frodo_subdevs[i],
    204 		    frodoprint, frodosubmatch);
    205 	}
    206 }
    207 
    208 int
    209 frodosubmatch(parent, cf, aux)
    210 	struct device *parent;
    211 	struct cfdata *cf;
    212 	void *aux;
    213 {
    214 	struct frodo_attach_args *fa = aux;
    215 
    216 	if (cf->frodocf_offset != FRODO_UNKNOWN_OFFSET &&
    217 	    cf->frodocf_offset != fa->fa_offset)
    218 		return (0);
    219 
    220 	return (config_match(parent, cf, aux));
    221 }
    222 
    223 int
    224 frodoprint(aux, pnp)
    225 	void *aux;
    226 	const char *pnp;
    227 {
    228 	struct frodo_attach_args *fa = aux;
    229 
    230 	if (pnp)
    231 		aprint_normal("%s at %s", fa->fa_name, pnp);
    232 	aprint_normal(" offset 0x%x", fa->fa_offset);
    233 	return (UNCONF);
    234 }
    235 
    236 void
    237 frodo_intr_establish(frdev, func, arg, line, priority)
    238 	struct device *frdev;
    239 	int (*func) __P((void *));
    240 	void *arg;
    241 	int line;
    242 	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(frdev, line)
    285 	struct device *frdev;
    286 	int line;
    287 {
    288 	struct frodo_softc *sc = (struct frodo_softc *)frdev;
    289 	struct hp300_intrhand *ih = sc->sc_ih;
    290 	int newpri;
    291 
    292 	if (sc->sc_intr[line].ih_fn == NULL) {
    293 		printf("%s: no handler for line %d\n",
    294 		    sc->sc_dev.dv_xname, line);
    295 		panic("frodo_intr_disestablish");
    296 	}
    297 
    298 	sc->sc_intr[line].ih_fn = NULL;
    299 	frodo_imask(sc, 0, (1 << line));
    300 
    301 	/* If this was the last, unhook ourselves. */
    302 	if (sc->sc_refcnt-- == 1) {
    303 		intr_disestablish(ih);
    304 		return;
    305 	}
    306 
    307 	/* Lower our priority, if appropriate. */
    308 	for (newpri = 0, line = 0; line < FRODO_NINTR; line++)
    309 		if (sc->sc_intr[line].ih_fn != NULL &&
    310 		    sc->sc_intr[line].ih_priority > newpri)
    311 			newpri = sc->sc_intr[line].ih_priority;
    312 
    313 	if (newpri != ih->ih_priority) {
    314 		intr_disestablish(ih);
    315 		sc->sc_ih = intr_establish(frodointr, sc, sc->sc_ipl, newpri);
    316 	}
    317 }
    318 
    319 int
    320 frodointr(arg)
    321 	void *arg;
    322 {
    323 	struct frodo_softc *sc = arg;
    324 	struct frodo_interhand *fih;
    325 	int line, taken = 0;
    326 
    327 	/* Any interrupts pending? */
    328 	if (FRODO_GETPEND(sc) == 0)
    329 		return (0);
    330 
    331 	do {
    332 		/*
    333 		 * Get pending interrupt; this also clears it for us.
    334 		 */
    335 		line = FRODO_IPEND(sc);
    336 		fih = &sc->sc_intr[line];
    337 		if (fih->ih_fn == NULL ||
    338 		    (*fih->ih_fn)(fih->ih_arg) == 0)
    339 			printf("%s: spurious interrupt on line %d\n",
    340 			    sc->sc_dev.dv_xname, line);
    341 		if (taken++ > 100)
    342 			panic("frodointr: looping!");
    343 	} while (FRODO_GETPEND(sc) != 0);
    344 
    345 	return (1);
    346 }
    347 
    348 void
    349 frodo_imask(sc, set, clear)
    350 	struct frodo_softc *sc;
    351 	u_int16_t set, clear;
    352 {
    353 	u_int16_t imask;
    354 
    355 	imask = FRODO_GETMASK(sc);
    356 
    357 	imask |= set;
    358 	imask &= ~clear;
    359 
    360 	FRODO_SETMASK(sc, imask);
    361 }
    362