xref: /freebsd/sys/isa/isa_common.c (revision ec22663bedf26566f31ca0c985e525abec12b6a5)
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  *
26c3aac50fSPeter Wemm  * $FreeBSD$
27a3be63b3SDoug Rabson  */
28a3be63b3SDoug Rabson /*
29a3be63b3SDoug Rabson  * Modifications for Intel architecture by Garrett A. Wollman.
30a3be63b3SDoug Rabson  * Copyright 1998 Massachusetts Institute of Technology
31a3be63b3SDoug Rabson  *
32a3be63b3SDoug Rabson  * Permission to use, copy, modify, and distribute this software and
33a3be63b3SDoug Rabson  * its documentation for any purpose and without fee is hereby
34a3be63b3SDoug Rabson  * granted, provided that both the above copyright notice and this
35a3be63b3SDoug Rabson  * permission notice appear in all copies, that both the above
36a3be63b3SDoug Rabson  * copyright notice and this permission notice appear in all
37a3be63b3SDoug Rabson  * supporting documentation, and that the name of M.I.T. not be used
38a3be63b3SDoug Rabson  * in advertising or publicity pertaining to distribution of the
39a3be63b3SDoug Rabson  * software without specific, written prior permission.  M.I.T. makes
40a3be63b3SDoug Rabson  * no representations about the suitability of this software for any
41a3be63b3SDoug Rabson  * purpose.  It is provided "as is" without express or implied
42a3be63b3SDoug Rabson  * warranty.
43a3be63b3SDoug Rabson  *
44a3be63b3SDoug Rabson  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
45a3be63b3SDoug Rabson  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
46a3be63b3SDoug Rabson  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
47a3be63b3SDoug Rabson  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
48a3be63b3SDoug Rabson  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49a3be63b3SDoug Rabson  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
50a3be63b3SDoug Rabson  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
51a3be63b3SDoug Rabson  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
52a3be63b3SDoug Rabson  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
53a3be63b3SDoug Rabson  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
54a3be63b3SDoug Rabson  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55a3be63b3SDoug Rabson  * SUCH DAMAGE.
56a3be63b3SDoug Rabson  */
57a3be63b3SDoug Rabson 
58a3be63b3SDoug Rabson /*
59a3be63b3SDoug Rabson  * Parts of the ISA bus implementation common to all architectures.
60a3be63b3SDoug Rabson  */
61a3be63b3SDoug Rabson 
62a3be63b3SDoug Rabson #include <sys/param.h>
63a3be63b3SDoug Rabson #include <sys/systm.h>
64a3be63b3SDoug Rabson #include <sys/kernel.h>
65a3be63b3SDoug Rabson #include <sys/bus.h>
66a3be63b3SDoug Rabson #include <sys/malloc.h>
67a3be63b3SDoug Rabson #include <sys/module.h>
68a3be63b3SDoug Rabson #include <machine/bus.h>
69a3be63b3SDoug Rabson #include <sys/rman.h>
70a3be63b3SDoug Rabson 
71a3be63b3SDoug Rabson #include <machine/resource.h>
72a3be63b3SDoug Rabson 
73a3be63b3SDoug Rabson #include <isa/isavar.h>
74a3be63b3SDoug Rabson #include <isa/isa_common.h>
75a3be63b3SDoug Rabson #ifdef __alpha__		/* XXX workaround a stupid warning */
76a3be63b3SDoug Rabson #include <alpha/isa/isavar.h>
77a3be63b3SDoug Rabson #endif
78a3be63b3SDoug Rabson 
79a3be63b3SDoug Rabson MALLOC_DEFINE(M_ISADEV, "isadev", "ISA device");
80a3be63b3SDoug Rabson 
81a3be63b3SDoug Rabson static devclass_t isa_devclass;
82ec22663bSDoug Rabson static int isa_running;
83a3be63b3SDoug Rabson 
84a3be63b3SDoug Rabson /*
85a3be63b3SDoug Rabson  * At 'probe' time, we add all the devices which we know about to the
86a3be63b3SDoug Rabson  * bus.  The generic attach routine will probe and attach them if they
87a3be63b3SDoug Rabson  * are alive.
88a3be63b3SDoug Rabson  */
89a3be63b3SDoug Rabson static int
90a3be63b3SDoug Rabson isa_probe(device_t dev)
91a3be63b3SDoug Rabson {
92a3be63b3SDoug Rabson 	device_set_desc(dev, "ISA bus");
93a3be63b3SDoug Rabson 	isa_init();		/* Allow machdep code to initialise */
94ec22663bSDoug Rabson 	return 0;
95a3be63b3SDoug Rabson }
96a3be63b3SDoug Rabson 
97a3be63b3SDoug Rabson extern device_t isa_bus_device;
98a3be63b3SDoug Rabson 
99a3be63b3SDoug Rabson static int
100a3be63b3SDoug Rabson isa_attach(device_t dev)
101a3be63b3SDoug Rabson {
102a3be63b3SDoug Rabson 	/*
1034249382dSDoug Rabson 	 * Arrange for isa_probe_children(dev) to be called later. XXX
104a3be63b3SDoug Rabson 	 */
105a3be63b3SDoug Rabson 	isa_bus_device = dev;
106a3be63b3SDoug Rabson 	return 0;
107a3be63b3SDoug Rabson }
108a3be63b3SDoug Rabson 
109a3be63b3SDoug Rabson /*
1104249382dSDoug Rabson  * Find a working set of memory regions for a child using the ranges
1114249382dSDoug Rabson  * in *config  and return the regions in *result. Returns non-zero if
1124249382dSDoug Rabson  * a set of ranges was found.
1134249382dSDoug Rabson  */
1144249382dSDoug Rabson static int
1154249382dSDoug Rabson isa_find_memory(device_t child,
1164249382dSDoug Rabson 		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++) {
1334249382dSDoug Rabson 		u_int32_t start, end, size, align;
1344249382dSDoug Rabson 		for (start = config->ic_mem[i].ir_start,
1354249382dSDoug Rabson 			     end = config->ic_mem[i].ir_end,
1364249382dSDoug Rabson 			     size = config->ic_mem[i].ir_size,
1374249382dSDoug Rabson 			     align = config->ic_mem[i].ir_align;
1384249382dSDoug Rabson 		     start + size - 1 <= end;
1394249382dSDoug Rabson 		     start += align) {
14025afb89bSDoug Rabson 			bus_set_resource(child, SYS_RES_MEMORY, i,
1414249382dSDoug Rabson 					 start, size);
1424249382dSDoug Rabson 			res[i] = bus_alloc_resource(child,
1434249382dSDoug Rabson 						    SYS_RES_MEMORY, &i,
1444249382dSDoug Rabson 						    0, ~0, 1, RF_ACTIVE);
1454249382dSDoug Rabson 			if (res[i]) {
1464249382dSDoug Rabson 				result->ic_mem[i].ir_start = start;
1474249382dSDoug Rabson 				result->ic_mem[i].ir_end = start + size - 1;
1484249382dSDoug Rabson 				result->ic_mem[i].ir_size = size;
1494249382dSDoug Rabson 				result->ic_mem[i].ir_align = align;
1504249382dSDoug Rabson 				break;
1514249382dSDoug Rabson 			}
1524249382dSDoug Rabson 		}
1534249382dSDoug Rabson 
1544249382dSDoug Rabson 		/*
1554249382dSDoug Rabson 		 * If we didn't find a place for memory range i, then
1564249382dSDoug Rabson 		 * give up now.
1574249382dSDoug Rabson 		 */
1584249382dSDoug Rabson 		if (!res[i]) {
1594249382dSDoug Rabson 			success = 0;
1604249382dSDoug Rabson 			break;
1614249382dSDoug Rabson 		}
1624249382dSDoug Rabson 	}
1634249382dSDoug Rabson 
1644249382dSDoug Rabson 	for (i = 0; i < ISA_NMEM; i++) {
1654249382dSDoug Rabson 		if (res[i])
1664249382dSDoug Rabson 			bus_release_resource(child, SYS_RES_MEMORY,
1674249382dSDoug Rabson 					     i, res[i]);
1684249382dSDoug Rabson 	}
1694249382dSDoug Rabson 
1704249382dSDoug Rabson 	return success;
1714249382dSDoug Rabson }
1724249382dSDoug Rabson 
1734249382dSDoug Rabson /*
1744249382dSDoug Rabson  * Find a working set of port regions for a child using the ranges
1754249382dSDoug Rabson  * in *config  and return the regions in *result. Returns non-zero if
1764249382dSDoug Rabson  * a set of ranges was found.
1774249382dSDoug Rabson  */
1784249382dSDoug Rabson static int
1794249382dSDoug Rabson isa_find_port(device_t child,
1804249382dSDoug Rabson 	      struct isa_config *config,
1814249382dSDoug Rabson 	      struct isa_config *result)
1824249382dSDoug Rabson {
1834249382dSDoug Rabson 	int success, i;
1844249382dSDoug Rabson 	struct resource *res[ISA_NPORT];
1854249382dSDoug Rabson 
1864249382dSDoug Rabson 	/*
1874249382dSDoug Rabson 	 * First clear out any existing resource definitions.
1884249382dSDoug Rabson 	 */
1894249382dSDoug Rabson 	for (i = 0; i < ISA_NPORT; i++) {
19025afb89bSDoug Rabson 		bus_delete_resource(child, SYS_RES_IOPORT, i);
1914249382dSDoug Rabson 		res[i] = NULL;
1924249382dSDoug Rabson 	}
1934249382dSDoug Rabson 
1944249382dSDoug Rabson 	success = 1;
1954249382dSDoug Rabson 	result->ic_nport = config->ic_nport;
1964249382dSDoug Rabson 	for (i = 0; i < config->ic_nport; i++) {
1974249382dSDoug Rabson 		u_int32_t start, end, size, align;
1984249382dSDoug Rabson 		for (start = config->ic_port[i].ir_start,
1994249382dSDoug Rabson 			     end = config->ic_port[i].ir_end,
2004249382dSDoug Rabson 			     size = config->ic_port[i].ir_size,
2014249382dSDoug Rabson 			     align = config->ic_port[i].ir_align;
2024249382dSDoug Rabson 		     start + size - 1 <= end;
2034249382dSDoug Rabson 		     start += align) {
20425afb89bSDoug Rabson 			bus_set_resource(child, SYS_RES_IOPORT, i,
2054249382dSDoug Rabson 					 start, size);
2064249382dSDoug Rabson 			res[i] = bus_alloc_resource(child,
2074249382dSDoug Rabson 						    SYS_RES_IOPORT, &i,
2084249382dSDoug Rabson 						    0, ~0, 1, RF_ACTIVE);
2094249382dSDoug Rabson 			if (res[i]) {
2104249382dSDoug Rabson 				result->ic_port[i].ir_start = start;
2114249382dSDoug Rabson 				result->ic_port[i].ir_end = start + size - 1;
2124249382dSDoug Rabson 				result->ic_port[i].ir_size = size;
2134249382dSDoug Rabson 				result->ic_port[i].ir_align = align;
2144249382dSDoug Rabson 				break;
2154249382dSDoug Rabson 			}
2164249382dSDoug Rabson 		}
2174249382dSDoug Rabson 
2184249382dSDoug Rabson 		/*
2194249382dSDoug Rabson 		 * If we didn't find a place for port range i, then
2204249382dSDoug Rabson 		 * give up now.
2214249382dSDoug Rabson 		 */
2224249382dSDoug Rabson 		if (!res[i]) {
2234249382dSDoug Rabson 			success = 0;
2244249382dSDoug Rabson 			break;
2254249382dSDoug Rabson 		}
2264249382dSDoug Rabson 	}
2274249382dSDoug Rabson 
2284249382dSDoug Rabson 	for (i = 0; i < ISA_NPORT; i++) {
2294249382dSDoug Rabson 		if (res[i])
2304249382dSDoug Rabson 			bus_release_resource(child, SYS_RES_IOPORT,
2314249382dSDoug Rabson 					     i, res[i]);
2324249382dSDoug Rabson 	}
2334249382dSDoug Rabson 
2344249382dSDoug Rabson 	return success;
2354249382dSDoug Rabson }
2364249382dSDoug Rabson 
2374249382dSDoug Rabson /*
2384249382dSDoug Rabson  * Return the index of the first bit in the mask (or -1 if mask is empty.
2394249382dSDoug Rabson  */
2404249382dSDoug Rabson static int
2414249382dSDoug Rabson find_first_bit(u_int32_t mask)
2424249382dSDoug Rabson {
2434249382dSDoug Rabson 	return ffs(mask) - 1;
2444249382dSDoug Rabson }
2454249382dSDoug Rabson 
2464249382dSDoug Rabson /*
2474249382dSDoug Rabson  * Return the index of the next bit in the mask, or -1 if there are no more.
2484249382dSDoug Rabson  */
2494249382dSDoug Rabson static int
2504249382dSDoug Rabson find_next_bit(u_int32_t mask, int bit)
2514249382dSDoug Rabson {
2524249382dSDoug Rabson 	bit++;
2534249382dSDoug Rabson 	while (bit < 32 && !(mask & (1 << bit)))
2544249382dSDoug Rabson 		bit++;
2554249382dSDoug Rabson 	if (bit != 32)
2564249382dSDoug Rabson 		return bit;
2574249382dSDoug Rabson 	return -1;
2584249382dSDoug Rabson }
2594249382dSDoug Rabson 
2604249382dSDoug Rabson /*
2614249382dSDoug Rabson  * Find a working set of irqs for a child using the masks in *config
2624249382dSDoug Rabson  * and return the regions in *result. Returns non-zero if a set of
2634249382dSDoug Rabson  * irqs was found.
2644249382dSDoug Rabson  */
2654249382dSDoug Rabson static int
2664249382dSDoug Rabson isa_find_irq(device_t child,
2674249382dSDoug Rabson 	     struct isa_config *config,
2684249382dSDoug Rabson 	     struct isa_config *result)
2694249382dSDoug Rabson {
2704249382dSDoug Rabson 	int success, i;
2714249382dSDoug Rabson 	struct resource *res[ISA_NIRQ];
2724249382dSDoug Rabson 
2734249382dSDoug Rabson 	/*
2744249382dSDoug Rabson 	 * First clear out any existing resource definitions.
2754249382dSDoug Rabson 	 */
2764249382dSDoug Rabson 	for (i = 0; i < ISA_NIRQ; i++) {
27725afb89bSDoug Rabson 		bus_delete_resource(child, SYS_RES_IRQ, i);
2784249382dSDoug Rabson 		res[i] = NULL;
2794249382dSDoug Rabson 	}
2804249382dSDoug Rabson 
2814249382dSDoug Rabson 	success = 1;
2824249382dSDoug Rabson 	result->ic_nirq = config->ic_nirq;
2834249382dSDoug Rabson 	for (i = 0; i < config->ic_nirq; i++) {
2844249382dSDoug Rabson 		u_int32_t mask = config->ic_irqmask[i];
2854249382dSDoug Rabson 		int irq;
2864249382dSDoug Rabson 		for (irq = find_first_bit(mask);
2874249382dSDoug Rabson 		     irq != -1;
2884249382dSDoug Rabson 		     irq = find_next_bit(mask, irq)) {
28925afb89bSDoug Rabson 			bus_set_resource(child, SYS_RES_IRQ, i,
2904249382dSDoug Rabson 					 irq, 1);
2914249382dSDoug Rabson 			res[i] = bus_alloc_resource(child,
2924249382dSDoug Rabson 						    SYS_RES_IRQ, &i,
2934249382dSDoug Rabson 						    0, ~0, 1, RF_ACTIVE);
2944249382dSDoug Rabson 			if (res[i]) {
2954249382dSDoug Rabson 				result->ic_irqmask[i] = (1 << irq);
2964249382dSDoug Rabson 				break;
2974249382dSDoug Rabson 			}
2984249382dSDoug Rabson 		}
2994249382dSDoug Rabson 
3004249382dSDoug Rabson 		/*
3014249382dSDoug Rabson 		 * If we didn't find a place for irq range i, then
3024249382dSDoug Rabson 		 * give up now.
3034249382dSDoug Rabson 		 */
3044249382dSDoug Rabson 		if (!res[i]) {
3054249382dSDoug Rabson 			success = 0;
3064249382dSDoug Rabson 			break;
3074249382dSDoug Rabson 		}
3084249382dSDoug Rabson 	}
3094249382dSDoug Rabson 
3104249382dSDoug Rabson 	for (i = 0; i < ISA_NIRQ; i++) {
3114249382dSDoug Rabson 		if (res[i])
3124249382dSDoug Rabson 			bus_release_resource(child, SYS_RES_IRQ,
3134249382dSDoug Rabson 					     i, res[i]);
3144249382dSDoug Rabson 	}
3154249382dSDoug Rabson 
3164249382dSDoug Rabson 	return success;
3174249382dSDoug Rabson }
3184249382dSDoug Rabson 
3194249382dSDoug Rabson /*
3204249382dSDoug Rabson  * Find a working set of drqs for a child using the masks in *config
3214249382dSDoug Rabson  * and return the regions in *result. Returns non-zero if a set of
3224249382dSDoug Rabson  * drqs was found.
3234249382dSDoug Rabson  */
3244249382dSDoug Rabson static int
3254249382dSDoug Rabson isa_find_drq(device_t child,
3264249382dSDoug Rabson 	     struct isa_config *config,
3274249382dSDoug Rabson 	     struct isa_config *result)
3284249382dSDoug Rabson {
3294249382dSDoug Rabson 	int success, i;
3304249382dSDoug Rabson 	struct resource *res[ISA_NDRQ];
3314249382dSDoug Rabson 
3324249382dSDoug Rabson 	/*
3334249382dSDoug Rabson 	 * First clear out any existing resource definitions.
3344249382dSDoug Rabson 	 */
3354249382dSDoug Rabson 	for (i = 0; i < ISA_NDRQ; i++) {
33625afb89bSDoug Rabson 		bus_delete_resource(child, SYS_RES_DRQ, i);
3374249382dSDoug Rabson 		res[i] = NULL;
3384249382dSDoug Rabson 	}
3394249382dSDoug Rabson 
3404249382dSDoug Rabson 	success = 1;
3414249382dSDoug Rabson 	result->ic_ndrq = config->ic_ndrq;
3424249382dSDoug Rabson 	for (i = 0; i < config->ic_ndrq; i++) {
3434249382dSDoug Rabson 		u_int32_t mask = config->ic_drqmask[i];
3444249382dSDoug Rabson 		int drq;
3454249382dSDoug Rabson 		for (drq = find_first_bit(mask);
3464249382dSDoug Rabson 		     drq != -1;
3474249382dSDoug Rabson 		     drq = find_next_bit(mask, drq)) {
34825afb89bSDoug Rabson 			bus_set_resource(child, SYS_RES_DRQ, i,
3494249382dSDoug Rabson 					 drq, 1);
3504249382dSDoug Rabson 			res[i] = bus_alloc_resource(child,
3514249382dSDoug Rabson 						    SYS_RES_DRQ, &i,
3524249382dSDoug Rabson 						    0, ~0, 1, RF_ACTIVE);
3534249382dSDoug Rabson 			if (res[i]) {
3544249382dSDoug Rabson 				result->ic_drqmask[i] = (1 << drq);
3554249382dSDoug Rabson 				break;
3564249382dSDoug Rabson 			}
3574249382dSDoug Rabson 		}
3584249382dSDoug Rabson 
3594249382dSDoug Rabson 		/*
3604249382dSDoug Rabson 		 * If we didn't find a place for drq range i, then
3614249382dSDoug Rabson 		 * give up now.
3624249382dSDoug Rabson 		 */
3634249382dSDoug Rabson 		if (!res[i]) {
3644249382dSDoug Rabson 			success = 0;
3654249382dSDoug Rabson 			break;
3664249382dSDoug Rabson 		}
3674249382dSDoug Rabson 	}
3684249382dSDoug Rabson 
3694249382dSDoug Rabson 	for (i = 0; i < ISA_NDRQ; i++) {
3704249382dSDoug Rabson 		if (res[i])
3714249382dSDoug Rabson 			bus_release_resource(child, SYS_RES_DRQ,
3724249382dSDoug Rabson 					     i, res[i]);
3734249382dSDoug Rabson 	}
3744249382dSDoug Rabson 
3754249382dSDoug Rabson 	return success;
3764249382dSDoug Rabson }
3774249382dSDoug Rabson 
3784249382dSDoug Rabson /*
3794249382dSDoug Rabson  * Attempt to find a working set of resources for a device. Return
3804249382dSDoug Rabson  * non-zero if a working configuration is found.
3814249382dSDoug Rabson  */
3824249382dSDoug Rabson static int
3834249382dSDoug Rabson isa_assign_resources(device_t child)
3844249382dSDoug Rabson {
3854249382dSDoug Rabson 	struct isa_device *idev = DEVTOISA(child);
3864249382dSDoug Rabson 	struct isa_config_entry *ice;
3874249382dSDoug Rabson 	struct isa_config config;
3884249382dSDoug Rabson 
3894249382dSDoug Rabson 	bzero(&config, sizeof config);
3904249382dSDoug Rabson 	TAILQ_FOREACH(ice, &idev->id_configs, ice_link) {
3914249382dSDoug Rabson 		if (!isa_find_memory(child, &ice->ice_config, &config))
3924249382dSDoug Rabson 			continue;
3934249382dSDoug Rabson 		if (!isa_find_port(child, &ice->ice_config, &config))
3944249382dSDoug Rabson 			continue;
3954249382dSDoug Rabson 		if (!isa_find_irq(child, &ice->ice_config, &config))
3964249382dSDoug Rabson 			continue;
3974249382dSDoug Rabson 		if (!isa_find_drq(child, &ice->ice_config, &config))
3984249382dSDoug Rabson 			continue;
3994249382dSDoug Rabson 
4004249382dSDoug Rabson 		/*
4014249382dSDoug Rabson 		 * A working configuration was found enable the device
4024249382dSDoug Rabson 		 * with this configuration.
4034249382dSDoug Rabson 		 */
4044249382dSDoug Rabson 		if (idev->id_config_cb) {
4054249382dSDoug Rabson 			idev->id_config_cb(idev->id_config_arg,
4064249382dSDoug Rabson 					   &config, 1);
4074249382dSDoug Rabson 			return 1;
4084249382dSDoug Rabson 		}
4094249382dSDoug Rabson 	}
4104249382dSDoug Rabson 
4114249382dSDoug Rabson 	/*
4124249382dSDoug Rabson 	 * Disable the device.
4134249382dSDoug Rabson 	 */
41425afb89bSDoug Rabson 	if (device_get_desc(child))
41525afb89bSDoug Rabson 	    device_printf(child, "<%s> can't assign resources\n",
41625afb89bSDoug Rabson 			  device_get_desc(child));
41725afb89bSDoug Rabson 	else
41825afb89bSDoug Rabson 	    device_printf(child, "can't assign resources\n");
4194249382dSDoug Rabson 	bzero(&config, sizeof config);
4204249382dSDoug Rabson 	if (idev->id_config_cb)
4214249382dSDoug Rabson 		idev->id_config_cb(idev->id_config_arg, &config, 0);
4224249382dSDoug Rabson 	device_disable(child);
4234249382dSDoug Rabson 
4244249382dSDoug Rabson 	return 0;
4254249382dSDoug Rabson }
4264249382dSDoug Rabson 
4274249382dSDoug Rabson /*
4284249382dSDoug Rabson  * Called after other devices have initialised to probe for isa devices.
4294249382dSDoug Rabson  */
4304249382dSDoug Rabson void
4314249382dSDoug Rabson isa_probe_children(device_t dev)
4324249382dSDoug Rabson {
4334249382dSDoug Rabson 	device_t *children;
4344249382dSDoug Rabson 	int nchildren, i;
4354249382dSDoug Rabson 
436ec22663bSDoug Rabson 	/*
437ec22663bSDoug Rabson 	 * Create all the children by calling driver's identify methods.
438ec22663bSDoug Rabson 	 */
439ec22663bSDoug Rabson 	bus_generic_probe(dev);
440ec22663bSDoug Rabson 
4414249382dSDoug Rabson 	if (device_get_children(dev, &children, &nchildren))
4424249382dSDoug Rabson 		return;
4434249382dSDoug Rabson 
4444249382dSDoug Rabson 	/*
445a2e6dbb3SDoug Rabson 	 * First disable all pnp devices so that they don't get
446a2e6dbb3SDoug Rabson 	 * matched by legacy probes.
447a2e6dbb3SDoug Rabson 	 */
448ec22663bSDoug Rabson 	if (bootverbose)
449ec22663bSDoug Rabson 		printf("isa_probe_children: disabling PnP devices\n");
450a2e6dbb3SDoug Rabson 	for (i = 0; i < nchildren; i++) {
451a2e6dbb3SDoug Rabson 		device_t child = children[i];
452a2e6dbb3SDoug Rabson 		struct isa_device *idev = DEVTOISA(child);
453a2e6dbb3SDoug Rabson 		struct isa_config config;
454a2e6dbb3SDoug Rabson 
455a2e6dbb3SDoug Rabson 		bzero(&config, sizeof config);
456a2e6dbb3SDoug Rabson 		if (idev->id_config_cb)
457a2e6dbb3SDoug Rabson 			idev->id_config_cb(idev->id_config_arg, &config, 0);
458a2e6dbb3SDoug Rabson 	}
459a2e6dbb3SDoug Rabson 
460a2e6dbb3SDoug Rabson 	/*
461a2e6dbb3SDoug Rabson 	 * Next probe all non-pnp devices so that they claim their
4624249382dSDoug Rabson 	 * resources first.
4634249382dSDoug Rabson 	 */
464ec22663bSDoug Rabson 	if (bootverbose)
465ec22663bSDoug Rabson 		printf("isa_probe_children: probing non-PnP devices\n");
4664249382dSDoug Rabson 	for (i = 0; i < nchildren; i++) {
4674249382dSDoug Rabson 		device_t child = children[i];
4684249382dSDoug Rabson 		struct isa_device *idev = DEVTOISA(child);
4694249382dSDoug Rabson 
4704249382dSDoug Rabson 		if (TAILQ_FIRST(&idev->id_configs))
4714249382dSDoug Rabson 			continue;
4724249382dSDoug Rabson 
4734249382dSDoug Rabson 		device_probe_and_attach(child);
4744249382dSDoug Rabson 	}
4754249382dSDoug Rabson 
4764249382dSDoug Rabson 	/*
477a2e6dbb3SDoug Rabson 	 * Finally assign resource to pnp devices and probe them.
4784249382dSDoug Rabson 	 */
479ec22663bSDoug Rabson 	if (bootverbose)
480ec22663bSDoug Rabson 		printf("isa_probe_children: probing PnP devices\n");
4814249382dSDoug Rabson 	for (i = 0; i < nchildren; i++) {
4824249382dSDoug Rabson 		device_t child = children[i];
4834249382dSDoug Rabson 		struct isa_device* idev = DEVTOISA(child);
4844249382dSDoug Rabson 
4854249382dSDoug Rabson 		if (!TAILQ_FIRST(&idev->id_configs))
4864249382dSDoug Rabson 			continue;
4874249382dSDoug Rabson 
4884249382dSDoug Rabson 		if (isa_assign_resources(child)) {
48925afb89bSDoug Rabson 			struct resource_list *rl = &idev->id_resources;
4904249382dSDoug Rabson 			struct resource_list_entry *rle;
4914249382dSDoug Rabson 
4924249382dSDoug Rabson 			device_probe_and_attach(child);
4934249382dSDoug Rabson 
4944249382dSDoug Rabson 			/*
4954249382dSDoug Rabson 			 * Claim any unallocated resources to keep other
4964249382dSDoug Rabson 			 * devices from using them.
4974249382dSDoug Rabson 			 */
49825afb89bSDoug Rabson 			SLIST_FOREACH(rle, rl, link) {
4994249382dSDoug Rabson 				if (!rle->res) {
5004249382dSDoug Rabson 					int rid = rle->rid;
50125afb89bSDoug Rabson 					resource_list_alloc(rl, dev, child,
5024249382dSDoug Rabson 							    rle->type,
5034249382dSDoug Rabson 							    &rid,
5044249382dSDoug Rabson 							    0, ~0, 1,
5054249382dSDoug Rabson 							    RF_ACTIVE);
5064249382dSDoug Rabson 				}
5074249382dSDoug Rabson 			}
5084249382dSDoug Rabson 		}
5094249382dSDoug Rabson 	}
5104249382dSDoug Rabson 
5114249382dSDoug Rabson 	free(children, M_TEMP);
512ec22663bSDoug Rabson 
513ec22663bSDoug Rabson 	isa_running = 1;
5144249382dSDoug Rabson }
5154249382dSDoug Rabson 
5164249382dSDoug Rabson /*
517a3be63b3SDoug Rabson  * Add a new child with default ivars.
518a3be63b3SDoug Rabson  */
519a3be63b3SDoug Rabson static device_t
520bea6af4dSDoug Rabson isa_add_child(device_t dev, int order, const char *name, int unit)
521a3be63b3SDoug Rabson {
522a3be63b3SDoug Rabson 	struct	isa_device *idev;
523a3be63b3SDoug Rabson 
524a3be63b3SDoug Rabson 	idev = malloc(sizeof(struct isa_device), M_ISADEV, M_NOWAIT);
525a3be63b3SDoug Rabson 	if (!idev)
526a3be63b3SDoug Rabson 		return 0;
527a3be63b3SDoug Rabson 	bzero(idev, sizeof *idev);
528a3be63b3SDoug Rabson 
529a3be63b3SDoug Rabson 	resource_list_init(&idev->id_resources);
5304249382dSDoug Rabson 	TAILQ_INIT(&idev->id_configs);
531a3be63b3SDoug Rabson 
532bea6af4dSDoug Rabson 	return device_add_child_ordered(dev, order, name, unit, idev);
533a3be63b3SDoug Rabson }
534a3be63b3SDoug Rabson 
535a3be63b3SDoug Rabson static void
536a3be63b3SDoug Rabson isa_print_resources(struct resource_list *rl, const char *name, int type,
53725afb89bSDoug Rabson 		    int count, const char *format)
538a3be63b3SDoug Rabson {
5394249382dSDoug Rabson 	struct resource_list_entry *rle;
5404249382dSDoug Rabson 	int printed;
5414249382dSDoug Rabson 	int i;
542a3be63b3SDoug Rabson 
5434249382dSDoug Rabson 	printed = 0;
54425afb89bSDoug Rabson 	for (i = 0; i < count; i++) {
5454249382dSDoug Rabson 		rle = resource_list_find(rl, type, i);
5464249382dSDoug Rabson 		if (rle) {
5474249382dSDoug Rabson 			if (printed == 0)
548a3be63b3SDoug Rabson 				printf(" %s ", name);
5494249382dSDoug Rabson 			else if (printed > 0)
550a3be63b3SDoug Rabson 				printf(",");
5514249382dSDoug Rabson 			printed++;
5524249382dSDoug Rabson 			printf(format, rle->start);
5534249382dSDoug Rabson 			if (rle->count > 1) {
554a3be63b3SDoug Rabson 				printf("-");
5554249382dSDoug Rabson 				printf(format, rle->start + rle->count - 1);
556a3be63b3SDoug Rabson 			}
5574249382dSDoug Rabson 		} else if (i > 3) {
5584249382dSDoug Rabson 			/* check the first few regardless */
5594249382dSDoug Rabson 			break;
560a3be63b3SDoug Rabson 		}
561a3be63b3SDoug Rabson 	}
562a3be63b3SDoug Rabson }
563a3be63b3SDoug Rabson 
56415317dd8SMatthew N. Dodd static int
565a3be63b3SDoug Rabson isa_print_child(device_t bus, device_t dev)
566a3be63b3SDoug Rabson {
567a3be63b3SDoug Rabson 	struct	isa_device *idev = DEVTOISA(dev);
568a3be63b3SDoug Rabson 	struct resource_list *rl = &idev->id_resources;
56915317dd8SMatthew N. Dodd 	int retval = 0;
57015317dd8SMatthew N. Dodd 
57115317dd8SMatthew N. Dodd 	retval += bus_print_child_header(bus, dev);
572a3be63b3SDoug Rabson 
573062acdb7SDoug Rabson 	if (SLIST_FIRST(rl) || device_get_flags(dev))
57415317dd8SMatthew N. Dodd 		retval += printf(" at");
575a3be63b3SDoug Rabson 
57625afb89bSDoug Rabson 	isa_print_resources(rl, "port", SYS_RES_IOPORT, ISA_NPORT, "%#lx");
57725afb89bSDoug Rabson 	isa_print_resources(rl, "iomem", SYS_RES_MEMORY, ISA_NMEM, "%#lx");
57825afb89bSDoug Rabson 	isa_print_resources(rl, "irq", SYS_RES_IRQ, ISA_NIRQ, "%ld");
57925afb89bSDoug Rabson 	isa_print_resources(rl, "drq", SYS_RES_DRQ, ISA_NDRQ, "%ld");
580062acdb7SDoug Rabson 	if (device_get_flags(dev))
581062acdb7SDoug Rabson 		retval += printf(" flags %#x", device_get_flags(dev));
582a3be63b3SDoug Rabson 
58315317dd8SMatthew N. Dodd 	retval += bus_print_child_footer(bus, dev);
58415317dd8SMatthew N. Dodd 
58515317dd8SMatthew N. Dodd 	return (retval);
586a3be63b3SDoug Rabson }
587a3be63b3SDoug Rabson 
588a3be63b3SDoug Rabson static int
589a3be63b3SDoug Rabson isa_read_ivar(device_t bus, device_t dev, int index, uintptr_t * result)
590a3be63b3SDoug Rabson {
591a3be63b3SDoug Rabson 	struct isa_device* idev = DEVTOISA(dev);
592a3be63b3SDoug Rabson 	struct resource_list *rl = &idev->id_resources;
593a3be63b3SDoug Rabson 	struct resource_list_entry *rle;
594a3be63b3SDoug Rabson 
595a3be63b3SDoug Rabson 	switch (index) {
596a3be63b3SDoug Rabson 	case ISA_IVAR_PORT_0:
597a3be63b3SDoug Rabson 		rle = resource_list_find(rl, SYS_RES_IOPORT, 0);
598a3be63b3SDoug Rabson 		if (rle)
599a3be63b3SDoug Rabson 			*result = rle->start;
600a3be63b3SDoug Rabson 		else
601a3be63b3SDoug Rabson 			*result = -1;
602a3be63b3SDoug Rabson 		break;
603a3be63b3SDoug Rabson 
604a3be63b3SDoug Rabson 	case ISA_IVAR_PORT_1:
605a3be63b3SDoug Rabson 		rle = resource_list_find(rl, SYS_RES_IOPORT, 1);
606a3be63b3SDoug Rabson 		if (rle)
607a3be63b3SDoug Rabson 			*result = rle->start;
608a3be63b3SDoug Rabson 		else
609a3be63b3SDoug Rabson 			*result = -1;
610a3be63b3SDoug Rabson 		break;
611a3be63b3SDoug Rabson 
612a3be63b3SDoug Rabson 	case ISA_IVAR_PORTSIZE_0:
613a3be63b3SDoug Rabson 		rle = resource_list_find(rl, SYS_RES_IOPORT, 0);
614a3be63b3SDoug Rabson 		if (rle)
615a3be63b3SDoug Rabson 			*result = rle->count;
616a3be63b3SDoug Rabson 		else
617a3be63b3SDoug Rabson 			*result = 0;
618a3be63b3SDoug Rabson 		break;
619a3be63b3SDoug Rabson 
620a3be63b3SDoug Rabson 	case ISA_IVAR_PORTSIZE_1:
621a3be63b3SDoug Rabson 		rle = resource_list_find(rl, SYS_RES_IOPORT, 1);
622a3be63b3SDoug Rabson 		if (rle)
623a3be63b3SDoug Rabson 			*result = rle->count;
624a3be63b3SDoug Rabson 		else
625a3be63b3SDoug Rabson 			*result = 0;
626a3be63b3SDoug Rabson 		break;
627a3be63b3SDoug Rabson 
628a3be63b3SDoug Rabson 	case ISA_IVAR_MADDR_0:
629a3be63b3SDoug Rabson 		rle = resource_list_find(rl, SYS_RES_MEMORY, 0);
630a3be63b3SDoug Rabson 		if (rle)
631a3be63b3SDoug Rabson 			*result = rle->start;
632a3be63b3SDoug Rabson 		else
633a3be63b3SDoug Rabson 			*result = -1;
634a3be63b3SDoug Rabson 		break;
635a3be63b3SDoug Rabson 
636a3be63b3SDoug Rabson 	case ISA_IVAR_MADDR_1:
637a3be63b3SDoug Rabson 		rle = resource_list_find(rl, SYS_RES_MEMORY, 1);
638a3be63b3SDoug Rabson 		if (rle)
639a3be63b3SDoug Rabson 			*result = rle->start;
640a3be63b3SDoug Rabson 		else
641a3be63b3SDoug Rabson 			*result = -1;
642a3be63b3SDoug Rabson 		break;
643a3be63b3SDoug Rabson 
644a3be63b3SDoug Rabson 	case ISA_IVAR_MSIZE_0:
645a3be63b3SDoug Rabson 		rle = resource_list_find(rl, SYS_RES_MEMORY, 0);
646a3be63b3SDoug Rabson 		if (rle)
647a3be63b3SDoug Rabson 			*result = rle->count;
648a3be63b3SDoug Rabson 		else
649a3be63b3SDoug Rabson 			*result = 0;
650a3be63b3SDoug Rabson 		break;
651a3be63b3SDoug Rabson 
652a3be63b3SDoug Rabson 	case ISA_IVAR_MSIZE_1:
653a3be63b3SDoug Rabson 		rle = resource_list_find(rl, SYS_RES_MEMORY, 1);
654a3be63b3SDoug Rabson 		if (rle)
655a3be63b3SDoug Rabson 			*result = rle->count;
656a3be63b3SDoug Rabson 		else
657a3be63b3SDoug Rabson 			*result = 0;
658a3be63b3SDoug Rabson 		break;
659a3be63b3SDoug Rabson 
660a3be63b3SDoug Rabson 	case ISA_IVAR_IRQ_0:
661a3be63b3SDoug Rabson 		rle = resource_list_find(rl, SYS_RES_IRQ, 0);
662a3be63b3SDoug Rabson 		if (rle)
663a3be63b3SDoug Rabson 			*result = rle->start;
664a3be63b3SDoug Rabson 		else
665a3be63b3SDoug Rabson 			*result = -1;
666a3be63b3SDoug Rabson 		break;
667a3be63b3SDoug Rabson 
668a3be63b3SDoug Rabson 	case ISA_IVAR_IRQ_1:
669a3be63b3SDoug Rabson 		rle = resource_list_find(rl, SYS_RES_IRQ, 1);
670a3be63b3SDoug Rabson 		if (rle)
671a3be63b3SDoug Rabson 			*result = rle->start;
672a3be63b3SDoug Rabson 		else
673a3be63b3SDoug Rabson 			*result = -1;
674a3be63b3SDoug Rabson 		break;
675a3be63b3SDoug Rabson 
676a3be63b3SDoug Rabson 	case ISA_IVAR_DRQ_0:
677a3be63b3SDoug Rabson 		rle = resource_list_find(rl, SYS_RES_DRQ, 0);
678a3be63b3SDoug Rabson 		if (rle)
679a3be63b3SDoug Rabson 			*result = rle->start;
680a3be63b3SDoug Rabson 		else
681a3be63b3SDoug Rabson 			*result = -1;
682a3be63b3SDoug Rabson 		break;
683a3be63b3SDoug Rabson 
684a3be63b3SDoug Rabson 	case ISA_IVAR_DRQ_1:
685a3be63b3SDoug Rabson 		rle = resource_list_find(rl, SYS_RES_DRQ, 1);
686a3be63b3SDoug Rabson 		if (rle)
687a3be63b3SDoug Rabson 			*result = rle->start;
688a3be63b3SDoug Rabson 		else
689a3be63b3SDoug Rabson 			*result = -1;
690a3be63b3SDoug Rabson 		break;
691a3be63b3SDoug Rabson 
692cfa84f46SDoug Rabson 	case ISA_IVAR_VENDORID:
693cfa84f46SDoug Rabson 		*result = idev->id_vendorid;
694cfa84f46SDoug Rabson 		break;
695cfa84f46SDoug Rabson 
696cfa84f46SDoug Rabson 	case ISA_IVAR_SERIAL:
697cfa84f46SDoug Rabson 		*result = idev->id_serial;
698cfa84f46SDoug Rabson 		break;
699cfa84f46SDoug Rabson 
700cfa84f46SDoug Rabson 	case ISA_IVAR_LOGICALID:
701cfa84f46SDoug Rabson 		*result = idev->id_logicalid;
702cfa84f46SDoug Rabson 		break;
703cfa84f46SDoug Rabson 
704cfa84f46SDoug Rabson 	case ISA_IVAR_COMPATID:
705cfa84f46SDoug Rabson 		*result = idev->id_compatid;
706cfa84f46SDoug Rabson 		break;
707cfa84f46SDoug Rabson 
708cfa84f46SDoug Rabson 	default:
709a3be63b3SDoug Rabson 		return ENOENT;
710a3be63b3SDoug Rabson 	}
711a3be63b3SDoug Rabson 
712cfa84f46SDoug Rabson 	return 0;
713cfa84f46SDoug Rabson }
714cfa84f46SDoug Rabson 
715a3be63b3SDoug Rabson static int
716a3be63b3SDoug Rabson isa_write_ivar(device_t bus, device_t dev,
717a3be63b3SDoug Rabson 	       int index, uintptr_t value)
718a3be63b3SDoug Rabson {
719a3be63b3SDoug Rabson 	struct isa_device* idev = DEVTOISA(dev);
720a3be63b3SDoug Rabson 
721a3be63b3SDoug Rabson 	switch (index) {
722a3be63b3SDoug Rabson 	case ISA_IVAR_PORT_0:
723a3be63b3SDoug Rabson 	case ISA_IVAR_PORT_1:
724a3be63b3SDoug Rabson 	case ISA_IVAR_PORTSIZE_0:
725a3be63b3SDoug Rabson 	case ISA_IVAR_PORTSIZE_1:
726a3be63b3SDoug Rabson 	case ISA_IVAR_MADDR_0:
727a3be63b3SDoug Rabson 	case ISA_IVAR_MADDR_1:
728a3be63b3SDoug Rabson 	case ISA_IVAR_MSIZE_0:
729a3be63b3SDoug Rabson 	case ISA_IVAR_MSIZE_1:
730a3be63b3SDoug Rabson 	case ISA_IVAR_IRQ_0:
731a3be63b3SDoug Rabson 	case ISA_IVAR_IRQ_1:
732a3be63b3SDoug Rabson 	case ISA_IVAR_DRQ_0:
733a3be63b3SDoug Rabson 	case ISA_IVAR_DRQ_1:
734a3be63b3SDoug Rabson 		return EINVAL;
735a3be63b3SDoug Rabson 
736cfa84f46SDoug Rabson 	case ISA_IVAR_VENDORID:
737cfa84f46SDoug Rabson 		idev->id_vendorid = value;
738cfa84f46SDoug Rabson 		break;
739cfa84f46SDoug Rabson 
740cfa84f46SDoug Rabson 	case ISA_IVAR_SERIAL:
741cfa84f46SDoug Rabson 		idev->id_serial = value;
742cfa84f46SDoug Rabson 		break;
743cfa84f46SDoug Rabson 
744cfa84f46SDoug Rabson 	case ISA_IVAR_LOGICALID:
745cfa84f46SDoug Rabson 		idev->id_logicalid = value;
746cfa84f46SDoug Rabson 		break;
747cfa84f46SDoug Rabson 
748cfa84f46SDoug Rabson 	case ISA_IVAR_COMPATID:
749cfa84f46SDoug Rabson 		idev->id_compatid = value;
750cfa84f46SDoug Rabson 		break;
751cfa84f46SDoug Rabson 
752a3be63b3SDoug Rabson 	default:
753a3be63b3SDoug Rabson 		return (ENOENT);
754a3be63b3SDoug Rabson 	}
755cfa84f46SDoug Rabson 
756a3be63b3SDoug Rabson 	return (0);
757a3be63b3SDoug Rabson }
758a3be63b3SDoug Rabson 
7594249382dSDoug Rabson /*
7604249382dSDoug Rabson  * Free any resources which the driver missed or which we were holding for
7614249382dSDoug Rabson  * it (see isa_probe_children).
7624249382dSDoug Rabson  */
7634249382dSDoug Rabson static void
7644249382dSDoug Rabson isa_child_detached(device_t dev, device_t child)
7654249382dSDoug Rabson {
7664249382dSDoug Rabson 	struct isa_device* idev = DEVTOISA(child);
76725afb89bSDoug Rabson 	struct resource_list *rl = &idev->id_resources;
7684249382dSDoug Rabson 	struct resource_list_entry *rle;
7694249382dSDoug Rabson 
7704249382dSDoug Rabson 	SLIST_FOREACH(rle, &idev->id_resources, link) {
7714249382dSDoug Rabson 		if (rle->res)
77225afb89bSDoug Rabson 			resource_list_release(rl, dev, child,
7734249382dSDoug Rabson 					      rle->type,
7744249382dSDoug Rabson 					      rle->rid,
7754249382dSDoug Rabson 					      rle->res);
7764249382dSDoug Rabson 	}
7774249382dSDoug Rabson }
7784249382dSDoug Rabson 
779ec22663bSDoug Rabson static void
780ec22663bSDoug Rabson isa_driver_added(device_t dev, driver_t *driver)
781ec22663bSDoug Rabson {
782ec22663bSDoug Rabson 	device_t *children;
783ec22663bSDoug Rabson 	int nchildren, i;
784ec22663bSDoug Rabson 
785ec22663bSDoug Rabson 	/*
786ec22663bSDoug Rabson 	 * Don't do anything if drivers are dynamically
787ec22663bSDoug Rabson 	 * added during autoconfiguration (cf. ymf724).
788ec22663bSDoug Rabson 	 * since that would end up calling identify
789ec22663bSDoug Rabson 	 * twice.
790ec22663bSDoug Rabson 	 */
791ec22663bSDoug Rabson 	if (!isa_running)
792ec22663bSDoug Rabson 		return;
793ec22663bSDoug Rabson 
794ec22663bSDoug Rabson 	DEVICE_IDENTIFY(driver, dev);
795ec22663bSDoug Rabson 	if (device_get_children(dev, &children, &nchildren))
796ec22663bSDoug Rabson 		return;
797ec22663bSDoug Rabson 
798ec22663bSDoug Rabson 	for (i = 0; i < nchildren; i++) {
799ec22663bSDoug Rabson 		device_t child = children[i];
800ec22663bSDoug Rabson 		struct isa_device *idev = DEVTOISA(child);
801ec22663bSDoug Rabson 		struct resource_list *rl = &idev->id_resources;
802ec22663bSDoug Rabson 		struct resource_list_entry *rle;
803ec22663bSDoug Rabson 
804ec22663bSDoug Rabson 		if (device_get_state(child) != DS_NOTPRESENT)
805ec22663bSDoug Rabson 			continue;
806ec22663bSDoug Rabson 
807ec22663bSDoug Rabson 		if (TAILQ_FIRST(&idev->id_configs))
808ec22663bSDoug Rabson 			if (!isa_assign_resources(child))
809ec22663bSDoug Rabson 				continue;
810ec22663bSDoug Rabson 
811ec22663bSDoug Rabson 		device_probe_and_attach(child);
812ec22663bSDoug Rabson 
813ec22663bSDoug Rabson 		if (TAILQ_FIRST(&idev->id_configs)) {
814ec22663bSDoug Rabson 			/*
815ec22663bSDoug Rabson 			 * Claim any unallocated resources to keep other
816ec22663bSDoug Rabson 			 * devices from using them.
817ec22663bSDoug Rabson 			 */
818ec22663bSDoug Rabson 			SLIST_FOREACH(rle, rl, link) {
819ec22663bSDoug Rabson 				if (!rle->res) {
820ec22663bSDoug Rabson 					int rid = rle->rid;
821ec22663bSDoug Rabson 					resource_list_alloc(rl, dev, child,
822ec22663bSDoug Rabson 							    rle->type,
823ec22663bSDoug Rabson 							    &rid,
824ec22663bSDoug Rabson 							    0, ~0, 1,
825ec22663bSDoug Rabson 							    RF_ACTIVE);
826ec22663bSDoug Rabson 				}
827ec22663bSDoug Rabson 			}
828ec22663bSDoug Rabson 		}
829ec22663bSDoug Rabson 	}
830ec22663bSDoug Rabson 
831ec22663bSDoug Rabson 	free(children, M_TEMP);
832ec22663bSDoug Rabson }
833ec22663bSDoug Rabson 
834a3be63b3SDoug Rabson static int
835a3be63b3SDoug Rabson isa_set_resource(device_t dev, device_t child, int type, int rid,
836a3be63b3SDoug Rabson 		 u_long start, u_long count)
837a3be63b3SDoug Rabson {
838a3be63b3SDoug Rabson 	struct isa_device* idev = DEVTOISA(child);
839a3be63b3SDoug Rabson 	struct resource_list *rl = &idev->id_resources;
840a3be63b3SDoug Rabson 
841a3be63b3SDoug Rabson 	if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
842a3be63b3SDoug Rabson 	    && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
843a3be63b3SDoug Rabson 		return EINVAL;
8444249382dSDoug Rabson 	if (rid < 0)
8454249382dSDoug Rabson 		return EINVAL;
8464249382dSDoug Rabson 	if (type == SYS_RES_IOPORT && rid >= ISA_NPORT)
8474249382dSDoug Rabson 		return EINVAL;
8484249382dSDoug Rabson 	if (type == SYS_RES_MEMORY && rid >= ISA_NMEM)
8494249382dSDoug Rabson 		return EINVAL;
8504249382dSDoug Rabson 	if (type == SYS_RES_IRQ && rid >= ISA_NIRQ)
8514249382dSDoug Rabson 		return EINVAL;
8524249382dSDoug Rabson 	if (type == SYS_RES_DRQ && rid >= ISA_NDRQ)
853a3be63b3SDoug Rabson 		return EINVAL;
854a3be63b3SDoug Rabson 
855a3be63b3SDoug Rabson 	resource_list_add(rl, type, rid, start, start + count - 1, count);
856a3be63b3SDoug Rabson 
857a3be63b3SDoug Rabson 	return 0;
858a3be63b3SDoug Rabson }
859a3be63b3SDoug Rabson 
860a3be63b3SDoug Rabson static int
861a3be63b3SDoug Rabson isa_get_resource(device_t dev, device_t child, int type, int rid,
862a3be63b3SDoug Rabson 		 u_long *startp, u_long *countp)
863a3be63b3SDoug Rabson {
864a3be63b3SDoug Rabson 	struct isa_device* idev = DEVTOISA(child);
865a3be63b3SDoug Rabson 	struct resource_list *rl = &idev->id_resources;
866a3be63b3SDoug Rabson 	struct resource_list_entry *rle;
867a3be63b3SDoug Rabson 
868a3be63b3SDoug Rabson 	rle = resource_list_find(rl, type, rid);
869a3be63b3SDoug Rabson 	if (!rle)
870a3be63b3SDoug Rabson 		return ENOENT;
871a3be63b3SDoug Rabson 
872a3be63b3SDoug Rabson 	*startp = rle->start;
873a3be63b3SDoug Rabson 	*countp = rle->count;
874a3be63b3SDoug Rabson 
875a3be63b3SDoug Rabson 	return 0;
876a3be63b3SDoug Rabson }
877a3be63b3SDoug Rabson 
878cfa84f46SDoug Rabson static void
879cfa84f46SDoug Rabson isa_delete_resource(device_t dev, device_t child, int type, int rid)
880cfa84f46SDoug Rabson {
881cfa84f46SDoug Rabson 	struct isa_device* idev = DEVTOISA(child);
882cfa84f46SDoug Rabson 	struct resource_list *rl = &idev->id_resources;
883cfa84f46SDoug Rabson 	resource_list_delete(rl, type, rid);
884cfa84f46SDoug Rabson }
885cfa84f46SDoug Rabson 
8864249382dSDoug Rabson static int
8874249382dSDoug Rabson isa_add_config(device_t dev, device_t child,
8884249382dSDoug Rabson 	       int priority, struct isa_config *config)
8894249382dSDoug Rabson {
8904249382dSDoug Rabson 	struct isa_device* idev = DEVTOISA(child);
8914249382dSDoug Rabson 	struct isa_config_entry *newice, *ice;
8924249382dSDoug Rabson 
8934249382dSDoug Rabson 	newice = malloc(sizeof *ice, M_DEVBUF, M_NOWAIT);
8944249382dSDoug Rabson 	if (!newice)
8954249382dSDoug Rabson 		return ENOMEM;
8964249382dSDoug Rabson 
8974249382dSDoug Rabson 	newice->ice_priority = priority;
8984249382dSDoug Rabson 	newice->ice_config = *config;
8994249382dSDoug Rabson 
9004249382dSDoug Rabson 	TAILQ_FOREACH(ice, &idev->id_configs, ice_link) {
9014249382dSDoug Rabson 		if (ice->ice_priority > priority)
9024249382dSDoug Rabson 			break;
9034249382dSDoug Rabson 	}
9044249382dSDoug Rabson 	if (ice)
9054249382dSDoug Rabson 		TAILQ_INSERT_BEFORE(ice, newice, ice_link);
9064249382dSDoug Rabson 	else
9074249382dSDoug Rabson 		TAILQ_INSERT_TAIL(&idev->id_configs, newice, ice_link);
9084249382dSDoug Rabson 
9094249382dSDoug Rabson 	return 0;
9104249382dSDoug Rabson }
9114249382dSDoug Rabson 
9124249382dSDoug Rabson static void
9134249382dSDoug Rabson isa_set_config_callback(device_t dev, device_t child,
9144249382dSDoug Rabson 			isa_config_cb *fn, void *arg)
9154249382dSDoug Rabson {
9164249382dSDoug Rabson 	struct isa_device* idev = DEVTOISA(child);
9174249382dSDoug Rabson 
9184249382dSDoug Rabson 	idev->id_config_cb = fn;
9194249382dSDoug Rabson 	idev->id_config_arg = arg;
9204249382dSDoug Rabson }
9214249382dSDoug Rabson 
9224249382dSDoug Rabson static int
9234249382dSDoug Rabson isa_pnp_probe(device_t dev, device_t child, struct isa_pnp_id *ids)
9244249382dSDoug Rabson {
9254249382dSDoug Rabson 	struct isa_device* idev = DEVTOISA(child);
9264249382dSDoug Rabson 
9274249382dSDoug Rabson 	if (!idev->id_vendorid)
9284249382dSDoug Rabson 		return ENOENT;
9294249382dSDoug Rabson 
9304249382dSDoug Rabson 	while (ids->ip_id) {
9314249382dSDoug Rabson 		/*
9324249382dSDoug Rabson 		 * Really ought to support >1 compat id per device.
9334249382dSDoug Rabson 		 */
9344249382dSDoug Rabson 		if (idev->id_logicalid == ids->ip_id
9354249382dSDoug Rabson 		    || idev->id_compatid == ids->ip_id) {
9362aadbbb6SDoug Rabson 			if (ids->ip_desc)
9374249382dSDoug Rabson 				device_set_desc(child, ids->ip_desc);
9384249382dSDoug Rabson 			return 0;
9394249382dSDoug Rabson 		}
9404249382dSDoug Rabson 		ids++;
9414249382dSDoug Rabson 	}
9424249382dSDoug Rabson 
9434249382dSDoug Rabson 	return ENXIO;
9444249382dSDoug Rabson }
9454249382dSDoug Rabson 
946a3be63b3SDoug Rabson static device_method_t isa_methods[] = {
947a3be63b3SDoug Rabson 	/* Device interface */
948a3be63b3SDoug Rabson 	DEVMETHOD(device_probe,		isa_probe),
949a3be63b3SDoug Rabson 	DEVMETHOD(device_attach,	isa_attach),
950a3be63b3SDoug Rabson 	DEVMETHOD(device_detach,	bus_generic_detach),
951a3be63b3SDoug Rabson 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
952a3be63b3SDoug Rabson 	DEVMETHOD(device_suspend,	bus_generic_suspend),
953a3be63b3SDoug Rabson 	DEVMETHOD(device_resume,	bus_generic_resume),
954a3be63b3SDoug Rabson 
955a3be63b3SDoug Rabson 	/* Bus interface */
956a3be63b3SDoug Rabson 	DEVMETHOD(bus_add_child,	isa_add_child),
957a3be63b3SDoug Rabson 	DEVMETHOD(bus_print_child,	isa_print_child),
958a3be63b3SDoug Rabson 	DEVMETHOD(bus_read_ivar,	isa_read_ivar),
959a3be63b3SDoug Rabson 	DEVMETHOD(bus_write_ivar,	isa_write_ivar),
9604249382dSDoug Rabson 	DEVMETHOD(bus_child_detached,	isa_child_detached),
961ec22663bSDoug Rabson 	DEVMETHOD(bus_driver_added,	isa_driver_added),
962a3be63b3SDoug Rabson 	DEVMETHOD(bus_alloc_resource,	isa_alloc_resource),
963a3be63b3SDoug Rabson 	DEVMETHOD(bus_release_resource,	isa_release_resource),
964a3be63b3SDoug Rabson 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
965a3be63b3SDoug Rabson 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
966a3be63b3SDoug Rabson 	DEVMETHOD(bus_setup_intr,	isa_setup_intr),
967a3be63b3SDoug Rabson 	DEVMETHOD(bus_teardown_intr,	isa_teardown_intr),
96825afb89bSDoug Rabson 	DEVMETHOD(bus_set_resource,	isa_set_resource),
96925afb89bSDoug Rabson 	DEVMETHOD(bus_get_resource,	isa_get_resource),
97025afb89bSDoug Rabson 	DEVMETHOD(bus_delete_resource,	isa_delete_resource),
971a3be63b3SDoug Rabson 
972a3be63b3SDoug Rabson 	/* ISA interface */
9734249382dSDoug Rabson 	DEVMETHOD(isa_add_config,	isa_add_config),
9744249382dSDoug Rabson 	DEVMETHOD(isa_set_config_callback, isa_set_config_callback),
9754249382dSDoug Rabson 	DEVMETHOD(isa_pnp_probe,	isa_pnp_probe),
976a3be63b3SDoug Rabson 
977a3be63b3SDoug Rabson 	{ 0, 0 }
978a3be63b3SDoug Rabson };
979a3be63b3SDoug Rabson 
980a3be63b3SDoug Rabson static driver_t isa_driver = {
981a3be63b3SDoug Rabson 	"isa",
982a3be63b3SDoug Rabson 	isa_methods,
983a3be63b3SDoug Rabson 	1,			/* no softc */
984a3be63b3SDoug Rabson };
985a3be63b3SDoug Rabson 
986a3be63b3SDoug Rabson /*
987a3be63b3SDoug Rabson  * ISA can be attached to a PCI-ISA bridge or directly to the nexus.
988a3be63b3SDoug Rabson  */
989a3be63b3SDoug Rabson DRIVER_MODULE(isa, isab, isa_driver, isa_devclass, 0, 0);
990a3be63b3SDoug Rabson #ifdef __i386__
991a3be63b3SDoug Rabson DRIVER_MODULE(isa, nexus, isa_driver, isa_devclass, 0, 0);
992a3be63b3SDoug Rabson #endif
9934249382dSDoug Rabson 
9944249382dSDoug Rabson /*
9954249382dSDoug Rabson  * A fallback driver for reporting un-matched pnp devices.
9964249382dSDoug Rabson  */
9974249382dSDoug Rabson 
9984249382dSDoug Rabson static int
9994249382dSDoug Rabson unknown_probe(device_t dev)
10004249382dSDoug Rabson {
10014249382dSDoug Rabson 	/*
10024249382dSDoug Rabson 	 * Only match pnp devices.
10034249382dSDoug Rabson 	 */
10044249382dSDoug Rabson 	if (isa_get_vendorid(dev) != 0)
10054249382dSDoug Rabson 		return -100;
10064249382dSDoug Rabson 	return ENXIO;
10074249382dSDoug Rabson }
10084249382dSDoug Rabson 
10094249382dSDoug Rabson static int
10104249382dSDoug Rabson unknown_attach(device_t dev)
10114249382dSDoug Rabson {
10124249382dSDoug Rabson 	return 0;
10134249382dSDoug Rabson }
10144249382dSDoug Rabson 
10154249382dSDoug Rabson static int
10164249382dSDoug Rabson unknown_detach(device_t dev)
10174249382dSDoug Rabson {
10184249382dSDoug Rabson 	return 0;
10194249382dSDoug Rabson }
10204249382dSDoug Rabson 
10214249382dSDoug Rabson static device_method_t unknown_methods[] = {
10224249382dSDoug Rabson 	/* Device interface */
10234249382dSDoug Rabson 	DEVMETHOD(device_probe,		unknown_probe),
10244249382dSDoug Rabson 	DEVMETHOD(device_attach,	unknown_attach),
10254249382dSDoug Rabson 	DEVMETHOD(device_detach,	unknown_detach),
10264249382dSDoug Rabson 
10274249382dSDoug Rabson 	{ 0, 0 }
10284249382dSDoug Rabson };
10294249382dSDoug Rabson 
10304249382dSDoug Rabson static driver_t unknown_driver = {
10314249382dSDoug Rabson 	"unknown",
10324249382dSDoug Rabson 	unknown_methods,
10334249382dSDoug Rabson 	1,			/* no softc */
10344249382dSDoug Rabson };
10354249382dSDoug Rabson 
10364249382dSDoug Rabson static devclass_t unknown_devclass;
10374249382dSDoug Rabson 
10384249382dSDoug Rabson DRIVER_MODULE(unknown, isa, unknown_driver, unknown_devclass, 0, 0);
1039