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
29 * Modifications for Intel architecture by Garrett A. Wollman.
30 * Copyright 1998 Massachusetts Institute of Technology
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
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
59 * Parts of the ISA bus implementation common to all architectures.
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/kernel.h>
66 #include <sys/malloc.h>
67 #include <sys/module.h>
68 #include <machine/bus.h>
71 #include <machine/resource.h>
73 #include <isa/isavar.h>
74 #include <isa/isa_common.h>
75 #ifdef __alpha__ /* XXX workaround a stupid warning */
76 #include <alpha/isa/isavar.h>
79 MALLOC_DEFINE(M_ISADEV, "isadev", "ISA device");
81 static devclass_t isa_devclass;
84 * At 'probe' time, we add all the devices which we know about to the
85 * bus. The generic attach routine will probe and attach them if they
89 isa_probe(device_t dev)
91 device_set_desc(dev, "ISA bus");
92 isa_init(); /* Allow machdep code to initialise */
93 return bus_generic_probe(dev);
96 extern device_t isa_bus_device;
99 isa_attach(device_t dev)
102 * Arrange for isa_probe_children(dev) to be called later. XXX
104 isa_bus_device = dev;
109 * Find a working set of memory regions for a child using the ranges
110 * in *config and return the regions in *result. Returns non-zero if
111 * a set of ranges was found.
114 isa_find_memory(device_t child,
115 struct isa_config *config,
116 struct isa_config *result)
118 device_t dev = device_get_parent(child);
120 struct resource *res[ISA_NMEM];
123 * First clear out any existing resource definitions.
125 for (i = 0; i < ISA_NMEM; i++) {
126 ISA_DELETE_RESOURCE(dev, 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 ISA_SET_RESOURCE(dev, child, SYS_RES_MEMORY, i,
142 res[i] = bus_alloc_resource(child,
144 0, ~0, 1, 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)
183 device_t dev = device_get_parent(child);
185 struct resource *res[ISA_NPORT];
188 * First clear out any existing resource definitions.
190 for (i = 0; i < ISA_NPORT; i++) {
191 ISA_DELETE_RESOURCE(dev, child, SYS_RES_IOPORT, i);
196 result->ic_nport = config->ic_nport;
197 for (i = 0; i < config->ic_nport; i++) {
198 u_int32_t start, end, size, align;
199 for (start = config->ic_port[i].ir_start,
200 end = config->ic_port[i].ir_end,
201 size = config->ic_port[i].ir_size,
202 align = config->ic_port[i].ir_align;
203 start + size - 1 <= end;
205 ISA_SET_RESOURCE(dev, child, SYS_RES_IOPORT, i,
207 res[i] = bus_alloc_resource(child,
209 0, ~0, 1, RF_ACTIVE);
211 result->ic_port[i].ir_start = start;
212 result->ic_port[i].ir_end = start + size - 1;
213 result->ic_port[i].ir_size = size;
214 result->ic_port[i].ir_align = align;
220 * If we didn't find a place for port range i, then
229 for (i = 0; i < ISA_NPORT; i++) {
231 bus_release_resource(child, SYS_RES_IOPORT,
239 * Return the index of the first bit in the mask (or -1 if mask is empty.
242 find_first_bit(u_int32_t mask)
244 return ffs(mask) - 1;
248 * Return the index of the next bit in the mask, or -1 if there are no more.
251 find_next_bit(u_int32_t mask, int bit)
254 while (bit < 32 && !(mask & (1 << bit)))
262 * Find a working set of irqs for a child using the masks in *config
263 * and return the regions in *result. Returns non-zero if a set of
267 isa_find_irq(device_t child,
268 struct isa_config *config,
269 struct isa_config *result)
271 device_t dev = device_get_parent(child);
273 struct resource *res[ISA_NIRQ];
276 * First clear out any existing resource definitions.
278 for (i = 0; i < ISA_NIRQ; i++) {
279 ISA_DELETE_RESOURCE(dev, child, SYS_RES_IRQ, i);
284 result->ic_nirq = config->ic_nirq;
285 for (i = 0; i < config->ic_nirq; i++) {
286 u_int32_t mask = config->ic_irqmask[i];
288 for (irq = find_first_bit(mask);
290 irq = find_next_bit(mask, irq)) {
291 ISA_SET_RESOURCE(dev, child, SYS_RES_IRQ, i,
293 res[i] = bus_alloc_resource(child,
295 0, ~0, 1, RF_ACTIVE);
297 result->ic_irqmask[i] = (1 << irq);
303 * If we didn't find a place for irq range i, then
312 for (i = 0; i < ISA_NIRQ; i++) {
314 bus_release_resource(child, SYS_RES_IRQ,
322 * Find a working set of drqs for a child using the masks in *config
323 * and return the regions in *result. Returns non-zero if a set of
327 isa_find_drq(device_t child,
328 struct isa_config *config,
329 struct isa_config *result)
331 device_t dev = device_get_parent(child);
333 struct resource *res[ISA_NDRQ];
336 * First clear out any existing resource definitions.
338 for (i = 0; i < ISA_NDRQ; i++) {
339 ISA_DELETE_RESOURCE(dev, child, SYS_RES_DRQ, i);
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];
348 for (drq = find_first_bit(mask);
350 drq = find_next_bit(mask, drq)) {
351 ISA_SET_RESOURCE(dev, child, SYS_RES_DRQ, i,
353 res[i] = bus_alloc_resource(child,
355 0, ~0, 1, RF_ACTIVE);
357 result->ic_drqmask[i] = (1 << drq);
363 * If we didn't find a place for drq range i, then
372 for (i = 0; i < ISA_NDRQ; i++) {
374 bus_release_resource(child, SYS_RES_DRQ,
382 * Attempt to find a working set of resources for a device. Return
383 * non-zero if a working configuration is found.
386 isa_assign_resources(device_t child)
388 struct isa_device *idev = DEVTOISA(child);
389 struct isa_config_entry *ice;
390 struct isa_config config;
392 bzero(&config, sizeof config);
393 TAILQ_FOREACH(ice, &idev->id_configs, ice_link) {
394 if (!isa_find_memory(child, &ice->ice_config, &config))
396 if (!isa_find_port(child, &ice->ice_config, &config))
398 if (!isa_find_irq(child, &ice->ice_config, &config))
400 if (!isa_find_drq(child, &ice->ice_config, &config))
404 * A working configuration was found enable the device
405 * with this configuration.
407 if (idev->id_config_cb) {
408 idev->id_config_cb(idev->id_config_arg,
415 * Disable the device.
417 bzero(&config, sizeof config);
418 if (idev->id_config_cb)
419 idev->id_config_cb(idev->id_config_arg, &config, 0);
420 device_disable(child);
426 * Called after other devices have initialised to probe for isa devices.
429 isa_probe_children(device_t dev)
434 if (device_get_children(dev, &children, &nchildren))
438 * First disable all pnp devices so that they don't get
439 * matched by legacy probes.
441 for (i = 0; i < nchildren; i++) {
442 device_t child = children[i];
443 struct isa_device *idev = DEVTOISA(child);
444 struct isa_config config;
446 bzero(&config, sizeof config);
447 if (idev->id_config_cb)
448 idev->id_config_cb(idev->id_config_arg, &config, 0);
452 * Next probe all non-pnp devices so that they claim their
455 for (i = 0; i < nchildren; i++) {
456 device_t child = children[i];
457 struct isa_device *idev = DEVTOISA(child);
459 if (TAILQ_FIRST(&idev->id_configs))
462 device_probe_and_attach(child);
466 * Finally assign resource to pnp devices and probe them.
468 for (i = 0; i < nchildren; i++) {
469 device_t child = children[i];
470 struct isa_device* idev = DEVTOISA(child);
472 if (!TAILQ_FIRST(&idev->id_configs))
475 if (isa_assign_resources(child)) {
476 struct resource_list_entry *rle;
478 device_probe_and_attach(child);
481 * Claim any unallocated resources to keep other
482 * devices from using them.
484 SLIST_FOREACH(rle, &idev->id_resources, link) {
487 resource_list_alloc(dev, child,
497 free(children, M_TEMP);
501 * Add a new child with default ivars.
504 isa_add_child(device_t dev, int order, const char *name, int unit)
506 struct isa_device *idev;
508 idev = malloc(sizeof(struct isa_device), M_ISADEV, M_NOWAIT);
511 bzero(idev, sizeof *idev);
513 resource_list_init(&idev->id_resources);
514 TAILQ_INIT(&idev->id_configs);
516 return device_add_child_ordered(dev, order, name, unit, idev);
520 isa_print_resources(struct resource_list *rl, const char *name, int type,
523 struct resource_list_entry *rle;
528 for (i = 0; i < 16; i++) {
529 rle = resource_list_find(rl, type, i);
532 printf(" %s ", name);
533 else if (printed > 0)
536 printf(format, rle->start);
537 if (rle->count > 1) {
539 printf(format, rle->start + rle->count - 1);
542 /* check the first few regardless */
549 isa_print_child(device_t bus, device_t dev)
551 struct isa_device *idev = DEVTOISA(dev);
552 struct resource_list *rl = &idev->id_resources;
555 retval += bus_print_child_header(bus, dev);
557 if (SLIST_FIRST(rl) || device_get_flags(dev))
558 retval += printf(" at");
560 isa_print_resources(rl, "port", SYS_RES_IOPORT, "%#lx");
561 isa_print_resources(rl, "iomem", SYS_RES_MEMORY, "%#lx");
562 isa_print_resources(rl, "irq", SYS_RES_IRQ, "%ld");
563 isa_print_resources(rl, "drq", SYS_RES_DRQ, "%ld");
564 if (device_get_flags(dev))
565 retval += printf(" flags %#x", device_get_flags(dev));
567 retval += bus_print_child_footer(bus, dev);
573 isa_read_ivar(device_t bus, device_t dev, int index, uintptr_t * result)
575 struct isa_device* idev = DEVTOISA(dev);
576 struct resource_list *rl = &idev->id_resources;
577 struct resource_list_entry *rle;
580 case ISA_IVAR_PORT_0:
581 rle = resource_list_find(rl, SYS_RES_IOPORT, 0);
583 *result = rle->start;
588 case ISA_IVAR_PORT_1:
589 rle = resource_list_find(rl, SYS_RES_IOPORT, 1);
591 *result = rle->start;
596 case ISA_IVAR_PORTSIZE_0:
597 rle = resource_list_find(rl, SYS_RES_IOPORT, 0);
599 *result = rle->count;
604 case ISA_IVAR_PORTSIZE_1:
605 rle = resource_list_find(rl, SYS_RES_IOPORT, 1);
607 *result = rle->count;
612 case ISA_IVAR_MADDR_0:
613 rle = resource_list_find(rl, SYS_RES_MEMORY, 0);
615 *result = rle->start;
620 case ISA_IVAR_MADDR_1:
621 rle = resource_list_find(rl, SYS_RES_MEMORY, 1);
623 *result = rle->start;
628 case ISA_IVAR_MSIZE_0:
629 rle = resource_list_find(rl, SYS_RES_MEMORY, 0);
631 *result = rle->count;
636 case ISA_IVAR_MSIZE_1:
637 rle = resource_list_find(rl, SYS_RES_MEMORY, 1);
639 *result = rle->count;
645 rle = resource_list_find(rl, SYS_RES_IRQ, 0);
647 *result = rle->start;
653 rle = resource_list_find(rl, SYS_RES_IRQ, 1);
655 *result = rle->start;
661 rle = resource_list_find(rl, SYS_RES_DRQ, 0);
663 *result = rle->start;
669 rle = resource_list_find(rl, SYS_RES_DRQ, 1);
671 *result = rle->start;
676 case ISA_IVAR_VENDORID:
677 *result = idev->id_vendorid;
680 case ISA_IVAR_SERIAL:
681 *result = idev->id_serial;
684 case ISA_IVAR_LOGICALID:
685 *result = idev->id_logicalid;
688 case ISA_IVAR_COMPATID:
689 *result = idev->id_compatid;
700 isa_write_ivar(device_t bus, device_t dev,
701 int index, uintptr_t value)
703 struct isa_device* idev = DEVTOISA(dev);
706 case ISA_IVAR_PORT_0:
707 case ISA_IVAR_PORT_1:
708 case ISA_IVAR_PORTSIZE_0:
709 case ISA_IVAR_PORTSIZE_1:
710 case ISA_IVAR_MADDR_0:
711 case ISA_IVAR_MADDR_1:
712 case ISA_IVAR_MSIZE_0:
713 case ISA_IVAR_MSIZE_1:
720 case ISA_IVAR_VENDORID:
721 idev->id_vendorid = value;
724 case ISA_IVAR_SERIAL:
725 idev->id_serial = value;
728 case ISA_IVAR_LOGICALID:
729 idev->id_logicalid = value;
732 case ISA_IVAR_COMPATID:
733 idev->id_compatid = value;
744 * Free any resources which the driver missed or which we were holding for
745 * it (see isa_probe_children).
748 isa_child_detached(device_t dev, device_t child)
750 struct isa_device* idev = DEVTOISA(child);
751 struct resource_list_entry *rle;
753 SLIST_FOREACH(rle, &idev->id_resources, link) {
755 resource_list_release(dev, child,
763 isa_set_resource(device_t dev, device_t child, int type, int rid,
764 u_long start, u_long count)
766 struct isa_device* idev = DEVTOISA(child);
767 struct resource_list *rl = &idev->id_resources;
769 if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
770 && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
774 if (type == SYS_RES_IOPORT && rid >= ISA_NPORT)
776 if (type == SYS_RES_MEMORY && rid >= ISA_NMEM)
778 if (type == SYS_RES_IRQ && rid >= ISA_NIRQ)
780 if (type == SYS_RES_DRQ && rid >= ISA_NDRQ)
783 resource_list_add(rl, type, rid, start, start + count - 1, count);
789 isa_get_resource(device_t dev, device_t child, int type, int rid,
790 u_long *startp, u_long *countp)
792 struct isa_device* idev = DEVTOISA(child);
793 struct resource_list *rl = &idev->id_resources;
794 struct resource_list_entry *rle;
796 rle = resource_list_find(rl, type, rid);
800 *startp = rle->start;
801 *countp = rle->count;
807 isa_delete_resource(device_t dev, device_t child, int type, int rid)
809 struct isa_device* idev = DEVTOISA(child);
810 struct resource_list *rl = &idev->id_resources;
811 resource_list_delete(rl, type, rid);
815 isa_add_config(device_t dev, device_t child,
816 int priority, struct isa_config *config)
818 struct isa_device* idev = DEVTOISA(child);
819 struct isa_config_entry *newice, *ice;
821 newice = malloc(sizeof *ice, M_DEVBUF, M_NOWAIT);
825 newice->ice_priority = priority;
826 newice->ice_config = *config;
828 TAILQ_FOREACH(ice, &idev->id_configs, ice_link) {
829 if (ice->ice_priority > priority)
833 TAILQ_INSERT_BEFORE(ice, newice, ice_link);
835 TAILQ_INSERT_TAIL(&idev->id_configs, newice, ice_link);
841 isa_set_config_callback(device_t dev, device_t child,
842 isa_config_cb *fn, void *arg)
844 struct isa_device* idev = DEVTOISA(child);
846 idev->id_config_cb = fn;
847 idev->id_config_arg = arg;
851 isa_pnp_probe(device_t dev, device_t child, struct isa_pnp_id *ids)
853 struct isa_device* idev = DEVTOISA(child);
855 if (!idev->id_vendorid)
860 * Really ought to support >1 compat id per device.
862 if (idev->id_logicalid == ids->ip_id
863 || idev->id_compatid == ids->ip_id) {
865 device_set_desc(child, ids->ip_desc);
874 static device_method_t isa_methods[] = {
875 /* Device interface */
876 DEVMETHOD(device_probe, isa_probe),
877 DEVMETHOD(device_attach, isa_attach),
878 DEVMETHOD(device_detach, bus_generic_detach),
879 DEVMETHOD(device_shutdown, bus_generic_shutdown),
880 DEVMETHOD(device_suspend, bus_generic_suspend),
881 DEVMETHOD(device_resume, bus_generic_resume),
884 DEVMETHOD(bus_add_child, isa_add_child),
885 DEVMETHOD(bus_print_child, isa_print_child),
886 DEVMETHOD(bus_read_ivar, isa_read_ivar),
887 DEVMETHOD(bus_write_ivar, isa_write_ivar),
888 DEVMETHOD(bus_child_detached, isa_child_detached),
889 DEVMETHOD(bus_alloc_resource, isa_alloc_resource),
890 DEVMETHOD(bus_release_resource, isa_release_resource),
891 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
892 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
893 DEVMETHOD(bus_setup_intr, isa_setup_intr),
894 DEVMETHOD(bus_teardown_intr, isa_teardown_intr),
897 DEVMETHOD(isa_set_resource, isa_set_resource),
898 DEVMETHOD(isa_get_resource, isa_get_resource),
899 DEVMETHOD(isa_delete_resource, isa_delete_resource),
900 DEVMETHOD(isa_add_config, isa_add_config),
901 DEVMETHOD(isa_set_config_callback, isa_set_config_callback),
902 DEVMETHOD(isa_pnp_probe, isa_pnp_probe),
907 static driver_t isa_driver = {
914 * ISA can be attached to a PCI-ISA bridge or directly to the nexus.
916 DRIVER_MODULE(isa, isab, isa_driver, isa_devclass, 0, 0);
918 DRIVER_MODULE(isa, nexus, isa_driver, isa_devclass, 0, 0);
922 * A fallback driver for reporting un-matched pnp devices.
926 unknown_probe(device_t dev)
929 * Only match pnp devices.
931 if (isa_get_vendorid(dev) != 0)
937 unknown_attach(device_t dev)
943 unknown_detach(device_t dev)
948 static device_method_t unknown_methods[] = {
949 /* Device interface */
950 DEVMETHOD(device_probe, unknown_probe),
951 DEVMETHOD(device_attach, unknown_attach),
952 DEVMETHOD(device_detach, unknown_detach),
957 static driver_t unknown_driver = {
963 static devclass_t unknown_devclass;
965 DRIVER_MODULE(unknown, isa, unknown_driver, unknown_devclass, 0, 0);