1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD AND MIT 3 * 4 * Copyright (c) 1999 Doug Rabson 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 /* 29 * Modifications for Intel architecture by Garrett A. Wollman. 30 * Copyright 1998 Massachusetts Institute of Technology 31 * 32 * Permission to use, copy, modify, and distribute this software and 33 * its documentation for any purpose and without fee is hereby 34 * granted, provided that both the above copyright notice and this 35 * permission notice appear in all copies, that both the above 36 * copyright notice and this permission notice appear in all 37 * supporting documentation, and that the name of M.I.T. not be used 38 * in advertising or publicity pertaining to distribution of the 39 * software without specific, written prior permission. M.I.T. makes 40 * no representations about the suitability of this software for any 41 * purpose. It is provided "as is" without express or implied 42 * warranty. 43 * 44 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 45 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 46 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 47 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 48 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 49 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 50 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 51 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 52 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 53 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 54 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 55 * SUCH DAMAGE. 56 */ 57 58 /* 59 * Parts of the ISA bus implementation common to all architectures. 60 */ 61 62 #include <sys/cdefs.h> 63 __FBSDID("$FreeBSD$"); 64 65 #include "opt_isa.h" 66 67 #include <sys/param.h> 68 #include <sys/systm.h> 69 #include <sys/kernel.h> 70 #include <sys/bus.h> 71 #include <sys/endian.h> 72 #include <sys/malloc.h> 73 #include <sys/module.h> 74 #include <machine/bus.h> 75 #include <sys/rman.h> 76 #include <sys/sysctl.h> 77 78 #include <machine/resource.h> 79 80 #include <isa/isavar.h> 81 #include <isa/isa_common.h> 82 83 static int isa_print_child(device_t bus, device_t dev); 84 85 static MALLOC_DEFINE(M_ISADEV, "isadev", "ISA device"); 86 87 static int isa_running; 88 89 /* 90 * At 'probe' time, we add all the devices which we know about to the 91 * bus. The generic attach routine will probe and attach them if they 92 * are alive. 93 */ 94 static int 95 isa_probe(device_t dev) 96 { 97 device_set_desc(dev, "ISA bus"); 98 isa_init(dev); /* Allow machdep code to initialise */ 99 return (0); 100 } 101 102 extern device_t isa_bus_device; 103 104 static int 105 isa_attach(device_t dev) 106 { 107 /* 108 * Arrange for isa_probe_children(dev) to be called later. XXX 109 */ 110 isa_bus_device = dev; 111 return (0); 112 } 113 114 /* 115 * Find a working set of memory regions for a child using the ranges 116 * in *config and return the regions in *result. Returns non-zero if 117 * a set of ranges was found. 118 */ 119 static int 120 isa_find_memory(device_t child, struct isa_config *config, 121 struct isa_config *result) 122 { 123 int success, i; 124 struct resource *res[ISA_NMEM]; 125 126 /* 127 * First clear out any existing resource definitions. 128 */ 129 for (i = 0; i < ISA_NMEM; i++) { 130 bus_delete_resource(child, SYS_RES_MEMORY, i); 131 res[i] = NULL; 132 } 133 134 success = 1; 135 result->ic_nmem = config->ic_nmem; 136 for (i = 0; i < config->ic_nmem; i++) { 137 uint32_t start, end, size, align; 138 139 size = config->ic_mem[i].ir_size; 140 141 /* the PnP device may have a null resource as filler */ 142 if (size == 0) { 143 result->ic_mem[i].ir_start = 0; 144 result->ic_mem[i].ir_end = 0; 145 result->ic_mem[i].ir_size = 0; 146 result->ic_mem[i].ir_align = 0; 147 continue; 148 } 149 150 for (start = config->ic_mem[i].ir_start, 151 end = config->ic_mem[i].ir_end, 152 align = config->ic_mem[i].ir_align; 153 start + size - 1 <= end && start + size > start; 154 start += MAX(align, 1)) { 155 bus_set_resource(child, SYS_RES_MEMORY, i, 156 start, size); 157 res[i] = bus_alloc_resource_any(child, 158 SYS_RES_MEMORY, &i, 159 rman_make_alignment_flags(align) /* !RF_ACTIVE */); 160 if (res[i]) { 161 result->ic_mem[i].ir_start = start; 162 result->ic_mem[i].ir_end = start + size - 1; 163 result->ic_mem[i].ir_size = size; 164 result->ic_mem[i].ir_align = align; 165 break; 166 } 167 } 168 169 /* 170 * If we didn't find a place for memory range i, then 171 * give up now. 172 */ 173 if (!res[i]) { 174 success = 0; 175 break; 176 } 177 } 178 179 for (i = 0; i < ISA_NMEM; i++) { 180 if (res[i]) 181 bus_release_resource(child, SYS_RES_MEMORY, 182 i, res[i]); 183 } 184 185 return (success); 186 } 187 188 /* 189 * Find a working set of port regions for a child using the ranges 190 * in *config and return the regions in *result. Returns non-zero if 191 * a set of ranges was found. 192 */ 193 static int 194 isa_find_port(device_t child, struct isa_config *config, 195 struct isa_config *result) 196 { 197 int success, i; 198 struct resource *res[ISA_NPORT]; 199 200 /* 201 * First clear out any existing resource definitions. 202 */ 203 for (i = 0; i < ISA_NPORT; i++) { 204 bus_delete_resource(child, SYS_RES_IOPORT, i); 205 res[i] = NULL; 206 } 207 208 success = 1; 209 result->ic_nport = config->ic_nport; 210 for (i = 0; i < config->ic_nport; i++) { 211 uint32_t start, end, size, align; 212 213 size = config->ic_port[i].ir_size; 214 215 /* the PnP device may have a null resource as filler */ 216 if (size == 0) { 217 result->ic_port[i].ir_start = 0; 218 result->ic_port[i].ir_end = 0; 219 result->ic_port[i].ir_size = 0; 220 result->ic_port[i].ir_align = 0; 221 continue; 222 } 223 224 for (start = config->ic_port[i].ir_start, 225 end = config->ic_port[i].ir_end, 226 align = config->ic_port[i].ir_align; 227 start + size - 1 <= end; 228 start += align) { 229 bus_set_resource(child, SYS_RES_IOPORT, i, 230 start, size); 231 res[i] = bus_alloc_resource_any(child, 232 SYS_RES_IOPORT, &i, 233 rman_make_alignment_flags(align) /* !RF_ACTIVE */); 234 if (res[i]) { 235 result->ic_port[i].ir_start = start; 236 result->ic_port[i].ir_end = start + size - 1; 237 result->ic_port[i].ir_size = size; 238 result->ic_port[i].ir_align = align; 239 break; 240 } 241 } 242 243 /* 244 * If we didn't find a place for port range i, then 245 * give up now. 246 */ 247 if (!res[i]) { 248 success = 0; 249 break; 250 } 251 } 252 253 for (i = 0; i < ISA_NPORT; i++) { 254 if (res[i]) 255 bus_release_resource(child, SYS_RES_IOPORT, 256 i, res[i]); 257 } 258 259 return success; 260 } 261 262 /* 263 * Return the index of the first bit in the mask (or -1 if mask is empty. 264 */ 265 static int 266 find_first_bit(uint32_t mask) 267 { 268 return (ffs(mask) - 1); 269 } 270 271 /* 272 * Return the index of the next bit in the mask, or -1 if there are no more. 273 */ 274 static int 275 find_next_bit(uint32_t mask, int bit) 276 { 277 bit++; 278 while (bit < 32 && !(mask & (1 << bit))) 279 bit++; 280 if (bit != 32) 281 return (bit); 282 return (-1); 283 } 284 285 /* 286 * Find a working set of irqs for a child using the masks in *config 287 * and return the regions in *result. Returns non-zero if a set of 288 * irqs was found. 289 */ 290 static int 291 isa_find_irq(device_t child, struct isa_config *config, 292 struct isa_config *result) 293 { 294 int success, i; 295 struct resource *res[ISA_NIRQ]; 296 297 /* 298 * First clear out any existing resource definitions. 299 */ 300 for (i = 0; i < ISA_NIRQ; i++) { 301 bus_delete_resource(child, SYS_RES_IRQ, i); 302 res[i] = NULL; 303 } 304 305 success = 1; 306 result->ic_nirq = config->ic_nirq; 307 for (i = 0; i < config->ic_nirq; i++) { 308 uint32_t mask = config->ic_irqmask[i]; 309 int irq; 310 311 /* the PnP device may have a null resource as filler */ 312 if (mask == 0) { 313 result->ic_irqmask[i] = 0; 314 continue; 315 } 316 317 for (irq = find_first_bit(mask); 318 irq != -1; 319 irq = find_next_bit(mask, irq)) { 320 bus_set_resource(child, SYS_RES_IRQ, i, 321 irq, 1); 322 res[i] = bus_alloc_resource_any(child, 323 SYS_RES_IRQ, &i, 324 0 /* !RF_ACTIVE */ ); 325 if (res[i]) { 326 result->ic_irqmask[i] = (1 << irq); 327 break; 328 } 329 } 330 331 /* 332 * If we didn't find a place for irq range i, then 333 * give up now. 334 */ 335 if (!res[i]) { 336 success = 0; 337 break; 338 } 339 } 340 341 for (i = 0; i < ISA_NIRQ; i++) { 342 if (res[i]) 343 bus_release_resource(child, SYS_RES_IRQ, 344 i, res[i]); 345 } 346 347 return (success); 348 } 349 350 /* 351 * Find a working set of drqs for a child using the masks in *config 352 * and return the regions in *result. Returns non-zero if a set of 353 * drqs was found. 354 */ 355 static int 356 isa_find_drq(device_t child, struct isa_config *config, 357 struct isa_config *result) 358 { 359 int success, i; 360 struct resource *res[ISA_NDRQ]; 361 362 /* 363 * First clear out any existing resource definitions. 364 */ 365 for (i = 0; i < ISA_NDRQ; i++) { 366 bus_delete_resource(child, SYS_RES_DRQ, i); 367 res[i] = NULL; 368 } 369 370 success = 1; 371 result->ic_ndrq = config->ic_ndrq; 372 for (i = 0; i < config->ic_ndrq; i++) { 373 uint32_t mask = config->ic_drqmask[i]; 374 int drq; 375 376 /* the PnP device may have a null resource as filler */ 377 if (mask == 0) { 378 result->ic_drqmask[i] = 0; 379 continue; 380 } 381 382 for (drq = find_first_bit(mask); 383 drq != -1; 384 drq = find_next_bit(mask, drq)) { 385 bus_set_resource(child, SYS_RES_DRQ, i, 386 drq, 1); 387 res[i] = bus_alloc_resource_any(child, 388 SYS_RES_DRQ, &i, 389 0 /* !RF_ACTIVE */); 390 if (res[i]) { 391 result->ic_drqmask[i] = (1 << drq); 392 break; 393 } 394 } 395 396 /* 397 * If we didn't find a place for drq range i, then 398 * give up now. 399 */ 400 if (!res[i]) { 401 success = 0; 402 break; 403 } 404 } 405 406 for (i = 0; i < ISA_NDRQ; i++) { 407 if (res[i]) 408 bus_release_resource(child, SYS_RES_DRQ, 409 i, res[i]); 410 } 411 412 return (success); 413 } 414 415 /* 416 * Attempt to find a working set of resources for a device. Return 417 * non-zero if a working configuration is found. 418 */ 419 static int 420 isa_assign_resources(device_t child) 421 { 422 struct isa_device *idev = DEVTOISA(child); 423 struct isa_config_entry *ice; 424 struct isa_config *cfg; 425 const char *reason; 426 427 reason = "Empty ISA id_configs"; 428 cfg = malloc(sizeof(struct isa_config), M_TEMP, M_NOWAIT|M_ZERO); 429 if (cfg == NULL) 430 return(0); 431 TAILQ_FOREACH(ice, &idev->id_configs, ice_link) { 432 reason = "memory"; 433 if (!isa_find_memory(child, &ice->ice_config, cfg)) 434 continue; 435 reason = "port"; 436 if (!isa_find_port(child, &ice->ice_config, cfg)) 437 continue; 438 reason = "irq"; 439 if (!isa_find_irq(child, &ice->ice_config, cfg)) 440 continue; 441 reason = "drq"; 442 if (!isa_find_drq(child, &ice->ice_config, cfg)) 443 continue; 444 445 /* 446 * A working configuration was found enable the device 447 * with this configuration. 448 */ 449 reason = "no callback"; 450 if (idev->id_config_cb) { 451 idev->id_config_cb(idev->id_config_arg, 452 cfg, 1); 453 free(cfg, M_TEMP); 454 return (1); 455 } 456 } 457 458 /* 459 * Disable the device. 460 */ 461 bus_print_child_header(device_get_parent(child), child); 462 printf(" can't assign resources (%s)\n", reason); 463 if (bootverbose) 464 isa_print_child(device_get_parent(child), child); 465 bzero(cfg, sizeof (*cfg)); 466 if (idev->id_config_cb) 467 idev->id_config_cb(idev->id_config_arg, cfg, 0); 468 device_disable(child); 469 470 free(cfg, M_TEMP); 471 return (0); 472 } 473 474 /* 475 * Claim any unallocated resources to keep other devices from using 476 * them. 477 */ 478 static void 479 isa_claim_resources(device_t dev, device_t child) 480 { 481 struct isa_device *idev = DEVTOISA(child); 482 struct resource_list *rl = &idev->id_resources; 483 struct resource_list_entry *rle; 484 int rid; 485 486 STAILQ_FOREACH(rle, rl, link) { 487 if (!rle->res) { 488 rid = rle->rid; 489 resource_list_alloc(rl, dev, child, rle->type, &rid, 490 0, ~0, 1, 0); 491 } 492 } 493 } 494 495 /* 496 * Called after other devices have initialised to probe for isa devices. 497 */ 498 void 499 isa_probe_children(device_t dev) 500 { 501 struct isa_device *idev; 502 device_t *children, child; 503 struct isa_config *cfg; 504 int nchildren, i, err; 505 506 /* 507 * Create all the non-hinted children by calling drivers' 508 * identify methods. 509 */ 510 bus_generic_probe(dev); 511 512 if (device_get_children(dev, &children, &nchildren)) 513 return; 514 515 /* 516 * First disable all pnp devices so that they don't get 517 * matched by legacy probes. 518 */ 519 if (bootverbose) 520 printf("isa_probe_children: disabling PnP devices\n"); 521 522 cfg = malloc(sizeof(*cfg), M_TEMP, M_NOWAIT|M_ZERO); 523 if (cfg == NULL) { 524 free(children, M_TEMP); 525 return; 526 } 527 528 for (i = 0; i < nchildren; i++) { 529 idev = DEVTOISA(children[i]); 530 531 bzero(cfg, sizeof(*cfg)); 532 if (idev->id_config_cb) 533 idev->id_config_cb(idev->id_config_arg, cfg, 0); 534 } 535 536 free(cfg, M_TEMP); 537 538 /* 539 * Next, probe all the PnP BIOS devices so they can subsume any 540 * hints. 541 */ 542 for (i = 0; i < nchildren; i++) { 543 child = children[i]; 544 idev = DEVTOISA(child); 545 546 if (idev->id_order > ISA_ORDER_PNPBIOS) 547 continue; 548 if (!TAILQ_EMPTY(&idev->id_configs) && 549 !isa_assign_resources(child)) 550 continue; 551 552 if (device_probe_and_attach(child) == 0) 553 isa_claim_resources(dev, child); 554 } 555 free(children, M_TEMP); 556 557 /* 558 * Next, enumerate hinted devices and probe all non-pnp devices so 559 * that they claim their resources first. 560 */ 561 bus_enumerate_hinted_children(dev); 562 if (device_get_children(dev, &children, &nchildren)) 563 return; 564 if (bootverbose) 565 printf("isa_probe_children: probing non-PnP devices\n"); 566 for (i = 0; i < nchildren; i++) { 567 child = children[i]; 568 idev = DEVTOISA(child); 569 570 if (device_is_attached(child) || 571 !TAILQ_EMPTY(&idev->id_configs)) 572 continue; 573 574 err = device_probe_and_attach(child); 575 if (err == 0 && idev->id_vendorid == 0 && 576 strcmp(kern_ident, "GENERIC") == 0) 577 device_printf(child, 578 "non-PNP ISA device will be removed from GENERIC in FreeBSD 12."); 579 } 580 581 /* 582 * Finally assign resource to pnp devices and probe them. 583 */ 584 if (bootverbose) 585 printf("isa_probe_children: probing PnP devices\n"); 586 for (i = 0; i < nchildren; i++) { 587 child = children[i]; 588 idev = DEVTOISA(child); 589 590 if (device_is_attached(child) || TAILQ_EMPTY(&idev->id_configs)) 591 continue; 592 593 if (isa_assign_resources(child)) { 594 device_probe_and_attach(child); 595 isa_claim_resources(dev, child); 596 } 597 } 598 599 free(children, M_TEMP); 600 601 isa_running = 1; 602 } 603 604 /* 605 * Add a new child with default ivars. 606 */ 607 static device_t 608 isa_add_child(device_t dev, u_int order, const char *name, int unit) 609 { 610 device_t child; 611 struct isa_device *idev; 612 613 child = device_add_child_ordered(dev, order, name, unit); 614 if (child == NULL) 615 return (child); 616 617 idev = malloc(sizeof(struct isa_device), M_ISADEV, M_NOWAIT | M_ZERO); 618 if (!idev) 619 return (0); 620 621 resource_list_init(&idev->id_resources); 622 TAILQ_INIT(&idev->id_configs); 623 idev->id_order = order; 624 625 device_set_ivars(child, idev); 626 627 return (child); 628 } 629 630 static int 631 isa_print_all_resources(device_t dev) 632 { 633 struct isa_device *idev = DEVTOISA(dev); 634 struct resource_list *rl = &idev->id_resources; 635 int retval = 0; 636 637 if (STAILQ_FIRST(rl) || device_get_flags(dev)) 638 retval += printf(" at"); 639 640 retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#jx"); 641 retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#jx"); 642 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd"); 643 retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%jd"); 644 if (device_get_flags(dev)) 645 retval += printf(" flags %#x", device_get_flags(dev)); 646 if (idev->id_vendorid) 647 retval += printf(" pnpid %s", pnp_eisaformat(idev->id_vendorid)); 648 649 return (retval); 650 } 651 652 static int 653 isa_print_child(device_t bus, device_t dev) 654 { 655 int retval = 0; 656 657 retval += bus_print_child_header(bus, dev); 658 retval += isa_print_all_resources(dev); 659 retval += bus_print_child_footer(bus, dev); 660 661 return (retval); 662 } 663 664 static void 665 isa_probe_nomatch(device_t dev, device_t child) 666 { 667 if (bootverbose) { 668 bus_print_child_header(dev, child); 669 printf(" failed to probe"); 670 isa_print_all_resources(child); 671 bus_print_child_footer(dev, child); 672 } 673 674 return; 675 } 676 677 static int 678 isa_read_ivar(device_t bus, device_t dev, int index, uintptr_t * result) 679 { 680 struct isa_device* idev = DEVTOISA(dev); 681 struct resource_list *rl = &idev->id_resources; 682 struct resource_list_entry *rle; 683 684 switch (index) { 685 case ISA_IVAR_PORT_0: 686 rle = resource_list_find(rl, SYS_RES_IOPORT, 0); 687 if (rle) 688 *result = rle->start; 689 else 690 *result = -1; 691 break; 692 693 case ISA_IVAR_PORT_1: 694 rle = resource_list_find(rl, SYS_RES_IOPORT, 1); 695 if (rle) 696 *result = rle->start; 697 else 698 *result = -1; 699 break; 700 701 case ISA_IVAR_PORTSIZE_0: 702 rle = resource_list_find(rl, SYS_RES_IOPORT, 0); 703 if (rle) 704 *result = rle->count; 705 else 706 *result = 0; 707 break; 708 709 case ISA_IVAR_PORTSIZE_1: 710 rle = resource_list_find(rl, SYS_RES_IOPORT, 1); 711 if (rle) 712 *result = rle->count; 713 else 714 *result = 0; 715 break; 716 717 case ISA_IVAR_MADDR_0: 718 rle = resource_list_find(rl, SYS_RES_MEMORY, 0); 719 if (rle) 720 *result = rle->start; 721 else 722 *result = -1; 723 break; 724 725 case ISA_IVAR_MADDR_1: 726 rle = resource_list_find(rl, SYS_RES_MEMORY, 1); 727 if (rle) 728 *result = rle->start; 729 else 730 *result = -1; 731 break; 732 733 case ISA_IVAR_MEMSIZE_0: 734 rle = resource_list_find(rl, SYS_RES_MEMORY, 0); 735 if (rle) 736 *result = rle->count; 737 else 738 *result = 0; 739 break; 740 741 case ISA_IVAR_MEMSIZE_1: 742 rle = resource_list_find(rl, SYS_RES_MEMORY, 1); 743 if (rle) 744 *result = rle->count; 745 else 746 *result = 0; 747 break; 748 749 case ISA_IVAR_IRQ_0: 750 rle = resource_list_find(rl, SYS_RES_IRQ, 0); 751 if (rle) 752 *result = rle->start; 753 else 754 *result = -1; 755 break; 756 757 case ISA_IVAR_IRQ_1: 758 rle = resource_list_find(rl, SYS_RES_IRQ, 1); 759 if (rle) 760 *result = rle->start; 761 else 762 *result = -1; 763 break; 764 765 case ISA_IVAR_DRQ_0: 766 rle = resource_list_find(rl, SYS_RES_DRQ, 0); 767 if (rle) 768 *result = rle->start; 769 else 770 *result = -1; 771 break; 772 773 case ISA_IVAR_DRQ_1: 774 rle = resource_list_find(rl, SYS_RES_DRQ, 1); 775 if (rle) 776 *result = rle->start; 777 else 778 *result = -1; 779 break; 780 781 case ISA_IVAR_VENDORID: 782 *result = idev->id_vendorid; 783 break; 784 785 case ISA_IVAR_SERIAL: 786 *result = idev->id_serial; 787 break; 788 789 case ISA_IVAR_LOGICALID: 790 *result = idev->id_logicalid; 791 break; 792 793 case ISA_IVAR_COMPATID: 794 *result = idev->id_compatid; 795 break; 796 797 case ISA_IVAR_CONFIGATTR: 798 *result = idev->id_config_attr; 799 break; 800 801 case ISA_IVAR_PNP_CSN: 802 *result = idev->id_pnp_csn; 803 break; 804 805 case ISA_IVAR_PNP_LDN: 806 *result = idev->id_pnp_ldn; 807 break; 808 809 case ISA_IVAR_PNPBIOS_HANDLE: 810 *result = idev->id_pnpbios_handle; 811 break; 812 813 default: 814 return (ENOENT); 815 } 816 817 return (0); 818 } 819 820 static int 821 isa_write_ivar(device_t bus, device_t dev, int index, uintptr_t value) 822 { 823 struct isa_device* idev = DEVTOISA(dev); 824 825 switch (index) { 826 case ISA_IVAR_PORT_0: 827 case ISA_IVAR_PORT_1: 828 case ISA_IVAR_PORTSIZE_0: 829 case ISA_IVAR_PORTSIZE_1: 830 case ISA_IVAR_MADDR_0: 831 case ISA_IVAR_MADDR_1: 832 case ISA_IVAR_MEMSIZE_0: 833 case ISA_IVAR_MEMSIZE_1: 834 case ISA_IVAR_IRQ_0: 835 case ISA_IVAR_IRQ_1: 836 case ISA_IVAR_DRQ_0: 837 case ISA_IVAR_DRQ_1: 838 return (EINVAL); 839 840 case ISA_IVAR_VENDORID: 841 idev->id_vendorid = value; 842 break; 843 844 case ISA_IVAR_SERIAL: 845 idev->id_serial = value; 846 break; 847 848 case ISA_IVAR_LOGICALID: 849 idev->id_logicalid = value; 850 break; 851 852 case ISA_IVAR_COMPATID: 853 idev->id_compatid = value; 854 break; 855 856 case ISA_IVAR_CONFIGATTR: 857 idev->id_config_attr = value; 858 break; 859 860 default: 861 return (ENOENT); 862 } 863 864 return (0); 865 } 866 867 /* 868 * Free any resources which the driver missed or which we were holding for 869 * it (see isa_probe_children). 870 */ 871 static void 872 isa_child_detached(device_t dev, device_t child) 873 { 874 struct isa_device* idev = DEVTOISA(child); 875 876 if (TAILQ_FIRST(&idev->id_configs)) 877 isa_claim_resources(dev, child); 878 } 879 880 static void 881 isa_driver_added(device_t dev, driver_t *driver) 882 { 883 device_t *children; 884 int nchildren, i; 885 886 /* 887 * Don't do anything if drivers are dynamically 888 * added during autoconfiguration (cf. ymf724). 889 * since that would end up calling identify 890 * twice. 891 */ 892 if (!isa_running) 893 return; 894 895 DEVICE_IDENTIFY(driver, dev); 896 if (device_get_children(dev, &children, &nchildren)) 897 return; 898 899 for (i = 0; i < nchildren; i++) { 900 device_t child = children[i]; 901 struct isa_device *idev = DEVTOISA(child); 902 struct resource_list *rl = &idev->id_resources; 903 struct resource_list_entry *rle; 904 905 if (device_get_state(child) != DS_NOTPRESENT) 906 continue; 907 if (!device_is_enabled(child)) 908 continue; 909 910 /* 911 * Free resources which we were holding on behalf of 912 * the device. 913 */ 914 STAILQ_FOREACH(rle, &idev->id_resources, link) { 915 if (rle->res) 916 resource_list_release(rl, dev, child, 917 rle->type, 918 rle->rid, 919 rle->res); 920 } 921 922 if (TAILQ_FIRST(&idev->id_configs)) 923 if (!isa_assign_resources(child)) 924 continue; 925 926 device_probe_and_attach(child); 927 928 if (TAILQ_FIRST(&idev->id_configs)) 929 isa_claim_resources(dev, child); 930 } 931 932 free(children, M_TEMP); 933 } 934 935 static int 936 isa_set_resource(device_t dev, device_t child, int type, int rid, 937 rman_res_t start, rman_res_t count) 938 { 939 struct isa_device* idev = DEVTOISA(child); 940 struct resource_list *rl = &idev->id_resources; 941 942 if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY 943 && type != SYS_RES_IRQ && type != SYS_RES_DRQ) 944 return (EINVAL); 945 if (rid < 0) 946 return (EINVAL); 947 if (type == SYS_RES_IOPORT && rid >= ISA_NPORT) 948 return (EINVAL); 949 if (type == SYS_RES_MEMORY && rid >= ISA_NMEM) 950 return (EINVAL); 951 if (type == SYS_RES_IRQ && rid >= ISA_NIRQ) 952 return (EINVAL); 953 if (type == SYS_RES_DRQ && rid >= ISA_NDRQ) 954 return (EINVAL); 955 956 resource_list_add(rl, type, rid, start, start + count - 1, count); 957 958 return (0); 959 } 960 961 static struct resource_list * 962 isa_get_resource_list (device_t dev, device_t child) 963 { 964 struct isa_device* idev = DEVTOISA(child); 965 struct resource_list *rl = &idev->id_resources; 966 967 if (!rl) 968 return (NULL); 969 970 return (rl); 971 } 972 973 static int 974 isa_add_config(device_t dev, device_t child, int priority, 975 struct isa_config *config) 976 { 977 struct isa_device* idev = DEVTOISA(child); 978 struct isa_config_entry *newice, *ice; 979 980 newice = malloc(sizeof *ice, M_DEVBUF, M_NOWAIT); 981 if (!newice) 982 return (ENOMEM); 983 984 newice->ice_priority = priority; 985 newice->ice_config = *config; 986 987 TAILQ_FOREACH(ice, &idev->id_configs, ice_link) { 988 if (ice->ice_priority > priority) 989 break; 990 } 991 if (ice) 992 TAILQ_INSERT_BEFORE(ice, newice, ice_link); 993 else 994 TAILQ_INSERT_TAIL(&idev->id_configs, newice, ice_link); 995 996 return (0); 997 } 998 999 static void 1000 isa_set_config_callback(device_t dev, device_t child, isa_config_cb *fn, 1001 void *arg) 1002 { 1003 struct isa_device* idev = DEVTOISA(child); 1004 1005 idev->id_config_cb = fn; 1006 idev->id_config_arg = arg; 1007 } 1008 1009 static int 1010 isa_pnp_probe(device_t dev, device_t child, struct isa_pnp_id *ids) 1011 { 1012 struct isa_device* idev = DEVTOISA(child); 1013 1014 if (!idev->id_vendorid) 1015 return (ENOENT); 1016 1017 while (ids && ids->ip_id) { 1018 /* 1019 * Really ought to support >1 compat id per device. 1020 */ 1021 if (idev->id_logicalid == ids->ip_id 1022 || idev->id_compatid == ids->ip_id) { 1023 if (ids->ip_desc) 1024 device_set_desc(child, ids->ip_desc); 1025 return (0); 1026 } 1027 ids++; 1028 } 1029 1030 return (ENXIO); 1031 } 1032 1033 static int 1034 isa_child_pnpinfo_str(device_t bus, device_t child, char *buf, 1035 size_t buflen) 1036 { 1037 struct isa_device *idev = DEVTOISA(child); 1038 1039 if (idev->id_vendorid) 1040 snprintf(buf, buflen, "pnpid=%s", 1041 pnp_eisaformat(idev->id_vendorid)); 1042 return (0); 1043 } 1044 1045 static int 1046 isa_child_location_str(device_t bus, device_t child, char *buf, 1047 size_t buflen) 1048 { 1049 #if 0 1050 /* id_pnphandle isn't there yet */ 1051 struct isa_device *idev = DEVTOISA(child); 1052 1053 if (idev->id_vendorid) 1054 snprintf(buf, buflen, "pnphandle=%d", idev->id_pnphandle); 1055 #endif 1056 /* Nothing here yet */ 1057 *buf = '\0'; 1058 return (0); 1059 } 1060 1061 static device_method_t isa_methods[] = { 1062 /* Device interface */ 1063 DEVMETHOD(device_probe, isa_probe), 1064 DEVMETHOD(device_attach, isa_attach), 1065 DEVMETHOD(device_detach, bus_generic_detach), 1066 DEVMETHOD(device_shutdown, bus_generic_shutdown), 1067 DEVMETHOD(device_suspend, bus_generic_suspend), 1068 DEVMETHOD(device_resume, bus_generic_resume), 1069 1070 /* Bus interface */ 1071 DEVMETHOD(bus_add_child, isa_add_child), 1072 DEVMETHOD(bus_print_child, isa_print_child), 1073 DEVMETHOD(bus_probe_nomatch, isa_probe_nomatch), 1074 DEVMETHOD(bus_read_ivar, isa_read_ivar), 1075 DEVMETHOD(bus_write_ivar, isa_write_ivar), 1076 DEVMETHOD(bus_child_detached, isa_child_detached), 1077 DEVMETHOD(bus_driver_added, isa_driver_added), 1078 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 1079 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 1080 1081 DEVMETHOD(bus_get_resource_list,isa_get_resource_list), 1082 DEVMETHOD(bus_alloc_resource, isa_alloc_resource), 1083 DEVMETHOD(bus_release_resource, isa_release_resource), 1084 DEVMETHOD(bus_set_resource, isa_set_resource), 1085 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 1086 DEVMETHOD(bus_delete_resource, bus_generic_rl_delete_resource), 1087 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 1088 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 1089 DEVMETHOD(bus_child_pnpinfo_str, isa_child_pnpinfo_str), 1090 DEVMETHOD(bus_child_location_str, isa_child_location_str), 1091 DEVMETHOD(bus_hinted_child, isa_hinted_child), 1092 DEVMETHOD(bus_hint_device_unit, isa_hint_device_unit), 1093 1094 /* ISA interface */ 1095 DEVMETHOD(isa_add_config, isa_add_config), 1096 DEVMETHOD(isa_set_config_callback, isa_set_config_callback), 1097 DEVMETHOD(isa_pnp_probe, isa_pnp_probe), 1098 1099 { 0, 0 } 1100 }; 1101 1102 DEFINE_CLASS_0(isa, isa_driver, isa_methods, 0); 1103 1104 devclass_t isa_devclass; 1105 1106 /* 1107 * ISA can be attached to a PCI-ISA bridge, or other locations on some 1108 * platforms. 1109 */ 1110 DRIVER_MODULE(isa, isab, isa_driver, isa_devclass, 0, 0); 1111 DRIVER_MODULE(isa, eisab, isa_driver, isa_devclass, 0, 0); 1112 MODULE_VERSION(isa, 1); 1113 1114 /* 1115 * Code common to ISA bridges. 1116 */ 1117 1118 devclass_t isab_devclass; 1119 1120 int 1121 isab_attach(device_t dev) 1122 { 1123 device_t child; 1124 1125 child = device_add_child(dev, "isa", 0); 1126 if (child != NULL) 1127 return (bus_generic_attach(dev)); 1128 return (ENXIO); 1129 } 1130 1131 char * 1132 pnp_eisaformat(uint32_t id) 1133 { 1134 uint8_t *data; 1135 static char idbuf[8]; 1136 const char hextoascii[] = "0123456789abcdef"; 1137 1138 id = htole32(id); 1139 data = (uint8_t *)&id; 1140 idbuf[0] = '@' + ((data[0] & 0x7c) >> 2); 1141 idbuf[1] = '@' + (((data[0] & 0x3) << 3) + ((data[1] & 0xe0) >> 5)); 1142 idbuf[2] = '@' + (data[1] & 0x1f); 1143 idbuf[3] = hextoascii[(data[2] >> 4)]; 1144 idbuf[4] = hextoascii[(data[2] & 0xf)]; 1145 idbuf[5] = hextoascii[(data[3] >> 4)]; 1146 idbuf[6] = hextoascii[(data[3] & 0xf)]; 1147 idbuf[7] = 0; 1148 return(idbuf); 1149 } 1150