1 /* $NetBSD: frodo.c,v 1.33 2021/04/24 23:36:37 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 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright (c) 1997 Michael Smith. All rights reserved. 34 * 35 * Redistribution and use in source and binary forms, with or without 36 * modification, are permitted provided that the following conditions 37 * are met: 38 * 1. Redistributions of source code must retain the above copyright 39 * notice, this list of conditions and the following disclaimer. 40 * 2. Redistributions in binary form must reproduce the above copyright 41 * notice, this list of conditions and the following disclaimer in the 42 * documentation and/or other materials provided with the distribution. 43 * 44 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 47 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 54 * SUCH DAMAGE. 55 */ 56 57 /* 58 * Support for the "Frodo" (a.k.a. "Apollo Utility") chip found 59 * in HP Apollo 9000/4xx workstations, as well as 9000/382 controllers. 60 */ 61 62 #include <sys/cdefs.h> 63 __KERNEL_RCSID(0, "$NetBSD: frodo.c,v 1.33 2021/04/24 23:36:37 thorpej Exp $"); 64 65 #define _HP300_INTR_H_PRIVATE 66 67 #include <sys/param.h> 68 #include <sys/systm.h> 69 #include <sys/kernel.h> 70 #include <sys/syslog.h> 71 #include <sys/device.h> 72 73 #include <machine/bus.h> 74 #include <machine/cpu.h> 75 #include <machine/intr.h> 76 #include <machine/hp300spu.h> 77 78 #include <hp300/dev/intiovar.h> 79 80 #include <hp300/dev/frodoreg.h> 81 #include <hp300/dev/frodovar.h> 82 83 /* 84 * Description of a Frodo interrupt handler. 85 */ 86 struct frodo_interhand { 87 int (*ih_fn)(void *); 88 void *ih_arg; 89 int ih_priority; 90 }; 91 92 struct frodo_softc { 93 device_t sc_dev; /* generic device glue */ 94 volatile uint8_t *sc_regs; /* register base */ 95 struct frodo_interhand sc_intr[FRODO_NINTR]; /* interrupt handlers */ 96 int sc_ipl; 97 void *sc_ih; /* out interrupt cookie */ 98 int sc_refcnt; /* number of interrupt refs */ 99 struct bus_space_tag sc_tag; /* bus space tag */ 100 }; 101 102 static int frodomatch(device_t, cfdata_t, void *); 103 static void frodoattach(device_t, device_t, void *); 104 105 static int frodoprint(void *, const char *); 106 static int frodosubmatch(device_t, cfdata_t, const int *, void *); 107 108 static int frodointr(void *); 109 110 static void frodo_imask(struct frodo_softc *, uint16_t, uint16_t); 111 112 CFATTACH_DECL_NEW(frodo, sizeof(struct frodo_softc), 113 frodomatch, frodoattach, NULL, NULL); 114 115 static const struct frodo_device frodo_subdevs[] = { 116 { "dnkbd", FRODO_APCI_OFFSET(0), FRODO_INTR_APCI0 }, 117 { "com", FRODO_APCI_OFFSET(1), FRODO_INTR_APCI1 }, 118 { "com", FRODO_APCI_OFFSET(2), FRODO_INTR_APCI2 }, 119 { "com", FRODO_APCI_OFFSET(3), FRODO_INTR_APCI3 }, 120 { "mcclock", FRODO_CALENDAR, FRODO_INTR_CALENDAR }, 121 { NULL, 0, 0 } 122 }; 123 124 static int 125 frodomatch(device_t parent, cfdata_t cf, void *aux) 126 { 127 struct intio_attach_args *ia = aux; 128 static int frodo_matched = 0; 129 130 /* only allow one instance */ 131 if (frodo_matched) 132 return 0; 133 134 if (strcmp(ia->ia_modname, "frodo") != 0) 135 return 0; 136 137 if (badaddr((void *)ia->ia_addr)) 138 return 0; 139 140 frodo_matched = 1; 141 return 1; 142 } 143 144 static void 145 frodoattach(device_t parent, device_t self, void *aux) 146 { 147 struct frodo_softc *sc = device_private(self); 148 struct intio_attach_args *ia = aux; 149 bus_space_tag_t bst = &sc->sc_tag; 150 const struct frodo_device *fd; 151 struct frodo_attach_args fa; 152 153 sc->sc_dev = self; 154 sc->sc_regs = (volatile uint8_t *)ia->ia_addr; 155 sc->sc_ipl = ia->ia_ipl; 156 157 if ((FRODO_READ(sc, FRODO_IISR) & FRODO_IISR_SERVICE) == 0) 158 aprint_error(": service mode enabled"); 159 aprint_normal("\n"); 160 161 /* Initialize bus_space_tag_t for frodo */ 162 frodo_init_bus_space(bst); 163 164 /* Clear all of the interrupt handlers. */ 165 memset(sc->sc_intr, 0, sizeof(sc->sc_intr)); 166 sc->sc_refcnt = 0; 167 168 /* 169 * Disable all of the interrupt lines; we reenable them 170 * as subdevices attach. 171 */ 172 frodo_imask(sc, 0, 0xffff); 173 174 /* Clear any pending interrupts. */ 175 FRODO_WRITE(sc, FRODO_PIC_PU, 0xff); 176 FRODO_WRITE(sc, FRODO_PIC_PL, 0xff); 177 178 /* Set interrupt polarities. */ 179 FRODO_WRITE(sc, FRODO_PIO_IPR, 0x10); 180 181 /* ...and configure for edge triggering. */ 182 FRODO_WRITE(sc, FRODO_PIO_IELR, 0xcf); 183 184 /* 185 * We defer hooking up our interrupt handler until 186 * a subdevice hooks up theirs. 187 */ 188 sc->sc_ih = NULL; 189 190 /* ... and attach subdevices. */ 191 for (fd = frodo_subdevs; fd->fd_name != NULL; fd++) { 192 /* 193 * Skip the first serial port if we're not a 425e; 194 * it's mapped to the DCA at select code 9 on all 195 * other models. 196 */ 197 if (fd->fd_offset == FRODO_APCI_OFFSET(1) && 198 mmuid != MMUID_425_E) 199 continue; 200 /* 201 * The mcclock is available only on a 425e. 202 */ 203 if (fd->fd_offset == FRODO_CALENDAR && mmuid != MMUID_425_E) 204 continue; 205 fa.fa_name = fd->fd_name; 206 fa.fa_bst = bst; 207 fa.fa_base = ia->ia_iobase; 208 fa.fa_offset = fd->fd_offset; 209 fa.fa_line = fd->fd_line; 210 config_found(self, &fa, frodoprint, 211 CFARG_SUBMATCH, frodosubmatch, 212 CFARG_EOL); 213 } 214 } 215 216 static int 217 frodosubmatch(device_t parent, cfdata_t cf, 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(device_t frdev, int (*func)(void *), void *arg, 241 int line, int priority) 242 { 243 struct frodo_softc *sc = device_private(frdev); 244 struct hp300_intrhand *ih = sc->sc_ih; 245 246 if (line < 0 || line >= FRODO_NINTR) { 247 aprint_error_dev(frdev, "bad interrupt line %d\n", line); 248 goto lose; 249 } 250 if (sc->sc_intr[line].ih_fn != NULL) { 251 aprint_error_dev(frdev, "interrupt line %d already used\n", 252 line); 253 goto lose; 254 } 255 256 /* Install the handler. */ 257 sc->sc_intr[line].ih_fn = func; 258 sc->sc_intr[line].ih_arg = arg; 259 sc->sc_intr[line].ih_priority = priority; 260 261 /* 262 * If this is the first one, establish the frodo 263 * interrupt handler. If not, reestablish at a 264 * higher priority if necessary. 265 */ 266 if (ih == NULL || ih->ih_priority < priority) { 267 if (ih != NULL) 268 intr_disestablish(ih); 269 sc->sc_ih = intr_establish(frodointr, sc, sc->sc_ipl, priority); 270 } 271 272 sc->sc_refcnt++; 273 274 /* Enable the interrupt line. */ 275 frodo_imask(sc, (1 << line), 0); 276 return; 277 lose: 278 panic("frodo_intr_establish"); 279 } 280 281 void 282 frodo_intr_disestablish(device_t frdev, int line) 283 { 284 struct frodo_softc *sc = device_private(frdev); 285 struct hp300_intrhand *ih = sc->sc_ih; 286 int newpri; 287 288 if (sc->sc_intr[line].ih_fn == NULL) { 289 printf("%s: no handler for line %d\n", 290 device_xname(sc->sc_dev), line); 291 panic("frodo_intr_disestablish"); 292 } 293 294 sc->sc_intr[line].ih_fn = NULL; 295 frodo_imask(sc, 0, (1 << line)); 296 297 /* If this was the last, unhook ourselves. */ 298 if (sc->sc_refcnt-- == 1) { 299 intr_disestablish(ih); 300 return; 301 } 302 303 /* Lower our priority, if appropriate. */ 304 for (newpri = 0, line = 0; line < FRODO_NINTR; line++) 305 if (sc->sc_intr[line].ih_fn != NULL && 306 sc->sc_intr[line].ih_priority > newpri) 307 newpri = sc->sc_intr[line].ih_priority; 308 309 if (newpri != ih->ih_priority) { 310 intr_disestablish(ih); 311 sc->sc_ih = intr_establish(frodointr, sc, sc->sc_ipl, newpri); 312 } 313 } 314 315 static int 316 frodointr(void *arg) 317 { 318 struct frodo_softc *sc = arg; 319 struct frodo_interhand *fih; 320 int line, taken = 0; 321 322 /* Any interrupts pending? */ 323 if (FRODO_GETPEND(sc) == 0) 324 return 0; 325 326 do { 327 /* 328 * Get pending interrupt; this also clears it for us. 329 */ 330 line = FRODO_IPEND(sc); 331 fih = &sc->sc_intr[line]; 332 if (fih->ih_fn == NULL || 333 (*fih->ih_fn)(fih->ih_arg) == 0) 334 printf("%s: spurious interrupt on line %d\n", 335 device_xname(sc->sc_dev), line); 336 if (taken++ > 100) 337 panic("frodointr: looping!"); 338 } while (FRODO_GETPEND(sc) != 0); 339 340 return 1; 341 } 342 343 static void 344 frodo_imask(struct frodo_softc *sc, uint16_t set, uint16_t clear) 345 { 346 uint16_t imask; 347 348 imask = FRODO_GETMASK(sc); 349 350 imask |= set; 351 imask &= ~clear; 352 353 FRODO_SETMASK(sc, imask); 354 } 355 356 /* 357 * frodo chip specific bus_space(9) support functions. 358 */ 359 static uint8_t frodo_bus_space_read_sparse_1(bus_space_tag_t, 360 bus_space_handle_t, bus_size_t); 361 static void frodo_bus_space_write_sparse_1(bus_space_tag_t, 362 bus_space_handle_t, bus_size_t, uint8_t); 363 364 static void frodo_bus_space_read_multi_sparse_1(bus_space_tag_t, 365 bus_space_handle_t, bus_size_t, uint8_t *, bus_size_t); 366 static void frodo_bus_space_write_multi_sparse_1(bus_space_tag_t, 367 bus_space_handle_t, bus_size_t, const uint8_t *, bus_size_t); 368 369 static void frodo_bus_space_read_region_sparse_1(bus_space_tag_t, 370 bus_space_handle_t, bus_size_t, uint8_t *, bus_size_t); 371 static void frodo_bus_space_write_region_sparse_1(bus_space_tag_t, 372 bus_space_handle_t, bus_size_t, const uint8_t *, bus_size_t); 373 374 static void frodo_bus_space_set_multi_sparse_1(bus_space_tag_t, 375 bus_space_handle_t, bus_size_t, uint8_t, bus_size_t); 376 377 static void frodo_bus_space_set_region_sparse_1(bus_space_tag_t, 378 bus_space_handle_t, bus_size_t, uint8_t, bus_size_t); 379 380 /* 381 * frodo_init_bus_space(): 382 * Initialize bus_space functions in bus_space_tag_t 383 * for frodo devices which have sparse address space. 384 */ 385 void 386 frodo_init_bus_space(bus_space_tag_t bst) 387 { 388 389 memset(bst, 0, sizeof(struct bus_space_tag)); 390 bst->bustype = HP300_BUS_SPACE_INTIO; 391 392 bst->bsr1 = frodo_bus_space_read_sparse_1; 393 bst->bsw1 = frodo_bus_space_write_sparse_1; 394 395 bst->bsrm1 = frodo_bus_space_read_multi_sparse_1; 396 bst->bswm1 = frodo_bus_space_write_multi_sparse_1; 397 398 bst->bsrr1 = frodo_bus_space_read_region_sparse_1; 399 bst->bswr1 = frodo_bus_space_write_region_sparse_1; 400 401 bst->bssm1 = frodo_bus_space_set_multi_sparse_1; 402 403 bst->bssr1 = frodo_bus_space_set_region_sparse_1; 404 } 405 406 static uint8_t 407 frodo_bus_space_read_sparse_1(bus_space_tag_t bst, bus_space_handle_t bsh, 408 bus_size_t offset) 409 { 410 411 return *(volatile uint8_t *)(bsh + (offset << 2)); 412 } 413 414 static void 415 frodo_bus_space_write_sparse_1(bus_space_tag_t bst, bus_space_handle_t bsh, 416 bus_size_t offset, uint8_t val) 417 { 418 419 *(volatile uint8_t *)(bsh + (offset << 2)) = val; 420 } 421 422 static void 423 frodo_bus_space_read_multi_sparse_1(bus_space_tag_t bst, bus_space_handle_t bsh, 424 bus_size_t offset, uint8_t *addr, bus_size_t len) 425 { 426 427 __asm volatile ( 428 " movl %0,%%a0 ;\n" 429 " movl %1,%%a1 ;\n" 430 " movl %2,%%d0 ;\n" 431 "1: movb %%a0@,%%a1@+ ;\n" 432 " subql #1,%%d0 ;\n" 433 " jne 1b" 434 : 435 : "r" (bsh + (offset << 2)), "g" (addr), "g" (len) 436 : "%a0","%a1","%d0"); 437 } 438 439 static void 440 frodo_bus_space_write_multi_sparse_1(bus_space_tag_t bst, 441 bus_space_handle_t bsh, bus_size_t offset, const uint8_t *addr, 442 bus_size_t len) 443 { 444 445 __asm volatile ( 446 " movl %0,%%a0 ;\n" 447 " movl %1,%%a1 ;\n" 448 " movl %2,%%d0 ;\n" 449 "1: movb %%a1@+,%%a0@ ;\n" 450 " subql #1,%%d0 ;\n" 451 " jne 1b" 452 : 453 : "r" (bsh + (offset << 2)), "g" (addr), "g" (len) 454 : "%a0","%a1","%d0"); 455 } 456 457 static void 458 frodo_bus_space_read_region_sparse_1(bus_space_tag_t bst, 459 bus_space_handle_t bsh, bus_size_t offset, uint8_t *addr, bus_size_t len) 460 { 461 __asm volatile ( 462 " movl %0,%%a0 ;\n" 463 " movl %1,%%a1 ;\n" 464 " movl %2,%%d0 ;\n" 465 "1: movb %%a0@,%%a1@+ ;\n" 466 " addql #4,%%a0 ;\n" 467 " subql #1,%%d0 ;\n" 468 " jne 1b" 469 : 470 : "r" (bsh + (offset << 2)), "g" (addr), "g" (len) 471 : "%a0","%a1","%d0"); 472 } 473 474 static void 475 frodo_bus_space_write_region_sparse_1(bus_space_tag_t bst, 476 bus_space_handle_t bsh, bus_size_t offset, const uint8_t *addr, 477 bus_size_t len) 478 { 479 480 __asm volatile ( 481 " movl %0,%%a0 ;\n" 482 " movl %1,%%a1 ;\n" 483 " movl %2,%%d0 ;\n" 484 "1: movb %%a1@+,%%a0@ ;\n" 485 " addql #4,%%a0 ;\n" 486 " subql #1,%%d0 ;\n" 487 " jne 1b" 488 : 489 : "r" (bsh + (offset << 2)), "g" (addr), "g" (len) 490 : "%a0","%a1","%d0"); 491 } 492 493 static void 494 frodo_bus_space_set_multi_sparse_1(bus_space_tag_t bst, bus_space_handle_t bsh, 495 bus_size_t offset, uint8_t val, bus_size_t count) 496 { 497 __asm volatile ( 498 " movl %0,%%a0 ;\n" 499 " movl %1,%%d1 ;\n" 500 " movl %2,%%d0 ;\n" 501 "1: movb %%d1,%%a0@ ;\n" 502 " subql #1,%%d0 ;\n" 503 " jne 1b" 504 : 505 : "r" (bsh + (offset << 2)), "g" (val), "g" (count) 506 : "%a0","%d0","%d1"); 507 } 508 509 static void 510 frodo_bus_space_set_region_sparse_1(bus_space_tag_t bst, bus_space_handle_t bsh, 511 bus_size_t offset, uint8_t val, bus_size_t count) 512 { 513 514 __asm volatile ( 515 " movl %0,%%a0 ;\n" 516 " movl %1,%%d1 ;\n" 517 " movl %2,%%d0 ;\n" 518 "1: movb %%d1,%%a0@ ;\n" 519 " addql #4,%%a0 ;\n" 520 " subql #1,%%d0 ;\n" 521 " jne 1b" 522 : 523 : "r" (bsh + (offset << 2)), "g" (val), "g" (count) 524 : "%a0","%d0","%d1"); 525 } 526