sys/bus/isa/pnp.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 | /* * Copyright (c) 1996, Sujal M. Patel * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD: src/sys/isa/pnp.c,v 1.5.2.1 2002/10/14 09:31:09 nyan Exp $ * from: pnp.c,v 1.11 1999/05/06 22:11:19 peter Exp */ #include <sys/param.h> #include <sys/systm.h> #include <sys/kernel.h> #include <sys/module.h> #include <sys/bus.h> #include <sys/malloc.h> #include "isavar.h" #include "pnpreg.h" #include "pnpvar.h" #include <machine/clock.h> typedef struct _pnp_id { u_int32_t vendor_id; u_int32_t serial; u_char checksum; } pnp_id; struct pnp_set_config_arg { int csn; /* Card number to configure */ int ldn; /* Logical device on card */ }; struct pnp_quirk { u_int32_t vendor_id; /* Vendor of the card */ u_int32_t logical_id; /* ID of the device with quirk */ int type; int arg1; int arg2; }; #define PNP_QUIRK_WRITE_REG 1 /* Need to write a pnp register */ #define PNP_QUIRK_EXTRA_IO 2 /* Has extra io ports */ struct pnp_quirk pnp_quirks[] = { /* * The Gravis UltraSound needs register 0xf2 to be set to 0xff * to enable power. * XXX need to know the logical device id. */ { 0x0100561e /* GRV0001 */, 0, PNP_QUIRK_WRITE_REG, 0xf2, 0xff }, /* * An emu8000 does not give us other than the first * port. */ { 0x0100561e /* GRV0001 */, 0, PNP_QUIRK_WRITE_REG, 0xf2, 0xff }, /* * An emu8000 does not give us other than the first * port. */ { 0x26008c0e /* SB16 */, 0x21008c0e, PNP_QUIRK_EXTRA_IO, 0x400, 0x800 }, { 0x42008c0e /* SB32(CTL0042) */, 0x21008c0e, PNP_QUIRK_EXTRA_IO, 0x400, 0x800 }, { 0x44008c0e /* SB32(CTL0044) */, 0x21008c0e, PNP_QUIRK_EXTRA_IO, 0x400, 0x800 }, { 0x49008c0e /* SB32(CTL0049) */, 0x21008c0e, PNP_QUIRK_EXTRA_IO, 0x400, 0x800 }, { 0xf1008c0e /* SB32(CTL00f1) */, 0x21008c0e, PNP_QUIRK_EXTRA_IO, 0x400, 0x800 }, { 0xc1008c0e /* SB64(CTL00c1) */, 0x22008c0e, PNP_QUIRK_EXTRA_IO, 0x400, 0x800 }, { 0xc5008c0e /* SB64(CTL00c5) */, 0x22008c0e, PNP_QUIRK_EXTRA_IO, 0x400, 0x800 }, { 0xe4008c0e /* SB64(CTL00e4) */, 0x22008c0e, PNP_QUIRK_EXTRA_IO, 0x400, 0x800 }, { 0 } }; /* The READ_DATA port that we are using currently */ static int pnp_rd_port; static void pnp_send_initiation_key(void); static int pnp_get_serial(pnp_id *p); static int pnp_isolation_protocol(device_t parent); char * pnp_eisaformat(u_int32_t id) { u_int8_t *data = (u_int8_t *) &id; static char idbuf[8]; const char hextoascii[] = "0123456789abcdef"; idbuf[0] = '@' + ((data[0] & 0x7c) >> 2); idbuf[1] = '@' + (((data[0] & 0x3) << 3) + ((data[1] & 0xe0) >> 5)); idbuf[2] = '@' + (data[1] & 0x1f); idbuf[3] = hextoascii[(data[2] >> 4)]; idbuf[4] = hextoascii[(data[2] & 0xf)]; idbuf[5] = hextoascii[(data[3] >> 4)]; idbuf[6] = hextoascii[(data[3] & 0xf)]; idbuf[7] = 0; return(idbuf); } static void pnp_write(int d, u_char r) { outb (_PNP_ADDRESS, d); outb (_PNP_WRITE_DATA, r); } #if 0 static u_char pnp_read(int d) { outb (_PNP_ADDRESS, d); return (inb(3 | (pnp_rd_port <<2))); } #endif /* * Send Initiation LFSR as described in "Plug and Play ISA Specification", * Intel May 94. */ static void pnp_send_initiation_key(void) { int cur, i; /* Reset the LSFR */ outb(_PNP_ADDRESS, 0); outb(_PNP_ADDRESS, 0); /* yes, we do need it twice! */ cur = 0x6a; outb(_PNP_ADDRESS, cur); for (i = 1; i < 32; i++) { cur = (cur >> 1) | (((cur ^ (cur >> 1)) << 7) & 0xff); outb(_PNP_ADDRESS, cur); } } /* * Get the device's serial number. Returns 1 if the serial is valid. */ static int pnp_get_serial(pnp_id *p) { int i, bit, valid = 0, sum = 0x6a; u_char *data = (u_char *)p; bzero(data, sizeof(char) * 9); outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION); for (i = 0; i < 72; i++) { bit = inb((pnp_rd_port << 2) | 0x3) == 0x55; DELAY(250); /* Delay 250 usec */ /* Can't Short Circuit the next evaluation, so 'and' is last */ bit = (inb((pnp_rd_port << 2) | 0x3) == 0xaa) && bit; DELAY(250); /* Delay 250 usec */ valid = valid || bit; if (i < 64) sum = (sum >> 1) | (((sum ^ (sum >> 1) ^ bit) << 7) & 0xff); data[i / 8] = (data[i / 8] >> 1) | (bit ? 0x80 : 0); } valid = valid && (data[8] == sum); return valid; } /* * Fill's the buffer with resource info from the device. * Returns the number of characters read. */ static int pnp_get_resource_info(u_char *buffer, int len) { int i, j, count; u_char temp; count = 0; for (i = 0; i < len; i++) { outb(_PNP_ADDRESS, PNP_STATUS); for (j = 0; j < 100; j++) { if ((inb((pnp_rd_port << 2) | 0x3)) & 0x1) break; DELAY(1); } if (j == 100) { kprintf("PnP device failed to report resource data\n"); return count; } outb(_PNP_ADDRESS, PNP_RESOURCE_DATA); temp = inb((pnp_rd_port << 2) | 0x3); if (buffer != NULL) buffer[i] = temp; count++; } return count; } /* * This function is called after the bus has assigned resource * locations for a logical device. */ static void pnp_set_config(void *arg, struct isa_config *config, int enable) { int csn = ((struct pnp_set_config_arg *) arg)->csn; int ldn = ((struct pnp_set_config_arg *) arg)->ldn; int i; /* * First put all cards into Sleep state with the initiation * key, then put our card into Config state. */ pnp_send_initiation_key(); pnp_write(PNP_WAKE, csn); /* * Select our logical device so that we can program it. */ pnp_write(PNP_SET_LDN, ldn); /* * Now program the resources. */ for (i = 0; i < config->ic_nmem; i++) { u_int32_t start = config->ic_mem[i].ir_start; u_int32_t size = config->ic_mem[i].ir_size; if (start & 0xff) panic("pnp_set_config: bogus memory assignment"); pnp_write(PNP_MEM_BASE_HIGH(i), (start >> 16) & 0xff); pnp_write(PNP_MEM_BASE_LOW(i), (start >> 8) & 0xff); pnp_write(PNP_MEM_RANGE_HIGH(i), (size >> 16) & 0xff); pnp_write(PNP_MEM_RANGE_LOW(i), (size >> 8) & 0xff); } for (; i < ISA_NMEM; i++) { pnp_write(PNP_MEM_BASE_HIGH(i), 0); pnp_write(PNP_MEM_BASE_LOW(i), 0); pnp_write(PNP_MEM_RANGE_HIGH(i), 0); pnp_write(PNP_MEM_RANGE_LOW(i), 0); } for (i = 0; i < config->ic_nport; i++) { u_int32_t start = config->ic_port[i].ir_start; pnp_write(PNP_IO_BASE_HIGH(i), (start >> 8) & 0xff); pnp_write(PNP_IO_BASE_LOW(i), (start >> 0) & 0xff); } for (; i < ISA_NPORT; i++) { pnp_write(PNP_IO_BASE_HIGH(i), 0); pnp_write(PNP_IO_BASE_LOW(i), 0); } for (i = 0; i < config->ic_nirq; i++) { int irq = ffs(config->ic_irqmask[i]) - 1; pnp_write(PNP_IRQ_LEVEL(i), irq); pnp_write(PNP_IRQ_TYPE(i), 2); /* XXX */ } for (; i < ISA_NIRQ; i++) { /* * IRQ 0 is not a valid interrupt selection and * represents no interrupt selection. */ pnp_write(PNP_IRQ_LEVEL(i), 0); } for (i = 0; i < config->ic_ndrq; i++) { int drq = ffs(config->ic_drqmask[i]) - 1; pnp_write(PNP_DMA_CHANNEL(i), drq); } for (; i < ISA_NDRQ; i++) { /* * DMA channel 4, the cascade channel is used to * indicate no DMA channel is active. */ pnp_write(PNP_DMA_CHANNEL(i), 4); } pnp_write(PNP_ACTIVATE, enable ? 1 : 0); /* * Wake everyone up again, we are finished. */ pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY); } /* * Process quirks for a logical device.. The card must be in Config state. */ void pnp_check_quirks(u_int32_t vendor_id, u_int32_t logical_id, int ldn, struct isa_config *config) { struct pnp_quirk *qp; for (qp = &pnp_quirks[0]; qp->vendor_id; qp++) { if (qp->vendor_id == vendor_id && (qp->logical_id == 0 || qp->logical_id == logical_id)) { switch (qp->type) { case PNP_QUIRK_WRITE_REG: pnp_write(PNP_SET_LDN, ldn); pnp_write(qp->arg1, qp->arg2); break; case PNP_QUIRK_EXTRA_IO: if (config == NULL) break; if (qp->arg1 != 0) { config->ic_nport++; config->ic_port[config->ic_nport - 1] = config->ic_port[0]; config->ic_port[config->ic_nport - 1].ir_start += qp->arg1; config->ic_port[config->ic_nport - 1].ir_end += qp->arg1; } if (qp->arg2 != 0) { config->ic_nport++; config->ic_port[config->ic_nport - 1] = config->ic_port[0]; config->ic_port[config->ic_nport - 1].ir_start += qp->arg2; config->ic_port[config->ic_nport - 1].ir_end += qp->arg2; } break; } } } } /* * Scan Resource Data for Logical Devices. * * This function exits as soon as it gets an error reading *ANY* * Resource Data or it reaches the end of Resource Data. In the first * case the return value will be TRUE, FALSE otherwise. */ static int pnp_create_devices(device_t parent, pnp_id *p, int csn, u_char *resources, int len) { u_char tag, *resp, *resinfo, *startres = NULL; int large_len, scanning = len, retval = FALSE; u_int32_t logical_id; device_t dev = NULL; int ldn = 0; struct pnp_set_config_arg *csnldn; char buf[100]; char *desc = NULL; resp = resources; while (scanning > 0) { tag = *resp++; scanning--; if (PNP_RES_TYPE(tag) != 0) { /* Large resource */ if (scanning < 2) { scanning = 0; continue; } large_len = resp[0] + (resp[1] << 8); resp += 2; if (scanning < large_len) { scanning = 0; continue; } resinfo = resp; resp += large_len; scanning -= large_len; if (PNP_LRES_NUM(tag) == PNP_TAG_ID_ANSI) { if (large_len > sizeof(buf) - 1) large_len = sizeof(buf) - 1; bcopy(resinfo, buf, large_len); /* * Trim trailing spaces. */ while (buf[large_len-1] == ' ') large_len--; buf[large_len] = '\0'; desc = buf; if (dev) device_set_desc_copy(dev, desc); continue; } continue; } /* Small resource */ if (scanning < PNP_SRES_LEN(tag)) { scanning = 0; continue; } resinfo = resp; resp += PNP_SRES_LEN(tag); scanning -= PNP_SRES_LEN(tag); switch (PNP_SRES_NUM(tag)) { case PNP_TAG_LOGICAL_DEVICE: /* * Parse the resources for the previous * logical device (if any). */ if (startres) { pnp_parse_resources(dev, startres, resinfo - startres - 1, ldn); dev = NULL; startres = NULL; } /* * A new logical device. Scan for end of * resources. */ bcopy(resinfo, &logical_id, 4); pnp_check_quirks(p->vendor_id, logical_id, ldn, NULL); dev = BUS_ADD_CHILD(parent, parent, ISA_ORDER_PNP, NULL, -1); if (desc) device_set_desc_copy(dev, desc); isa_set_vendorid(dev, p->vendor_id); isa_set_serial(dev, p->serial); isa_set_logicalid(dev, logical_id); csnldn = kmalloc(sizeof *csnldn, M_DEVBUF, M_WAITOK); csnldn->csn = csn; csnldn->ldn = ldn; ISA_SET_CONFIG_CALLBACK(parent, dev, pnp_set_config, csnldn); ldn++; startres = resp; break; case PNP_TAG_END: if (!startres) { device_printf(parent, "malformed resources\n"); scanning = 0; break; } pnp_parse_resources(dev, startres, resinfo - startres - 1, ldn); dev = NULL; startres = NULL; scanning = 0; break; default: /* Skip this resource */ break; } } return retval; } /* * Read 'amount' bytes of resources from the card, allocating memory * as needed. If a buffer is already available, it should be passed in * '*resourcesp' and its length in '*spacep'. The number of resource * bytes already in the buffer should be passed in '*lenp'. The memory * allocated will be returned in '*resourcesp' with its size and the * number of bytes of resources in '*spacep' and '*lenp' respectively. */ static int pnp_read_bytes(int amount, u_char **resourcesp, int *spacep, int *lenp) { u_char *resources = *resourcesp; u_char *newres; int space = *spacep; int len = *lenp; if (space == 0) { space = 1024; resources = kmalloc(space, M_TEMP, M_WAITOK); } if (len + amount > space) { int extra = 1024; while (len + amount > space + extra) extra += 1024; newres = kmalloc(space + extra, M_TEMP, M_WAITOK); bcopy(resources, newres, len); kfree(resources, M_TEMP); resources = newres; space += extra; } if (pnp_get_resource_info(resources + len, amount) != amount) return EINVAL; len += amount; *resourcesp = resources; *spacep = space; *lenp = len; return 0; } /* * Read all resources from the card, allocating memory as needed. If a * buffer is already available, it should be passed in '*resourcesp' * and its length in '*spacep'. The memory allocated will be returned * in '*resourcesp' with its size and the number of bytes of resources * in '*spacep' and '*lenp' respectively. */ static int pnp_read_resources(u_char **resourcesp, int *spacep, int *lenp) { u_char *resources = *resourcesp; int space = *spacep; int len = 0; int error, done; u_char tag; error = 0; done = 0; while (!done) { error = pnp_read_bytes(1, &resources, &space, &len); if (error) goto out; tag = resources[len-1]; if (PNP_RES_TYPE(tag) == 0) { /* * Small resource, read contents. */ error = pnp_read_bytes(PNP_SRES_LEN(tag), &resources, &space, &len); if (error) goto out; if (PNP_SRES_NUM(tag) == PNP_TAG_END) done = 1; } else { /* * Large resource, read length and contents. */ error = pnp_read_bytes(2, &resources, &space, &len); if (error) goto out; error = pnp_read_bytes(resources[len-2] + (resources[len-1] << 8), &resources, &space, &len); if (error) goto out; } } out: *resourcesp = resources; *spacep = space; *lenp = len; return error; } /* * Run the isolation protocol. Use pnp_rd_port as the READ_DATA port * value (caller should try multiple READ_DATA locations before giving * up). Upon exiting, all cards are aware that they should use * pnp_rd_port as the READ_DATA port. * * In the first pass, a csn is assigned to each board and pnp_id's * are saved to an array, pnp_devices. In the second pass, each * card is woken up and the device configuration is called. */ static int pnp_isolation_protocol(device_t parent) { int csn; pnp_id id; int found = 0, len; u_char *resources = NULL; int space = 0; int error; /* * Put all cards into the Sleep state so that we can clear * their CSNs. */ pnp_send_initiation_key(); /* * Clear the CSN for all cards. */ pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_RESET_CSN); /* * Move all cards to the Isolation state. */ pnp_write(PNP_WAKE, 0); /* * Tell them where the read point is going to be this time. */ pnp_write(PNP_SET_RD_DATA, pnp_rd_port); for (csn = 1; csn < PNP_MAX_CARDS; csn++) { /* * Start the serial isolation protocol. */ outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION); DELAY(1000); /* Delay 1 msec */ if (pnp_get_serial(&id)) { /* * We have read the id from a card * successfully. The card which won the * isolation protocol will be in Isolation * mode and all others will be in Sleep. * Program the CSN of the isolated card * (taking it to Config state) and read its * resources, creating devices as we find * logical devices on the card. */ pnp_write(PNP_SET_CSN, csn); error = pnp_read_resources(&resources, &space, &len); if (error) break; pnp_create_devices(parent, &id, csn, resources, len); found++; } else break; /* * Put this card back to the Sleep state and * simultaneously move all cards which don't have a * CSN yet to Isolation state. */ pnp_write(PNP_WAKE, 0); } /* * Unless we have chosen the wrong read port, all cards will * be in Sleep state. Put them back into WaitForKey for * now. Their resources will be programmed later. */ pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY); /* * Cleanup. */ if (resources) kfree(resources, M_TEMP); return found; } /* * pnp_identify() * * autoconfiguration of pnp devices. This routine just runs the * isolation protocol over several ports, until one is successful. * * may be called more than once ? * */ static int pnp_identify(driver_t *driver, device_t parent) { int num_pnp_devs; /* * We do not support rescanning PNP devices, just return * success (leave the previously scanned devices intact). */ if (device_get_state(parent) == DS_ATTACHED) return (0); if (device_get_state(parent) == DS_INPROGRESS) return (0); #if 0 if (pnp_ldn_overrides[0].csn == 0) { if (bootverbose) kprintf("Initializing PnP override table\n"); bzero (pnp_ldn_overrides, sizeof(pnp_ldn_overrides)); pnp_ldn_overrides[0].csn = 255 ; } #endif /* Try various READ_DATA ports from 0x203-0x3ff */ for (pnp_rd_port = 0x80; (pnp_rd_port < 0xff); pnp_rd_port += 0x10) { if (bootverbose) kprintf("Trying Read_Port at %x\n", (pnp_rd_port << 2) | 0x3); num_pnp_devs = pnp_isolation_protocol(parent); if (num_pnp_devs) break; } return (num_pnp_devs ? 0 : ENXIO); } /* * This causes pnp_identify() to be called for any attached ISA bus in * the system. */ static device_method_t pnp_methods[] = { /* Device interface */ DEVMETHOD(device_identify, pnp_identify), DEVMETHOD_END }; static driver_t pnp_driver = { "pnp", pnp_methods, 1, /* no softc */ }; static devclass_t pnp_devclass; DRIVER_MODULE(pnp, isa, pnp_driver, pnp_devclass, NULL, NULL); |