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