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