xref: /freebsd/sys/isa/isa_common.c (revision 132580b5afe793421e1fa57085e8e9c5fa928b86)
1a3be63b3SDoug Rabson /*-
2a3be63b3SDoug Rabson  * Copyright (c) 1999 Doug Rabson
3a3be63b3SDoug Rabson  * All rights reserved.
4a3be63b3SDoug Rabson  *
5a3be63b3SDoug Rabson  * Redistribution and use in source and binary forms, with or without
6a3be63b3SDoug Rabson  * modification, are permitted provided that the following conditions
7a3be63b3SDoug Rabson  * are met:
8a3be63b3SDoug Rabson  * 1. Redistributions of source code must retain the above copyright
9a3be63b3SDoug Rabson  *    notice, this list of conditions and the following disclaimer.
10a3be63b3SDoug Rabson  * 2. Redistributions in binary form must reproduce the above copyright
11a3be63b3SDoug Rabson  *    notice, this list of conditions and the following disclaimer in the
12a3be63b3SDoug Rabson  *    documentation and/or other materials provided with the distribution.
13a3be63b3SDoug Rabson  *
14a3be63b3SDoug Rabson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15a3be63b3SDoug Rabson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16a3be63b3SDoug Rabson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17a3be63b3SDoug Rabson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18a3be63b3SDoug Rabson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19a3be63b3SDoug Rabson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20a3be63b3SDoug Rabson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21a3be63b3SDoug Rabson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22a3be63b3SDoug Rabson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23a3be63b3SDoug Rabson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24a3be63b3SDoug Rabson  * SUCH DAMAGE.
25a3be63b3SDoug Rabson  */
26a3be63b3SDoug Rabson /*
27a3be63b3SDoug Rabson  * Modifications for Intel architecture by Garrett A. Wollman.
28a3be63b3SDoug Rabson  * Copyright 1998 Massachusetts Institute of Technology
29a3be63b3SDoug Rabson  *
30a3be63b3SDoug Rabson  * Permission to use, copy, modify, and distribute this software and
31a3be63b3SDoug Rabson  * its documentation for any purpose and without fee is hereby
32a3be63b3SDoug Rabson  * granted, provided that both the above copyright notice and this
33a3be63b3SDoug Rabson  * permission notice appear in all copies, that both the above
34a3be63b3SDoug Rabson  * copyright notice and this permission notice appear in all
35a3be63b3SDoug Rabson  * supporting documentation, and that the name of M.I.T. not be used
36a3be63b3SDoug Rabson  * in advertising or publicity pertaining to distribution of the
37a3be63b3SDoug Rabson  * software without specific, written prior permission.  M.I.T. makes
38a3be63b3SDoug Rabson  * no representations about the suitability of this software for any
39a3be63b3SDoug Rabson  * purpose.  It is provided "as is" without express or implied
40a3be63b3SDoug Rabson  * warranty.
41a3be63b3SDoug Rabson  *
42a3be63b3SDoug Rabson  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
43a3be63b3SDoug Rabson  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
44a3be63b3SDoug Rabson  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
45a3be63b3SDoug Rabson  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
46a3be63b3SDoug Rabson  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47a3be63b3SDoug Rabson  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
48a3be63b3SDoug Rabson  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
49a3be63b3SDoug Rabson  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
50a3be63b3SDoug Rabson  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
51a3be63b3SDoug Rabson  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
52a3be63b3SDoug Rabson  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53a3be63b3SDoug Rabson  * SUCH DAMAGE.
54a3be63b3SDoug Rabson  */
55a3be63b3SDoug Rabson 
56a3be63b3SDoug Rabson /*
57a3be63b3SDoug Rabson  * Parts of the ISA bus implementation common to all architectures.
58a3be63b3SDoug Rabson  */
59a3be63b3SDoug Rabson 
608c9bbf48SDavid E. O'Brien #include <sys/cdefs.h>
618c9bbf48SDavid E. O'Brien __FBSDID("$FreeBSD$");
628c9bbf48SDavid E. O'Brien 
63acb8c149SMarius Strobl #include "opt_isa.h"
64acb8c149SMarius Strobl 
65a3be63b3SDoug Rabson #include <sys/param.h>
66a3be63b3SDoug Rabson #include <sys/systm.h>
67a3be63b3SDoug Rabson #include <sys/kernel.h>
68a3be63b3SDoug Rabson #include <sys/bus.h>
69a3be63b3SDoug Rabson #include <sys/malloc.h>
70a3be63b3SDoug Rabson #include <sys/module.h>
71a3be63b3SDoug Rabson #include <machine/bus.h>
72a3be63b3SDoug Rabson #include <sys/rman.h>
73a3be63b3SDoug Rabson 
74a3be63b3SDoug Rabson #include <machine/resource.h>
75a3be63b3SDoug Rabson 
76a3be63b3SDoug Rabson #include <isa/isavar.h>
77a3be63b3SDoug Rabson #include <isa/isa_common.h>
78a3be63b3SDoug Rabson 
792b2b44c9SJohn Baldwin static int	isa_print_child(device_t bus, device_t dev);
802b2b44c9SJohn Baldwin 
81959b7375SPoul-Henning Kamp static MALLOC_DEFINE(M_ISADEV, "isadev", "ISA device");
82a3be63b3SDoug Rabson 
83ec22663bSDoug Rabson static int isa_running;
84a3be63b3SDoug Rabson 
85a3be63b3SDoug Rabson /*
86a3be63b3SDoug Rabson  * At 'probe' time, we add all the devices which we know about to the
87a3be63b3SDoug Rabson  * bus.  The generic attach routine will probe and attach them if they
88a3be63b3SDoug Rabson  * are alive.
89a3be63b3SDoug Rabson  */
90a3be63b3SDoug Rabson static int
91a3be63b3SDoug Rabson isa_probe(device_t dev)
92a3be63b3SDoug Rabson {
93a3be63b3SDoug Rabson 	device_set_desc(dev, "ISA bus");
9401f1aed2SThomas Moestl 	isa_init(dev);		/* Allow machdep code to initialise */
9509aafea1SWarner Losh 	return (0);
96a3be63b3SDoug Rabson }
97a3be63b3SDoug Rabson 
98a3be63b3SDoug Rabson extern device_t isa_bus_device;
99a3be63b3SDoug Rabson 
100a3be63b3SDoug Rabson static int
101a3be63b3SDoug Rabson isa_attach(device_t dev)
102a3be63b3SDoug Rabson {
103a3be63b3SDoug Rabson 	/*
1044249382dSDoug Rabson 	 * Arrange for isa_probe_children(dev) to be called later. XXX
105a3be63b3SDoug Rabson 	 */
106a3be63b3SDoug Rabson 	isa_bus_device = dev;
10709aafea1SWarner Losh 	return (0);
108a3be63b3SDoug Rabson }
109a3be63b3SDoug Rabson 
110a3be63b3SDoug Rabson /*
1114249382dSDoug Rabson  * Find a working set of memory regions for a child using the ranges
1124249382dSDoug Rabson  * in *config  and return the regions in *result. Returns non-zero if
1134249382dSDoug Rabson  * a set of ranges was found.
1144249382dSDoug Rabson  */
1154249382dSDoug Rabson static int
11609aafea1SWarner Losh isa_find_memory(device_t child, struct isa_config *config,
1174249382dSDoug Rabson     struct isa_config *result)
1184249382dSDoug Rabson {
1194249382dSDoug Rabson 	int success, i;
1204249382dSDoug Rabson 	struct resource *res[ISA_NMEM];
1214249382dSDoug Rabson 
1224249382dSDoug Rabson 	/*
1234249382dSDoug Rabson 	 * First clear out any existing resource definitions.
1244249382dSDoug Rabson 	 */
1254249382dSDoug Rabson 	for (i = 0; i < ISA_NMEM; i++) {
12625afb89bSDoug Rabson 		bus_delete_resource(child, SYS_RES_MEMORY, i);
1274249382dSDoug Rabson 		res[i] = NULL;
1284249382dSDoug Rabson 	}
1294249382dSDoug Rabson 
1304249382dSDoug Rabson 	success = 1;
1314249382dSDoug Rabson 	result->ic_nmem = config->ic_nmem;
1324249382dSDoug Rabson 	for (i = 0; i < config->ic_nmem; i++) {
13309aafea1SWarner Losh 		uint32_t start, end, size, align;
134c3959391SKazutaka YOKOTA 
135c3959391SKazutaka YOKOTA 		size = config->ic_mem[i].ir_size;
136c3959391SKazutaka YOKOTA 
137c3959391SKazutaka YOKOTA 		/* the PnP device may have a null resource as filler */
138c3959391SKazutaka YOKOTA 		if (size == 0) {
139c3959391SKazutaka YOKOTA 			result->ic_mem[i].ir_start = 0;
140c3959391SKazutaka YOKOTA 			result->ic_mem[i].ir_end = 0;
141c3959391SKazutaka YOKOTA 			result->ic_mem[i].ir_size = 0;
142c3959391SKazutaka YOKOTA 			result->ic_mem[i].ir_align = 0;
143c3959391SKazutaka YOKOTA 			continue;
144c3959391SKazutaka YOKOTA 		}
145c3959391SKazutaka YOKOTA 
1464249382dSDoug Rabson 		for (start = config->ic_mem[i].ir_start,
1474249382dSDoug Rabson 			     end = config->ic_mem[i].ir_end,
1484249382dSDoug Rabson 			     align = config->ic_mem[i].ir_align;
149784d07b4SWarner Losh 		     start + size - 1 <= end && start + size > start;
15090dea4f9SJohn Baldwin 		     start += MAX(align, 1)) {
15125afb89bSDoug Rabson 			bus_set_resource(child, SYS_RES_MEMORY, i,
1524249382dSDoug Rabson 					 start, size);
1539b01161eSWarner Losh 			res[i] = bus_alloc_resource(child,
1549b01161eSWarner Losh 			    SYS_RES_MEMORY, &i, 0, ~0, 1,
1559b01161eSWarner Losh 			    rman_make_alignment_flags(align) /* !RF_ACTIVE */);
1564249382dSDoug Rabson 			if (res[i]) {
1574249382dSDoug Rabson 				result->ic_mem[i].ir_start = start;
1584249382dSDoug Rabson 				result->ic_mem[i].ir_end = start + size - 1;
1594249382dSDoug Rabson 				result->ic_mem[i].ir_size = size;
1604249382dSDoug Rabson 				result->ic_mem[i].ir_align = align;
1614249382dSDoug Rabson 				break;
1624249382dSDoug Rabson 			}
1634249382dSDoug Rabson 		}
1644249382dSDoug Rabson 
1654249382dSDoug Rabson 		/*
1664249382dSDoug Rabson 		 * If we didn't find a place for memory range i, then
1674249382dSDoug Rabson 		 * give up now.
1684249382dSDoug Rabson 		 */
1694249382dSDoug Rabson 		if (!res[i]) {
1704249382dSDoug Rabson 			success = 0;
1714249382dSDoug Rabson 			break;
1724249382dSDoug Rabson 		}
1734249382dSDoug Rabson 	}
1744249382dSDoug Rabson 
1754249382dSDoug Rabson 	for (i = 0; i < ISA_NMEM; i++) {
1764249382dSDoug Rabson 		if (res[i])
1774249382dSDoug Rabson 			bus_release_resource(child, SYS_RES_MEMORY,
1784249382dSDoug Rabson 					     i, res[i]);
1794249382dSDoug Rabson 	}
1804249382dSDoug Rabson 
18109aafea1SWarner Losh 	return (success);
1824249382dSDoug Rabson }
1834249382dSDoug Rabson 
1844249382dSDoug Rabson /*
1854249382dSDoug Rabson  * Find a working set of port regions for a child using the ranges
1864249382dSDoug Rabson  * in *config  and return the regions in *result. Returns non-zero if
1874249382dSDoug Rabson  * a set of ranges was found.
1884249382dSDoug Rabson  */
1894249382dSDoug Rabson static int
19009aafea1SWarner Losh isa_find_port(device_t child, struct isa_config *config,
1914249382dSDoug Rabson     struct isa_config *result)
1924249382dSDoug Rabson {
1934249382dSDoug Rabson 	int success, i;
1944249382dSDoug Rabson 	struct resource *res[ISA_NPORT];
1954249382dSDoug Rabson 
1964249382dSDoug Rabson 	/*
1974249382dSDoug Rabson 	 * First clear out any existing resource definitions.
1984249382dSDoug Rabson 	 */
1994249382dSDoug Rabson 	for (i = 0; i < ISA_NPORT; i++) {
20025afb89bSDoug Rabson 		bus_delete_resource(child, SYS_RES_IOPORT, i);
2014249382dSDoug Rabson 		res[i] = NULL;
2024249382dSDoug Rabson 	}
2034249382dSDoug Rabson 
2044249382dSDoug Rabson 	success = 1;
2054249382dSDoug Rabson 	result->ic_nport = config->ic_nport;
2064249382dSDoug Rabson 	for (i = 0; i < config->ic_nport; i++) {
20709aafea1SWarner Losh 		uint32_t start, end, size, align;
208c3959391SKazutaka YOKOTA 
209c3959391SKazutaka YOKOTA 		size = config->ic_port[i].ir_size;
210c3959391SKazutaka YOKOTA 
211c3959391SKazutaka YOKOTA 		/* the PnP device may have a null resource as filler */
212c3959391SKazutaka YOKOTA 		if (size == 0) {
213c3959391SKazutaka YOKOTA 			result->ic_port[i].ir_start = 0;
214c3959391SKazutaka YOKOTA 			result->ic_port[i].ir_end = 0;
215c3959391SKazutaka YOKOTA 			result->ic_port[i].ir_size = 0;
216c3959391SKazutaka YOKOTA 			result->ic_port[i].ir_align = 0;
217c3959391SKazutaka YOKOTA 			continue;
218c3959391SKazutaka YOKOTA 		}
219c3959391SKazutaka YOKOTA 
2204249382dSDoug Rabson 		for (start = config->ic_port[i].ir_start,
2214249382dSDoug Rabson 			     end = config->ic_port[i].ir_end,
2224249382dSDoug Rabson 			     align = config->ic_port[i].ir_align;
2234249382dSDoug Rabson 		     start + size - 1 <= end;
2244249382dSDoug Rabson 		     start += align) {
22525afb89bSDoug Rabson 			bus_set_resource(child, SYS_RES_IOPORT, i,
2264249382dSDoug Rabson 					 start, size);
2279b01161eSWarner Losh 			res[i] = bus_alloc_resource(child,
2289b01161eSWarner Losh 			    SYS_RES_IOPORT, &i, 0, ~0, 1,
2299b01161eSWarner Losh 			    rman_make_alignment_flags(align) /* !RF_ACTIVE */);
2304249382dSDoug Rabson 			if (res[i]) {
2314249382dSDoug Rabson 				result->ic_port[i].ir_start = start;
2324249382dSDoug Rabson 				result->ic_port[i].ir_end = start + size - 1;
2334249382dSDoug Rabson 				result->ic_port[i].ir_size = size;
2344249382dSDoug Rabson 				result->ic_port[i].ir_align = align;
2354249382dSDoug Rabson 				break;
2364249382dSDoug Rabson 			}
2374249382dSDoug Rabson 		}
2384249382dSDoug Rabson 
2394249382dSDoug Rabson 		/*
2404249382dSDoug Rabson 		 * If we didn't find a place for port range i, then
2414249382dSDoug Rabson 		 * give up now.
2424249382dSDoug Rabson 		 */
2434249382dSDoug Rabson 		if (!res[i]) {
2444249382dSDoug Rabson 			success = 0;
2454249382dSDoug Rabson 			break;
2464249382dSDoug Rabson 		}
2474249382dSDoug Rabson 	}
2484249382dSDoug Rabson 
2494249382dSDoug Rabson 	for (i = 0; i < ISA_NPORT; i++) {
2504249382dSDoug Rabson 		if (res[i])
2514249382dSDoug Rabson 			bus_release_resource(child, SYS_RES_IOPORT,
2524249382dSDoug Rabson 					     i, res[i]);
2534249382dSDoug Rabson 	}
2544249382dSDoug Rabson 
2554249382dSDoug Rabson 	return success;
2564249382dSDoug Rabson }
2574249382dSDoug Rabson 
2584249382dSDoug Rabson /*
2594249382dSDoug Rabson  * Return the index of the first bit in the mask (or -1 if mask is empty.
2604249382dSDoug Rabson  */
2614249382dSDoug Rabson static int
26209aafea1SWarner Losh find_first_bit(uint32_t mask)
2634249382dSDoug Rabson {
26409aafea1SWarner Losh 	return (ffs(mask) - 1);
2654249382dSDoug Rabson }
2664249382dSDoug Rabson 
2674249382dSDoug Rabson /*
2684249382dSDoug Rabson  * Return the index of the next bit in the mask, or -1 if there are no more.
2694249382dSDoug Rabson  */
2704249382dSDoug Rabson static int
27109aafea1SWarner Losh find_next_bit(uint32_t mask, int bit)
2724249382dSDoug Rabson {
2734249382dSDoug Rabson 	bit++;
2744249382dSDoug Rabson 	while (bit < 32 && !(mask & (1 << bit)))
2754249382dSDoug Rabson 		bit++;
2764249382dSDoug Rabson 	if (bit != 32)
27709aafea1SWarner Losh 		return (bit);
27809aafea1SWarner Losh 	return (-1);
2794249382dSDoug Rabson }
2804249382dSDoug Rabson 
2814249382dSDoug Rabson /*
2824249382dSDoug Rabson  * Find a working set of irqs for a child using the masks in *config
2834249382dSDoug Rabson  * and return the regions in *result. Returns non-zero if a set of
2844249382dSDoug Rabson  * irqs was found.
2854249382dSDoug Rabson  */
2864249382dSDoug Rabson static int
28709aafea1SWarner Losh isa_find_irq(device_t child, struct isa_config *config,
2884249382dSDoug Rabson     struct isa_config *result)
2894249382dSDoug Rabson {
2904249382dSDoug Rabson 	int success, i;
2914249382dSDoug Rabson 	struct resource *res[ISA_NIRQ];
2924249382dSDoug Rabson 
2934249382dSDoug Rabson 	/*
2944249382dSDoug Rabson 	 * First clear out any existing resource definitions.
2954249382dSDoug Rabson 	 */
2964249382dSDoug Rabson 	for (i = 0; i < ISA_NIRQ; i++) {
29725afb89bSDoug Rabson 		bus_delete_resource(child, SYS_RES_IRQ, i);
2984249382dSDoug Rabson 		res[i] = NULL;
2994249382dSDoug Rabson 	}
3004249382dSDoug Rabson 
3014249382dSDoug Rabson 	success = 1;
3024249382dSDoug Rabson 	result->ic_nirq = config->ic_nirq;
3034249382dSDoug Rabson 	for (i = 0; i < config->ic_nirq; i++) {
30409aafea1SWarner Losh 		uint32_t mask = config->ic_irqmask[i];
3054249382dSDoug Rabson 		int irq;
306c3959391SKazutaka YOKOTA 
307c3959391SKazutaka YOKOTA 		/* the PnP device may have a null resource as filler */
308c3959391SKazutaka YOKOTA 		if (mask == 0) {
309c3959391SKazutaka YOKOTA 			result->ic_irqmask[i] = 0;
310c3959391SKazutaka YOKOTA 			continue;
311c3959391SKazutaka YOKOTA 		}
312c3959391SKazutaka YOKOTA 
3134249382dSDoug Rabson 		for (irq = find_first_bit(mask);
3144249382dSDoug Rabson 		     irq != -1;
3154249382dSDoug Rabson 		     irq = find_next_bit(mask, irq)) {
31625afb89bSDoug Rabson 			bus_set_resource(child, SYS_RES_IRQ, i,
3174249382dSDoug Rabson 					 irq, 1);
3185f96beb9SNate Lawson 			res[i] = bus_alloc_resource_any(child,
3194249382dSDoug Rabson 							SYS_RES_IRQ, &i,
3205f96beb9SNate Lawson 							0 /* !RF_ACTIVE */ );
3214249382dSDoug Rabson 			if (res[i]) {
3224249382dSDoug Rabson 				result->ic_irqmask[i] = (1 << irq);
3234249382dSDoug Rabson 				break;
3244249382dSDoug Rabson 			}
3254249382dSDoug Rabson 		}
3264249382dSDoug Rabson 
3274249382dSDoug Rabson 		/*
3284249382dSDoug Rabson 		 * If we didn't find a place for irq range i, then
3294249382dSDoug Rabson 		 * give up now.
3304249382dSDoug Rabson 		 */
3314249382dSDoug Rabson 		if (!res[i]) {
3324249382dSDoug Rabson 			success = 0;
3334249382dSDoug Rabson 			break;
3344249382dSDoug Rabson 		}
3354249382dSDoug Rabson 	}
3364249382dSDoug Rabson 
3374249382dSDoug Rabson 	for (i = 0; i < ISA_NIRQ; i++) {
3384249382dSDoug Rabson 		if (res[i])
3394249382dSDoug Rabson 			bus_release_resource(child, SYS_RES_IRQ,
3404249382dSDoug Rabson 					     i, res[i]);
3414249382dSDoug Rabson 	}
3424249382dSDoug Rabson 
34309aafea1SWarner Losh 	return (success);
3444249382dSDoug Rabson }
3454249382dSDoug Rabson 
3464249382dSDoug Rabson /*
3474249382dSDoug Rabson  * Find a working set of drqs for a child using the masks in *config
3484249382dSDoug Rabson  * and return the regions in *result. Returns non-zero if a set of
3494249382dSDoug Rabson  * drqs was found.
3504249382dSDoug Rabson  */
3514249382dSDoug Rabson static int
35209aafea1SWarner Losh isa_find_drq(device_t child, struct isa_config *config,
3534249382dSDoug Rabson     struct isa_config *result)
3544249382dSDoug Rabson {
3554249382dSDoug Rabson 	int success, i;
3564249382dSDoug Rabson 	struct resource *res[ISA_NDRQ];
3574249382dSDoug Rabson 
3584249382dSDoug Rabson 	/*
3594249382dSDoug Rabson 	 * First clear out any existing resource definitions.
3604249382dSDoug Rabson 	 */
3614249382dSDoug Rabson 	for (i = 0; i < ISA_NDRQ; i++) {
36225afb89bSDoug Rabson 		bus_delete_resource(child, SYS_RES_DRQ, i);
3634249382dSDoug Rabson 		res[i] = NULL;
3644249382dSDoug Rabson 	}
3654249382dSDoug Rabson 
3664249382dSDoug Rabson 	success = 1;
3674249382dSDoug Rabson 	result->ic_ndrq = config->ic_ndrq;
3684249382dSDoug Rabson 	for (i = 0; i < config->ic_ndrq; i++) {
36909aafea1SWarner Losh 		uint32_t mask = config->ic_drqmask[i];
3704249382dSDoug Rabson 		int drq;
371c3959391SKazutaka YOKOTA 
372c3959391SKazutaka YOKOTA 		/* the PnP device may have a null resource as filler */
373c3959391SKazutaka YOKOTA 		if (mask == 0) {
374c3959391SKazutaka YOKOTA 			result->ic_drqmask[i] = 0;
375c3959391SKazutaka YOKOTA 			continue;
376c3959391SKazutaka YOKOTA 		}
377c3959391SKazutaka YOKOTA 
3784249382dSDoug Rabson 		for (drq = find_first_bit(mask);
3794249382dSDoug Rabson 		     drq != -1;
3804249382dSDoug Rabson 		     drq = find_next_bit(mask, drq)) {
38125afb89bSDoug Rabson 			bus_set_resource(child, SYS_RES_DRQ, i,
3824249382dSDoug Rabson 					 drq, 1);
3835f96beb9SNate Lawson 			res[i] = bus_alloc_resource_any(child,
3844249382dSDoug Rabson 							SYS_RES_DRQ, &i,
3855f96beb9SNate Lawson 							0 /* !RF_ACTIVE */);
3864249382dSDoug Rabson 			if (res[i]) {
3874249382dSDoug Rabson 				result->ic_drqmask[i] = (1 << drq);
3884249382dSDoug Rabson 				break;
3894249382dSDoug Rabson 			}
3904249382dSDoug Rabson 		}
3914249382dSDoug Rabson 
3924249382dSDoug Rabson 		/*
3934249382dSDoug Rabson 		 * If we didn't find a place for drq range i, then
3944249382dSDoug Rabson 		 * give up now.
3954249382dSDoug Rabson 		 */
3964249382dSDoug Rabson 		if (!res[i]) {
3974249382dSDoug Rabson 			success = 0;
3984249382dSDoug Rabson 			break;
3994249382dSDoug Rabson 		}
4004249382dSDoug Rabson 	}
4014249382dSDoug Rabson 
4024249382dSDoug Rabson 	for (i = 0; i < ISA_NDRQ; i++) {
4034249382dSDoug Rabson 		if (res[i])
4044249382dSDoug Rabson 			bus_release_resource(child, SYS_RES_DRQ,
4054249382dSDoug Rabson 					     i, res[i]);
4064249382dSDoug Rabson 	}
4074249382dSDoug Rabson 
40809aafea1SWarner Losh 	return (success);
4094249382dSDoug Rabson }
4104249382dSDoug Rabson 
4114249382dSDoug Rabson /*
4124249382dSDoug Rabson  * Attempt to find a working set of resources for a device. Return
4134249382dSDoug Rabson  * non-zero if a working configuration is found.
4144249382dSDoug Rabson  */
4154249382dSDoug Rabson static int
4164249382dSDoug Rabson isa_assign_resources(device_t child)
4174249382dSDoug Rabson {
4184249382dSDoug Rabson 	struct isa_device *idev = DEVTOISA(child);
4194249382dSDoug Rabson 	struct isa_config_entry *ice;
4203b82ede0SJulian Elischer 	struct isa_config *cfg;
4210077e822SAlfred Perlstein 	const char *reason;
4224249382dSDoug Rabson 
4230077e822SAlfred Perlstein 	reason = "Empty ISA id_configs";
4243b82ede0SJulian Elischer 	cfg = malloc(sizeof(struct isa_config), M_TEMP, M_NOWAIT|M_ZERO);
4253b82ede0SJulian Elischer 	if (cfg == NULL)
4263b82ede0SJulian Elischer 		return(0);
4274249382dSDoug Rabson 	TAILQ_FOREACH(ice, &idev->id_configs, ice_link) {
428b2e4739fSAlfred Perlstein 		reason = "memory";
4293b82ede0SJulian Elischer 		if (!isa_find_memory(child, &ice->ice_config, cfg))
4304249382dSDoug Rabson 			continue;
431b2e4739fSAlfred Perlstein 		reason = "port";
4323b82ede0SJulian Elischer 		if (!isa_find_port(child, &ice->ice_config, cfg))
4334249382dSDoug Rabson 			continue;
434b2e4739fSAlfred Perlstein 		reason = "irq";
4353b82ede0SJulian Elischer 		if (!isa_find_irq(child, &ice->ice_config, cfg))
4364249382dSDoug Rabson 			continue;
437b2e4739fSAlfred Perlstein 		reason = "drq";
4383b82ede0SJulian Elischer 		if (!isa_find_drq(child, &ice->ice_config, cfg))
4394249382dSDoug Rabson 			continue;
4404249382dSDoug Rabson 
4414249382dSDoug Rabson 		/*
4424249382dSDoug Rabson 		 * A working configuration was found enable the device
4434249382dSDoug Rabson 		 * with this configuration.
4444249382dSDoug Rabson 		 */
445b2e4739fSAlfred Perlstein 		reason = "no callback";
4464249382dSDoug Rabson 		if (idev->id_config_cb) {
4474249382dSDoug Rabson 			idev->id_config_cb(idev->id_config_arg,
4483b82ede0SJulian Elischer 					   cfg, 1);
4493b82ede0SJulian Elischer 			free(cfg, M_TEMP);
45009aafea1SWarner Losh 			return (1);
4514249382dSDoug Rabson 		}
4524249382dSDoug Rabson 	}
4534249382dSDoug Rabson 
4544249382dSDoug Rabson 	/*
4554249382dSDoug Rabson 	 * Disable the device.
4564249382dSDoug Rabson 	 */
4572b2b44c9SJohn Baldwin 	bus_print_child_header(device_get_parent(child), child);
458b2e4739fSAlfred Perlstein 	printf(" can't assign resources (%s)\n", reason);
4592b2b44c9SJohn Baldwin 	if (bootverbose)
4602b2b44c9SJohn Baldwin 		isa_print_child(device_get_parent(child), child);
4613b82ede0SJulian Elischer 	bzero(cfg, sizeof (*cfg));
4624249382dSDoug Rabson 	if (idev->id_config_cb)
4633b82ede0SJulian Elischer 		idev->id_config_cb(idev->id_config_arg, cfg, 0);
4644249382dSDoug Rabson 	device_disable(child);
4654249382dSDoug Rabson 
4663b82ede0SJulian Elischer 	free(cfg, M_TEMP);
46709aafea1SWarner Losh 	return (0);
4684249382dSDoug Rabson }
4694249382dSDoug Rabson 
4704249382dSDoug Rabson void
4714249382dSDoug Rabson isa_probe_children(device_t dev)
4724249382dSDoug Rabson {
4734249382dSDoug Rabson 	device_t *children;
4743b82ede0SJulian Elischer 	struct isa_config *cfg;
4754249382dSDoug Rabson 	int nchildren, i;
4764249382dSDoug Rabson 
477ec22663bSDoug Rabson 	/*
478ec22663bSDoug Rabson 	 * Create all the children by calling driver's identify methods.
479ec22663bSDoug Rabson 	 */
480ec22663bSDoug Rabson 	bus_generic_probe(dev);
481ec22663bSDoug Rabson 
4824249382dSDoug Rabson 	if (device_get_children(dev, &children, &nchildren))
4834249382dSDoug Rabson 		return;
4844249382dSDoug Rabson 
4854249382dSDoug Rabson 	/*
486a2e6dbb3SDoug Rabson 	 * First disable all pnp devices so that they don't get
487a2e6dbb3SDoug Rabson 	 * matched by legacy probes.
488a2e6dbb3SDoug Rabson 	 */
489ec22663bSDoug Rabson 	if (bootverbose)
490ec22663bSDoug Rabson 		printf("isa_probe_children: disabling PnP devices\n");
4913b82ede0SJulian Elischer 
4923b82ede0SJulian Elischer 	cfg = malloc(sizeof(*cfg), M_TEMP, M_NOWAIT|M_ZERO);
4933b82ede0SJulian Elischer 	if (cfg == NULL) {
4943b82ede0SJulian Elischer 		free(children, M_TEMP);
4953b82ede0SJulian Elischer 		return;
4963b82ede0SJulian Elischer 	}
4973b82ede0SJulian Elischer 
498a2e6dbb3SDoug Rabson 	for (i = 0; i < nchildren; i++) {
499a2e6dbb3SDoug Rabson 		device_t child = children[i];
500a2e6dbb3SDoug Rabson 		struct isa_device *idev = DEVTOISA(child);
501a2e6dbb3SDoug Rabson 
5023b82ede0SJulian Elischer 		bzero(cfg, sizeof(*cfg));
503a2e6dbb3SDoug Rabson 		if (idev->id_config_cb)
5043b82ede0SJulian Elischer 			idev->id_config_cb(idev->id_config_arg, cfg, 0);
505a2e6dbb3SDoug Rabson 	}
506a2e6dbb3SDoug Rabson 
5073b82ede0SJulian Elischer 	free(cfg, M_TEMP);
5083b82ede0SJulian Elischer 
509a2e6dbb3SDoug Rabson 	/*
510a2e6dbb3SDoug Rabson 	 * Next probe all non-pnp devices so that they claim their
5114249382dSDoug Rabson 	 * resources first.
5124249382dSDoug Rabson 	 */
513ec22663bSDoug Rabson 	if (bootverbose)
514ec22663bSDoug Rabson 		printf("isa_probe_children: probing non-PnP devices\n");
5154249382dSDoug Rabson 	for (i = 0; i < nchildren; i++) {
5164249382dSDoug Rabson 		device_t child = children[i];
5174249382dSDoug Rabson 		struct isa_device *idev = DEVTOISA(child);
5184249382dSDoug Rabson 
5194249382dSDoug Rabson 		if (TAILQ_FIRST(&idev->id_configs))
5204249382dSDoug Rabson 			continue;
5214249382dSDoug Rabson 
5224249382dSDoug Rabson 		device_probe_and_attach(child);
5234249382dSDoug Rabson 	}
5244249382dSDoug Rabson 
5254249382dSDoug Rabson 	/*
526a2e6dbb3SDoug Rabson 	 * Finally assign resource to pnp devices and probe them.
5274249382dSDoug Rabson 	 */
528ec22663bSDoug Rabson 	if (bootverbose)
529ec22663bSDoug Rabson 		printf("isa_probe_children: probing PnP devices\n");
5304249382dSDoug Rabson 	for (i = 0; i < nchildren; i++) {
5314249382dSDoug Rabson 		device_t child = children[i];
5324249382dSDoug Rabson 		struct isa_device* idev = DEVTOISA(child);
5334249382dSDoug Rabson 
5344249382dSDoug Rabson 		if (!TAILQ_FIRST(&idev->id_configs))
5354249382dSDoug Rabson 			continue;
5364249382dSDoug Rabson 
5374249382dSDoug Rabson 		if (isa_assign_resources(child)) {
53825afb89bSDoug Rabson 			struct resource_list *rl = &idev->id_resources;
5394249382dSDoug Rabson 			struct resource_list_entry *rle;
5404249382dSDoug Rabson 
5414249382dSDoug Rabson 			device_probe_and_attach(child);
5424249382dSDoug Rabson 
5434249382dSDoug Rabson 			/*
5444249382dSDoug Rabson 			 * Claim any unallocated resources to keep other
5454249382dSDoug Rabson 			 * devices from using them.
5464249382dSDoug Rabson 			 */
54736fed965SWarner Losh 			STAILQ_FOREACH(rle, rl, link) {
5484249382dSDoug Rabson 				if (!rle->res) {
5494249382dSDoug Rabson 					int rid = rle->rid;
55025afb89bSDoug Rabson 					resource_list_alloc(rl, dev, child,
5514249382dSDoug Rabson 							    rle->type,
5524249382dSDoug Rabson 							    &rid,
553328d36e9SDoug Rabson 							    0, ~0, 1, 0);
5544249382dSDoug Rabson 				}
5554249382dSDoug Rabson 			}
5564249382dSDoug Rabson 		}
5574249382dSDoug Rabson 	}
5584249382dSDoug Rabson 
5594249382dSDoug Rabson 	free(children, M_TEMP);
560ec22663bSDoug Rabson 
561ec22663bSDoug Rabson 	isa_running = 1;
5624249382dSDoug Rabson }
5634249382dSDoug Rabson 
5644249382dSDoug Rabson /*
565a3be63b3SDoug Rabson  * Add a new child with default ivars.
566a3be63b3SDoug Rabson  */
567a3be63b3SDoug Rabson static device_t
568bea6af4dSDoug Rabson isa_add_child(device_t dev, int order, const char *name, int unit)
569a3be63b3SDoug Rabson {
570fe0d4089SMatthew N. Dodd 	device_t child;
571a3be63b3SDoug Rabson 	struct	isa_device *idev;
572a3be63b3SDoug Rabson 
573f46708e0SPoul-Henning Kamp 	child = device_add_child_ordered(dev, order, name, unit);
574f46708e0SPoul-Henning Kamp 	if (child == NULL)
575f46708e0SPoul-Henning Kamp 		return (child);
576f46708e0SPoul-Henning Kamp 
5777cc0979fSDavid Malone 	idev = malloc(sizeof(struct isa_device), M_ISADEV, M_NOWAIT | M_ZERO);
578a3be63b3SDoug Rabson 	if (!idev)
57909aafea1SWarner Losh 		return (0);
580a3be63b3SDoug Rabson 
581a3be63b3SDoug Rabson 	resource_list_init(&idev->id_resources);
5824249382dSDoug Rabson 	TAILQ_INIT(&idev->id_configs);
583a3be63b3SDoug Rabson 
584fe0d4089SMatthew N. Dodd 	device_set_ivars(child, idev);
585fe0d4089SMatthew N. Dodd 
586f46708e0SPoul-Henning Kamp 	return (child);
587a3be63b3SDoug Rabson }
588a3be63b3SDoug Rabson 
589328d36e9SDoug Rabson static int
590328d36e9SDoug Rabson isa_print_all_resources(device_t dev)
591a3be63b3SDoug Rabson {
592a3be63b3SDoug Rabson 	struct	isa_device *idev = DEVTOISA(dev);
593a3be63b3SDoug Rabson 	struct resource_list *rl = &idev->id_resources;
59415317dd8SMatthew N. Dodd 	int retval = 0;
59515317dd8SMatthew N. Dodd 
59636fed965SWarner Losh 	if (STAILQ_FIRST(rl) || device_get_flags(dev))
59715317dd8SMatthew N. Dodd 		retval += printf(" at");
598a3be63b3SDoug Rabson 
59901f1aed2SThomas Moestl 	retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
60001f1aed2SThomas Moestl 	retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
60101f1aed2SThomas Moestl 	retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
60201f1aed2SThomas Moestl 	retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%ld");
603062acdb7SDoug Rabson 	if (device_get_flags(dev))
604062acdb7SDoug Rabson 		retval += printf(" flags %#x", device_get_flags(dev));
605acb8c149SMarius Strobl #ifdef ISAPNP
606a81804d9SWarner Losh 	if (idev->id_vendorid)
607a81804d9SWarner Losh 		retval += printf(" pnpid %s", pnp_eisaformat(idev->id_vendorid));
608acb8c149SMarius Strobl #endif
609a3be63b3SDoug Rabson 
61009aafea1SWarner Losh 	return (retval);
611328d36e9SDoug Rabson }
612328d36e9SDoug Rabson 
613328d36e9SDoug Rabson static int
614328d36e9SDoug Rabson isa_print_child(device_t bus, device_t dev)
615328d36e9SDoug Rabson {
616328d36e9SDoug Rabson 	int retval = 0;
617328d36e9SDoug Rabson 
618328d36e9SDoug Rabson 	retval += bus_print_child_header(bus, dev);
619328d36e9SDoug Rabson 	retval += isa_print_all_resources(dev);
62015317dd8SMatthew N. Dodd 	retval += bus_print_child_footer(bus, dev);
62115317dd8SMatthew N. Dodd 
62215317dd8SMatthew N. Dodd 	return (retval);
623a3be63b3SDoug Rabson }
624a3be63b3SDoug Rabson 
625328d36e9SDoug Rabson static void
626328d36e9SDoug Rabson isa_probe_nomatch(device_t dev, device_t child)
627328d36e9SDoug Rabson {
6282b2b44c9SJohn Baldwin 	if (bootverbose) {
6292b2b44c9SJohn Baldwin 		bus_print_child_header(dev, child);
6302b2b44c9SJohn Baldwin 		printf(" failed to probe");
631328d36e9SDoug Rabson 		isa_print_all_resources(child);
6322b2b44c9SJohn Baldwin 		bus_print_child_footer(dev, child);
6332b2b44c9SJohn Baldwin 	}
634328d36e9SDoug Rabson 
635328d36e9SDoug Rabson 	return;
636328d36e9SDoug Rabson }
637328d36e9SDoug Rabson 
638a3be63b3SDoug Rabson static int
639a3be63b3SDoug Rabson isa_read_ivar(device_t bus, device_t dev, int index, uintptr_t * result)
640a3be63b3SDoug Rabson {
641a3be63b3SDoug Rabson 	struct isa_device* idev = DEVTOISA(dev);
642a3be63b3SDoug Rabson 	struct resource_list *rl = &idev->id_resources;
643a3be63b3SDoug Rabson 	struct resource_list_entry *rle;
644a3be63b3SDoug Rabson 
645a3be63b3SDoug Rabson 	switch (index) {
646a3be63b3SDoug Rabson 	case ISA_IVAR_PORT_0:
647a3be63b3SDoug Rabson 		rle = resource_list_find(rl, SYS_RES_IOPORT, 0);
648a3be63b3SDoug Rabson 		if (rle)
649a3be63b3SDoug Rabson 			*result = rle->start;
650a3be63b3SDoug Rabson 		else
651a3be63b3SDoug Rabson 			*result = -1;
652a3be63b3SDoug Rabson 		break;
653a3be63b3SDoug Rabson 
654a3be63b3SDoug Rabson 	case ISA_IVAR_PORT_1:
655a3be63b3SDoug Rabson 		rle = resource_list_find(rl, SYS_RES_IOPORT, 1);
656a3be63b3SDoug Rabson 		if (rle)
657a3be63b3SDoug Rabson 			*result = rle->start;
658a3be63b3SDoug Rabson 		else
659a3be63b3SDoug Rabson 			*result = -1;
660a3be63b3SDoug Rabson 		break;
661a3be63b3SDoug Rabson 
662a3be63b3SDoug Rabson 	case ISA_IVAR_PORTSIZE_0:
663a3be63b3SDoug Rabson 		rle = resource_list_find(rl, SYS_RES_IOPORT, 0);
664a3be63b3SDoug Rabson 		if (rle)
665a3be63b3SDoug Rabson 			*result = rle->count;
666a3be63b3SDoug Rabson 		else
667a3be63b3SDoug Rabson 			*result = 0;
668a3be63b3SDoug Rabson 		break;
669a3be63b3SDoug Rabson 
670a3be63b3SDoug Rabson 	case ISA_IVAR_PORTSIZE_1:
671a3be63b3SDoug Rabson 		rle = resource_list_find(rl, SYS_RES_IOPORT, 1);
672a3be63b3SDoug Rabson 		if (rle)
673a3be63b3SDoug Rabson 			*result = rle->count;
674a3be63b3SDoug Rabson 		else
675a3be63b3SDoug Rabson 			*result = 0;
676a3be63b3SDoug Rabson 		break;
677a3be63b3SDoug Rabson 
678a3be63b3SDoug Rabson 	case ISA_IVAR_MADDR_0:
679a3be63b3SDoug Rabson 		rle = resource_list_find(rl, SYS_RES_MEMORY, 0);
680a3be63b3SDoug Rabson 		if (rle)
681a3be63b3SDoug Rabson 			*result = rle->start;
682a3be63b3SDoug Rabson 		else
683a3be63b3SDoug Rabson 			*result = -1;
684a3be63b3SDoug Rabson 		break;
685a3be63b3SDoug Rabson 
686a3be63b3SDoug Rabson 	case ISA_IVAR_MADDR_1:
687a3be63b3SDoug Rabson 		rle = resource_list_find(rl, SYS_RES_MEMORY, 1);
688a3be63b3SDoug Rabson 		if (rle)
689a3be63b3SDoug Rabson 			*result = rle->start;
690a3be63b3SDoug Rabson 		else
691a3be63b3SDoug Rabson 			*result = -1;
692a3be63b3SDoug Rabson 		break;
693a3be63b3SDoug Rabson 
69463e30378SJohn Baldwin 	case ISA_IVAR_MEMSIZE_0:
695a3be63b3SDoug Rabson 		rle = resource_list_find(rl, SYS_RES_MEMORY, 0);
696a3be63b3SDoug Rabson 		if (rle)
697a3be63b3SDoug Rabson 			*result = rle->count;
698a3be63b3SDoug Rabson 		else
699a3be63b3SDoug Rabson 			*result = 0;
700a3be63b3SDoug Rabson 		break;
701a3be63b3SDoug Rabson 
70263e30378SJohn Baldwin 	case ISA_IVAR_MEMSIZE_1:
703a3be63b3SDoug Rabson 		rle = resource_list_find(rl, SYS_RES_MEMORY, 1);
704a3be63b3SDoug Rabson 		if (rle)
705a3be63b3SDoug Rabson 			*result = rle->count;
706a3be63b3SDoug Rabson 		else
707a3be63b3SDoug Rabson 			*result = 0;
708a3be63b3SDoug Rabson 		break;
709a3be63b3SDoug Rabson 
710a3be63b3SDoug Rabson 	case ISA_IVAR_IRQ_0:
711a3be63b3SDoug Rabson 		rle = resource_list_find(rl, SYS_RES_IRQ, 0);
712a3be63b3SDoug Rabson 		if (rle)
713a3be63b3SDoug Rabson 			*result = rle->start;
714a3be63b3SDoug Rabson 		else
715a3be63b3SDoug Rabson 			*result = -1;
716a3be63b3SDoug Rabson 		break;
717a3be63b3SDoug Rabson 
718a3be63b3SDoug Rabson 	case ISA_IVAR_IRQ_1:
719a3be63b3SDoug Rabson 		rle = resource_list_find(rl, SYS_RES_IRQ, 1);
720a3be63b3SDoug Rabson 		if (rle)
721a3be63b3SDoug Rabson 			*result = rle->start;
722a3be63b3SDoug Rabson 		else
723a3be63b3SDoug Rabson 			*result = -1;
724a3be63b3SDoug Rabson 		break;
725a3be63b3SDoug Rabson 
726a3be63b3SDoug Rabson 	case ISA_IVAR_DRQ_0:
727a3be63b3SDoug Rabson 		rle = resource_list_find(rl, SYS_RES_DRQ, 0);
728a3be63b3SDoug Rabson 		if (rle)
729a3be63b3SDoug Rabson 			*result = rle->start;
730a3be63b3SDoug Rabson 		else
731a3be63b3SDoug Rabson 			*result = -1;
732a3be63b3SDoug Rabson 		break;
733a3be63b3SDoug Rabson 
734a3be63b3SDoug Rabson 	case ISA_IVAR_DRQ_1:
735a3be63b3SDoug Rabson 		rle = resource_list_find(rl, SYS_RES_DRQ, 1);
736a3be63b3SDoug Rabson 		if (rle)
737a3be63b3SDoug Rabson 			*result = rle->start;
738a3be63b3SDoug Rabson 		else
739a3be63b3SDoug Rabson 			*result = -1;
740a3be63b3SDoug Rabson 		break;
741a3be63b3SDoug Rabson 
742cfa84f46SDoug Rabson 	case ISA_IVAR_VENDORID:
743cfa84f46SDoug Rabson 		*result = idev->id_vendorid;
744cfa84f46SDoug Rabson 		break;
745cfa84f46SDoug Rabson 
746cfa84f46SDoug Rabson 	case ISA_IVAR_SERIAL:
747cfa84f46SDoug Rabson 		*result = idev->id_serial;
748cfa84f46SDoug Rabson 		break;
749cfa84f46SDoug Rabson 
750cfa84f46SDoug Rabson 	case ISA_IVAR_LOGICALID:
751cfa84f46SDoug Rabson 		*result = idev->id_logicalid;
752cfa84f46SDoug Rabson 		break;
753cfa84f46SDoug Rabson 
754cfa84f46SDoug Rabson 	case ISA_IVAR_COMPATID:
755cfa84f46SDoug Rabson 		*result = idev->id_compatid;
756cfa84f46SDoug Rabson 		break;
757cfa84f46SDoug Rabson 
7587abb4662SKazutaka YOKOTA 	case ISA_IVAR_CONFIGATTR:
7597abb4662SKazutaka YOKOTA 		*result = idev->id_config_attr;
7607abb4662SKazutaka YOKOTA 		break;
7617abb4662SKazutaka YOKOTA 
762132580b5SWarner Losh 	case ISA_IVAR_PNP_CSN:
763132580b5SWarner Losh 		*result = idev->id_pnp_csn;
764132580b5SWarner Losh 		break;
765132580b5SWarner Losh 
766132580b5SWarner Losh 	case ISA_IVAR_PNP_LDN:
767132580b5SWarner Losh 		*result = idev->id_pnp_ldn;
768132580b5SWarner Losh 		break;
769132580b5SWarner Losh 
770132580b5SWarner Losh 	case ISA_IVAR_PNPBIOS_HANDLE:
771132580b5SWarner Losh 		*result = idev->id_pnpbios_handle;
772132580b5SWarner Losh 		break;
773132580b5SWarner Losh 
774cfa84f46SDoug Rabson 	default:
77509aafea1SWarner Losh 		return (ENOENT);
776a3be63b3SDoug Rabson 	}
777a3be63b3SDoug Rabson 
77809aafea1SWarner Losh 	return (0);
779cfa84f46SDoug Rabson }
780cfa84f46SDoug Rabson 
781a3be63b3SDoug Rabson static int
78209aafea1SWarner Losh isa_write_ivar(device_t bus, device_t dev, int index, uintptr_t value)
783a3be63b3SDoug Rabson {
784a3be63b3SDoug Rabson 	struct isa_device* idev = DEVTOISA(dev);
785a3be63b3SDoug Rabson 
786a3be63b3SDoug Rabson 	switch (index) {
787a3be63b3SDoug Rabson 	case ISA_IVAR_PORT_0:
788a3be63b3SDoug Rabson 	case ISA_IVAR_PORT_1:
789a3be63b3SDoug Rabson 	case ISA_IVAR_PORTSIZE_0:
790a3be63b3SDoug Rabson 	case ISA_IVAR_PORTSIZE_1:
791a3be63b3SDoug Rabson 	case ISA_IVAR_MADDR_0:
792a3be63b3SDoug Rabson 	case ISA_IVAR_MADDR_1:
79363e30378SJohn Baldwin 	case ISA_IVAR_MEMSIZE_0:
79463e30378SJohn Baldwin 	case ISA_IVAR_MEMSIZE_1:
795a3be63b3SDoug Rabson 	case ISA_IVAR_IRQ_0:
796a3be63b3SDoug Rabson 	case ISA_IVAR_IRQ_1:
797a3be63b3SDoug Rabson 	case ISA_IVAR_DRQ_0:
798a3be63b3SDoug Rabson 	case ISA_IVAR_DRQ_1:
79909aafea1SWarner Losh 		return (EINVAL);
800a3be63b3SDoug Rabson 
801cfa84f46SDoug Rabson 	case ISA_IVAR_VENDORID:
802cfa84f46SDoug Rabson 		idev->id_vendorid = value;
803cfa84f46SDoug Rabson 		break;
804cfa84f46SDoug Rabson 
805cfa84f46SDoug Rabson 	case ISA_IVAR_SERIAL:
806cfa84f46SDoug Rabson 		idev->id_serial = value;
807cfa84f46SDoug Rabson 		break;
808cfa84f46SDoug Rabson 
809cfa84f46SDoug Rabson 	case ISA_IVAR_LOGICALID:
810cfa84f46SDoug Rabson 		idev->id_logicalid = value;
811cfa84f46SDoug Rabson 		break;
812cfa84f46SDoug Rabson 
813cfa84f46SDoug Rabson 	case ISA_IVAR_COMPATID:
814cfa84f46SDoug Rabson 		idev->id_compatid = value;
815cfa84f46SDoug Rabson 		break;
816cfa84f46SDoug Rabson 
8177abb4662SKazutaka YOKOTA 	case ISA_IVAR_CONFIGATTR:
8187abb4662SKazutaka YOKOTA 		idev->id_config_attr = value;
8197abb4662SKazutaka YOKOTA 		break;
8207abb4662SKazutaka YOKOTA 
821a3be63b3SDoug Rabson 	default:
822a3be63b3SDoug Rabson 		return (ENOENT);
823a3be63b3SDoug Rabson 	}
824cfa84f46SDoug Rabson 
825a3be63b3SDoug Rabson 	return (0);
826a3be63b3SDoug Rabson }
827a3be63b3SDoug Rabson 
8284249382dSDoug Rabson /*
8294249382dSDoug Rabson  * Free any resources which the driver missed or which we were holding for
8304249382dSDoug Rabson  * it (see isa_probe_children).
8314249382dSDoug Rabson  */
8324249382dSDoug Rabson static void
8334249382dSDoug Rabson isa_child_detached(device_t dev, device_t child)
8344249382dSDoug Rabson {
8354249382dSDoug Rabson 	struct isa_device* idev = DEVTOISA(child);
83625afb89bSDoug Rabson 	struct resource_list *rl = &idev->id_resources;
8374249382dSDoug Rabson 	struct resource_list_entry *rle;
8384249382dSDoug Rabson 
839328d36e9SDoug Rabson 	if (TAILQ_FIRST(&idev->id_configs)) {
840328d36e9SDoug Rabson 		/*
841328d36e9SDoug Rabson 		 * Claim any unallocated resources to keep other
842328d36e9SDoug Rabson 		 * devices from using them.
843328d36e9SDoug Rabson 		 */
84436fed965SWarner Losh 		STAILQ_FOREACH(rle, rl, link) {
845328d36e9SDoug Rabson 			if (!rle->res) {
846328d36e9SDoug Rabson 				int rid = rle->rid;
847328d36e9SDoug Rabson 				resource_list_alloc(rl, dev, child,
8484249382dSDoug Rabson 						    rle->type,
849328d36e9SDoug Rabson 						    &rid, 0, ~0, 1, 0);
850328d36e9SDoug Rabson 			}
851328d36e9SDoug Rabson 		}
8524249382dSDoug Rabson 	}
8534249382dSDoug Rabson }
8544249382dSDoug Rabson 
855ec22663bSDoug Rabson static void
856ec22663bSDoug Rabson isa_driver_added(device_t dev, driver_t *driver)
857ec22663bSDoug Rabson {
858ec22663bSDoug Rabson 	device_t *children;
859ec22663bSDoug Rabson 	int nchildren, i;
860ec22663bSDoug Rabson 
861ec22663bSDoug Rabson 	/*
862ec22663bSDoug Rabson 	 * Don't do anything if drivers are dynamically
863ec22663bSDoug Rabson 	 * added during autoconfiguration (cf. ymf724).
864ec22663bSDoug Rabson 	 * since that would end up calling identify
865ec22663bSDoug Rabson 	 * twice.
866ec22663bSDoug Rabson 	 */
867ec22663bSDoug Rabson 	if (!isa_running)
868ec22663bSDoug Rabson 		return;
869ec22663bSDoug Rabson 
870ec22663bSDoug Rabson 	DEVICE_IDENTIFY(driver, dev);
871ec22663bSDoug Rabson 	if (device_get_children(dev, &children, &nchildren))
872ec22663bSDoug Rabson 		return;
873ec22663bSDoug Rabson 
874ec22663bSDoug Rabson 	for (i = 0; i < nchildren; i++) {
875ec22663bSDoug Rabson 		device_t child = children[i];
876ec22663bSDoug Rabson 		struct isa_device *idev = DEVTOISA(child);
877ec22663bSDoug Rabson 		struct resource_list *rl = &idev->id_resources;
878ec22663bSDoug Rabson 		struct resource_list_entry *rle;
879ec22663bSDoug Rabson 
880ec22663bSDoug Rabson 		if (device_get_state(child) != DS_NOTPRESENT)
881ec22663bSDoug Rabson 			continue;
882328d36e9SDoug Rabson 		if (!device_is_enabled(child))
883328d36e9SDoug Rabson 			continue;
884328d36e9SDoug Rabson 
885328d36e9SDoug Rabson 		/*
886328d36e9SDoug Rabson 		 * Free resources which we were holding on behalf of
887328d36e9SDoug Rabson 		 * the device.
888328d36e9SDoug Rabson 		 */
88936fed965SWarner Losh 		STAILQ_FOREACH(rle, &idev->id_resources, link) {
890328d36e9SDoug Rabson 			if (rle->res)
891328d36e9SDoug Rabson 				resource_list_release(rl, dev, child,
892328d36e9SDoug Rabson 						      rle->type,
893328d36e9SDoug Rabson 						      rle->rid,
894328d36e9SDoug Rabson 						      rle->res);
895328d36e9SDoug Rabson 		}
896ec22663bSDoug Rabson 
897ec22663bSDoug Rabson 		if (TAILQ_FIRST(&idev->id_configs))
898ec22663bSDoug Rabson 			if (!isa_assign_resources(child))
899ec22663bSDoug Rabson 				continue;
900ec22663bSDoug Rabson 
901ec22663bSDoug Rabson 		device_probe_and_attach(child);
902ec22663bSDoug Rabson 
903ec22663bSDoug Rabson 		if (TAILQ_FIRST(&idev->id_configs)) {
904ec22663bSDoug Rabson 			/*
905ec22663bSDoug Rabson 			 * Claim any unallocated resources to keep other
906ec22663bSDoug Rabson 			 * devices from using them.
907ec22663bSDoug Rabson 			 */
90836fed965SWarner Losh 			STAILQ_FOREACH(rle, rl, link) {
909ec22663bSDoug Rabson 				if (!rle->res) {
910ec22663bSDoug Rabson 					int rid = rle->rid;
911ec22663bSDoug Rabson 					resource_list_alloc(rl, dev, child,
912ec22663bSDoug Rabson 							    rle->type,
913328d36e9SDoug Rabson 							    &rid, 0, ~0, 1, 0);
914ec22663bSDoug Rabson 				}
915ec22663bSDoug Rabson 			}
916ec22663bSDoug Rabson 		}
917ec22663bSDoug Rabson 	}
918ec22663bSDoug Rabson 
919ec22663bSDoug Rabson 	free(children, M_TEMP);
920ec22663bSDoug Rabson }
921ec22663bSDoug Rabson 
922a3be63b3SDoug Rabson static int
923a3be63b3SDoug Rabson isa_set_resource(device_t dev, device_t child, int type, int rid,
924a3be63b3SDoug Rabson     u_long start, u_long count)
925a3be63b3SDoug Rabson {
926a3be63b3SDoug Rabson 	struct isa_device* idev = DEVTOISA(child);
927a3be63b3SDoug Rabson 	struct resource_list *rl = &idev->id_resources;
928a3be63b3SDoug Rabson 
929a3be63b3SDoug Rabson 	if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
930a3be63b3SDoug Rabson 	    && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
93109aafea1SWarner Losh 		return (EINVAL);
9324249382dSDoug Rabson 	if (rid < 0)
93309aafea1SWarner Losh 		return (EINVAL);
9344249382dSDoug Rabson 	if (type == SYS_RES_IOPORT && rid >= ISA_NPORT)
93509aafea1SWarner Losh 		return (EINVAL);
9364249382dSDoug Rabson 	if (type == SYS_RES_MEMORY && rid >= ISA_NMEM)
93709aafea1SWarner Losh 		return (EINVAL);
9384249382dSDoug Rabson 	if (type == SYS_RES_IRQ && rid >= ISA_NIRQ)
93909aafea1SWarner Losh 		return (EINVAL);
9404249382dSDoug Rabson 	if (type == SYS_RES_DRQ && rid >= ISA_NDRQ)
94109aafea1SWarner Losh 		return (EINVAL);
942a3be63b3SDoug Rabson 
943a3be63b3SDoug Rabson 	resource_list_add(rl, type, rid, start, start + count - 1, count);
944a3be63b3SDoug Rabson 
94509aafea1SWarner Losh 	return (0);
946a3be63b3SDoug Rabson }
947a3be63b3SDoug Rabson 
9481581afb3SMatthew N. Dodd static struct resource_list *
9491581afb3SMatthew N. Dodd isa_get_resource_list (device_t dev, device_t child)
950a3be63b3SDoug Rabson {
951a3be63b3SDoug Rabson 	struct isa_device* idev = DEVTOISA(child);
952a3be63b3SDoug Rabson 	struct resource_list *rl = &idev->id_resources;
953a3be63b3SDoug Rabson 
9541581afb3SMatthew N. Dodd 	if (!rl)
9551581afb3SMatthew N. Dodd 		return (NULL);
956a3be63b3SDoug Rabson 
9571581afb3SMatthew N. Dodd 	return (rl);
958cfa84f46SDoug Rabson }
959cfa84f46SDoug Rabson 
9604249382dSDoug Rabson static int
96109aafea1SWarner Losh isa_add_config(device_t dev, device_t child, int priority,
96209aafea1SWarner Losh     struct isa_config *config)
9634249382dSDoug Rabson {
9644249382dSDoug Rabson 	struct isa_device* idev = DEVTOISA(child);
9654249382dSDoug Rabson 	struct isa_config_entry *newice, *ice;
9664249382dSDoug Rabson 
9674249382dSDoug Rabson 	newice = malloc(sizeof *ice, M_DEVBUF, M_NOWAIT);
9684249382dSDoug Rabson 	if (!newice)
96909aafea1SWarner Losh 		return (ENOMEM);
9704249382dSDoug Rabson 
9714249382dSDoug Rabson 	newice->ice_priority = priority;
9724249382dSDoug Rabson 	newice->ice_config = *config;
9734249382dSDoug Rabson 
9744249382dSDoug Rabson 	TAILQ_FOREACH(ice, &idev->id_configs, ice_link) {
9754249382dSDoug Rabson 		if (ice->ice_priority > priority)
9764249382dSDoug Rabson 			break;
9774249382dSDoug Rabson 	}
9784249382dSDoug Rabson 	if (ice)
9794249382dSDoug Rabson 		TAILQ_INSERT_BEFORE(ice, newice, ice_link);
9804249382dSDoug Rabson 	else
9814249382dSDoug Rabson 		TAILQ_INSERT_TAIL(&idev->id_configs, newice, ice_link);
9824249382dSDoug Rabson 
98309aafea1SWarner Losh 	return (0);
9844249382dSDoug Rabson }
9854249382dSDoug Rabson 
9864249382dSDoug Rabson static void
98709aafea1SWarner Losh isa_set_config_callback(device_t dev, device_t child, isa_config_cb *fn,
98809aafea1SWarner Losh     void *arg)
9894249382dSDoug Rabson {
9904249382dSDoug Rabson 	struct isa_device* idev = DEVTOISA(child);
9914249382dSDoug Rabson 
9924249382dSDoug Rabson 	idev->id_config_cb = fn;
9934249382dSDoug Rabson 	idev->id_config_arg = arg;
9944249382dSDoug Rabson }
9954249382dSDoug Rabson 
9964249382dSDoug Rabson static int
9974249382dSDoug Rabson isa_pnp_probe(device_t dev, device_t child, struct isa_pnp_id *ids)
9984249382dSDoug Rabson {
9994249382dSDoug Rabson 	struct isa_device* idev = DEVTOISA(child);
10004249382dSDoug Rabson 
10014249382dSDoug Rabson 	if (!idev->id_vendorid)
100209aafea1SWarner Losh 		return (ENOENT);
10034249382dSDoug Rabson 
10042e5bbc3fSMike Smith 	while (ids && ids->ip_id) {
10054249382dSDoug Rabson 		/*
10064249382dSDoug Rabson 		 * Really ought to support >1 compat id per device.
10074249382dSDoug Rabson 		 */
10084249382dSDoug Rabson 		if (idev->id_logicalid == ids->ip_id
10094249382dSDoug Rabson 		    || idev->id_compatid == ids->ip_id) {
10102aadbbb6SDoug Rabson 			if (ids->ip_desc)
10114249382dSDoug Rabson 				device_set_desc(child, ids->ip_desc);
101209aafea1SWarner Losh 			return (0);
10134249382dSDoug Rabson 		}
10144249382dSDoug Rabson 		ids++;
10154249382dSDoug Rabson 	}
10164249382dSDoug Rabson 
101709aafea1SWarner Losh 	return (ENXIO);
10184249382dSDoug Rabson }
10194249382dSDoug Rabson 
1020a81804d9SWarner Losh static int
1021a81804d9SWarner Losh isa_child_pnpinfo_str(device_t bus, device_t child, char *buf,
1022a81804d9SWarner Losh     size_t buflen)
1023a81804d9SWarner Losh {
1024acb8c149SMarius Strobl #ifdef ISAPNP
1025a81804d9SWarner Losh 	struct isa_device *idev = DEVTOISA(child);
1026a81804d9SWarner Losh 
1027a81804d9SWarner Losh 	if (idev->id_vendorid)
1028a81804d9SWarner Losh 		snprintf(buf, buflen, "pnpid=%s",
1029a81804d9SWarner Losh 		    pnp_eisaformat(idev->id_vendorid));
1030acb8c149SMarius Strobl #endif
1031a81804d9SWarner Losh 	return (0);
1032a81804d9SWarner Losh }
1033a81804d9SWarner Losh 
1034a81804d9SWarner Losh static int
1035a81804d9SWarner Losh isa_child_location_str(device_t bus, device_t child, char *buf,
1036a81804d9SWarner Losh     size_t buflen)
1037a81804d9SWarner Losh {
1038132580b5SWarner Losh #if 0
1039132580b5SWarner Losh 	/* id_pnphandle isn't there yet */
1040132580b5SWarner Losh 	struct isa_device *idev = DEVTOISA(child);
1041132580b5SWarner Losh 
1042132580b5SWarner Losh 	if (idev->id_vendorid)
1043132580b5SWarner Losh 		snprintf(buf, buflen, "pnphandle=%d", idev->id_pnphandle);
1044132580b5SWarner Losh #endif
1045a81804d9SWarner Losh 	/* Nothing here yet */
1046a81804d9SWarner Losh 	*buf = '\0';
1047a81804d9SWarner Losh 	return (0);
1048a81804d9SWarner Losh }
1049a81804d9SWarner Losh 
1050a3be63b3SDoug Rabson static device_method_t isa_methods[] = {
1051a3be63b3SDoug Rabson 	/* Device interface */
1052a3be63b3SDoug Rabson 	DEVMETHOD(device_probe,		isa_probe),
1053a3be63b3SDoug Rabson 	DEVMETHOD(device_attach,	isa_attach),
1054a3be63b3SDoug Rabson 	DEVMETHOD(device_detach,	bus_generic_detach),
1055a3be63b3SDoug Rabson 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
1056a3be63b3SDoug Rabson 	DEVMETHOD(device_suspend,	bus_generic_suspend),
1057a3be63b3SDoug Rabson 	DEVMETHOD(device_resume,	bus_generic_resume),
1058a3be63b3SDoug Rabson 
1059a3be63b3SDoug Rabson 	/* Bus interface */
1060a3be63b3SDoug Rabson 	DEVMETHOD(bus_add_child,	isa_add_child),
1061a3be63b3SDoug Rabson 	DEVMETHOD(bus_print_child,	isa_print_child),
1062328d36e9SDoug Rabson 	DEVMETHOD(bus_probe_nomatch,	isa_probe_nomatch),
1063a3be63b3SDoug Rabson 	DEVMETHOD(bus_read_ivar,	isa_read_ivar),
1064a3be63b3SDoug Rabson 	DEVMETHOD(bus_write_ivar,	isa_write_ivar),
10654249382dSDoug Rabson 	DEVMETHOD(bus_child_detached,	isa_child_detached),
1066ec22663bSDoug Rabson 	DEVMETHOD(bus_driver_added,	isa_driver_added),
1067a3be63b3SDoug Rabson 	DEVMETHOD(bus_setup_intr,	isa_setup_intr),
1068a3be63b3SDoug Rabson 	DEVMETHOD(bus_teardown_intr,	isa_teardown_intr),
10691581afb3SMatthew N. Dodd 
10701581afb3SMatthew N. Dodd 	DEVMETHOD(bus_get_resource_list,isa_get_resource_list),
10711581afb3SMatthew N. Dodd 	DEVMETHOD(bus_alloc_resource,	isa_alloc_resource),
10721581afb3SMatthew N. Dodd 	DEVMETHOD(bus_release_resource,	isa_release_resource),
107325afb89bSDoug Rabson 	DEVMETHOD(bus_set_resource,	isa_set_resource),
10741581afb3SMatthew N. Dodd 	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
10751581afb3SMatthew N. Dodd 	DEVMETHOD(bus_delete_resource,	bus_generic_rl_delete_resource),
10761581afb3SMatthew N. Dodd 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
10771581afb3SMatthew N. Dodd 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
1078a81804d9SWarner Losh 	DEVMETHOD(bus_child_pnpinfo_str, isa_child_pnpinfo_str),
1079a81804d9SWarner Losh 	DEVMETHOD(bus_child_location_str, isa_child_location_str),
1080a3be63b3SDoug Rabson 
1081a3be63b3SDoug Rabson 	/* ISA interface */
10824249382dSDoug Rabson 	DEVMETHOD(isa_add_config,	isa_add_config),
10834249382dSDoug Rabson 	DEVMETHOD(isa_set_config_callback, isa_set_config_callback),
10844249382dSDoug Rabson 	DEVMETHOD(isa_pnp_probe,	isa_pnp_probe),
1085a3be63b3SDoug Rabson 
1086a3be63b3SDoug Rabson 	{ 0, 0 }
1087a3be63b3SDoug Rabson };
1088a3be63b3SDoug Rabson 
1089812fb8f2SWarner Losh driver_t isa_driver = {
1090a3be63b3SDoug Rabson 	"isa",
1091a3be63b3SDoug Rabson 	isa_methods,
1092a3be63b3SDoug Rabson 	1,			/* no softc */
1093a3be63b3SDoug Rabson };
1094a3be63b3SDoug Rabson 
1095812fb8f2SWarner Losh devclass_t isa_devclass;
1096812fb8f2SWarner Losh 
1097a3be63b3SDoug Rabson /*
1098812fb8f2SWarner Losh  * ISA can be attached to a PCI-ISA bridge, or other locations on some
1099812fb8f2SWarner Losh  * platforms.
1100a3be63b3SDoug Rabson  */
1101a3be63b3SDoug Rabson DRIVER_MODULE(isa, isab, isa_driver, isa_devclass, 0, 0);
11025aea03b0SMike Smith DRIVER_MODULE(isa, eisab, isa_driver, isa_devclass, 0, 0);
1103f7130d09SMatthew N. Dodd MODULE_VERSION(isa, 1);
1104c37faf26SJohn Baldwin 
1105c37faf26SJohn Baldwin /*
1106c37faf26SJohn Baldwin  * Code common to ISA bridges.
1107c37faf26SJohn Baldwin  */
1108c37faf26SJohn Baldwin 
1109c37faf26SJohn Baldwin devclass_t isab_devclass;
1110c37faf26SJohn Baldwin 
1111c37faf26SJohn Baldwin int
1112c37faf26SJohn Baldwin isab_attach(device_t dev)
1113c37faf26SJohn Baldwin {
1114c37faf26SJohn Baldwin 	device_t child;
1115c37faf26SJohn Baldwin 
1116c37faf26SJohn Baldwin 	child = device_add_child(dev, "isa", 0);
1117c37faf26SJohn Baldwin 	if (child != NULL)
1118c37faf26SJohn Baldwin 		return (bus_generic_attach(dev));
1119c37faf26SJohn Baldwin 	return (ENXIO);
1120c37faf26SJohn Baldwin }
1121