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)
119 struct resource *res[ISA_NMEM];
122 * First clear out any existing resource definitions.
124 for (i = 0; i < ISA_NMEM; i++) {
125 bus_delete_resource(child, SYS_RES_MEMORY, i);
130 result->ic_nmem = config->ic_nmem;
131 for (i = 0; i < config->ic_nmem; i++) {
132 u_int32_t start, end, size, align;
133 for (start = config->ic_mem[i].ir_start,
134 end = config->ic_mem[i].ir_end,
135 size = config->ic_mem[i].ir_size,
136 align = config->ic_mem[i].ir_align;
137 start + size - 1 <= end;
139 bus_set_resource(child, SYS_RES_MEMORY, i,
141 res[i] = bus_alloc_resource(child,
143 0, ~0, 1, RF_ACTIVE);
145 result->ic_mem[i].ir_start = start;
146 result->ic_mem[i].ir_end = start + size - 1;
147 result->ic_mem[i].ir_size = size;
148 result->ic_mem[i].ir_align = align;
154 * If we didn't find a place for memory range i, then
163 for (i = 0; i < ISA_NMEM; i++) {
165 bus_release_resource(child, SYS_RES_MEMORY,
173 * Find a working set of port regions for a child using the ranges
174 * in *config and return the regions in *result. Returns non-zero if
175 * a set of ranges was found.
178 isa_find_port(device_t child,
179 struct isa_config *config,
180 struct isa_config *result)
183 struct resource *res[ISA_NPORT];
186 * First clear out any existing resource definitions.
188 for (i = 0; i < ISA_NPORT; i++) {
189 bus_delete_resource(child, SYS_RES_IOPORT, i);
194 result->ic_nport = config->ic_nport;
195 for (i = 0; i < config->ic_nport; i++) {
196 u_int32_t start, end, size, align;
197 for (start = config->ic_port[i].ir_start,
198 end = config->ic_port[i].ir_end,
199 size = config->ic_port[i].ir_size,
200 align = config->ic_port[i].ir_align;
201 start + size - 1 <= end;
203 bus_set_resource(child, SYS_RES_IOPORT, i,
205 res[i] = bus_alloc_resource(child,
207 0, ~0, 1, RF_ACTIVE);
209 result->ic_port[i].ir_start = start;
210 result->ic_port[i].ir_end = start + size - 1;
211 result->ic_port[i].ir_size = size;
212 result->ic_port[i].ir_align = align;
218 * If we didn't find a place for port range i, then
227 for (i = 0; i < ISA_NPORT; i++) {
229 bus_release_resource(child, SYS_RES_IOPORT,
237 * Return the index of the first bit in the mask (or -1 if mask is empty.
240 find_first_bit(u_int32_t mask)
242 return ffs(mask) - 1;
246 * Return the index of the next bit in the mask, or -1 if there are no more.
249 find_next_bit(u_int32_t mask, int bit)
252 while (bit < 32 && !(mask & (1 << bit)))
260 * Find a working set of irqs for a child using the masks in *config
261 * and return the regions in *result. Returns non-zero if a set of
265 isa_find_irq(device_t child,
266 struct isa_config *config,
267 struct isa_config *result)
270 struct resource *res[ISA_NIRQ];
273 * First clear out any existing resource definitions.
275 for (i = 0; i < ISA_NIRQ; i++) {
276 bus_delete_resource(child, SYS_RES_IRQ, i);
281 result->ic_nirq = config->ic_nirq;
282 for (i = 0; i < config->ic_nirq; i++) {
283 u_int32_t mask = config->ic_irqmask[i];
285 for (irq = find_first_bit(mask);
287 irq = find_next_bit(mask, irq)) {
288 bus_set_resource(child, SYS_RES_IRQ, i,
290 res[i] = bus_alloc_resource(child,
292 0, ~0, 1, RF_ACTIVE);
294 result->ic_irqmask[i] = (1 << irq);
300 * If we didn't find a place for irq range i, then
309 for (i = 0; i < ISA_NIRQ; i++) {
311 bus_release_resource(child, SYS_RES_IRQ,
319 * Find a working set of drqs for a child using the masks in *config
320 * and return the regions in *result. Returns non-zero if a set of
324 isa_find_drq(device_t child,
325 struct isa_config *config,
326 struct isa_config *result)
329 struct resource *res[ISA_NDRQ];
332 * First clear out any existing resource definitions.
334 for (i = 0; i < ISA_NDRQ; i++) {
335 bus_delete_resource(child, SYS_RES_DRQ, i);
340 result->ic_ndrq = config->ic_ndrq;
341 for (i = 0; i < config->ic_ndrq; i++) {
342 u_int32_t mask = config->ic_drqmask[i];
344 for (drq = find_first_bit(mask);
346 drq = find_next_bit(mask, drq)) {
347 bus_set_resource(child, SYS_RES_DRQ, i,
349 res[i] = bus_alloc_resource(child,
351 0, ~0, 1, RF_ACTIVE);
353 result->ic_drqmask[i] = (1 << drq);
359 * If we didn't find a place for drq range i, then
368 for (i = 0; i < ISA_NDRQ; i++) {
370 bus_release_resource(child, SYS_RES_DRQ,
378 * Attempt to find a working set of resources for a device. Return
379 * non-zero if a working configuration is found.
382 isa_assign_resources(device_t child)
384 struct isa_device *idev = DEVTOISA(child);
385 struct isa_config_entry *ice;
386 struct isa_config config;
388 bzero(&config, sizeof config);
389 TAILQ_FOREACH(ice, &idev->id_configs, ice_link) {
390 if (!isa_find_memory(child, &ice->ice_config, &config))
392 if (!isa_find_port(child, &ice->ice_config, &config))
394 if (!isa_find_irq(child, &ice->ice_config, &config))
396 if (!isa_find_drq(child, &ice->ice_config, &config))
400 * A working configuration was found enable the device
401 * with this configuration.
403 if (idev->id_config_cb) {
404 idev->id_config_cb(idev->id_config_arg,
411 * Disable the device.
413 if (device_get_desc(child))
414 device_printf(child, "<%s> can't assign resources\n",
415 device_get_desc(child));
417 device_printf(child, "can't assign resources\n");
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)
435 if (device_get_children(dev, &children, &nchildren))
439 * First disable all pnp devices so that they don't get
440 * matched by legacy probes.
442 for (i = 0; i < nchildren; i++) {
443 device_t child = children[i];
444 struct isa_device *idev = DEVTOISA(child);
445 struct isa_config config;
447 bzero(&config, sizeof config);
448 if (idev->id_config_cb)
449 idev->id_config_cb(idev->id_config_arg, &config, 0);
453 * Next probe all non-pnp devices so that they claim their
456 for (i = 0; i < nchildren; i++) {
457 device_t child = children[i];
458 struct isa_device *idev = DEVTOISA(child);
460 if (TAILQ_FIRST(&idev->id_configs))
463 device_probe_and_attach(child);
467 * Finally assign resource to pnp devices and probe them.
469 for (i = 0; i < nchildren; i++) {
470 device_t child = children[i];
471 struct isa_device* idev = DEVTOISA(child);
473 if (!TAILQ_FIRST(&idev->id_configs))
476 if (isa_assign_resources(child)) {
477 struct resource_list *rl = &idev->id_resources;
478 struct resource_list_entry *rle;
480 device_probe_and_attach(child);
483 * Claim any unallocated resources to keep other
484 * devices from using them.
486 SLIST_FOREACH(rle, rl, link) {
489 resource_list_alloc(rl, dev, child,
499 free(children, M_TEMP);
503 * Add a new child with default ivars.
506 isa_add_child(device_t dev, int order, const char *name, int unit)
508 struct isa_device *idev;
510 idev = malloc(sizeof(struct isa_device), M_ISADEV, M_NOWAIT);
513 bzero(idev, sizeof *idev);
515 resource_list_init(&idev->id_resources);
516 TAILQ_INIT(&idev->id_configs);
518 return device_add_child_ordered(dev, order, name, unit, idev);
522 isa_print_resources(struct resource_list *rl, const char *name, int type,
523 int count, const char *format)
525 struct resource_list_entry *rle;
530 for (i = 0; i < count; i++) {
531 rle = resource_list_find(rl, type, i);
534 printf(" %s ", name);
535 else if (printed > 0)
538 printf(format, rle->start);
539 if (rle->count > 1) {
541 printf(format, rle->start + rle->count - 1);
544 /* check the first few regardless */
551 isa_print_child(device_t bus, device_t dev)
553 struct isa_device *idev = DEVTOISA(dev);
554 struct resource_list *rl = &idev->id_resources;
557 retval += bus_print_child_header(bus, dev);
559 if (SLIST_FIRST(rl) || device_get_flags(dev))
560 retval += printf(" at");
562 isa_print_resources(rl, "port", SYS_RES_IOPORT, ISA_NPORT, "%#lx");
563 isa_print_resources(rl, "iomem", SYS_RES_MEMORY, ISA_NMEM, "%#lx");
564 isa_print_resources(rl, "irq", SYS_RES_IRQ, ISA_NIRQ, "%ld");
565 isa_print_resources(rl, "drq", SYS_RES_DRQ, ISA_NDRQ, "%ld");
566 if (device_get_flags(dev))
567 retval += printf(" flags %#x", device_get_flags(dev));
569 retval += bus_print_child_footer(bus, dev);
575 isa_read_ivar(device_t bus, device_t dev, int index, uintptr_t * result)
577 struct isa_device* idev = DEVTOISA(dev);
578 struct resource_list *rl = &idev->id_resources;
579 struct resource_list_entry *rle;
582 case ISA_IVAR_PORT_0:
583 rle = resource_list_find(rl, SYS_RES_IOPORT, 0);
585 *result = rle->start;
590 case ISA_IVAR_PORT_1:
591 rle = resource_list_find(rl, SYS_RES_IOPORT, 1);
593 *result = rle->start;
598 case ISA_IVAR_PORTSIZE_0:
599 rle = resource_list_find(rl, SYS_RES_IOPORT, 0);
601 *result = rle->count;
606 case ISA_IVAR_PORTSIZE_1:
607 rle = resource_list_find(rl, SYS_RES_IOPORT, 1);
609 *result = rle->count;
614 case ISA_IVAR_MADDR_0:
615 rle = resource_list_find(rl, SYS_RES_MEMORY, 0);
617 *result = rle->start;
622 case ISA_IVAR_MADDR_1:
623 rle = resource_list_find(rl, SYS_RES_MEMORY, 1);
625 *result = rle->start;
630 case ISA_IVAR_MSIZE_0:
631 rle = resource_list_find(rl, SYS_RES_MEMORY, 0);
633 *result = rle->count;
638 case ISA_IVAR_MSIZE_1:
639 rle = resource_list_find(rl, SYS_RES_MEMORY, 1);
641 *result = rle->count;
647 rle = resource_list_find(rl, SYS_RES_IRQ, 0);
649 *result = rle->start;
655 rle = resource_list_find(rl, SYS_RES_IRQ, 1);
657 *result = rle->start;
663 rle = resource_list_find(rl, SYS_RES_DRQ, 0);
665 *result = rle->start;
671 rle = resource_list_find(rl, SYS_RES_DRQ, 1);
673 *result = rle->start;
678 case ISA_IVAR_VENDORID:
679 *result = idev->id_vendorid;
682 case ISA_IVAR_SERIAL:
683 *result = idev->id_serial;
686 case ISA_IVAR_LOGICALID:
687 *result = idev->id_logicalid;
690 case ISA_IVAR_COMPATID:
691 *result = idev->id_compatid;
702 isa_write_ivar(device_t bus, device_t dev,
703 int index, uintptr_t value)
705 struct isa_device* idev = DEVTOISA(dev);
708 case ISA_IVAR_PORT_0:
709 case ISA_IVAR_PORT_1:
710 case ISA_IVAR_PORTSIZE_0:
711 case ISA_IVAR_PORTSIZE_1:
712 case ISA_IVAR_MADDR_0:
713 case ISA_IVAR_MADDR_1:
714 case ISA_IVAR_MSIZE_0:
715 case ISA_IVAR_MSIZE_1:
722 case ISA_IVAR_VENDORID:
723 idev->id_vendorid = value;
726 case ISA_IVAR_SERIAL:
727 idev->id_serial = value;
730 case ISA_IVAR_LOGICALID:
731 idev->id_logicalid = value;
734 case ISA_IVAR_COMPATID:
735 idev->id_compatid = value;
746 * Free any resources which the driver missed or which we were holding for
747 * it (see isa_probe_children).
750 isa_child_detached(device_t dev, device_t child)
752 struct isa_device* idev = DEVTOISA(child);
753 struct resource_list *rl = &idev->id_resources;
754 struct resource_list_entry *rle;
756 SLIST_FOREACH(rle, &idev->id_resources, link) {
758 resource_list_release(rl, dev, child,
766 isa_set_resource(device_t dev, device_t child, int type, int rid,
767 u_long start, u_long count)
769 struct isa_device* idev = DEVTOISA(child);
770 struct resource_list *rl = &idev->id_resources;
772 if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
773 && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
777 if (type == SYS_RES_IOPORT && rid >= ISA_NPORT)
779 if (type == SYS_RES_MEMORY && rid >= ISA_NMEM)
781 if (type == SYS_RES_IRQ && rid >= ISA_NIRQ)
783 if (type == SYS_RES_DRQ && rid >= ISA_NDRQ)
786 resource_list_add(rl, type, rid, start, start + count - 1, count);
792 isa_get_resource(device_t dev, device_t child, int type, int rid,
793 u_long *startp, u_long *countp)
795 struct isa_device* idev = DEVTOISA(child);
796 struct resource_list *rl = &idev->id_resources;
797 struct resource_list_entry *rle;
799 rle = resource_list_find(rl, type, rid);
803 *startp = rle->start;
804 *countp = rle->count;
810 isa_delete_resource(device_t dev, device_t child, int type, int rid)
812 struct isa_device* idev = DEVTOISA(child);
813 struct resource_list *rl = &idev->id_resources;
814 resource_list_delete(rl, type, rid);
818 isa_add_config(device_t dev, device_t child,
819 int priority, struct isa_config *config)
821 struct isa_device* idev = DEVTOISA(child);
822 struct isa_config_entry *newice, *ice;
824 newice = malloc(sizeof *ice, M_DEVBUF, M_NOWAIT);
828 newice->ice_priority = priority;
829 newice->ice_config = *config;
831 TAILQ_FOREACH(ice, &idev->id_configs, ice_link) {
832 if (ice->ice_priority > priority)
836 TAILQ_INSERT_BEFORE(ice, newice, ice_link);
838 TAILQ_INSERT_TAIL(&idev->id_configs, newice, ice_link);
844 isa_set_config_callback(device_t dev, device_t child,
845 isa_config_cb *fn, void *arg)
847 struct isa_device* idev = DEVTOISA(child);
849 idev->id_config_cb = fn;
850 idev->id_config_arg = arg;
854 isa_pnp_probe(device_t dev, device_t child, struct isa_pnp_id *ids)
856 struct isa_device* idev = DEVTOISA(child);
858 if (!idev->id_vendorid)
863 * Really ought to support >1 compat id per device.
865 if (idev->id_logicalid == ids->ip_id
866 || idev->id_compatid == ids->ip_id) {
868 device_set_desc(child, ids->ip_desc);
877 static device_method_t isa_methods[] = {
878 /* Device interface */
879 DEVMETHOD(device_probe, isa_probe),
880 DEVMETHOD(device_attach, isa_attach),
881 DEVMETHOD(device_detach, bus_generic_detach),
882 DEVMETHOD(device_shutdown, bus_generic_shutdown),
883 DEVMETHOD(device_suspend, bus_generic_suspend),
884 DEVMETHOD(device_resume, bus_generic_resume),
887 DEVMETHOD(bus_add_child, isa_add_child),
888 DEVMETHOD(bus_print_child, isa_print_child),
889 DEVMETHOD(bus_read_ivar, isa_read_ivar),
890 DEVMETHOD(bus_write_ivar, isa_write_ivar),
891 DEVMETHOD(bus_child_detached, isa_child_detached),
892 DEVMETHOD(bus_alloc_resource, isa_alloc_resource),
893 DEVMETHOD(bus_release_resource, isa_release_resource),
894 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
895 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
896 DEVMETHOD(bus_setup_intr, isa_setup_intr),
897 DEVMETHOD(bus_teardown_intr, isa_teardown_intr),
898 DEVMETHOD(bus_set_resource, isa_set_resource),
899 DEVMETHOD(bus_get_resource, isa_get_resource),
900 DEVMETHOD(bus_delete_resource, isa_delete_resource),
903 DEVMETHOD(isa_add_config, isa_add_config),
904 DEVMETHOD(isa_set_config_callback, isa_set_config_callback),
905 DEVMETHOD(isa_pnp_probe, isa_pnp_probe),
910 static driver_t isa_driver = {
917 * ISA can be attached to a PCI-ISA bridge or directly to the nexus.
919 DRIVER_MODULE(isa, isab, isa_driver, isa_devclass, 0, 0);
921 DRIVER_MODULE(isa, nexus, isa_driver, isa_devclass, 0, 0);
925 * A fallback driver for reporting un-matched pnp devices.
929 unknown_probe(device_t dev)
932 * Only match pnp devices.
934 if (isa_get_vendorid(dev) != 0)
940 unknown_attach(device_t dev)
946 unknown_detach(device_t dev)
951 static device_method_t unknown_methods[] = {
952 /* Device interface */
953 DEVMETHOD(device_probe, unknown_probe),
954 DEVMETHOD(device_attach, unknown_attach),
955 DEVMETHOD(device_detach, unknown_detach),
960 static driver_t unknown_driver = {
966 static devclass_t unknown_devclass;
968 DRIVER_MODULE(unknown, isa, unknown_driver, unknown_devclass, 0, 0);