2 * Copyright (c) 1999 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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.6 2005/04/30 23:04:21 swildner Exp $
30 * Modifications for Intel architecture by Garrett A. Wollman.
31 * Copyright 1998 Massachusetts Institute of Technology
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
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
60 * Parts of the ISA bus implementation common to all architectures.
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/kernel.h>
67 #include <sys/malloc.h>
68 #include <sys/module.h>
69 #include <machine/bus.h>
72 #include <machine/resource.h>
75 #include "isa_common.h"
77 static int isa_print_child (device_t, device_t);
79 MALLOC_DEFINE(M_ISADEV, "isadev", "ISA device");
81 static devclass_t isa_devclass;
82 static int isa_running;
85 * At 'probe' time, we add all the devices which we know about to the
86 * bus. The generic attach routine will probe and attach them if they
90 isa_probe(device_t dev)
92 device_set_desc(dev, "ISA bus");
93 isa_init(); /* Allow machdep code to initialise */
97 extern device_t isa_bus_device;
100 isa_attach(device_t dev)
103 * Arrange for isa_probe_children(dev) to be called later. XXX
105 isa_bus_device = dev;
110 * Find a working set of memory regions for a child using the ranges
111 * in *config and return the regions in *result. Returns non-zero if
112 * a set of ranges was found.
115 isa_find_memory(device_t child,
116 struct isa_config *config,
117 struct isa_config *result)
120 struct resource *res[ISA_NMEM];
123 * First clear out any existing resource definitions.
125 for (i = 0; i < ISA_NMEM; i++) {
126 bus_delete_resource(child, SYS_RES_MEMORY, i);
131 result->ic_nmem = config->ic_nmem;
132 for (i = 0; i < config->ic_nmem; i++) {
133 u_int32_t start, end, size, align;
134 for (start = config->ic_mem[i].ir_start,
135 end = config->ic_mem[i].ir_end,
136 size = config->ic_mem[i].ir_size,
137 align = config->ic_mem[i].ir_align;
138 start + size - 1 <= end;
140 bus_set_resource(child, SYS_RES_MEMORY, i,
142 res[i] = bus_alloc_resource(child,
144 0, ~0, 1, 0 /* !RF_ACTIVE */);
146 result->ic_mem[i].ir_start = start;
147 result->ic_mem[i].ir_end = start + size - 1;
148 result->ic_mem[i].ir_size = size;
149 result->ic_mem[i].ir_align = align;
155 * If we didn't find a place for memory range i, then
164 for (i = 0; i < ISA_NMEM; i++) {
166 bus_release_resource(child, SYS_RES_MEMORY,
174 * Find a working set of port regions for a child using the ranges
175 * in *config and return the regions in *result. Returns non-zero if
176 * a set of ranges was found.
179 isa_find_port(device_t child,
180 struct isa_config *config,
181 struct isa_config *result)
184 struct resource *res[ISA_NPORT];
187 * First clear out any existing resource definitions.
189 for (i = 0; i < ISA_NPORT; i++) {
190 bus_delete_resource(child, SYS_RES_IOPORT, i);
195 result->ic_nport = config->ic_nport;
196 for (i = 0; i < config->ic_nport; i++) {
197 u_int32_t start, end, size, align;
198 for (start = config->ic_port[i].ir_start,
199 end = config->ic_port[i].ir_end,
200 size = config->ic_port[i].ir_size,
201 align = config->ic_port[i].ir_align;
202 start + size - 1 <= end;
204 bus_set_resource(child, SYS_RES_IOPORT, i,
206 res[i] = bus_alloc_resource(child,
208 0, ~0, 1, 0 /* !RF_ACTIVE */);
210 result->ic_port[i].ir_start = start;
211 result->ic_port[i].ir_end = start + size - 1;
212 result->ic_port[i].ir_size = size;
213 result->ic_port[i].ir_align = align;
219 * If we didn't find a place for port range i, then
228 for (i = 0; i < ISA_NPORT; i++) {
230 bus_release_resource(child, SYS_RES_IOPORT,
238 * Return the index of the first bit in the mask (or -1 if mask is empty.
241 find_first_bit(u_int32_t mask)
243 return ffs(mask) - 1;
247 * Return the index of the next bit in the mask, or -1 if there are no more.
250 find_next_bit(u_int32_t mask, int bit)
253 while (bit < 32 && !(mask & (1 << bit)))
261 * Find a working set of irqs for a child using the masks in *config
262 * and return the regions in *result. Returns non-zero if a set of
266 isa_find_irq(device_t child,
267 struct isa_config *config,
268 struct isa_config *result)
271 struct resource *res[ISA_NIRQ];
274 * First clear out any existing resource definitions.
276 for (i = 0; i < ISA_NIRQ; i++) {
277 bus_delete_resource(child, SYS_RES_IRQ, i);
282 result->ic_nirq = config->ic_nirq;
283 for (i = 0; i < config->ic_nirq; i++) {
284 u_int32_t mask = config->ic_irqmask[i];
286 for (irq = find_first_bit(mask);
288 irq = find_next_bit(mask, irq)) {
289 bus_set_resource(child, SYS_RES_IRQ, i,
291 res[i] = bus_alloc_resource(child,
293 0, ~0, 1, 0 /* !RF_ACTIVE */ );
295 result->ic_irqmask[i] = (1 << irq);
301 * If we didn't find a place for irq range i, then
310 for (i = 0; i < ISA_NIRQ; i++) {
312 bus_release_resource(child, SYS_RES_IRQ,
320 * Find a working set of drqs for a child using the masks in *config
321 * and return the regions in *result. Returns non-zero if a set of
325 isa_find_drq(device_t child,
326 struct isa_config *config,
327 struct isa_config *result)
330 struct resource *res[ISA_NDRQ];
333 * First clear out any existing resource definitions.
335 for (i = 0; i < ISA_NDRQ; i++) {
336 bus_delete_resource(child, SYS_RES_DRQ, i);
341 result->ic_ndrq = config->ic_ndrq;
342 for (i = 0; i < config->ic_ndrq; i++) {
343 u_int32_t mask = config->ic_drqmask[i];
345 for (drq = find_first_bit(mask);
347 drq = find_next_bit(mask, drq)) {
348 bus_set_resource(child, SYS_RES_DRQ, i,
350 res[i] = bus_alloc_resource(child,
352 0, ~0, 1, 0 /* !RF_ACTIVE */);
354 result->ic_drqmask[i] = (1 << drq);
360 * If we didn't find a place for drq range i, then
369 for (i = 0; i < ISA_NDRQ; i++) {
371 bus_release_resource(child, SYS_RES_DRQ,
379 * Attempt to find a working set of resources for a device. Return
380 * non-zero if a working configuration is found.
383 isa_assign_resources(device_t child)
385 struct isa_device *idev = DEVTOISA(child);
386 struct isa_config_entry *ice;
387 struct isa_config config;
389 bzero(&config, sizeof config);
390 TAILQ_FOREACH(ice, &idev->id_configs, ice_link) {
391 if (!isa_find_memory(child, &ice->ice_config, &config))
393 if (!isa_find_port(child, &ice->ice_config, &config))
395 if (!isa_find_irq(child, &ice->ice_config, &config))
397 if (!isa_find_drq(child, &ice->ice_config, &config))
401 * A working configuration was found enable the device
402 * with this configuration.
404 if (idev->id_config_cb) {
405 idev->id_config_cb(idev->id_config_arg,
412 * Disable the device.
414 bus_print_child_header(device_get_parent(child), child);
415 printf(" can't assign resources\n");
417 isa_print_child(device_get_parent(child), child);
418 bzero(&config, sizeof config);
419 if (idev->id_config_cb)
420 idev->id_config_cb(idev->id_config_arg, &config, 0);
421 device_disable(child);
427 * Called after other devices have initialised to probe for isa devices.
430 isa_probe_children(device_t dev)
436 * Create all the children by calling driver's identify methods.
438 bus_generic_probe(dev);
440 if (device_get_children(dev, &children, &nchildren))
444 * First disable all pnp devices so that they don't get
445 * matched by legacy probes.
448 printf("isa_probe_children: disabling PnP devices\n");
449 for (i = 0; i < nchildren; i++) {
450 device_t child = children[i];
451 struct isa_device *idev = DEVTOISA(child);
452 struct isa_config config;
454 bzero(&config, sizeof config);
455 if (idev->id_config_cb)
456 idev->id_config_cb(idev->id_config_arg, &config, 0);
460 * Next probe all non-pnp devices so that they claim their
464 printf("isa_probe_children: probing non-PnP devices\n");
465 for (i = 0; i < nchildren; i++) {
466 device_t child = children[i];
467 struct isa_device *idev = DEVTOISA(child);
469 if (TAILQ_FIRST(&idev->id_configs))
472 device_probe_and_attach(child);
476 * Finally assign resource to pnp devices and probe them.
479 printf("isa_probe_children: probing PnP devices\n");
480 for (i = 0; i < nchildren; i++) {
481 device_t child = children[i];
482 struct isa_device* idev = DEVTOISA(child);
484 if (!TAILQ_FIRST(&idev->id_configs))
487 if (isa_assign_resources(child)) {
488 struct resource_list *rl = &idev->id_resources;
489 struct resource_list_entry *rle;
491 device_probe_and_attach(child);
494 * Claim any unallocated resources to keep other
495 * devices from using them.
497 SLIST_FOREACH(rle, rl, link) {
500 resource_list_alloc(rl, dev, child,
510 free(children, M_TEMP);
516 * Add a new child with default ivars.
519 isa_add_child(device_t dev, int order, const char *name, int unit)
522 struct isa_device *idev;
524 idev = malloc(sizeof(struct isa_device), M_ISADEV, M_WAITOK | M_ZERO);
526 resource_list_init(&idev->id_resources);
527 TAILQ_INIT(&idev->id_configs);
529 child = device_add_child_ordered(dev, order, name, unit);
530 device_set_ivars(child, idev);
536 isa_print_resources(struct resource_list *rl, const char *name, int type,
537 int count, const char *format)
539 struct resource_list_entry *rle;
544 for (i = 0; i < count; i++) {
545 rle = resource_list_find(rl, type, i);
548 retval += printf(" %s ", name);
549 else if (printed > 0)
550 retval += printf(",");
552 retval += printf(format, rle->start);
553 if (rle->count > 1) {
554 retval += printf("-");
555 retval += printf(format,
556 rle->start + rle->count - 1);
559 /* check the first few regardless */
567 isa_print_all_resources(device_t dev)
569 struct isa_device *idev = DEVTOISA(dev);
570 struct resource_list *rl = &idev->id_resources;
573 if (SLIST_FIRST(rl) || device_get_flags(dev))
574 retval += printf(" at");
576 retval += isa_print_resources(rl, "port", SYS_RES_IOPORT,
578 retval += isa_print_resources(rl, "iomem", SYS_RES_MEMORY,
580 retval += isa_print_resources(rl, "irq", SYS_RES_IRQ,
582 retval += isa_print_resources(rl, "drq", SYS_RES_DRQ,
584 if (device_get_flags(dev))
585 retval += printf(" flags %#x", device_get_flags(dev));
591 isa_print_child(device_t bus, device_t dev)
595 retval += bus_print_child_header(bus, dev);
596 retval += isa_print_all_resources(dev);
597 retval += bus_print_child_footer(bus, dev);
603 isa_probe_nomatch(device_t dev, device_t child)
606 bus_print_child_header(dev, child);
607 printf(" failed to probe");
608 isa_print_all_resources(child);
609 bus_print_child_footer(dev, child);
616 isa_read_ivar(device_t bus, device_t dev, int index, uintptr_t * result)
618 struct isa_device* idev = DEVTOISA(dev);
619 struct resource_list *rl = &idev->id_resources;
620 struct resource_list_entry *rle;
623 case ISA_IVAR_PORT_0:
624 rle = resource_list_find(rl, SYS_RES_IOPORT, 0);
626 *result = rle->start;
631 case ISA_IVAR_PORT_1:
632 rle = resource_list_find(rl, SYS_RES_IOPORT, 1);
634 *result = rle->start;
639 case ISA_IVAR_PORTSIZE_0:
640 rle = resource_list_find(rl, SYS_RES_IOPORT, 0);
642 *result = rle->count;
647 case ISA_IVAR_PORTSIZE_1:
648 rle = resource_list_find(rl, SYS_RES_IOPORT, 1);
650 *result = rle->count;
655 case ISA_IVAR_MADDR_0:
656 rle = resource_list_find(rl, SYS_RES_MEMORY, 0);
658 *result = rle->start;
663 case ISA_IVAR_MADDR_1:
664 rle = resource_list_find(rl, SYS_RES_MEMORY, 1);
666 *result = rle->start;
671 case ISA_IVAR_MSIZE_0:
672 rle = resource_list_find(rl, SYS_RES_MEMORY, 0);
674 *result = rle->count;
679 case ISA_IVAR_MSIZE_1:
680 rle = resource_list_find(rl, SYS_RES_MEMORY, 1);
682 *result = rle->count;
688 rle = resource_list_find(rl, SYS_RES_IRQ, 0);
690 *result = rle->start;
696 rle = resource_list_find(rl, SYS_RES_IRQ, 1);
698 *result = rle->start;
704 rle = resource_list_find(rl, SYS_RES_DRQ, 0);
706 *result = rle->start;
712 rle = resource_list_find(rl, SYS_RES_DRQ, 1);
714 *result = rle->start;
719 case ISA_IVAR_VENDORID:
720 *result = idev->id_vendorid;
723 case ISA_IVAR_SERIAL:
724 *result = idev->id_serial;
727 case ISA_IVAR_LOGICALID:
728 *result = idev->id_logicalid;
731 case ISA_IVAR_COMPATID:
732 *result = idev->id_compatid;
743 isa_write_ivar(device_t bus, device_t dev,
744 int index, uintptr_t value)
746 struct isa_device* idev = DEVTOISA(dev);
749 case ISA_IVAR_PORT_0:
750 case ISA_IVAR_PORT_1:
751 case ISA_IVAR_PORTSIZE_0:
752 case ISA_IVAR_PORTSIZE_1:
753 case ISA_IVAR_MADDR_0:
754 case ISA_IVAR_MADDR_1:
755 case ISA_IVAR_MSIZE_0:
756 case ISA_IVAR_MSIZE_1:
763 case ISA_IVAR_VENDORID:
764 idev->id_vendorid = value;
767 case ISA_IVAR_SERIAL:
768 idev->id_serial = value;
771 case ISA_IVAR_LOGICALID:
772 idev->id_logicalid = value;
775 case ISA_IVAR_COMPATID:
776 idev->id_compatid = value;
787 * Free any resources which the driver missed or which we were holding for
788 * it (see isa_probe_children).
791 isa_child_detached(device_t dev, device_t child)
793 struct isa_device* idev = DEVTOISA(child);
794 struct resource_list *rl = &idev->id_resources;
795 struct resource_list_entry *rle;
797 if (TAILQ_FIRST(&idev->id_configs)) {
799 * Claim any unallocated resources to keep other
800 * devices from using them.
802 SLIST_FOREACH(rle, rl, link) {
805 resource_list_alloc(rl, dev, child,
814 isa_driver_added(device_t dev, driver_t *driver)
820 * Don't do anything if drivers are dynamically
821 * added during autoconfiguration (cf. ymf724).
822 * since that would end up calling identify
828 DEVICE_IDENTIFY(driver, dev);
829 if (device_get_children(dev, &children, &nchildren))
832 for (i = 0; i < nchildren; i++) {
833 device_t child = children[i];
834 struct isa_device *idev = DEVTOISA(child);
835 struct resource_list *rl = &idev->id_resources;
836 struct resource_list_entry *rle;
838 if (device_get_state(child) != DS_NOTPRESENT)
840 if (!device_is_enabled(child))
844 * Free resources which we were holding on behalf of
847 SLIST_FOREACH(rle, &idev->id_resources, link) {
849 resource_list_release(rl, dev, child,
855 if (TAILQ_FIRST(&idev->id_configs))
856 if (!isa_assign_resources(child))
859 device_probe_and_attach(child);
861 if (TAILQ_FIRST(&idev->id_configs)) {
863 * Claim any unallocated resources to keep other
864 * devices from using them.
866 SLIST_FOREACH(rle, rl, link) {
869 resource_list_alloc(rl, dev, child,
879 free(children, M_TEMP);
883 isa_set_resource(device_t dev, device_t child, int type, int rid,
884 u_long start, u_long count)
886 struct isa_device* idev = DEVTOISA(child);
887 struct resource_list *rl = &idev->id_resources;
889 if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
890 && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
894 if (type == SYS_RES_IOPORT && rid >= ISA_NPORT)
896 if (type == SYS_RES_MEMORY && rid >= ISA_NMEM)
898 if (type == SYS_RES_IRQ && rid >= ISA_NIRQ)
900 if (type == SYS_RES_DRQ && rid >= ISA_NDRQ)
903 resource_list_add(rl, type, rid, start, start + count - 1, count);
909 isa_get_resource(device_t dev, device_t child, int type, int rid,
910 u_long *startp, u_long *countp)
912 struct isa_device* idev = DEVTOISA(child);
913 struct resource_list *rl = &idev->id_resources;
914 struct resource_list_entry *rle;
916 rle = resource_list_find(rl, type, rid);
921 *startp = rle->start;
923 *countp = rle->count;
929 isa_delete_resource(device_t dev, device_t child, int type, int rid)
931 struct isa_device* idev = DEVTOISA(child);
932 struct resource_list *rl = &idev->id_resources;
933 resource_list_delete(rl, type, rid);
937 isa_add_config(device_t dev, device_t child,
938 int priority, struct isa_config *config)
940 struct isa_device* idev = DEVTOISA(child);
941 struct isa_config_entry *newice, *ice;
943 newice = malloc(sizeof *ice, M_DEVBUF, M_WAITOK);
944 newice->ice_priority = priority;
945 newice->ice_config = *config;
947 TAILQ_FOREACH(ice, &idev->id_configs, ice_link) {
948 if (ice->ice_priority > priority)
952 TAILQ_INSERT_BEFORE(ice, newice, ice_link);
954 TAILQ_INSERT_TAIL(&idev->id_configs, newice, ice_link);
960 isa_set_config_callback(device_t dev, device_t child,
961 isa_config_cb *fn, void *arg)
963 struct isa_device* idev = DEVTOISA(child);
965 idev->id_config_cb = fn;
966 idev->id_config_arg = arg;
970 isa_pnp_probe(device_t dev, device_t child, struct isa_pnp_id *ids)
972 struct isa_device* idev = DEVTOISA(child);
974 if (!idev->id_vendorid)
979 * Really ought to support >1 compat id per device.
981 if (idev->id_logicalid == ids->ip_id
982 || idev->id_compatid == ids->ip_id) {
984 device_set_desc(child, ids->ip_desc);
993 static device_method_t isa_methods[] = {
994 /* Device interface */
995 DEVMETHOD(device_probe, isa_probe),
996 DEVMETHOD(device_attach, isa_attach),
997 DEVMETHOD(device_detach, bus_generic_detach),
998 DEVMETHOD(device_shutdown, bus_generic_shutdown),
999 DEVMETHOD(device_suspend, bus_generic_suspend),
1000 DEVMETHOD(device_resume, bus_generic_resume),
1003 DEVMETHOD(bus_add_child, isa_add_child),
1004 DEVMETHOD(bus_print_child, isa_print_child),
1005 DEVMETHOD(bus_probe_nomatch, isa_probe_nomatch),
1006 DEVMETHOD(bus_read_ivar, isa_read_ivar),
1007 DEVMETHOD(bus_write_ivar, isa_write_ivar),
1008 DEVMETHOD(bus_child_detached, isa_child_detached),
1009 DEVMETHOD(bus_driver_added, isa_driver_added),
1010 DEVMETHOD(bus_alloc_resource, isa_alloc_resource),
1011 DEVMETHOD(bus_release_resource, isa_release_resource),
1012 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
1013 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
1014 DEVMETHOD(bus_setup_intr, isa_setup_intr),
1015 DEVMETHOD(bus_teardown_intr, isa_teardown_intr),
1016 DEVMETHOD(bus_set_resource, isa_set_resource),
1017 DEVMETHOD(bus_get_resource, isa_get_resource),
1018 DEVMETHOD(bus_delete_resource, isa_delete_resource),
1021 DEVMETHOD(isa_add_config, isa_add_config),
1022 DEVMETHOD(isa_set_config_callback, isa_set_config_callback),
1023 DEVMETHOD(isa_pnp_probe, isa_pnp_probe),
1028 static driver_t isa_driver = {
1035 * ISA can be attached to a PCI-ISA bridge or directly to the nexus.
1037 DRIVER_MODULE(isa, isab, isa_driver, isa_devclass, 0, 0);
1039 DRIVER_MODULE(isa, nexus, isa_driver, isa_devclass, 0, 0);