1 /* $NetBSD: frodo.c,v 1.2 1997/10/04 09:59:51 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 1997 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 #define _HP300_INTR_H_PRIVATE 70 71 #include <sys/param.h> 72 #include <sys/systm.h> 73 #include <sys/kernel.h> 74 #include <sys/syslog.h> 75 #include <sys/device.h> 76 77 #include <machine/cpu.h> 78 #include <machine/intr.h> 79 #include <machine/hp300spu.h> 80 81 #include <hp300/dev/intiovar.h> 82 83 #include <hp300/dev/frodoreg.h> 84 #include <hp300/dev/frodovar.h> 85 86 /* 87 * Description of a Frodo interrupt handler. 88 */ 89 struct frodo_isr { 90 int (*isr_func) __P((void *)); 91 void *isr_arg; 92 int isr_priority; 93 }; 94 95 struct frodo_softc { 96 struct device sc_dev; /* generic device glue */ 97 volatile u_int8_t *sc_regs; /* register base */ 98 struct frodo_isr sc_intr[FRODO_NINTR]; /* interrupt handlers */ 99 void *sc_ih; /* out interrupt cookie */ 100 int sc_refcnt; /* number of interrupt refs */ 101 }; 102 103 int frodomatch __P((struct device *, struct cfdata *, void *)); 104 void frodoattach __P((struct device *, struct device *, void *)); 105 106 int frodoprint __P((void *, const char *)); 107 int frodosubmatch __P((struct device *, struct cfdata *, void *)); 108 109 int frodointr __P((void *)); 110 111 void frodo_imask __P((struct frodo_softc *, u_int16_t, u_int16_t)); 112 113 struct cfattach frodo_ca = { 114 sizeof(struct frodo_softc), frodomatch, frodoattach 115 }; 116 117 struct cfdriver frodo_cd = { 118 NULL, "frodo", DV_DULL 119 }; 120 121 struct frodo_attach_args frodo_subdevs[] = { 122 { "dnkbd", FRODO_APCI_OFFSET(0), FRODO_INTR_APCI0 }, 123 { "apci", FRODO_APCI_OFFSET(1), FRODO_INTR_APCI1 }, 124 { "apci", FRODO_APCI_OFFSET(2), FRODO_INTR_APCI2 }, 125 { "apci", FRODO_APCI_OFFSET(3), FRODO_INTR_APCI3 }, 126 { NULL, 0, 0 }, 127 }; 128 129 int 130 frodomatch(parent, match, aux) 131 struct device *parent; 132 struct cfdata *match; 133 void *aux; 134 { 135 struct intio_attach_args *ia = aux; 136 caddr_t va; 137 static int frodo_matched = 0; 138 139 /* only allow one instance */ 140 if (frodo_matched) 141 return (0); 142 143 /* only 4xx workstations can have this */ 144 switch (machineid) { 145 case HP_400: 146 case HP_425: 147 case HP_433: 148 break; 149 150 default: 151 return (0); 152 } 153 154 /* make sure the hardware is there in any case */ 155 va = (caddr_t)IIOV(FRODO_BASE); 156 if (badaddr(va)) 157 return (0); 158 159 frodo_matched = 1; 160 ia->ia_addr = va; 161 return (1); 162 } 163 164 void 165 frodoattach(parent, self, aux) 166 struct device *parent, *self; 167 void *aux; 168 { 169 struct frodo_softc *sc = (struct frodo_softc *)self; 170 struct intio_attach_args *ia = aux; 171 int i; 172 173 sc->sc_regs = (volatile u_int8_t *)ia->ia_addr; 174 175 if ((FRODO_READ(sc, FRODO_IISR) & FRODO_IISR_SERVICE) == 0) 176 printf(": service mode enabled"); 177 printf("\n"); 178 179 /* Clear all of the interrupt handlers. */ 180 bzero(sc->sc_intr, sizeof(sc->sc_intr)); 181 sc->sc_refcnt = 0; 182 183 /* 184 * Disable all of the interrupt lines; we reenable them 185 * as subdevices attach. 186 */ 187 frodo_imask(sc, 0, 0xffff); 188 189 /* Clear any pending interrupts. */ 190 FRODO_WRITE(sc, FRODO_PIC_PU, 0xff); 191 FRODO_WRITE(sc, FRODO_PIC_PL, 0xff); 192 193 /* Set interrupt polarities. */ 194 FRODO_WRITE(sc, FRODO_PIO_IPR, 0x10); 195 196 /* ...and configure for edge triggering. */ 197 FRODO_WRITE(sc, FRODO_PIO_IELR, 0xcf); 198 199 /* 200 * We defer hooking up our interrupt handler until 201 * a subdevice hooks up theirs. 202 */ 203 sc->sc_ih = NULL; 204 205 /* ... and attach subdevices. */ 206 for (i = 0; frodo_subdevs[i].fa_name != NULL; i++) 207 config_found_sm(self, &frodo_subdevs[i], 208 frodoprint, frodosubmatch); 209 } 210 211 int 212 frodosubmatch(parent, cf, aux) 213 struct device *parent; 214 struct cfdata *cf; 215 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 ((*cf->cf_attach->ca_match)(parent, cf, aux)); 224 } 225 226 int 227 frodoprint(aux, pnp) 228 void *aux; 229 const char *pnp; 230 { 231 struct frodo_attach_args *fa = aux; 232 233 if (pnp) 234 printf("%s at %s", fa->fa_name, pnp); 235 printf(" offset 0x%x", fa->fa_offset); 236 return (UNCONF); 237 } 238 239 void 240 frodo_intr_establish(frdev, func, arg, line, priority) 241 struct device *frdev; 242 int (*func) __P((void *)); 243 void *arg; 244 int line; 245 int priority; 246 { 247 struct frodo_softc *sc = (struct frodo_softc *)frdev; 248 struct isr *isr = sc->sc_ih; 249 250 if (line < 0 || line >= FRODO_NINTR) { 251 printf("%s: bad interrupt line %d\n", 252 sc->sc_dev.dv_xname, line); 253 goto lose; 254 } 255 if (sc->sc_intr[line].isr_func != NULL) { 256 printf("%s: interrupt line %d already used\n", 257 sc->sc_dev.dv_xname, line); 258 goto lose; 259 } 260 261 /* Install the handler. */ 262 sc->sc_intr[line].isr_func = func; 263 sc->sc_intr[line].isr_arg = arg; 264 sc->sc_intr[line].isr_priority = priority; 265 266 /* 267 * If this is the first one, establish the frodo 268 * interrupt handler. If not, reestablish at a 269 * higher priority if necessary. 270 */ 271 if (isr == NULL || isr->isr_priority < priority) { 272 if (isr != NULL) 273 intr_disestablish(isr); 274 sc->sc_ih = intr_establish(frodointr, sc, 5, priority); 275 } 276 277 sc->sc_refcnt++; 278 279 /* Enable the interrupt line. */ 280 frodo_imask(sc, (1 << line), 0); 281 return; 282 lose: 283 panic("frodo_intr_establish"); 284 } 285 286 void 287 frodo_intr_disestablish(frdev, line) 288 struct device *frdev; 289 int line; 290 { 291 struct frodo_softc *sc = (struct frodo_softc *)frdev; 292 struct isr *isr = sc->sc_ih; 293 int newpri; 294 295 if (sc->sc_intr[line].isr_func == NULL) { 296 printf("%s: no handler for line %d\n", 297 sc->sc_dev.dv_xname, line); 298 panic("frodo_intr_disestablish"); 299 } 300 301 sc->sc_intr[line].isr_func = NULL; 302 frodo_imask(sc, 0, (1 << line)); 303 304 /* If this was the last, unhook ourselves. */ 305 if (sc->sc_refcnt-- == 1) { 306 intr_disestablish(isr); 307 return; 308 } 309 310 /* Lower our priority, if appropriate. */ 311 for (newpri = 0, line = 0; line < FRODO_NINTR; line++) 312 if (sc->sc_intr[line].isr_func != NULL && 313 sc->sc_intr[line].isr_priority > newpri) 314 newpri = sc->sc_intr[line].isr_priority; 315 316 if (newpri != isr->isr_priority) { 317 intr_disestablish(isr); 318 sc->sc_ih = intr_establish(frodointr, sc, 5, newpri); 319 } 320 } 321 322 int 323 frodointr(arg) 324 void *arg; 325 { 326 struct frodo_softc *sc = arg; 327 struct frodo_isr *fisr; 328 int line, taken = 0; 329 330 /* Any interrupts pending? */ 331 if (FRODO_GETPEND(sc) == 0) 332 return (0); 333 334 do { 335 /* 336 * Get pending interrupt; this also clears it for us. 337 */ 338 line = FRODO_IPEND(sc); 339 fisr = &sc->sc_intr[line]; 340 if (fisr->isr_func == NULL || 341 (*fisr->isr_func)(fisr->isr_arg) == 0) 342 printf("%s: spurious interrupt on line %d\n", 343 sc->sc_dev.dv_xname, line); 344 if (taken++ > 100) 345 panic("frodointr: looping!"); 346 } while (FRODO_GETPEND(sc) != 0); 347 348 return (1); 349 } 350 351 void 352 frodo_imask(sc, set, clear) 353 struct frodo_softc *sc; 354 u_int16_t set, clear; 355 { 356 u_int16_t imask; 357 358 imask = FRODO_GETMASK(sc); 359 360 imask |= set; 361 imask &= ~clear; 362 363 FRODO_SETMASK(sc, imask); 364 } 365