1 /* $NetBSD: sbus.c,v 1.38 2000/07/09 20:57:47 pk Exp $ */ 2 3 /*- 4 * Copyright (c) 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Paul Kranenburg. 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) 1992, 1993 41 * The Regents of the University of California. All rights reserved. 42 * 43 * This software was developed by the Computer Systems Engineering group 44 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 45 * contributed to Berkeley. 46 * 47 * All advertising materials mentioning features or use of this software 48 * must display the following acknowledgement: 49 * This product includes software developed by the University of 50 * California, Lawrence Berkeley Laboratory. 51 * 52 * Redistribution and use in source and binary forms, with or without 53 * modification, are permitted provided that the following conditions 54 * are met: 55 * 1. Redistributions of source code must retain the above copyright 56 * notice, this list of conditions and the following disclaimer. 57 * 2. Redistributions in binary form must reproduce the above copyright 58 * notice, this list of conditions and the following disclaimer in the 59 * documentation and/or other materials provided with the distribution. 60 * 3. All advertising materials mentioning features or use of this software 61 * must display the following acknowledgement: 62 * This product includes software developed by the University of 63 * California, Berkeley and its contributors. 64 * 4. Neither the name of the University nor the names of its contributors 65 * may be used to endorse or promote products derived from this software 66 * without specific prior written permission. 67 * 68 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 69 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 70 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 71 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 72 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 73 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 74 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 75 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 76 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 77 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 78 * SUCH DAMAGE. 79 * 80 * @(#)sbus.c 8.1 (Berkeley) 6/11/93 81 */ 82 83 /* 84 * Sbus stuff. 85 */ 86 87 #include <sys/param.h> 88 #include <sys/malloc.h> 89 #include <sys/kernel.h> 90 #include <sys/systm.h> 91 #include <sys/device.h> 92 93 #include <uvm/uvm_extern.h> 94 95 #include <machine/bus.h> 96 #include <sparc/dev/sbusreg.h> 97 #include <dev/sbus/sbusvar.h> 98 #include <dev/sbus/xboxvar.h> 99 100 #include <sparc/sparc/iommuvar.h> 101 #include <machine/autoconf.h> 102 103 104 void sbusreset __P((int)); 105 106 static bus_space_tag_t sbus_alloc_bustag __P((struct sbus_softc *)); 107 static int sbus_get_intr __P((struct sbus_softc *, int, 108 struct sbus_intr **, int *)); 109 static int sbus_bus_mmap __P((bus_space_tag_t, bus_type_t, bus_addr_t, 110 int, bus_space_handle_t *)); 111 static int _sbus_bus_map __P(( 112 bus_space_tag_t, 113 bus_type_t, /*slot*/ 114 bus_addr_t, /*offset*/ 115 bus_size_t, /*size*/ 116 int, /*flags*/ 117 vaddr_t, /*preferred virtual address */ 118 bus_space_handle_t *)); 119 static void *sbus_intr_establish __P(( 120 bus_space_tag_t, 121 int, /*Sbus interrupt level*/ 122 int, /*`device class' priority*/ 123 int, /*flags*/ 124 int (*) __P((void *)), /*handler*/ 125 void *)); /*handler arg*/ 126 127 128 /* autoconfiguration driver */ 129 int sbus_match_mainbus __P((struct device *, struct cfdata *, void *)); 130 int sbus_match_iommu __P((struct device *, struct cfdata *, void *)); 131 int sbus_match_xbox __P((struct device *, struct cfdata *, void *)); 132 void sbus_attach_mainbus __P((struct device *, struct device *, void *)); 133 void sbus_attach_iommu __P((struct device *, struct device *, void *)); 134 void sbus_attach_xbox __P((struct device *, struct device *, void *)); 135 136 static int sbus_error __P((void)); 137 int (*sbuserr_handler) __P((void)); 138 139 struct cfattach sbus_mainbus_ca = { 140 sizeof(struct sbus_softc), sbus_match_mainbus, sbus_attach_mainbus 141 }; 142 struct cfattach sbus_iommu_ca = { 143 sizeof(struct sbus_softc), sbus_match_iommu, sbus_attach_iommu 144 }; 145 struct cfattach sbus_xbox_ca = { 146 sizeof(struct sbus_softc), sbus_match_xbox, sbus_attach_xbox 147 }; 148 149 extern struct cfdriver sbus_cd; 150 151 /* The "primary" Sbus */ 152 struct sbus_softc *sbus_sc; 153 154 /* If the PROM does not provide the `ranges' property, we make up our own */ 155 struct sbus_range sbus_translations[] = { 156 /* Assume a maximum of 4 Sbus slots, all mapped to on-board io space */ 157 { 0, 0, PMAP_OBIO, SBUS_ADDR(0,0), 1 << 25 }, 158 { 1, 0, PMAP_OBIO, SBUS_ADDR(1,0), 1 << 25 }, 159 { 2, 0, PMAP_OBIO, SBUS_ADDR(2,0), 1 << 25 }, 160 { 3, 0, PMAP_OBIO, SBUS_ADDR(3,0), 1 << 25 } 161 }; 162 163 /* 164 * Child devices receive the Sbus interrupt level in their attach 165 * arguments. We translate these to CPU IPLs using the following 166 * tables. Note: obio bus interrupt levels are identical to the 167 * processor IPL. 168 * 169 * The second set of tables is used when the Sbus interrupt level 170 * cannot be had from the PROM as an `interrupt' property. We then 171 * fall back on the `intr' property which contains the CPU IPL. 172 */ 173 174 /* Translate Sbus interrupt level to processor IPL */ 175 static int intr_sbus2ipl_4c[] = { 176 0, 1, 2, 3, 5, 7, 8, 9 177 }; 178 static int intr_sbus2ipl_4m[] = { 179 0, 2, 3, 5, 7, 9, 11, 13 180 }; 181 182 /* 183 * This value is or'ed into the attach args' interrupt level cookie 184 * if the interrupt level comes from an `intr' property, i.e. it is 185 * not an Sbus interrupt level. 186 */ 187 #define SBUS_INTR_COMPAT 0x80000000 188 189 190 /* 191 * Print the location of some sbus-attached device (called just 192 * before attaching that device). If `sbus' is not NULL, the 193 * device was found but not configured; print the sbus as well. 194 * Return UNCONF (config_find ignores this if the device was configured). 195 */ 196 int 197 sbus_print(args, busname) 198 void *args; 199 const char *busname; 200 { 201 struct sbus_attach_args *sa = args; 202 int i; 203 204 if (busname) 205 printf("%s at %s", sa->sa_name, busname); 206 printf(" slot %d offset 0x%x", sa->sa_slot, sa->sa_offset); 207 for (i = 0; i < sa->sa_nintr; i++) { 208 u_int32_t level = sa->sa_intr[i].sbi_pri; 209 struct sbus_softc *sc = 210 (struct sbus_softc *) sa->sa_bustag->cookie; 211 212 printf(" level %d", level & ~SBUS_INTR_COMPAT); 213 if ((level & SBUS_INTR_COMPAT) == 0) { 214 int ipl = sc->sc_intr2ipl[level]; 215 if (ipl != level) 216 printf(" (ipl %d)", ipl); 217 } 218 } 219 return (UNCONF); 220 } 221 222 int 223 sbus_match_mainbus(parent, cf, aux) 224 struct device *parent; 225 struct cfdata *cf; 226 void *aux; 227 { 228 struct mainbus_attach_args *ma = aux; 229 230 if (CPU_ISSUN4) 231 return (0); 232 233 return (strcmp(cf->cf_driver->cd_name, ma->ma_name) == 0); 234 } 235 236 int 237 sbus_match_iommu(parent, cf, aux) 238 struct device *parent; 239 struct cfdata *cf; 240 void *aux; 241 { 242 struct iommu_attach_args *ia = aux; 243 244 if (CPU_ISSUN4) 245 return (0); 246 247 return (strcmp(cf->cf_driver->cd_name, ia->iom_name) == 0); 248 } 249 250 int 251 sbus_match_xbox(parent, cf, aux) 252 struct device *parent; 253 struct cfdata *cf; 254 void *aux; 255 { 256 struct xbox_attach_args *xa = aux; 257 258 if (CPU_ISSUN4) 259 return (0); 260 261 return (strcmp(cf->cf_driver->cd_name, xa->xa_name) == 0); 262 } 263 264 /* 265 * Attach an Sbus. 266 */ 267 void 268 sbus_attach_mainbus(parent, self, aux) 269 struct device *parent; 270 struct device *self; 271 void *aux; 272 { 273 struct sbus_softc *sc = (struct sbus_softc *)self; 274 struct mainbus_attach_args *ma = aux; 275 int node = ma->ma_node; 276 277 /* 278 * XXX there is only one Sbus, for now -- do not know how to 279 * address children on others 280 */ 281 if (sc->sc_dev.dv_unit > 0) { 282 printf(" unsupported\n"); 283 return; 284 } 285 286 sc->sc_bustag = ma->ma_bustag; 287 sc->sc_dmatag = ma->ma_dmatag; 288 289 #if 0 /* sbus at mainbus (sun4c): `reg' prop is not control space */ 290 if (ma->ma_size == 0) 291 printf("%s: no Sbus registers", self->dv_xname); 292 293 if (bus_space_map2(ma->ma_bustag, 294 (bus_type_t)ma->ma_iospace, 295 (bus_addr_t)ma->ma_paddr, 296 (bus_size_t)ma->ma_size, 297 BUS_SPACE_MAP_LINEAR, 298 0, &sc->sc_bh) != 0) { 299 panic("%s: can't map sbusbusreg", self->dv_xname); 300 } 301 #endif 302 303 /* Setup interrupt translation tables */ 304 sc->sc_intr2ipl = CPU_ISSUN4C 305 ? intr_sbus2ipl_4c 306 : intr_sbus2ipl_4m; 307 308 /* 309 * Record clock frequency for synchronous SCSI. 310 * IS THIS THE CORRECT DEFAULT?? 311 */ 312 sc->sc_clockfreq = getpropint(node, "clock-frequency", 25*1000*1000); 313 printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq)); 314 315 sbus_sc = sc; 316 sbus_attach_common(sc, "sbus", node, NULL); 317 } 318 319 320 void 321 sbus_attach_iommu(parent, self, aux) 322 struct device *parent; 323 struct device *self; 324 void *aux; 325 { 326 struct sbus_softc *sc = (struct sbus_softc *)self; 327 struct iommu_attach_args *ia = aux; 328 int node = ia->iom_node; 329 330 sc->sc_bustag = ia->iom_bustag; 331 sc->sc_dmatag = ia->iom_dmatag; 332 333 if (ia->iom_nreg == 0) 334 panic("%s: no Sbus registers", self->dv_xname); 335 336 if (bus_space_map2(ia->iom_bustag, 337 (bus_type_t)ia->iom_reg[0].ior_iospace, 338 (bus_addr_t)ia->iom_reg[0].ior_pa, 339 (bus_size_t)ia->iom_reg[0].ior_size, 340 BUS_SPACE_MAP_LINEAR, 341 0, &sc->sc_bh) != 0) { 342 panic("%s: can't map sbusbusreg", self->dv_xname); 343 } 344 345 /* Setup interrupt translation tables */ 346 sc->sc_intr2ipl = CPU_ISSUN4C ? intr_sbus2ipl_4c : intr_sbus2ipl_4m; 347 348 /* 349 * Record clock frequency for synchronous SCSI. 350 * IS THIS THE CORRECT DEFAULT?? 351 */ 352 sc->sc_clockfreq = getpropint(node, "clock-frequency", 25*1000*1000); 353 printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq)); 354 355 sbus_sc = sc; 356 sbuserr_handler = sbus_error; 357 sbus_attach_common(sc, "sbus", node, NULL); 358 } 359 360 void 361 sbus_attach_xbox(parent, self, aux) 362 struct device *parent; 363 struct device *self; 364 void *aux; 365 { 366 struct sbus_softc *sc = (struct sbus_softc *)self; 367 struct xbox_attach_args *xa = aux; 368 int node = xa->xa_node; 369 370 sc->sc_bustag = xa->xa_bustag; 371 sc->sc_dmatag = xa->xa_dmatag; 372 373 /* Setup interrupt translation tables */ 374 sc->sc_intr2ipl = CPU_ISSUN4C ? intr_sbus2ipl_4c : intr_sbus2ipl_4m; 375 376 /* 377 * Record clock frequency for synchronous SCSI. 378 * IS THIS THE CORRECT DEFAULT?? 379 */ 380 sc->sc_clockfreq = getpropint(node, "clock-frequency", 25*1000*1000); 381 printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq)); 382 383 sbus_attach_common(sc, "sbus", node, NULL); 384 } 385 386 void 387 sbus_attach_common(sc, busname, busnode, specials) 388 struct sbus_softc *sc; 389 char *busname; 390 int busnode; 391 const char * const *specials; 392 { 393 int node0, node, error; 394 const char *sp; 395 const char *const *ssp; 396 bus_space_tag_t sbt; 397 struct sbus_attach_args sa; 398 399 sbt = sbus_alloc_bustag(sc); 400 401 /* 402 * Get the SBus burst transfer size if burst transfers are supported 403 */ 404 sc->sc_burst = getpropint(busnode, "burst-sizes", 0); 405 406 407 if (CPU_ISSUN4M) { 408 /* 409 * Some models (e.g. SS20) erroneously report 64-bit 410 * burst capability. We mask it out here for all SUN4Ms, 411 * since probably no member of that class supports 412 * 64-bit Sbus bursts. 413 */ 414 sc->sc_burst &= ~SBUS_BURST_64; 415 } 416 417 /* 418 * Collect address translations from the OBP. 419 */ 420 error = getprop(busnode, "ranges", sizeof(struct rom_range), 421 &sc->sc_nrange, (void **)&sc->sc_range); 422 switch (error) { 423 case 0: 424 break; 425 case ENOENT: 426 /* Fall back to our own `range' construction */ 427 sc->sc_range = sbus_translations; 428 sc->sc_nrange = 429 sizeof(sbus_translations)/sizeof(sbus_translations[0]); 430 break; 431 default: 432 panic("%s: error getting ranges property", sc->sc_dev.dv_xname); 433 } 434 435 /* 436 * Loop through ROM children, fixing any relative addresses 437 * and then configuring each device. 438 * `specials' is an array of device names that are treated 439 * specially: 440 */ 441 node0 = firstchild(busnode); 442 for (ssp = specials ; ssp != NULL && *(sp = *ssp) != 0; ssp++) { 443 if ((node = findnode(node0, sp)) == 0) { 444 panic("could not find %s amongst %s devices", 445 sp, busname); 446 } 447 448 if (sbus_setup_attach_args(sc, sbt, sc->sc_dmatag, 449 node, &sa) != 0) { 450 panic("sbus_attach: %s: incomplete", sp); 451 } 452 (void) config_found(&sc->sc_dev, (void *)&sa, sbus_print); 453 sbus_destroy_attach_args(&sa); 454 } 455 456 for (node = node0; node; node = nextsibling(node)) { 457 char *name = getpropstring(node, "name"); 458 for (ssp = specials, sp = NULL; 459 ssp != NULL && (sp = *ssp) != NULL; 460 ssp++) 461 if (strcmp(name, sp) == 0) 462 break; 463 464 if (sp != NULL) 465 /* Already configured as an "early" device */ 466 continue; 467 468 if (sbus_setup_attach_args(sc, sbt, sc->sc_dmatag, 469 node, &sa) != 0) { 470 printf("sbus_attach: %s: incomplete\n", name); 471 continue; 472 } 473 (void) config_found(&sc->sc_dev, (void *)&sa, sbus_print); 474 sbus_destroy_attach_args(&sa); 475 } 476 } 477 478 int 479 sbus_setup_attach_args(sc, bustag, dmatag, node, sa) 480 struct sbus_softc *sc; 481 bus_space_tag_t bustag; 482 bus_dma_tag_t dmatag; 483 int node; 484 struct sbus_attach_args *sa; 485 { 486 int n, error; 487 488 bzero(sa, sizeof(struct sbus_attach_args)); 489 error = getprop(node, "name", 1, &n, (void **)&sa->sa_name); 490 if (error != 0) 491 return (error); 492 sa->sa_name[n] = '\0'; 493 494 sa->sa_bustag = bustag; 495 sa->sa_dmatag = dmatag; 496 sa->sa_node = node; 497 498 error = getprop(node, "reg", sizeof(struct sbus_reg), 499 &sa->sa_nreg, (void **)&sa->sa_reg); 500 if (error != 0) { 501 char buf[32]; 502 if (error != ENOENT || 503 !node_has_property(node, "device_type") || 504 strcmp(getpropstringA(node, "device_type", buf, sizeof buf), 505 "hierarchical") != 0) 506 return (error); 507 } 508 for (n = 0; n < sa->sa_nreg; n++) { 509 /* Convert to relative addressing, if necessary */ 510 u_int32_t base = sa->sa_reg[n].sbr_offset; 511 if (SBUS_ABS(base)) { 512 sa->sa_reg[n].sbr_slot = SBUS_ABS_TO_SLOT(base); 513 sa->sa_reg[n].sbr_offset = SBUS_ABS_TO_OFFSET(base); 514 } 515 } 516 517 if ((error = sbus_get_intr(sc, node, &sa->sa_intr, &sa->sa_nintr)) != 0) 518 return (error); 519 520 error = getprop(node, "address", sizeof(u_int32_t), 521 &sa->sa_npromvaddrs, (void **)&sa->sa_promvaddrs); 522 if (error != 0 && error != ENOENT) 523 return (error); 524 525 return (0); 526 } 527 528 void 529 sbus_destroy_attach_args(sa) 530 struct sbus_attach_args *sa; 531 { 532 if (sa->sa_name != NULL) 533 free(sa->sa_name, M_DEVBUF); 534 535 if (sa->sa_nreg != 0) 536 free(sa->sa_reg, M_DEVBUF); 537 538 if (sa->sa_intr) 539 free(sa->sa_intr, M_DEVBUF); 540 541 if (sa->sa_promvaddrs) 542 free(sa->sa_promvaddrs, M_DEVBUF); 543 544 bzero(sa, sizeof(struct sbus_attach_args));/*DEBUG*/ 545 } 546 547 548 int 549 _sbus_bus_map(t, btype, offset, size, flags, vaddr, hp) 550 bus_space_tag_t t; 551 bus_type_t btype; 552 bus_addr_t offset; 553 bus_size_t size; 554 int flags; 555 vaddr_t vaddr; 556 bus_space_handle_t *hp; 557 { 558 struct sbus_softc *sc = t->cookie; 559 int slot = btype; 560 int i; 561 562 for (i = 0; i < sc->sc_nrange; i++) { 563 bus_addr_t paddr; 564 bus_type_t iospace; 565 566 if (sc->sc_range[i].cspace != slot) 567 continue; 568 569 /* We've found the connection to the parent bus */ 570 paddr = sc->sc_range[i].poffset + offset; 571 iospace = sc->sc_range[i].pspace; 572 return (bus_space_map2(sc->sc_bustag, iospace, paddr, 573 size, flags, vaddr, hp)); 574 } 575 576 return (EINVAL); 577 } 578 579 int 580 sbus_bus_mmap(t, btype, paddr, flags, hp) 581 bus_space_tag_t t; 582 bus_type_t btype; 583 bus_addr_t paddr; 584 int flags; 585 bus_space_handle_t *hp; 586 { 587 int slot = (int)btype; 588 int offset = (int)paddr; 589 struct sbus_softc *sc = t->cookie; 590 int i; 591 592 for (i = 0; i < sc->sc_nrange; i++) { 593 bus_addr_t paddr; 594 bus_addr_t iospace; 595 596 if (sc->sc_range[i].cspace != slot) 597 continue; 598 599 paddr = sc->sc_range[i].poffset + offset; 600 iospace = (bus_addr_t)sc->sc_range[i].pspace; 601 return (bus_space_mmap(sc->sc_bustag, iospace, paddr, 602 flags, hp)); 603 } 604 605 return (-1); 606 } 607 608 609 /* 610 * Each attached device calls sbus_establish after it initializes 611 * its sbusdev portion. 612 */ 613 void 614 sbus_establish(sd, dev) 615 register struct sbusdev *sd; 616 register struct device *dev; 617 { 618 register struct sbus_softc *sc; 619 register struct device *curdev; 620 621 /* 622 * We have to look for the sbus by name, since it is not necessarily 623 * our immediate parent (i.e. sun4m /iommu/sbus/espdma/esp) 624 * We don't just use the device structure of the above-attached 625 * sbus, since we might (in the future) support multiple sbus's. 626 */ 627 for (curdev = dev->dv_parent; ; curdev = curdev->dv_parent) { 628 if (!curdev || !curdev->dv_xname) 629 panic("sbus_establish: can't find sbus parent for %s", 630 sd->sd_dev->dv_xname 631 ? sd->sd_dev->dv_xname 632 : "<unknown>" ); 633 634 if (strncmp(curdev->dv_xname, "sbus", 4) == 0) 635 break; 636 } 637 sc = (struct sbus_softc *) curdev; 638 639 sd->sd_dev = dev; 640 sd->sd_bchain = sc->sc_sbdev; 641 sc->sc_sbdev = sd; 642 } 643 644 /* 645 * Reset the given sbus. (???) 646 */ 647 void 648 sbusreset(sbus) 649 int sbus; 650 { 651 register struct sbusdev *sd; 652 struct sbus_softc *sc = sbus_cd.cd_devs[sbus]; 653 struct device *dev; 654 655 printf("reset %s:", sc->sc_dev.dv_xname); 656 for (sd = sc->sc_sbdev; sd != NULL; sd = sd->sd_bchain) { 657 if (sd->sd_reset) { 658 dev = sd->sd_dev; 659 (*sd->sd_reset)(dev); 660 printf(" %s", dev->dv_xname); 661 } 662 } 663 } 664 665 666 /* 667 * Get interrupt attributes for an Sbus device. 668 */ 669 int 670 sbus_get_intr(sc, node, ipp, np) 671 struct sbus_softc *sc; 672 int node; 673 struct sbus_intr **ipp; 674 int *np; 675 { 676 int error, n; 677 u_int32_t *ipl = NULL; 678 679 /* 680 * The `interrupts' property contains the Sbus interrupt level. 681 */ 682 if (getprop(node, "interrupts", sizeof(int), np, (void **)&ipl) == 0) { 683 /* Change format to an `struct sbus_intr' array */ 684 struct sbus_intr *ip; 685 ip = malloc(*np * sizeof(struct sbus_intr), M_DEVBUF, M_NOWAIT); 686 if (ip == NULL) 687 return (ENOMEM); 688 for (n = 0; n < *np; n++) { 689 ip[n].sbi_pri = ipl[n]; 690 ip[n].sbi_vec = 0; 691 } 692 free(ipl, M_DEVBUF); 693 *ipp = ip; 694 return (0); 695 } 696 697 /* 698 * Fall back on `intr' property. 699 */ 700 *ipp = NULL; 701 error = getprop(node, "intr", sizeof(struct sbus_intr), 702 np, (void **)ipp); 703 switch (error) { 704 case 0: 705 for (n = *np; n-- > 0;) { 706 (*ipp)[n].sbi_pri &= 0xf; 707 (*ipp)[n].sbi_pri |= SBUS_INTR_COMPAT; 708 } 709 break; 710 case ENOENT: 711 error = 0; 712 break; 713 } 714 715 return (error); 716 } 717 718 719 /* 720 * Install an interrupt handler for an Sbus device. 721 */ 722 void * 723 sbus_intr_establish(t, pri, level, flags, handler, arg) 724 bus_space_tag_t t; 725 int pri; 726 int level; 727 int flags; 728 int (*handler) __P((void *)); 729 void *arg; 730 { 731 struct sbus_softc *sc = t->cookie; 732 struct intrhand *ih; 733 int pil; 734 735 ih = (struct intrhand *) 736 malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT); 737 if (ih == NULL) 738 return (NULL); 739 740 /* 741 * Translate Sbus interrupt priority to CPU interrupt level 742 */ 743 if ((flags & BUS_INTR_ESTABLISH_SOFTINTR) != 0) 744 pil = pri; 745 else if ((pri & SBUS_INTR_COMPAT) != 0) 746 pil = pri & ~SBUS_INTR_COMPAT; 747 else 748 pil = sc->sc_intr2ipl[pri]; 749 750 ih->ih_fun = handler; 751 ih->ih_arg = arg; 752 if ((flags & BUS_INTR_ESTABLISH_FASTTRAP) != 0) 753 intr_fasttrap(pil, (void (*)__P((void)))handler); 754 else 755 intr_establish(pil, ih); 756 return (ih); 757 } 758 759 static bus_space_tag_t 760 sbus_alloc_bustag(sc) 761 struct sbus_softc *sc; 762 { 763 bus_space_tag_t sbt; 764 765 sbt = (bus_space_tag_t) 766 malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT); 767 if (sbt == NULL) 768 return (NULL); 769 770 bzero(sbt, sizeof *sbt); 771 sbt->cookie = sc; 772 sbt->parent = sc->sc_bustag; 773 sbt->sparc_bus_map = _sbus_bus_map; 774 sbt->sparc_bus_mmap = sbus_bus_mmap; 775 sbt->sparc_intr_establish = sbus_intr_establish; 776 return (sbt); 777 } 778 779 int 780 sbus_error() 781 { 782 struct sbus_softc *sc = sbus_sc; 783 bus_space_handle_t bh = sc->sc_bh; 784 u_int32_t afsr, afva; 785 char bits[64]; 786 static int straytime, nstray; 787 int timesince; 788 789 afsr = bus_space_read_4(sc->sc_bustag, bh, SBUS_AFSR_REG); 790 afva = bus_space_read_4(sc->sc_bustag, bh, SBUS_AFAR_REG); 791 printf("sbus error:\n\tAFSR %s\n", 792 bitmask_snprintf(afsr, SBUS_AFSR_BITS, bits, sizeof(bits))); 793 printf("\taddress: 0x%x%x\n", afsr & SBUS_AFSR_PAH, afva); 794 795 /* For now, do the same dance as on stray interrupts */ 796 timesince = time.tv_sec - straytime; 797 if (timesince <= 10) { 798 if (++nstray > 9) 799 panic("too many SBus errors"); 800 } else { 801 straytime = time.tv_sec; 802 nstray = 1; 803 } 804 805 /* Unlock registers and clear interrupt */ 806 bus_space_write_4(sc->sc_bustag, bh, SBUS_AFSR_REG, afsr); 807 808 return (0); 809 } 810