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