1 /* $NetBSD: sbus.c,v 1.39 2000/11/01 06:27:45 eeh 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 sa->sa_frequency = sc->sc_clockfreq; 498 499 error = getprop(node, "reg", sizeof(struct sbus_reg), 500 &sa->sa_nreg, (void **)&sa->sa_reg); 501 if (error != 0) { 502 char buf[32]; 503 if (error != ENOENT || 504 !node_has_property(node, "device_type") || 505 strcmp(getpropstringA(node, "device_type", buf, sizeof buf), 506 "hierarchical") != 0) 507 return (error); 508 } 509 for (n = 0; n < sa->sa_nreg; n++) { 510 /* Convert to relative addressing, if necessary */ 511 u_int32_t base = sa->sa_reg[n].sbr_offset; 512 if (SBUS_ABS(base)) { 513 sa->sa_reg[n].sbr_slot = SBUS_ABS_TO_SLOT(base); 514 sa->sa_reg[n].sbr_offset = SBUS_ABS_TO_OFFSET(base); 515 } 516 } 517 518 if ((error = sbus_get_intr(sc, node, &sa->sa_intr, &sa->sa_nintr)) != 0) 519 return (error); 520 521 error = getprop(node, "address", sizeof(u_int32_t), 522 &sa->sa_npromvaddrs, (void **)&sa->sa_promvaddrs); 523 if (error != 0 && error != ENOENT) 524 return (error); 525 526 return (0); 527 } 528 529 void 530 sbus_destroy_attach_args(sa) 531 struct sbus_attach_args *sa; 532 { 533 if (sa->sa_name != NULL) 534 free(sa->sa_name, M_DEVBUF); 535 536 if (sa->sa_nreg != 0) 537 free(sa->sa_reg, M_DEVBUF); 538 539 if (sa->sa_intr) 540 free(sa->sa_intr, M_DEVBUF); 541 542 if (sa->sa_promvaddrs) 543 free(sa->sa_promvaddrs, M_DEVBUF); 544 545 bzero(sa, sizeof(struct sbus_attach_args));/*DEBUG*/ 546 } 547 548 549 int 550 _sbus_bus_map(t, btype, offset, size, flags, vaddr, hp) 551 bus_space_tag_t t; 552 bus_type_t btype; 553 bus_addr_t offset; 554 bus_size_t size; 555 int flags; 556 vaddr_t vaddr; 557 bus_space_handle_t *hp; 558 { 559 struct sbus_softc *sc = t->cookie; 560 int slot = btype; 561 int i; 562 563 for (i = 0; i < sc->sc_nrange; i++) { 564 bus_addr_t paddr; 565 bus_type_t iospace; 566 567 if (sc->sc_range[i].cspace != slot) 568 continue; 569 570 /* We've found the connection to the parent bus */ 571 paddr = sc->sc_range[i].poffset + offset; 572 iospace = sc->sc_range[i].pspace; 573 return (bus_space_map2(sc->sc_bustag, iospace, paddr, 574 size, flags, vaddr, hp)); 575 } 576 577 return (EINVAL); 578 } 579 580 int 581 sbus_bus_mmap(t, btype, paddr, flags, hp) 582 bus_space_tag_t t; 583 bus_type_t btype; 584 bus_addr_t paddr; 585 int flags; 586 bus_space_handle_t *hp; 587 { 588 int slot = (int)btype; 589 int offset = (int)paddr; 590 struct sbus_softc *sc = t->cookie; 591 int i; 592 593 for (i = 0; i < sc->sc_nrange; i++) { 594 bus_addr_t paddr; 595 bus_addr_t iospace; 596 597 if (sc->sc_range[i].cspace != slot) 598 continue; 599 600 paddr = sc->sc_range[i].poffset + offset; 601 iospace = (bus_addr_t)sc->sc_range[i].pspace; 602 return (bus_space_mmap(sc->sc_bustag, iospace, paddr, 603 flags, hp)); 604 } 605 606 return (-1); 607 } 608 609 610 /* 611 * Each attached device calls sbus_establish after it initializes 612 * its sbusdev portion. 613 */ 614 void 615 sbus_establish(sd, dev) 616 register struct sbusdev *sd; 617 register struct device *dev; 618 { 619 register struct sbus_softc *sc; 620 register struct device *curdev; 621 622 /* 623 * We have to look for the sbus by name, since it is not necessarily 624 * our immediate parent (i.e. sun4m /iommu/sbus/espdma/esp) 625 * We don't just use the device structure of the above-attached 626 * sbus, since we might (in the future) support multiple sbus's. 627 */ 628 for (curdev = dev->dv_parent; ; curdev = curdev->dv_parent) { 629 if (!curdev || !curdev->dv_xname) 630 panic("sbus_establish: can't find sbus parent for %s", 631 sd->sd_dev->dv_xname 632 ? sd->sd_dev->dv_xname 633 : "<unknown>" ); 634 635 if (strncmp(curdev->dv_xname, "sbus", 4) == 0) 636 break; 637 } 638 sc = (struct sbus_softc *) curdev; 639 640 sd->sd_dev = dev; 641 sd->sd_bchain = sc->sc_sbdev; 642 sc->sc_sbdev = sd; 643 } 644 645 /* 646 * Reset the given sbus. (???) 647 */ 648 void 649 sbusreset(sbus) 650 int sbus; 651 { 652 register struct sbusdev *sd; 653 struct sbus_softc *sc = sbus_cd.cd_devs[sbus]; 654 struct device *dev; 655 656 printf("reset %s:", sc->sc_dev.dv_xname); 657 for (sd = sc->sc_sbdev; sd != NULL; sd = sd->sd_bchain) { 658 if (sd->sd_reset) { 659 dev = sd->sd_dev; 660 (*sd->sd_reset)(dev); 661 printf(" %s", dev->dv_xname); 662 } 663 } 664 } 665 666 667 /* 668 * Get interrupt attributes for an Sbus device. 669 */ 670 int 671 sbus_get_intr(sc, node, ipp, np) 672 struct sbus_softc *sc; 673 int node; 674 struct sbus_intr **ipp; 675 int *np; 676 { 677 int error, n; 678 u_int32_t *ipl = NULL; 679 680 /* 681 * The `interrupts' property contains the Sbus interrupt level. 682 */ 683 if (getprop(node, "interrupts", sizeof(int), np, (void **)&ipl) == 0) { 684 /* Change format to an `struct sbus_intr' array */ 685 struct sbus_intr *ip; 686 ip = malloc(*np * sizeof(struct sbus_intr), M_DEVBUF, M_NOWAIT); 687 if (ip == NULL) 688 return (ENOMEM); 689 for (n = 0; n < *np; n++) { 690 ip[n].sbi_pri = ipl[n]; 691 ip[n].sbi_vec = 0; 692 } 693 free(ipl, M_DEVBUF); 694 *ipp = ip; 695 return (0); 696 } 697 698 /* 699 * Fall back on `intr' property. 700 */ 701 *ipp = NULL; 702 error = getprop(node, "intr", sizeof(struct sbus_intr), 703 np, (void **)ipp); 704 switch (error) { 705 case 0: 706 for (n = *np; n-- > 0;) { 707 (*ipp)[n].sbi_pri &= 0xf; 708 (*ipp)[n].sbi_pri |= SBUS_INTR_COMPAT; 709 } 710 break; 711 case ENOENT: 712 error = 0; 713 break; 714 } 715 716 return (error); 717 } 718 719 720 /* 721 * Install an interrupt handler for an Sbus device. 722 */ 723 void * 724 sbus_intr_establish(t, pri, level, flags, handler, arg) 725 bus_space_tag_t t; 726 int pri; 727 int level; 728 int flags; 729 int (*handler) __P((void *)); 730 void *arg; 731 { 732 struct sbus_softc *sc = t->cookie; 733 struct intrhand *ih; 734 int pil; 735 736 ih = (struct intrhand *) 737 malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT); 738 if (ih == NULL) 739 return (NULL); 740 741 /* 742 * Translate Sbus interrupt priority to CPU interrupt level 743 */ 744 if ((flags & BUS_INTR_ESTABLISH_SOFTINTR) != 0) 745 pil = pri; 746 else if ((pri & SBUS_INTR_COMPAT) != 0) 747 pil = pri & ~SBUS_INTR_COMPAT; 748 else 749 pil = sc->sc_intr2ipl[pri]; 750 751 ih->ih_fun = handler; 752 ih->ih_arg = arg; 753 if ((flags & BUS_INTR_ESTABLISH_FASTTRAP) != 0) 754 intr_fasttrap(pil, (void (*)__P((void)))handler); 755 else 756 intr_establish(pil, ih); 757 return (ih); 758 } 759 760 static bus_space_tag_t 761 sbus_alloc_bustag(sc) 762 struct sbus_softc *sc; 763 { 764 bus_space_tag_t sbt; 765 766 sbt = (bus_space_tag_t) 767 malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT); 768 if (sbt == NULL) 769 return (NULL); 770 771 bzero(sbt, sizeof *sbt); 772 sbt->cookie = sc; 773 sbt->parent = sc->sc_bustag; 774 sbt->sparc_bus_map = _sbus_bus_map; 775 sbt->sparc_bus_mmap = sbus_bus_mmap; 776 sbt->sparc_intr_establish = sbus_intr_establish; 777 return (sbt); 778 } 779 780 int 781 sbus_error() 782 { 783 struct sbus_softc *sc = sbus_sc; 784 bus_space_handle_t bh = sc->sc_bh; 785 u_int32_t afsr, afva; 786 char bits[64]; 787 static int straytime, nstray; 788 int timesince; 789 790 afsr = bus_space_read_4(sc->sc_bustag, bh, SBUS_AFSR_REG); 791 afva = bus_space_read_4(sc->sc_bustag, bh, SBUS_AFAR_REG); 792 printf("sbus error:\n\tAFSR %s\n", 793 bitmask_snprintf(afsr, SBUS_AFSR_BITS, bits, sizeof(bits))); 794 printf("\taddress: 0x%x%x\n", afsr & SBUS_AFSR_PAH, afva); 795 796 /* For now, do the same dance as on stray interrupts */ 797 timesince = time.tv_sec - straytime; 798 if (timesince <= 10) { 799 if (++nstray > 9) 800 panic("too many SBus errors"); 801 } else { 802 straytime = time.tv_sec; 803 nstray = 1; 804 } 805 806 /* Unlock registers and clear interrupt */ 807 bus_space_write_4(sc->sc_bustag, bh, SBUS_AFSR_REG, afsr); 808 809 return (0); 810 } 811