1 /* $NetBSD: isapnp.c,v 1.34 1999/04/12 19:31:27 mjl Exp $ */ 2 3 /*- 4 * Copyright (c) 1996 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Christos Zoulas. 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 * ISA PnP bus autoconfiguration. 41 */ 42 43 #include "isadma.h" 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/device.h> 48 #include <sys/malloc.h> 49 50 #include <machine/bus.h> 51 52 #include <dev/isa/isavar.h> 53 54 #include <dev/isapnp/isapnpreg.h> 55 #include <dev/isapnp/isapnpvar.h> 56 #include <dev/isapnp/isapnpdevs.h> 57 58 #include "wss_isapnp.h" /* XXX part of disgusting CS chip hack */ 59 60 #ifndef ISAPNP_ALLOC_INTR_MASK 61 #define ISAPNP_ALLOC_INTR_MASK (~0) 62 #endif 63 64 static void isapnp_init __P((struct isapnp_softc *)); 65 static __inline u_char isapnp_shift_bit __P((struct isapnp_softc *)); 66 static int isapnp_findcard __P((struct isapnp_softc *)); 67 static void isapnp_free_region __P((bus_space_tag_t, struct isapnp_region *)); 68 static int isapnp_alloc_region __P((bus_space_tag_t, struct isapnp_region *)); 69 static int isapnp_alloc_irq __P((isa_chipset_tag_t, struct isapnp_pin *)); 70 static int isapnp_alloc_drq __P((isa_chipset_tag_t, struct isapnp_pin *)); 71 static int isapnp_testconfig __P((bus_space_tag_t, bus_space_tag_t, 72 struct isapnp_attach_args *, int)); 73 static struct isapnp_attach_args *isapnp_bestconfig __P((struct isapnp_softc *, 74 struct isapnp_attach_args **)); 75 static void isapnp_print_region __P((const char *, struct isapnp_region *, 76 size_t)); 77 static void isapnp_configure __P((struct isapnp_softc *, 78 const struct isapnp_attach_args *)); 79 static void isapnp_print_pin __P((const char *, struct isapnp_pin *, size_t)); 80 static int isapnp_print __P((void *, const char *)); 81 #ifdef _KERNEL 82 static int isapnp_submatch __P((struct device *, struct cfdata *, void *)); 83 #endif 84 static int isapnp_find __P((struct isapnp_softc *, int)); 85 static int isapnp_match __P((struct device *, struct cfdata *, void *)); 86 static void isapnp_attach __P((struct device *, struct device *, void *)); 87 static void isapnp_callback __P((struct device *)); 88 89 struct cfattach isapnp_ca = { 90 sizeof(struct isapnp_softc), isapnp_match, isapnp_attach 91 }; 92 93 /* 94 * This keeps track if which ISA's we have been probed on. 95 */ 96 struct isapnp_probe_cookie { 97 LIST_ENTRY(isapnp_probe_cookie) ipc_link; 98 struct device *ipc_parent; 99 }; 100 LIST_HEAD(, isapnp_probe_cookie) isapnp_probes = 101 LIST_HEAD_INITIALIZER(isapnp_probes); 102 103 /* isapnp_init(): 104 * Write the PNP initiation key to wake up the cards... 105 */ 106 static void 107 isapnp_init(sc) 108 struct isapnp_softc *sc; 109 { 110 int i; 111 u_char v = ISAPNP_LFSR_INIT; 112 113 /* First write 0's twice to enter the Wait for Key state */ 114 ISAPNP_WRITE_ADDR(sc, 0); 115 ISAPNP_WRITE_ADDR(sc, 0); 116 117 /* Send the 32 byte sequence to awake the logic */ 118 for (i = 0; i < ISAPNP_LFSR_LENGTH; i++) { 119 ISAPNP_WRITE_ADDR(sc, v); 120 v = ISAPNP_LFSR_NEXT(v); 121 } 122 } 123 124 125 /* isapnp_shift_bit(): 126 * Read a bit at a time from the config card. 127 */ 128 static __inline u_char 129 isapnp_shift_bit(sc) 130 struct isapnp_softc *sc; 131 { 132 u_char c1, c2; 133 134 DELAY(250); 135 c1 = ISAPNP_READ_DATA(sc); 136 DELAY(250); 137 c2 = ISAPNP_READ_DATA(sc); 138 139 if (c1 == 0x55 && c2 == 0xAA) 140 return 0x80; 141 else 142 return 0; 143 } 144 145 146 /* isapnp_findcard(): 147 * Attempt to read the vendor/serial/checksum for a card 148 * If a card is found [the checksum matches], assign the 149 * next card number to it and return 1 150 */ 151 static int 152 isapnp_findcard(sc) 153 struct isapnp_softc *sc; 154 { 155 u_char v = ISAPNP_LFSR_INIT, csum, w; 156 int i, b; 157 158 if (sc->sc_ncards == ISAPNP_MAX_CARDS) { 159 printf("%s: Too many pnp cards\n", sc->sc_dev.dv_xname); 160 return 0; 161 } 162 163 /* Set the read port */ 164 isapnp_write_reg(sc, ISAPNP_WAKE, 0); 165 isapnp_write_reg(sc, ISAPNP_SET_RD_PORT, sc->sc_read_port >> 2); 166 sc->sc_read_port |= 3; 167 DELAY(1000); 168 169 ISAPNP_WRITE_ADDR(sc, ISAPNP_SERIAL_ISOLATION); 170 DELAY(1000); 171 172 /* Read the 8 bytes of the Vendor ID and Serial Number */ 173 for(i = 0; i < 8; i++) { 174 /* Read each bit separately */ 175 for (w = 0, b = 0; b < 8; b++) { 176 u_char neg = isapnp_shift_bit(sc); 177 178 w >>= 1; 179 w |= neg; 180 v = ISAPNP_LFSR_NEXT(v) ^ neg; 181 } 182 sc->sc_id[sc->sc_ncards][i] = w; 183 } 184 185 /* Read the remaining checksum byte */ 186 for (csum = 0, b = 0; b < 8; b++) { 187 u_char neg = isapnp_shift_bit(sc); 188 189 csum >>= 1; 190 csum |= neg; 191 } 192 sc->sc_id[sc->sc_ncards][8] = csum; 193 194 if (csum == v) { 195 sc->sc_ncards++; 196 isapnp_write_reg(sc, ISAPNP_CARD_SELECT_NUM, sc->sc_ncards); 197 return 1; 198 } 199 return 0; 200 } 201 202 203 /* isapnp_free_region(): 204 * Free a region 205 */ 206 static void 207 isapnp_free_region(t, r) 208 bus_space_tag_t t; 209 struct isapnp_region *r; 210 { 211 if (r->length == 0) 212 return; 213 214 #ifdef _KERNEL 215 bus_space_unmap(t, r->h, r->length); 216 #endif 217 } 218 219 220 /* isapnp_alloc_region(): 221 * Allocate a single region if possible 222 */ 223 static int 224 isapnp_alloc_region(t, r) 225 bus_space_tag_t t; 226 struct isapnp_region *r; 227 { 228 int error = 0; 229 230 if (r->length == 0) { 231 r->base = 0; 232 return 0; 233 } 234 235 for (r->base = r->minbase; r->base <= r->maxbase; 236 r->base += r->align) { 237 #ifdef _KERNEL 238 error = bus_space_map(t, r->base, r->length, 0, &r->h); 239 #endif 240 if (error == 0) 241 return 0; 242 if (r->align == 0) 243 break; 244 } 245 return error; 246 } 247 248 249 /* isapnp_alloc_irq(): 250 * Allocate an irq 251 */ 252 static int 253 isapnp_alloc_irq(ic, i) 254 isa_chipset_tag_t ic; 255 struct isapnp_pin *i; 256 { 257 int irq; 258 #define LEVEL_IRQ (ISAPNP_IRQTYPE_LEVEL_PLUS|ISAPNP_IRQTYPE_LEVEL_MINUS) 259 i->type = (i->flags & LEVEL_IRQ) ? IST_LEVEL : IST_EDGE; 260 261 if (i->bits == 0) { 262 i->num = 0; 263 return 0; 264 } 265 266 if (isa_intr_alloc(ic, ISAPNP_ALLOC_INTR_MASK & i->bits, 267 i->type, &irq) == 0) { 268 i->num = irq; 269 return 0; 270 } 271 272 return EINVAL; 273 } 274 275 /* isapnp_alloc_drq(): 276 * Allocate a drq 277 */ 278 static int 279 isapnp_alloc_drq(ic, i) 280 isa_chipset_tag_t ic; 281 struct isapnp_pin *i; 282 { 283 #if NISADMA > 0 284 int b; 285 286 if (i->bits == 0) { 287 i->num = 0; 288 return 0; 289 } 290 291 for (b = 0; b < 8; b++) 292 if ((i->bits & (1 << b)) && isa_drq_isfree(ic, b)) { 293 i->num = b; 294 return 0; 295 } 296 #endif /* NISADMA > 0 */ 297 298 return EINVAL; 299 } 300 301 /* isapnp_testconfig(): 302 * Test/Allocate the regions used 303 */ 304 static int 305 isapnp_testconfig(iot, memt, ipa, alloc) 306 bus_space_tag_t iot, memt; 307 struct isapnp_attach_args *ipa; 308 int alloc; 309 { 310 int nio = 0, nmem = 0, nmem32 = 0, nirq = 0, ndrq = 0; 311 int error = 0; 312 313 #ifdef DEBUG_ISAPNP 314 isapnp_print_attach(ipa); 315 #endif 316 317 for (; nio < ipa->ipa_nio; nio++) { 318 error = isapnp_alloc_region(iot, &ipa->ipa_io[nio]); 319 if (error) 320 goto bad; 321 } 322 323 for (; nmem < ipa->ipa_nmem; nmem++) { 324 error = isapnp_alloc_region(memt, &ipa->ipa_mem[nmem]); 325 if (error) 326 goto bad; 327 } 328 329 for (; nmem32 < ipa->ipa_nmem32; nmem32++) { 330 error = isapnp_alloc_region(memt, &ipa->ipa_mem32[nmem32]); 331 if (error) 332 goto bad; 333 } 334 335 for (; nirq < ipa->ipa_nirq; nirq++) { 336 error = isapnp_alloc_irq(ipa->ipa_ic, &ipa->ipa_irq[nirq]); 337 if (error) 338 goto bad; 339 } 340 341 for (; ndrq < ipa->ipa_ndrq; ndrq++) { 342 error = isapnp_alloc_drq(ipa->ipa_ic, &ipa->ipa_drq[ndrq]); 343 if (error) 344 goto bad; 345 } 346 347 if (alloc) 348 return error; 349 350 bad: 351 #ifdef notyet 352 for (ndrq--; ndrq >= 0; ndrq--) 353 isapnp_free_pin(&ipa->ipa_drq[ndrq]); 354 355 for (nirq--; nirq >= 0; nirq--) 356 isapnp_free_pin(&ipa->ipa_irq[nirq]); 357 #endif 358 359 for (nmem32--; nmem32 >= 0; nmem32--) 360 isapnp_free_region(memt, &ipa->ipa_mem32[nmem32]); 361 362 for (nmem--; nmem >= 0; nmem--) 363 isapnp_free_region(memt, &ipa->ipa_mem[nmem]); 364 365 for (nio--; nio >= 0; nio--) 366 isapnp_free_region(iot, &ipa->ipa_io[nio]); 367 368 return error; 369 } 370 371 372 /* isapnp_config(): 373 * Test/Allocate the regions used 374 */ 375 int 376 isapnp_config(iot, memt, ipa) 377 bus_space_tag_t iot, memt; 378 struct isapnp_attach_args *ipa; 379 { 380 return isapnp_testconfig(iot, memt, ipa, 1); 381 } 382 383 384 /* isapnp_unconfig(): 385 * Free the regions used 386 */ 387 void 388 isapnp_unconfig(iot, memt, ipa) 389 bus_space_tag_t iot, memt; 390 struct isapnp_attach_args *ipa; 391 { 392 int i; 393 394 #ifdef notyet 395 for (i = 0; i < ipa->ipa_ndrq; i++) 396 isapnp_free_pin(&ipa->ipa_drq[i]); 397 398 for (i = 0; i < ipa->ipa_nirq; i++) 399 isapnp_free_pin(&ipa->ipa_irq[i]); 400 #endif 401 402 for (i = 0; i < ipa->ipa_nmem32; i++) 403 isapnp_free_region(memt, &ipa->ipa_mem32[i]); 404 405 for (i = 0; i < ipa->ipa_nmem; i++) 406 isapnp_free_region(memt, &ipa->ipa_mem[i]); 407 408 for (i = 0; i < ipa->ipa_nio; i++) 409 isapnp_free_region(iot, &ipa->ipa_io[i]); 410 } 411 412 413 /* isapnp_bestconfig(): 414 * Return the best configuration for each logical device, remove and 415 * free all other configurations. 416 */ 417 static struct isapnp_attach_args * 418 isapnp_bestconfig(sc, ipa) 419 struct isapnp_softc *sc; 420 struct isapnp_attach_args **ipa; 421 { 422 struct isapnp_attach_args *c, *best, *f = *ipa; 423 int error; 424 425 for (;;) { 426 if (f == NULL) 427 return NULL; 428 429 #define SAMEDEV(a, b) (strcmp((a)->ipa_devlogic, (b)->ipa_devlogic) == 0) 430 431 /* Find the best config */ 432 for (best = c = f; c != NULL; c = c->ipa_sibling) { 433 if (!SAMEDEV(c, f)) 434 continue; 435 if (c->ipa_pref < best->ipa_pref) 436 best = c; 437 } 438 439 /* 440 * Make sure the ISA chipset is initialized! We need 441 * it to test the best config! 442 */ 443 best->ipa_ic = sc->sc_ic; 444 445 /* Test the best config */ 446 error = isapnp_testconfig(sc->sc_iot, sc->sc_memt, best, 0); 447 448 /* Remove this config from the list */ 449 if (best == f) 450 f = f->ipa_sibling; 451 else { 452 for (c = f; c->ipa_sibling != best; c = c->ipa_sibling) 453 continue; 454 c->ipa_sibling = best->ipa_sibling; 455 } 456 457 if (error) { 458 best->ipa_pref = ISAPNP_DEP_CONFLICTING; 459 460 for (c = f; c != NULL; c = c->ipa_sibling) 461 if (c != best && SAMEDEV(c, best)) 462 break; 463 /* Last config for this logical device is conflicting */ 464 if (c == NULL) { 465 *ipa = f; 466 return best; 467 } 468 469 ISAPNP_FREE(best); 470 continue; 471 } 472 else { 473 /* Remove all other configs for this device */ 474 struct isapnp_attach_args *l = NULL, *n = NULL, *d; 475 476 for (c = f; c; ) { 477 if (c == best) 478 continue; 479 d = c->ipa_sibling; 480 if (SAMEDEV(c, best)) 481 ISAPNP_FREE(c); 482 else { 483 if (n) 484 n->ipa_sibling = c; 485 486 else 487 l = c; 488 n = c; 489 c->ipa_sibling = NULL; 490 } 491 c = d; 492 } 493 f = l; 494 } 495 *ipa = f; 496 return best; 497 } 498 } 499 500 501 /* isapnp_id_to_vendor(): 502 * Convert a pnp ``compressed ascii'' vendor id to a string 503 */ 504 char * 505 isapnp_id_to_vendor(v, id) 506 char *v; 507 const u_char *id; 508 { 509 static const char hex[] = "0123456789ABCDEF"; 510 char *p = v; 511 512 *p++ = 'A' + (id[0] >> 2) - 1; 513 *p++ = 'A' + ((id[0] & 3) << 3) + (id[1] >> 5) - 1; 514 *p++ = 'A' + (id[1] & 0x1f) - 1; 515 *p++ = hex[id[2] >> 4]; 516 *p++ = hex[id[2] & 0x0f]; 517 *p++ = hex[id[3] >> 4]; 518 *p++ = hex[id[3] & 0x0f]; 519 *p = '\0'; 520 521 return v; 522 } 523 524 525 /* isapnp_print_region(): 526 * Print a region allocation 527 */ 528 static void 529 isapnp_print_region(str, r, n) 530 const char *str; 531 struct isapnp_region *r; 532 size_t n; 533 { 534 size_t i; 535 536 if (n == 0) 537 return; 538 539 printf(" %s ", str); 540 for (i = 0; i < n; i++, r++) { 541 printf("0x%x", r->base); 542 if (r->length) 543 printf("/%d", r->length); 544 if (i != n - 1) 545 printf(","); 546 } 547 } 548 549 550 /* isapnp_print_pin(): 551 * Print an irq/drq assignment 552 */ 553 static void 554 isapnp_print_pin(str, p, n) 555 const char *str; 556 struct isapnp_pin *p; 557 size_t n; 558 { 559 size_t i; 560 561 if (n == 0) 562 return; 563 564 printf(" %s ", str); 565 for (i = 0; i < n; i++, p++) { 566 printf("%d", p->num); 567 if (i != n - 1) 568 printf(","); 569 } 570 } 571 572 573 /* isapnp_print(): 574 * Print the configuration line for an ISA PnP card. 575 */ 576 static int 577 isapnp_print(aux, str) 578 void *aux; 579 const char *str; 580 { 581 struct isapnp_attach_args *ipa = aux; 582 583 if (str != NULL) 584 printf("%s: <%s, %s, %s, %s>", 585 str, ipa->ipa_devident, ipa->ipa_devlogic, 586 ipa->ipa_devcompat, ipa->ipa_devclass); 587 588 isapnp_print_region("port", ipa->ipa_io, ipa->ipa_nio); 589 isapnp_print_region("mem", ipa->ipa_mem, ipa->ipa_nmem); 590 isapnp_print_region("mem32", ipa->ipa_mem32, ipa->ipa_nmem32); 591 isapnp_print_pin("irq", ipa->ipa_irq, ipa->ipa_nirq); 592 isapnp_print_pin("drq", ipa->ipa_drq, ipa->ipa_ndrq); 593 594 return UNCONF; 595 } 596 597 598 #ifdef _KERNEL 599 /* isapnp_submatch(): 600 * Probe the logical device... 601 */ 602 static int 603 isapnp_submatch(parent, match, aux) 604 struct device *parent; 605 struct cfdata *match; 606 void *aux; 607 { 608 struct cfdata *cf = (struct cfdata *) match; 609 return ((*cf->cf_attach->ca_match)(parent, match, aux)); 610 } 611 612 613 /* isapnp_devmatch(): 614 * Match a probed device with the information from the driver 615 */ 616 int 617 isapnp_devmatch(ipa, dinfo, variant) 618 const struct isapnp_attach_args *ipa; 619 const struct isapnp_devinfo *dinfo; 620 int *variant; 621 { 622 const struct isapnp_matchinfo *match; 623 int n; 624 625 for (match = dinfo->devlogic, n = dinfo->nlogic; n--; match++) 626 if (strcmp(match->name, ipa->ipa_devlogic) == 0) { 627 *variant = match->variant; 628 return (1); 629 } 630 631 for (match = dinfo->devcompat, n = dinfo->ncompat; n--; match++) 632 if (strcmp(match->name, ipa->ipa_devcompat) == 0) { 633 *variant = match->variant; 634 return (1); 635 } 636 637 return (0); 638 } 639 640 641 /* isapnp_isa_attach_hook(): 642 * This routine is called from the isa attach code and 643 * is a kludge; we are resetting all the cards here in order 644 * to undo any card configuration that the bios did for us, in order 645 * to avoid having the PnP devices match an isa probe. The correct 646 * way of doing this is to read the PnP BIOS and find the card settings 647 * from there. Unfortunately it is not as easy as it sounds. 648 */ 649 void 650 isapnp_isa_attach_hook(isa_sc) 651 struct isa_softc *isa_sc; 652 { 653 struct isapnp_softc sc; 654 655 sc.sc_iot = isa_sc->sc_iot; 656 sc.sc_ncards = 0; 657 658 if (isapnp_map(&sc)) 659 return; 660 661 #if NWSS_ISAPNP > 0 662 /* 663 * XXX XXX 664 * This a totally disgusting hack, but I can't figure out another way. 665 * It seems that many CS audio chips have a bug (as far as I can 666 * understand). The reset below does not really reset the chip, it 667 * remains in a catatonic state and will not respond when probed. 668 * The chip can be used both as a WSS and as a SB device, and a 669 * single read at the WSS address (0x534) takes it out of this 670 * non-responsive state. 671 * The read has to happen at this point in time (or earlier) so 672 * it cannot be moved to the wss_isapnp.c driver. 673 * (BTW, We're not alone in having problems with these chips: 674 * Windoze 98 couldn't detect the sound chip on a Dell when I tried.) 675 * 676 * Lennart Augustsson <augustss@netbsd.org> 677 * 678 * (Implementation from John Kohl <jtk@kolvir.arlington.ma.us>) 679 */ 680 { 681 bus_space_handle_t ioh; 682 int rv; 683 if ((rv = bus_space_map(sc.sc_iot, 0x534, 1, 0, &ioh)) == 0) { 684 DPRINTF(("wss probe kludge\n")); 685 (void)bus_space_read_1(sc.sc_iot, ioh, 0); 686 bus_space_unmap(sc.sc_iot, ioh, 1); 687 } else { 688 DPRINTF(("wss probe kludge failed to map: %d\n", rv)); 689 } 690 } 691 #endif 692 693 isapnp_init(&sc); 694 695 isapnp_write_reg(&sc, ISAPNP_CONFIG_CONTROL, ISAPNP_CC_RESET_DRV); 696 DELAY(2000); 697 698 isapnp_unmap(&sc); 699 } 700 #endif 701 702 703 /* isapnp_find(): 704 * Probe and add cards 705 */ 706 static int 707 isapnp_find(sc, all) 708 struct isapnp_softc *sc; 709 int all; 710 { 711 int p; 712 713 isapnp_init(sc); 714 715 isapnp_write_reg(sc, ISAPNP_CONFIG_CONTROL, ISAPNP_CC_RESET_DRV); 716 DELAY(2000); 717 718 isapnp_init(sc); 719 DELAY(2000); 720 721 for (p = ISAPNP_RDDATA_MIN; p <= ISAPNP_RDDATA_MAX; p += 4) { 722 sc->sc_read_port = p; 723 if (isapnp_map_readport(sc)) 724 continue; 725 DPRINTF(("%s: Trying port %x\r", sc->sc_dev.dv_xname, p)); 726 if (isapnp_findcard(sc)) 727 break; 728 isapnp_unmap_readport(sc); 729 } 730 731 if (p > ISAPNP_RDDATA_MAX) { 732 sc->sc_read_port = 0; 733 return 0; 734 } 735 736 if (all) 737 while (isapnp_findcard(sc)) 738 continue; 739 740 return 1; 741 } 742 743 744 /* isapnp_configure(): 745 * Configure a PnP card 746 * XXX: The memory configuration code is wrong. We need to check the 747 * range/length bit an do appropriate sets. 748 */ 749 static void 750 isapnp_configure(sc, ipa) 751 struct isapnp_softc *sc; 752 const struct isapnp_attach_args *ipa; 753 { 754 int i; 755 static u_char isapnp_mem_range[] = ISAPNP_MEM_DESC; 756 static u_char isapnp_io_range[] = ISAPNP_IO_DESC; 757 static u_char isapnp_irq_range[] = ISAPNP_IRQ_DESC; 758 static u_char isapnp_drq_range[] = ISAPNP_DRQ_DESC; 759 static u_char isapnp_mem32_range[] = ISAPNP_MEM32_DESC; 760 const struct isapnp_region *r; 761 const struct isapnp_pin *p; 762 struct isapnp_region rz; 763 struct isapnp_pin pz; 764 765 memset(&pz, 0, sizeof(pz)); 766 memset(&rz, 0, sizeof(rz)); 767 768 #define B0(a) ((a) & 0xff) 769 #define B1(a) (((a) >> 8) & 0xff) 770 #define B2(a) (((a) >> 16) & 0xff) 771 #define B3(a) (((a) >> 24) & 0xff) 772 773 for (i = 0; i < sizeof(isapnp_io_range); i++) { 774 if (i < ipa->ipa_nio) 775 r = &ipa->ipa_io[i]; 776 else 777 r = &rz; 778 779 isapnp_write_reg(sc, 780 isapnp_io_range[i] + ISAPNP_IO_BASE_15_8, B1(r->base)); 781 isapnp_write_reg(sc, 782 isapnp_io_range[i] + ISAPNP_IO_BASE_7_0, B0(r->base)); 783 } 784 785 for (i = 0; i < sizeof(isapnp_mem_range); i++) { 786 if (i < ipa->ipa_nmem) 787 r = &ipa->ipa_mem[i]; 788 else 789 r = &rz; 790 791 isapnp_write_reg(sc, 792 isapnp_mem_range[i] + ISAPNP_MEM_BASE_23_16, B2(r->base)); 793 isapnp_write_reg(sc, 794 isapnp_mem_range[i] + ISAPNP_MEM_BASE_15_8, B1(r->base)); 795 796 isapnp_write_reg(sc, 797 isapnp_mem_range[i] + ISAPNP_MEM_LRANGE_23_16, 798 B2(r->length)); 799 isapnp_write_reg(sc, 800 isapnp_mem_range[i] + ISAPNP_MEM_LRANGE_15_8, 801 B1(r->length)); 802 } 803 804 for (i = 0; i < sizeof(isapnp_irq_range); i++) { 805 u_char v; 806 807 if (i < ipa->ipa_nirq) 808 p = &ipa->ipa_irq[i]; 809 else 810 p = &pz; 811 812 isapnp_write_reg(sc, 813 isapnp_irq_range[i] + ISAPNP_IRQ_NUMBER, p->num); 814 815 switch (p->flags) { 816 case ISAPNP_IRQTYPE_LEVEL_PLUS: 817 v = ISAPNP_IRQ_LEVEL|ISAPNP_IRQ_HIGH; 818 break; 819 820 case ISAPNP_IRQTYPE_EDGE_PLUS: 821 v = ISAPNP_IRQ_HIGH; 822 break; 823 824 case ISAPNP_IRQTYPE_LEVEL_MINUS: 825 v = ISAPNP_IRQ_LEVEL; 826 break; 827 828 default: 829 case ISAPNP_IRQTYPE_EDGE_MINUS: 830 v = 0; 831 break; 832 } 833 isapnp_write_reg(sc, 834 isapnp_irq_range[i] + ISAPNP_IRQ_CONTROL, v); 835 } 836 837 for (i = 0; i < sizeof(isapnp_drq_range); i++) { 838 u_char v; 839 840 if (i < ipa->ipa_ndrq) 841 v = ipa->ipa_drq[i].num; 842 else 843 v = 4; 844 845 isapnp_write_reg(sc, isapnp_drq_range[i], v); 846 } 847 848 for (i = 0; i < sizeof(isapnp_mem32_range); i++) { 849 if (i < ipa->ipa_nmem32) 850 r = &ipa->ipa_mem32[i]; 851 else 852 r = &rz; 853 854 isapnp_write_reg(sc, 855 isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_31_24, 856 B3(r->base)); 857 isapnp_write_reg(sc, 858 isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_23_16, 859 B2(r->base)); 860 isapnp_write_reg(sc, 861 isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_15_8, 862 B1(r->base)); 863 isapnp_write_reg(sc, 864 isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_7_0, 865 B0(r->base)); 866 867 isapnp_write_reg(sc, 868 isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_31_24, 869 B3(r->length)); 870 isapnp_write_reg(sc, 871 isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_23_16, 872 B2(r->length)); 873 isapnp_write_reg(sc, 874 isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_15_8, 875 B1(r->length)); 876 isapnp_write_reg(sc, 877 isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_7_0, 878 B0(r->length)); 879 } 880 } 881 882 883 /* isapnp_match(): 884 * Probe routine 885 */ 886 static int 887 isapnp_match(parent, match, aux) 888 struct device *parent; 889 struct cfdata *match; 890 void *aux; 891 { 892 struct isapnp_softc sc; 893 struct isa_attach_args *ia = aux; 894 struct isapnp_probe_cookie *ipc; 895 896 /* 897 * Ensure we only probe ISA PnP once; we don't actually consume 898 * bus resources, so we have to prevent being cloned forever. 899 */ 900 for (ipc = LIST_FIRST(&isapnp_probes); ipc != NULL; 901 ipc = LIST_NEXT(ipc, ipc_link)) 902 if (ipc->ipc_parent == parent) 903 return (0); 904 905 ipc = malloc(sizeof(*ipc), M_DEVBUF, M_NOWAIT); 906 if (ipc == NULL) 907 panic("isapnp_match: can't allocate probe cookie"); 908 909 ipc->ipc_parent = parent; 910 LIST_INSERT_HEAD(&isapnp_probes, ipc, ipc_link); 911 912 sc.sc_iot = ia->ia_iot; 913 (void) strcpy(sc.sc_dev.dv_xname, "(isapnp probe)"); 914 915 if (isapnp_map(&sc)) 916 return 0; 917 918 isapnp_unmap(&sc); 919 920 /* 921 * We always match. We must let all legacy ISA devices map 922 * their address spaces before we look for a read port. 923 */ 924 ia->ia_iobase = ISAPNP_ADDR; 925 ia->ia_iosize = 1; 926 927 return (1); 928 } 929 930 931 /* isapnp_attach 932 * Attach the PnP `bus'. 933 */ 934 static void 935 isapnp_attach(parent, self, aux) 936 struct device *parent, *self; 937 void *aux; 938 { 939 struct isapnp_softc *sc = (struct isapnp_softc *) self; 940 struct isa_attach_args *ia = aux; 941 942 sc->sc_iot = ia->ia_iot; 943 sc->sc_memt = ia->ia_memt; 944 sc->sc_ic = ia->ia_ic; 945 sc->sc_dmat = ia->ia_dmat; 946 sc->sc_ncards = 0; 947 948 printf(": ISA Plug 'n Play device support\n"); 949 950 if (isapnp_map(sc)) { 951 printf("%s: unable to map PnP register\n", 952 sc->sc_dev.dv_xname); 953 return; 954 } 955 956 #ifdef _KERNEL 957 /* 958 * Defer configuration until the rest of the ISA devices have 959 * attached themselves. 960 */ 961 config_defer(self, isapnp_callback); 962 #else 963 isapnp_callback(self); 964 #endif 965 } 966 967 /* isapnp_callback 968 * Find and attach PnP cards. 969 */ 970 void 971 isapnp_callback(self) 972 struct device *self; 973 { 974 struct isapnp_softc *sc = (struct isapnp_softc *)self; 975 struct isapnp_attach_args *ipa, *lpa; 976 int c, d; 977 978 /* 979 * Look for cards. If none are found, we say so and just return. 980 */ 981 if (isapnp_find(sc, 1) == 0) { 982 printf("%s: no ISA Plug 'n Play devices found\n", 983 sc->sc_dev.dv_xname); 984 return; 985 } 986 987 printf("%s: read port 0x%x\n", sc->sc_dev.dv_xname, sc->sc_read_port); 988 989 /* 990 * Now configure all of the cards. 991 */ 992 for (c = 0; c < sc->sc_ncards; c++) { 993 /* Good morning card c */ 994 isapnp_write_reg(sc, ISAPNP_WAKE, c + 1); 995 996 if ((ipa = isapnp_get_resource(sc, c)) == NULL) 997 continue; 998 999 DPRINTF(("Selecting attachments\n")); 1000 for (d = 0; 1001 (lpa = isapnp_bestconfig(sc, &ipa)) != NULL; d++) { 1002 isapnp_write_reg(sc, ISAPNP_LOGICAL_DEV_NUM, d); 1003 isapnp_configure(sc, lpa); 1004 #ifdef DEBUG_ISAPNP 1005 { 1006 struct isapnp_attach_args pa; 1007 1008 isapnp_get_config(sc, &pa); 1009 isapnp_print_config(&pa); 1010 } 1011 #endif 1012 1013 DPRINTF(("%s: configuring <%s, %s, %s, %s>\n", 1014 sc->sc_dev.dv_xname, 1015 lpa->ipa_devident, lpa->ipa_devlogic, 1016 lpa->ipa_devcompat, lpa->ipa_devclass)); 1017 if (lpa->ipa_pref == ISAPNP_DEP_CONFLICTING) { 1018 printf("%s: <%s, %s, %s, %s> ignored; %s\n", 1019 sc->sc_dev.dv_xname, 1020 lpa->ipa_devident, lpa->ipa_devlogic, 1021 lpa->ipa_devcompat, lpa->ipa_devclass, 1022 "resource conflict"); 1023 ISAPNP_FREE(lpa); 1024 continue; 1025 } 1026 1027 lpa->ipa_ic = sc->sc_ic; 1028 lpa->ipa_iot = sc->sc_iot; 1029 lpa->ipa_memt = sc->sc_memt; 1030 lpa->ipa_dmat = sc->sc_dmat; 1031 1032 isapnp_write_reg(sc, ISAPNP_ACTIVATE, 1); 1033 #ifdef _KERNEL 1034 if (config_found_sm(self, lpa, isapnp_print, 1035 isapnp_submatch) == NULL) 1036 isapnp_write_reg(sc, ISAPNP_ACTIVATE, 0); 1037 #else 1038 isapnp_print(lpa, NULL); 1039 printf("\n"); 1040 #endif 1041 ISAPNP_FREE(lpa); 1042 } 1043 isapnp_write_reg(sc, ISAPNP_WAKE, 0); /* Good night cards */ 1044 } 1045 } 1046