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 probe all non-pnp devices so that they claim their
441 for (i = 0; i < nchildren; i++) {
442 device_t child = children[i];
443 struct isa_device *idev = DEVTOISA(child);
445 if (TAILQ_FIRST(&idev->id_configs))
448 device_probe_and_attach(child);
452 * Next assign resource to pnp devices and probe them.
454 for (i = 0; i < nchildren; i++) {
455 device_t child = children[i];
456 struct isa_device* idev = DEVTOISA(child);
458 if (!TAILQ_FIRST(&idev->id_configs))
461 if (isa_assign_resources(child)) {
462 struct resource_list_entry *rle;
464 device_probe_and_attach(child);
467 * Claim any unallocated resources to keep other
468 * devices from using them.
470 SLIST_FOREACH(rle, &idev->id_resources, link) {
473 resource_list_alloc(dev, child,
483 free(children, M_TEMP);
487 * Add a new child with default ivars.
490 isa_add_child(device_t dev, int order, const char *name, int unit)
492 struct isa_device *idev;
494 idev = malloc(sizeof(struct isa_device), M_ISADEV, M_NOWAIT);
497 bzero(idev, sizeof *idev);
499 resource_list_init(&idev->id_resources);
500 TAILQ_INIT(&idev->id_configs);
502 return device_add_child_ordered(dev, order, name, unit, idev);
506 isa_print_resources(struct resource_list *rl, const char *name, int type,
509 struct resource_list_entry *rle;
514 for (i = 0; i < 16; i++) {
515 rle = resource_list_find(rl, type, i);
518 printf(" %s ", name);
519 else if (printed > 0)
522 printf(format, rle->start);
523 if (rle->count > 1) {
525 printf(format, rle->start + rle->count - 1);
528 /* check the first few regardless */
535 isa_print_child(device_t bus, device_t dev)
537 struct isa_device *idev = DEVTOISA(dev);
538 struct resource_list *rl = &idev->id_resources;
541 retval += bus_print_child_header(bus, dev);
543 if (SLIST_FIRST(rl) || device_get_flags(dev))
544 retval += printf(" at");
546 isa_print_resources(rl, "port", SYS_RES_IOPORT, "%#lx");
547 isa_print_resources(rl, "iomem", SYS_RES_MEMORY, "%#lx");
548 isa_print_resources(rl, "irq", SYS_RES_IRQ, "%ld");
549 isa_print_resources(rl, "drq", SYS_RES_DRQ, "%ld");
550 if (device_get_flags(dev))
551 retval += printf(" flags %#x", device_get_flags(dev));
553 retval += bus_print_child_footer(bus, dev);
559 isa_read_ivar(device_t bus, device_t dev, int index, uintptr_t * result)
561 struct isa_device* idev = DEVTOISA(dev);
562 struct resource_list *rl = &idev->id_resources;
563 struct resource_list_entry *rle;
566 case ISA_IVAR_PORT_0:
567 rle = resource_list_find(rl, SYS_RES_IOPORT, 0);
569 *result = rle->start;
574 case ISA_IVAR_PORT_1:
575 rle = resource_list_find(rl, SYS_RES_IOPORT, 1);
577 *result = rle->start;
582 case ISA_IVAR_PORTSIZE_0:
583 rle = resource_list_find(rl, SYS_RES_IOPORT, 0);
585 *result = rle->count;
590 case ISA_IVAR_PORTSIZE_1:
591 rle = resource_list_find(rl, SYS_RES_IOPORT, 1);
593 *result = rle->count;
598 case ISA_IVAR_MADDR_0:
599 rle = resource_list_find(rl, SYS_RES_MEMORY, 0);
601 *result = rle->start;
606 case ISA_IVAR_MADDR_1:
607 rle = resource_list_find(rl, SYS_RES_MEMORY, 1);
609 *result = rle->start;
614 case ISA_IVAR_MSIZE_0:
615 rle = resource_list_find(rl, SYS_RES_MEMORY, 0);
617 *result = rle->count;
622 case ISA_IVAR_MSIZE_1:
623 rle = resource_list_find(rl, SYS_RES_MEMORY, 1);
625 *result = rle->count;
631 rle = resource_list_find(rl, SYS_RES_IRQ, 0);
633 *result = rle->start;
639 rle = resource_list_find(rl, SYS_RES_IRQ, 1);
641 *result = rle->start;
647 rle = resource_list_find(rl, SYS_RES_DRQ, 0);
649 *result = rle->start;
655 rle = resource_list_find(rl, SYS_RES_DRQ, 1);
657 *result = rle->start;
662 case ISA_IVAR_VENDORID:
663 *result = idev->id_vendorid;
666 case ISA_IVAR_SERIAL:
667 *result = idev->id_serial;
670 case ISA_IVAR_LOGICALID:
671 *result = idev->id_logicalid;
674 case ISA_IVAR_COMPATID:
675 *result = idev->id_compatid;
686 isa_write_ivar(device_t bus, device_t dev,
687 int index, uintptr_t value)
689 struct isa_device* idev = DEVTOISA(dev);
692 case ISA_IVAR_PORT_0:
693 case ISA_IVAR_PORT_1:
694 case ISA_IVAR_PORTSIZE_0:
695 case ISA_IVAR_PORTSIZE_1:
696 case ISA_IVAR_MADDR_0:
697 case ISA_IVAR_MADDR_1:
698 case ISA_IVAR_MSIZE_0:
699 case ISA_IVAR_MSIZE_1:
706 case ISA_IVAR_VENDORID:
707 idev->id_vendorid = value;
710 case ISA_IVAR_SERIAL:
711 idev->id_serial = value;
714 case ISA_IVAR_LOGICALID:
715 idev->id_logicalid = value;
718 case ISA_IVAR_COMPATID:
719 idev->id_compatid = value;
730 * Free any resources which the driver missed or which we were holding for
731 * it (see isa_probe_children).
734 isa_child_detached(device_t dev, device_t child)
736 struct isa_device* idev = DEVTOISA(child);
737 struct resource_list_entry *rle;
739 SLIST_FOREACH(rle, &idev->id_resources, link) {
741 resource_list_release(dev, child,
749 isa_set_resource(device_t dev, device_t child, int type, int rid,
750 u_long start, u_long count)
752 struct isa_device* idev = DEVTOISA(child);
753 struct resource_list *rl = &idev->id_resources;
755 if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
756 && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
760 if (type == SYS_RES_IOPORT && rid >= ISA_NPORT)
762 if (type == SYS_RES_MEMORY && rid >= ISA_NMEM)
764 if (type == SYS_RES_IRQ && rid >= ISA_NIRQ)
766 if (type == SYS_RES_DRQ && rid >= ISA_NDRQ)
769 resource_list_add(rl, type, rid, start, start + count - 1, count);
775 isa_get_resource(device_t dev, device_t child, int type, int rid,
776 u_long *startp, u_long *countp)
778 struct isa_device* idev = DEVTOISA(child);
779 struct resource_list *rl = &idev->id_resources;
780 struct resource_list_entry *rle;
782 rle = resource_list_find(rl, type, rid);
786 *startp = rle->start;
787 *countp = rle->count;
793 isa_delete_resource(device_t dev, device_t child, int type, int rid)
795 struct isa_device* idev = DEVTOISA(child);
796 struct resource_list *rl = &idev->id_resources;
797 resource_list_delete(rl, type, rid);
801 isa_add_config(device_t dev, device_t child,
802 int priority, struct isa_config *config)
804 struct isa_device* idev = DEVTOISA(child);
805 struct isa_config_entry *newice, *ice;
807 newice = malloc(sizeof *ice, M_DEVBUF, M_NOWAIT);
811 newice->ice_priority = priority;
812 newice->ice_config = *config;
814 TAILQ_FOREACH(ice, &idev->id_configs, ice_link) {
815 if (ice->ice_priority > priority)
819 TAILQ_INSERT_BEFORE(ice, newice, ice_link);
821 TAILQ_INSERT_TAIL(&idev->id_configs, newice, ice_link);
827 isa_set_config_callback(device_t dev, device_t child,
828 isa_config_cb *fn, void *arg)
830 struct isa_device* idev = DEVTOISA(child);
832 idev->id_config_cb = fn;
833 idev->id_config_arg = arg;
837 isa_pnp_probe(device_t dev, device_t child, struct isa_pnp_id *ids)
839 struct isa_device* idev = DEVTOISA(child);
841 if (!idev->id_vendorid)
846 * Really ought to support >1 compat id per device.
848 if (idev->id_logicalid == ids->ip_id
849 || idev->id_compatid == ids->ip_id) {
851 device_set_desc(child, ids->ip_desc);
860 static device_method_t isa_methods[] = {
861 /* Device interface */
862 DEVMETHOD(device_probe, isa_probe),
863 DEVMETHOD(device_attach, isa_attach),
864 DEVMETHOD(device_detach, bus_generic_detach),
865 DEVMETHOD(device_shutdown, bus_generic_shutdown),
866 DEVMETHOD(device_suspend, bus_generic_suspend),
867 DEVMETHOD(device_resume, bus_generic_resume),
870 DEVMETHOD(bus_add_child, isa_add_child),
871 DEVMETHOD(bus_print_child, isa_print_child),
872 DEVMETHOD(bus_read_ivar, isa_read_ivar),
873 DEVMETHOD(bus_write_ivar, isa_write_ivar),
874 DEVMETHOD(bus_child_detached, isa_child_detached),
875 DEVMETHOD(bus_alloc_resource, isa_alloc_resource),
876 DEVMETHOD(bus_release_resource, isa_release_resource),
877 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
878 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
879 DEVMETHOD(bus_setup_intr, isa_setup_intr),
880 DEVMETHOD(bus_teardown_intr, isa_teardown_intr),
883 DEVMETHOD(isa_set_resource, isa_set_resource),
884 DEVMETHOD(isa_get_resource, isa_get_resource),
885 DEVMETHOD(isa_delete_resource, isa_delete_resource),
886 DEVMETHOD(isa_add_config, isa_add_config),
887 DEVMETHOD(isa_set_config_callback, isa_set_config_callback),
888 DEVMETHOD(isa_pnp_probe, isa_pnp_probe),
893 static driver_t isa_driver = {
900 * ISA can be attached to a PCI-ISA bridge or directly to the nexus.
902 DRIVER_MODULE(isa, isab, isa_driver, isa_devclass, 0, 0);
904 DRIVER_MODULE(isa, nexus, isa_driver, isa_devclass, 0, 0);
908 * A fallback driver for reporting un-matched pnp devices.
912 unknown_probe(device_t dev)
915 * Only match pnp devices.
917 if (isa_get_vendorid(dev) != 0)
923 unknown_attach(device_t dev)
929 unknown_detach(device_t dev)
934 static device_method_t unknown_methods[] = {
935 /* Device interface */
936 DEVMETHOD(device_probe, unknown_probe),
937 DEVMETHOD(device_attach, unknown_attach),
938 DEVMETHOD(device_detach, unknown_detach),
943 static driver_t unknown_driver = {
949 static devclass_t unknown_devclass;
951 DRIVER_MODULE(unknown, isa, unknown_driver, unknown_devclass, 0, 0);