isa_common.c revision 51905
147398Sdfr/*-
247398Sdfr * Copyright (c) 1999 Doug Rabson
347398Sdfr * All rights reserved.
447398Sdfr *
547398Sdfr * Redistribution and use in source and binary forms, with or without
647398Sdfr * modification, are permitted provided that the following conditions
747398Sdfr * are met:
847398Sdfr * 1. Redistributions of source code must retain the above copyright
947398Sdfr *    notice, this list of conditions and the following disclaimer.
1047398Sdfr * 2. Redistributions in binary form must reproduce the above copyright
1147398Sdfr *    notice, this list of conditions and the following disclaimer in the
1247398Sdfr *    documentation and/or other materials provided with the distribution.
1347398Sdfr *
1447398Sdfr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1547398Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1647398Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1747398Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1847398Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1947398Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2047398Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2147398Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2247398Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2347398Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2447398Sdfr * SUCH DAMAGE.
2547398Sdfr *
2650477Speter * $FreeBSD: head/sys/isa/isa_common.c 51905 1999-10-03 12:13:06Z dfr $
2747398Sdfr */
2847398Sdfr/*
2947398Sdfr * Modifications for Intel architecture by Garrett A. Wollman.
3047398Sdfr * Copyright 1998 Massachusetts Institute of Technology
3147398Sdfr *
3247398Sdfr * Permission to use, copy, modify, and distribute this software and
3347398Sdfr * its documentation for any purpose and without fee is hereby
3447398Sdfr * granted, provided that both the above copyright notice and this
3547398Sdfr * permission notice appear in all copies, that both the above
3647398Sdfr * copyright notice and this permission notice appear in all
3747398Sdfr * supporting documentation, and that the name of M.I.T. not be used
3847398Sdfr * in advertising or publicity pertaining to distribution of the
3947398Sdfr * software without specific, written prior permission.  M.I.T. makes
4047398Sdfr * no representations about the suitability of this software for any
4147398Sdfr * purpose.  It is provided "as is" without express or implied
4247398Sdfr * warranty.
4347398Sdfr *
4447398Sdfr * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
4547398Sdfr * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
4647398Sdfr * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
4747398Sdfr * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
4847398Sdfr * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4947398Sdfr * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
5047398Sdfr * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
5147398Sdfr * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
5247398Sdfr * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
5347398Sdfr * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
5447398Sdfr * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5547398Sdfr * SUCH DAMAGE.
5647398Sdfr */
5747398Sdfr
5847398Sdfr/*
5947398Sdfr * Parts of the ISA bus implementation common to all architectures.
6047398Sdfr */
6147398Sdfr
6247398Sdfr#include <sys/param.h>
6347398Sdfr#include <sys/systm.h>
6447398Sdfr#include <sys/kernel.h>
6547398Sdfr#include <sys/bus.h>
6647398Sdfr#include <sys/malloc.h>
6747398Sdfr#include <sys/module.h>
6847398Sdfr#include <machine/bus.h>
6947398Sdfr#include <sys/rman.h>
7047398Sdfr
7147398Sdfr#include <machine/resource.h>
7247398Sdfr
7347398Sdfr#include <isa/isavar.h>
7447398Sdfr#include <isa/isa_common.h>
7547398Sdfr#ifdef __alpha__		/* XXX workaround a stupid warning */
7647398Sdfr#include <alpha/isa/isavar.h>
7747398Sdfr#endif
7847398Sdfr
7947398SdfrMALLOC_DEFINE(M_ISADEV, "isadev", "ISA device");
8047398Sdfr
8147398Sdfrstatic devclass_t isa_devclass;
8247398Sdfr
8347398Sdfr/*
8447398Sdfr * At 'probe' time, we add all the devices which we know about to the
8547398Sdfr * bus.  The generic attach routine will probe and attach them if they
8647398Sdfr * are alive.
8747398Sdfr */
8847398Sdfrstatic int
8947398Sdfrisa_probe(device_t dev)
9047398Sdfr{
9147398Sdfr	device_set_desc(dev, "ISA bus");
9247398Sdfr	isa_init();		/* Allow machdep code to initialise */
9347398Sdfr	return bus_generic_probe(dev);
9447398Sdfr}
9547398Sdfr
9647398Sdfrextern device_t isa_bus_device;
9747398Sdfr
9847398Sdfrstatic int
9947398Sdfrisa_attach(device_t dev)
10047398Sdfr{
10147398Sdfr	/*
10250769Sdfr	 * Arrange for isa_probe_children(dev) to be called later. XXX
10347398Sdfr	 */
10447398Sdfr	isa_bus_device = dev;
10547398Sdfr	return 0;
10647398Sdfr}
10747398Sdfr
10847398Sdfr/*
10950769Sdfr * Find a working set of memory regions for a child using the ranges
11050769Sdfr * in *config  and return the regions in *result. Returns non-zero if
11150769Sdfr * a set of ranges was found.
11250769Sdfr */
11350769Sdfrstatic int
11450769Sdfrisa_find_memory(device_t child,
11550769Sdfr		struct isa_config *config,
11650769Sdfr		struct isa_config *result)
11750769Sdfr{
11850769Sdfr	device_t dev = device_get_parent(child);
11950769Sdfr	int success, i;
12050769Sdfr	struct resource *res[ISA_NMEM];
12150769Sdfr
12250769Sdfr	/*
12350769Sdfr	 * First clear out any existing resource definitions.
12450769Sdfr	 */
12550769Sdfr	for (i = 0; i < ISA_NMEM; i++) {
12650769Sdfr		ISA_DELETE_RESOURCE(dev, child, SYS_RES_MEMORY, i);
12750769Sdfr		res[i] = NULL;
12850769Sdfr	}
12950769Sdfr
13050769Sdfr	success = 1;
13150769Sdfr	result->ic_nmem = config->ic_nmem;
13250769Sdfr	for (i = 0; i < config->ic_nmem; i++) {
13350769Sdfr		u_int32_t start, end, size, align;
13450769Sdfr		for (start = config->ic_mem[i].ir_start,
13550769Sdfr			     end = config->ic_mem[i].ir_end,
13650769Sdfr			     size = config->ic_mem[i].ir_size,
13750769Sdfr			     align = config->ic_mem[i].ir_align;
13850769Sdfr		     start + size - 1 <= end;
13950769Sdfr		     start += align) {
14050769Sdfr			ISA_SET_RESOURCE(dev, child, SYS_RES_MEMORY, i,
14150769Sdfr					 start, size);
14250769Sdfr			res[i] = bus_alloc_resource(child,
14350769Sdfr						    SYS_RES_MEMORY, &i,
14450769Sdfr						    0, ~0, 1, RF_ACTIVE);
14550769Sdfr			if (res[i]) {
14650769Sdfr				result->ic_mem[i].ir_start = start;
14750769Sdfr				result->ic_mem[i].ir_end = start + size - 1;
14850769Sdfr				result->ic_mem[i].ir_size = size;
14950769Sdfr				result->ic_mem[i].ir_align = align;
15050769Sdfr				break;
15150769Sdfr			}
15250769Sdfr		}
15350769Sdfr
15450769Sdfr		/*
15550769Sdfr		 * If we didn't find a place for memory range i, then
15650769Sdfr		 * give up now.
15750769Sdfr		 */
15850769Sdfr		if (!res[i]) {
15950769Sdfr			success = 0;
16050769Sdfr			break;
16150769Sdfr		}
16250769Sdfr	}
16350769Sdfr
16450769Sdfr	for (i = 0; i < ISA_NMEM; i++) {
16550769Sdfr		if (res[i])
16650769Sdfr			bus_release_resource(child, SYS_RES_MEMORY,
16750769Sdfr					     i, res[i]);
16850769Sdfr	}
16950769Sdfr
17050769Sdfr	return success;
17150769Sdfr}
17250769Sdfr
17350769Sdfr/*
17450769Sdfr * Find a working set of port regions for a child using the ranges
17550769Sdfr * in *config  and return the regions in *result. Returns non-zero if
17650769Sdfr * a set of ranges was found.
17750769Sdfr */
17850769Sdfrstatic int
17950769Sdfrisa_find_port(device_t child,
18050769Sdfr	      struct isa_config *config,
18150769Sdfr	      struct isa_config *result)
18250769Sdfr{
18350769Sdfr	device_t dev = device_get_parent(child);
18450769Sdfr	int success, i;
18550769Sdfr	struct resource *res[ISA_NPORT];
18650769Sdfr
18750769Sdfr	/*
18850769Sdfr	 * First clear out any existing resource definitions.
18950769Sdfr	 */
19050769Sdfr	for (i = 0; i < ISA_NPORT; i++) {
19150769Sdfr		ISA_DELETE_RESOURCE(dev, child, SYS_RES_IOPORT, i);
19250769Sdfr		res[i] = NULL;
19350769Sdfr	}
19450769Sdfr
19550769Sdfr	success = 1;
19650769Sdfr	result->ic_nport = config->ic_nport;
19750769Sdfr	for (i = 0; i < config->ic_nport; i++) {
19850769Sdfr		u_int32_t start, end, size, align;
19950769Sdfr		for (start = config->ic_port[i].ir_start,
20050769Sdfr			     end = config->ic_port[i].ir_end,
20150769Sdfr			     size = config->ic_port[i].ir_size,
20250769Sdfr			     align = config->ic_port[i].ir_align;
20350769Sdfr		     start + size - 1 <= end;
20450769Sdfr		     start += align) {
20550769Sdfr			ISA_SET_RESOURCE(dev, child, SYS_RES_IOPORT, i,
20650769Sdfr					 start, size);
20750769Sdfr			res[i] = bus_alloc_resource(child,
20850769Sdfr						    SYS_RES_IOPORT, &i,
20950769Sdfr						    0, ~0, 1, RF_ACTIVE);
21050769Sdfr			if (res[i]) {
21150769Sdfr				result->ic_port[i].ir_start = start;
21250769Sdfr				result->ic_port[i].ir_end = start + size - 1;
21350769Sdfr				result->ic_port[i].ir_size = size;
21450769Sdfr				result->ic_port[i].ir_align = align;
21550769Sdfr				break;
21650769Sdfr			}
21750769Sdfr		}
21850769Sdfr
21950769Sdfr		/*
22050769Sdfr		 * If we didn't find a place for port range i, then
22150769Sdfr		 * give up now.
22250769Sdfr		 */
22350769Sdfr		if (!res[i]) {
22450769Sdfr			success = 0;
22550769Sdfr			break;
22650769Sdfr		}
22750769Sdfr	}
22850769Sdfr
22950769Sdfr	for (i = 0; i < ISA_NPORT; i++) {
23050769Sdfr		if (res[i])
23150769Sdfr			bus_release_resource(child, SYS_RES_IOPORT,
23250769Sdfr					     i, res[i]);
23350769Sdfr	}
23450769Sdfr
23550769Sdfr	return success;
23650769Sdfr}
23750769Sdfr
23850769Sdfr/*
23950769Sdfr * Return the index of the first bit in the mask (or -1 if mask is empty.
24050769Sdfr */
24150769Sdfrstatic int
24250769Sdfrfind_first_bit(u_int32_t mask)
24350769Sdfr{
24450769Sdfr	return ffs(mask) - 1;
24550769Sdfr}
24650769Sdfr
24750769Sdfr/*
24850769Sdfr * Return the index of the next bit in the mask, or -1 if there are no more.
24950769Sdfr */
25050769Sdfrstatic int
25150769Sdfrfind_next_bit(u_int32_t mask, int bit)
25250769Sdfr{
25350769Sdfr	bit++;
25450769Sdfr	while (bit < 32 && !(mask & (1 << bit)))
25550769Sdfr		bit++;
25650769Sdfr	if (bit != 32)
25750769Sdfr		return bit;
25850769Sdfr	return -1;
25950769Sdfr}
26050769Sdfr
26150769Sdfr/*
26250769Sdfr * Find a working set of irqs for a child using the masks in *config
26350769Sdfr * and return the regions in *result. Returns non-zero if a set of
26450769Sdfr * irqs was found.
26550769Sdfr */
26650769Sdfrstatic int
26750769Sdfrisa_find_irq(device_t child,
26850769Sdfr	     struct isa_config *config,
26950769Sdfr	     struct isa_config *result)
27050769Sdfr{
27150769Sdfr	device_t dev = device_get_parent(child);
27250769Sdfr	int success, i;
27350769Sdfr	struct resource *res[ISA_NIRQ];
27450769Sdfr
27550769Sdfr	/*
27650769Sdfr	 * First clear out any existing resource definitions.
27750769Sdfr	 */
27850769Sdfr	for (i = 0; i < ISA_NIRQ; i++) {
27950769Sdfr		ISA_DELETE_RESOURCE(dev, child, SYS_RES_IRQ, i);
28050769Sdfr		res[i] = NULL;
28150769Sdfr	}
28250769Sdfr
28350769Sdfr	success = 1;
28450769Sdfr	result->ic_nirq = config->ic_nirq;
28550769Sdfr	for (i = 0; i < config->ic_nirq; i++) {
28650769Sdfr		u_int32_t mask = config->ic_irqmask[i];
28750769Sdfr		int irq;
28850769Sdfr		for (irq = find_first_bit(mask);
28950769Sdfr		     irq != -1;
29050769Sdfr		     irq = find_next_bit(mask, irq)) {
29150769Sdfr			ISA_SET_RESOURCE(dev, child, SYS_RES_IRQ, i,
29250769Sdfr					 irq, 1);
29350769Sdfr			res[i] = bus_alloc_resource(child,
29450769Sdfr						    SYS_RES_IRQ, &i,
29550769Sdfr						    0, ~0, 1, RF_ACTIVE);
29650769Sdfr			if (res[i]) {
29750769Sdfr				result->ic_irqmask[i] = (1 << irq);
29850769Sdfr				break;
29950769Sdfr			}
30050769Sdfr		}
30150769Sdfr
30250769Sdfr		/*
30350769Sdfr		 * If we didn't find a place for irq range i, then
30450769Sdfr		 * give up now.
30550769Sdfr		 */
30650769Sdfr		if (!res[i]) {
30750769Sdfr			success = 0;
30850769Sdfr			break;
30950769Sdfr		}
31050769Sdfr	}
31150769Sdfr
31250769Sdfr	for (i = 0; i < ISA_NIRQ; i++) {
31350769Sdfr		if (res[i])
31450769Sdfr			bus_release_resource(child, SYS_RES_IRQ,
31550769Sdfr					     i, res[i]);
31650769Sdfr	}
31750769Sdfr
31850769Sdfr	return success;
31950769Sdfr}
32050769Sdfr
32150769Sdfr/*
32250769Sdfr * Find a working set of drqs for a child using the masks in *config
32350769Sdfr * and return the regions in *result. Returns non-zero if a set of
32450769Sdfr * drqs was found.
32550769Sdfr */
32650769Sdfrstatic int
32750769Sdfrisa_find_drq(device_t child,
32850769Sdfr	     struct isa_config *config,
32950769Sdfr	     struct isa_config *result)
33050769Sdfr{
33150769Sdfr	device_t dev = device_get_parent(child);
33250769Sdfr	int success, i;
33350769Sdfr	struct resource *res[ISA_NDRQ];
33450769Sdfr
33550769Sdfr	/*
33650769Sdfr	 * First clear out any existing resource definitions.
33750769Sdfr	 */
33850769Sdfr	for (i = 0; i < ISA_NDRQ; i++) {
33950769Sdfr		ISA_DELETE_RESOURCE(dev, child, SYS_RES_DRQ, i);
34050769Sdfr		res[i] = NULL;
34150769Sdfr	}
34250769Sdfr
34350769Sdfr	success = 1;
34450769Sdfr	result->ic_ndrq = config->ic_ndrq;
34550769Sdfr	for (i = 0; i < config->ic_ndrq; i++) {
34650769Sdfr		u_int32_t mask = config->ic_drqmask[i];
34750769Sdfr		int drq;
34850769Sdfr		for (drq = find_first_bit(mask);
34950769Sdfr		     drq != -1;
35050769Sdfr		     drq = find_next_bit(mask, drq)) {
35150769Sdfr			ISA_SET_RESOURCE(dev, child, SYS_RES_DRQ, i,
35250769Sdfr					 drq, 1);
35350769Sdfr			res[i] = bus_alloc_resource(child,
35450769Sdfr						    SYS_RES_DRQ, &i,
35550769Sdfr						    0, ~0, 1, RF_ACTIVE);
35650769Sdfr			if (res[i]) {
35750769Sdfr				result->ic_drqmask[i] = (1 << drq);
35850769Sdfr				break;
35950769Sdfr			}
36050769Sdfr		}
36150769Sdfr
36250769Sdfr		/*
36350769Sdfr		 * If we didn't find a place for drq range i, then
36450769Sdfr		 * give up now.
36550769Sdfr		 */
36650769Sdfr		if (!res[i]) {
36750769Sdfr			success = 0;
36850769Sdfr			break;
36950769Sdfr		}
37050769Sdfr	}
37150769Sdfr
37250769Sdfr	for (i = 0; i < ISA_NDRQ; i++) {
37350769Sdfr		if (res[i])
37450769Sdfr			bus_release_resource(child, SYS_RES_DRQ,
37550769Sdfr					     i, res[i]);
37650769Sdfr	}
37750769Sdfr
37850769Sdfr	return success;
37950769Sdfr}
38050769Sdfr
38150769Sdfr/*
38250769Sdfr * Attempt to find a working set of resources for a device. Return
38350769Sdfr * non-zero if a working configuration is found.
38450769Sdfr */
38550769Sdfrstatic int
38650769Sdfrisa_assign_resources(device_t child)
38750769Sdfr{
38850769Sdfr	struct isa_device *idev = DEVTOISA(child);
38950769Sdfr	struct isa_config_entry *ice;
39050769Sdfr	struct isa_config config;
39150769Sdfr
39250769Sdfr	bzero(&config, sizeof config);
39350769Sdfr	TAILQ_FOREACH(ice, &idev->id_configs, ice_link) {
39450769Sdfr		if (!isa_find_memory(child, &ice->ice_config, &config))
39550769Sdfr			continue;
39650769Sdfr		if (!isa_find_port(child, &ice->ice_config, &config))
39750769Sdfr			continue;
39850769Sdfr		if (!isa_find_irq(child, &ice->ice_config, &config))
39950769Sdfr			continue;
40050769Sdfr		if (!isa_find_drq(child, &ice->ice_config, &config))
40150769Sdfr			continue;
40250769Sdfr
40350769Sdfr		/*
40450769Sdfr		 * A working configuration was found enable the device
40550769Sdfr		 * with this configuration.
40650769Sdfr		 */
40750769Sdfr		if (idev->id_config_cb) {
40850769Sdfr			idev->id_config_cb(idev->id_config_arg,
40950769Sdfr					   &config, 1);
41050769Sdfr			return 1;
41150769Sdfr		}
41250769Sdfr	}
41350769Sdfr
41450769Sdfr	/*
41550769Sdfr	 * Disable the device.
41650769Sdfr	 */
41750769Sdfr	bzero(&config, sizeof config);
41850769Sdfr	if (idev->id_config_cb)
41950769Sdfr		idev->id_config_cb(idev->id_config_arg, &config, 0);
42050769Sdfr	device_disable(child);
42150769Sdfr
42250769Sdfr	return 0;
42350769Sdfr}
42450769Sdfr
42550769Sdfr/*
42650769Sdfr * Called after other devices have initialised to probe for isa devices.
42750769Sdfr */
42850769Sdfrvoid
42950769Sdfrisa_probe_children(device_t dev)
43050769Sdfr{
43150769Sdfr	device_t *children;
43250769Sdfr	int nchildren, i;
43350769Sdfr
43450769Sdfr	if (device_get_children(dev, &children, &nchildren))
43550769Sdfr		return;
43650769Sdfr
43750769Sdfr	/*
43851905Sdfr	 * First disable all pnp devices so that they don't get
43951905Sdfr	 * matched by legacy probes.
44051905Sdfr	 */
44151905Sdfr	for (i = 0; i < nchildren; i++) {
44251905Sdfr		device_t child = children[i];
44351905Sdfr		struct isa_device *idev = DEVTOISA(child);
44451905Sdfr		struct isa_config config;
44551905Sdfr
44651905Sdfr		bzero(&config, sizeof config);
44751905Sdfr		if (idev->id_config_cb)
44851905Sdfr			idev->id_config_cb(idev->id_config_arg, &config, 0);
44951905Sdfr	}
45051905Sdfr
45151905Sdfr	/*
45251905Sdfr	 * Next probe all non-pnp devices so that they claim their
45350769Sdfr	 * resources first.
45450769Sdfr	 */
45550769Sdfr	for (i = 0; i < nchildren; i++) {
45650769Sdfr		device_t child = children[i];
45750769Sdfr		struct isa_device *idev = DEVTOISA(child);
45850769Sdfr
45950769Sdfr		if (TAILQ_FIRST(&idev->id_configs))
46050769Sdfr			continue;
46150769Sdfr
46250769Sdfr		device_probe_and_attach(child);
46350769Sdfr	}
46450769Sdfr
46550769Sdfr	/*
46651905Sdfr	 * Finally assign resource to pnp devices and probe them.
46750769Sdfr	 */
46850769Sdfr	for (i = 0; i < nchildren; i++) {
46950769Sdfr		device_t child = children[i];
47050769Sdfr		struct isa_device* idev = DEVTOISA(child);
47150769Sdfr
47250769Sdfr		if (!TAILQ_FIRST(&idev->id_configs))
47350769Sdfr			continue;
47450769Sdfr
47550769Sdfr		if (isa_assign_resources(child)) {
47650769Sdfr			struct resource_list_entry *rle;
47750769Sdfr
47850769Sdfr			device_probe_and_attach(child);
47950769Sdfr
48050769Sdfr			/*
48150769Sdfr			 * Claim any unallocated resources to keep other
48250769Sdfr			 * devices from using them.
48350769Sdfr			 */
48450769Sdfr			SLIST_FOREACH(rle, &idev->id_resources, link) {
48550769Sdfr				if (!rle->res) {
48650769Sdfr					int rid = rle->rid;
48750769Sdfr					resource_list_alloc(dev, child,
48850769Sdfr							    rle->type,
48950769Sdfr							    &rid,
49050769Sdfr							    0, ~0, 1,
49150769Sdfr							    RF_ACTIVE);
49250769Sdfr				}
49350769Sdfr			}
49450769Sdfr		}
49550769Sdfr	}
49650769Sdfr
49750769Sdfr	free(children, M_TEMP);
49850769Sdfr}
49950769Sdfr
50050769Sdfr/*
50147398Sdfr * Add a new child with default ivars.
50247398Sdfr */
50347398Sdfrstatic device_t
50447578Sdfrisa_add_child(device_t dev, int order, const char *name, int unit)
50547398Sdfr{
50647398Sdfr	struct	isa_device *idev;
50747398Sdfr
50847398Sdfr	idev = malloc(sizeof(struct isa_device), M_ISADEV, M_NOWAIT);
50947398Sdfr	if (!idev)
51047398Sdfr		return 0;
51147398Sdfr	bzero(idev, sizeof *idev);
51247398Sdfr
51347398Sdfr	resource_list_init(&idev->id_resources);
51450769Sdfr	TAILQ_INIT(&idev->id_configs);
51547398Sdfr
51647578Sdfr	return device_add_child_ordered(dev, order, name, unit, idev);
51747398Sdfr}
51847398Sdfr
51947398Sdfrstatic void
52047398Sdfrisa_print_resources(struct resource_list *rl, const char *name, int type,
52147398Sdfr		    const char *format)
52247398Sdfr{
52350769Sdfr	struct resource_list_entry *rle;
52450769Sdfr	int printed;
52550769Sdfr	int i;
52647398Sdfr
52750769Sdfr	printed = 0;
52850769Sdfr	for (i = 0; i < 16; i++) {
52950769Sdfr		rle = resource_list_find(rl, type, i);
53050769Sdfr		if (rle) {
53150769Sdfr			if (printed == 0)
53250769Sdfr				printf(" %s ", name);
53350769Sdfr			else if (printed > 0)
53447398Sdfr				printf(",");
53550769Sdfr			printed++;
53650769Sdfr			printf(format, rle->start);
53750769Sdfr			if (rle->count > 1) {
53847398Sdfr				printf("-");
53950769Sdfr				printf(format, rle->start + rle->count - 1);
54047398Sdfr			}
54150769Sdfr		} else if (i > 3) {
54250769Sdfr			/* check the first few regardless */
54350769Sdfr			break;
54447398Sdfr		}
54547398Sdfr	}
54647398Sdfr}
54747398Sdfr
54849195Smdoddstatic int
54947398Sdfrisa_print_child(device_t bus, device_t dev)
55047398Sdfr{
55147398Sdfr	struct	isa_device *idev = DEVTOISA(dev);
55247398Sdfr	struct resource_list *rl = &idev->id_resources;
55349195Smdodd	int retval = 0;
55447398Sdfr
55549195Smdodd	retval += bus_print_child_header(bus, dev);
55649195Smdodd
55751052Sdfr	if (SLIST_FIRST(rl) || device_get_flags(dev))
55849195Smdodd		retval += printf(" at");
55947398Sdfr
56049198Smdodd	isa_print_resources(rl, "port", SYS_RES_IOPORT, "%#lx");
56149198Smdodd	isa_print_resources(rl, "iomem", SYS_RES_MEMORY, "%#lx");
56249198Smdodd	isa_print_resources(rl, "irq", SYS_RES_IRQ, "%ld");
56349198Smdodd	isa_print_resources(rl, "drq", SYS_RES_DRQ, "%ld");
56451052Sdfr	if (device_get_flags(dev))
56551052Sdfr		retval += printf(" flags %#x", device_get_flags(dev));
56647398Sdfr
56749195Smdodd	retval += bus_print_child_footer(bus, dev);
56849195Smdodd
56949195Smdodd	return (retval);
57047398Sdfr}
57147398Sdfr
57247398Sdfrstatic int
57347398Sdfrisa_read_ivar(device_t bus, device_t dev, int index, uintptr_t * result)
57447398Sdfr{
57547398Sdfr	struct isa_device* idev = DEVTOISA(dev);
57647398Sdfr	struct resource_list *rl = &idev->id_resources;
57747398Sdfr	struct resource_list_entry *rle;
57847398Sdfr
57947398Sdfr	switch (index) {
58047398Sdfr	case ISA_IVAR_PORT_0:
58147398Sdfr		rle = resource_list_find(rl, SYS_RES_IOPORT, 0);
58247398Sdfr		if (rle)
58347398Sdfr			*result = rle->start;
58447398Sdfr		else
58547398Sdfr			*result = -1;
58647398Sdfr		break;
58747398Sdfr
58847398Sdfr	case ISA_IVAR_PORT_1:
58947398Sdfr		rle = resource_list_find(rl, SYS_RES_IOPORT, 1);
59047398Sdfr		if (rle)
59147398Sdfr			*result = rle->start;
59247398Sdfr		else
59347398Sdfr			*result = -1;
59447398Sdfr		break;
59547398Sdfr
59647398Sdfr	case ISA_IVAR_PORTSIZE_0:
59747398Sdfr		rle = resource_list_find(rl, SYS_RES_IOPORT, 0);
59847398Sdfr		if (rle)
59947398Sdfr			*result = rle->count;
60047398Sdfr		else
60147398Sdfr			*result = 0;
60247398Sdfr		break;
60347398Sdfr
60447398Sdfr	case ISA_IVAR_PORTSIZE_1:
60547398Sdfr		rle = resource_list_find(rl, SYS_RES_IOPORT, 1);
60647398Sdfr		if (rle)
60747398Sdfr			*result = rle->count;
60847398Sdfr		else
60947398Sdfr			*result = 0;
61047398Sdfr		break;
61147398Sdfr
61247398Sdfr	case ISA_IVAR_MADDR_0:
61347398Sdfr		rle = resource_list_find(rl, SYS_RES_MEMORY, 0);
61447398Sdfr		if (rle)
61547398Sdfr			*result = rle->start;
61647398Sdfr		else
61747398Sdfr			*result = -1;
61847398Sdfr		break;
61947398Sdfr
62047398Sdfr	case ISA_IVAR_MADDR_1:
62147398Sdfr		rle = resource_list_find(rl, SYS_RES_MEMORY, 1);
62247398Sdfr		if (rle)
62347398Sdfr			*result = rle->start;
62447398Sdfr		else
62547398Sdfr			*result = -1;
62647398Sdfr		break;
62747398Sdfr
62847398Sdfr	case ISA_IVAR_MSIZE_0:
62947398Sdfr		rle = resource_list_find(rl, SYS_RES_MEMORY, 0);
63047398Sdfr		if (rle)
63147398Sdfr			*result = rle->count;
63247398Sdfr		else
63347398Sdfr			*result = 0;
63447398Sdfr		break;
63547398Sdfr
63647398Sdfr	case ISA_IVAR_MSIZE_1:
63747398Sdfr		rle = resource_list_find(rl, SYS_RES_MEMORY, 1);
63847398Sdfr		if (rle)
63947398Sdfr			*result = rle->count;
64047398Sdfr		else
64147398Sdfr			*result = 0;
64247398Sdfr		break;
64347398Sdfr
64447398Sdfr	case ISA_IVAR_IRQ_0:
64547398Sdfr		rle = resource_list_find(rl, SYS_RES_IRQ, 0);
64647398Sdfr		if (rle)
64747398Sdfr			*result = rle->start;
64847398Sdfr		else
64947398Sdfr			*result = -1;
65047398Sdfr		break;
65147398Sdfr
65247398Sdfr	case ISA_IVAR_IRQ_1:
65347398Sdfr		rle = resource_list_find(rl, SYS_RES_IRQ, 1);
65447398Sdfr		if (rle)
65547398Sdfr			*result = rle->start;
65647398Sdfr		else
65747398Sdfr			*result = -1;
65847398Sdfr		break;
65947398Sdfr
66047398Sdfr	case ISA_IVAR_DRQ_0:
66147398Sdfr		rle = resource_list_find(rl, SYS_RES_DRQ, 0);
66247398Sdfr		if (rle)
66347398Sdfr			*result = rle->start;
66447398Sdfr		else
66547398Sdfr			*result = -1;
66647398Sdfr		break;
66747398Sdfr
66847398Sdfr	case ISA_IVAR_DRQ_1:
66947398Sdfr		rle = resource_list_find(rl, SYS_RES_DRQ, 1);
67047398Sdfr		if (rle)
67147398Sdfr			*result = rle->start;
67247398Sdfr		else
67347398Sdfr			*result = -1;
67447398Sdfr		break;
67547398Sdfr
67647613Sdfr	case ISA_IVAR_VENDORID:
67747613Sdfr		*result = idev->id_vendorid;
67847613Sdfr		break;
67947613Sdfr
68047613Sdfr	case ISA_IVAR_SERIAL:
68147613Sdfr		*result = idev->id_serial;
68247613Sdfr		break;
68347613Sdfr
68447613Sdfr	case ISA_IVAR_LOGICALID:
68547613Sdfr		*result = idev->id_logicalid;
68647613Sdfr		break;
68747613Sdfr
68847613Sdfr	case ISA_IVAR_COMPATID:
68947613Sdfr		*result = idev->id_compatid;
69047613Sdfr		break;
69147613Sdfr
69247613Sdfr	default:
69347613Sdfr		return ENOENT;
69447398Sdfr	}
69547613Sdfr
69647613Sdfr	return 0;
69747398Sdfr}
69847398Sdfr
69947398Sdfrstatic int
70047398Sdfrisa_write_ivar(device_t bus, device_t dev,
70147398Sdfr	       int index, uintptr_t value)
70247398Sdfr{
70347398Sdfr	struct isa_device* idev = DEVTOISA(dev);
70447398Sdfr
70547398Sdfr	switch (index) {
70647398Sdfr	case ISA_IVAR_PORT_0:
70747398Sdfr	case ISA_IVAR_PORT_1:
70847398Sdfr	case ISA_IVAR_PORTSIZE_0:
70947398Sdfr	case ISA_IVAR_PORTSIZE_1:
71047398Sdfr	case ISA_IVAR_MADDR_0:
71147398Sdfr	case ISA_IVAR_MADDR_1:
71247398Sdfr	case ISA_IVAR_MSIZE_0:
71347398Sdfr	case ISA_IVAR_MSIZE_1:
71447398Sdfr	case ISA_IVAR_IRQ_0:
71547398Sdfr	case ISA_IVAR_IRQ_1:
71647398Sdfr	case ISA_IVAR_DRQ_0:
71747398Sdfr	case ISA_IVAR_DRQ_1:
71847398Sdfr		return EINVAL;
71947398Sdfr
72047613Sdfr	case ISA_IVAR_VENDORID:
72147613Sdfr		idev->id_vendorid = value;
72247613Sdfr		break;
72347613Sdfr
72447613Sdfr	case ISA_IVAR_SERIAL:
72547613Sdfr		idev->id_serial = value;
72647613Sdfr		break;
72747613Sdfr
72847613Sdfr	case ISA_IVAR_LOGICALID:
72947613Sdfr		idev->id_logicalid = value;
73047613Sdfr		break;
73147613Sdfr
73247613Sdfr	case ISA_IVAR_COMPATID:
73347613Sdfr		idev->id_compatid = value;
73447613Sdfr		break;
73547613Sdfr
73647398Sdfr	default:
73747398Sdfr		return (ENOENT);
73847398Sdfr	}
73947613Sdfr
74047398Sdfr	return (0);
74147398Sdfr}
74247398Sdfr
74350769Sdfr/*
74450769Sdfr * Free any resources which the driver missed or which we were holding for
74550769Sdfr * it (see isa_probe_children).
74650769Sdfr */
74750769Sdfrstatic void
74850769Sdfrisa_child_detached(device_t dev, device_t child)
74950769Sdfr{
75050769Sdfr	struct isa_device* idev = DEVTOISA(child);
75150769Sdfr	struct resource_list_entry *rle;
75250769Sdfr
75350769Sdfr	SLIST_FOREACH(rle, &idev->id_resources, link) {
75450769Sdfr		if (rle->res)
75550769Sdfr			resource_list_release(dev, child,
75650769Sdfr					      rle->type,
75750769Sdfr					      rle->rid,
75850769Sdfr					      rle->res);
75950769Sdfr	}
76050769Sdfr}
76150769Sdfr
76247398Sdfrstatic int
76347398Sdfrisa_set_resource(device_t dev, device_t child, int type, int rid,
76447398Sdfr		 u_long start, u_long count)
76547398Sdfr{
76647398Sdfr	struct isa_device* idev = DEVTOISA(child);
76747398Sdfr	struct resource_list *rl = &idev->id_resources;
76847398Sdfr
76947398Sdfr	if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
77047398Sdfr	    && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
77147398Sdfr		return EINVAL;
77250769Sdfr	if (rid < 0)
77347398Sdfr		return EINVAL;
77450769Sdfr	if (type == SYS_RES_IOPORT && rid >= ISA_NPORT)
77550769Sdfr		return EINVAL;
77650769Sdfr	if (type == SYS_RES_MEMORY && rid >= ISA_NMEM)
77750769Sdfr		return EINVAL;
77850769Sdfr	if (type == SYS_RES_IRQ && rid >= ISA_NIRQ)
77950769Sdfr		return EINVAL;
78050769Sdfr	if (type == SYS_RES_DRQ && rid >= ISA_NDRQ)
78150769Sdfr		return EINVAL;
78247398Sdfr
78347398Sdfr	resource_list_add(rl, type, rid, start, start + count - 1, count);
78447398Sdfr
78547398Sdfr	return 0;
78647398Sdfr}
78747398Sdfr
78847398Sdfrstatic int
78947398Sdfrisa_get_resource(device_t dev, device_t child, int type, int rid,
79047398Sdfr		 u_long *startp, u_long *countp)
79147398Sdfr{
79247398Sdfr	struct isa_device* idev = DEVTOISA(child);
79347398Sdfr	struct resource_list *rl = &idev->id_resources;
79447398Sdfr	struct resource_list_entry *rle;
79547398Sdfr
79647398Sdfr	rle = resource_list_find(rl, type, rid);
79747398Sdfr	if (!rle)
79847398Sdfr		return ENOENT;
79947398Sdfr
80047398Sdfr	*startp = rle->start;
80147398Sdfr	*countp = rle->count;
80247398Sdfr
80347398Sdfr	return 0;
80447398Sdfr}
80547398Sdfr
80647613Sdfrstatic void
80747613Sdfrisa_delete_resource(device_t dev, device_t child, int type, int rid)
80847613Sdfr{
80947613Sdfr	struct isa_device* idev = DEVTOISA(child);
81047613Sdfr	struct resource_list *rl = &idev->id_resources;
81147613Sdfr	resource_list_delete(rl, type, rid);
81247613Sdfr}
81347613Sdfr
81450769Sdfrstatic int
81550769Sdfrisa_add_config(device_t dev, device_t child,
81650769Sdfr	       int priority, struct isa_config *config)
81750769Sdfr{
81850769Sdfr	struct isa_device* idev = DEVTOISA(child);
81950769Sdfr	struct isa_config_entry *newice, *ice;
82050769Sdfr
82150769Sdfr	newice = malloc(sizeof *ice, M_DEVBUF, M_NOWAIT);
82250769Sdfr	if (!newice)
82350769Sdfr		return ENOMEM;
82450769Sdfr
82550769Sdfr	newice->ice_priority = priority;
82650769Sdfr	newice->ice_config = *config;
82750769Sdfr
82850769Sdfr	TAILQ_FOREACH(ice, &idev->id_configs, ice_link) {
82950769Sdfr		if (ice->ice_priority > priority)
83050769Sdfr			break;
83150769Sdfr	}
83250769Sdfr	if (ice)
83350769Sdfr		TAILQ_INSERT_BEFORE(ice, newice, ice_link);
83450769Sdfr	else
83550769Sdfr		TAILQ_INSERT_TAIL(&idev->id_configs, newice, ice_link);
83650769Sdfr
83750769Sdfr	return 0;
83850769Sdfr}
83950769Sdfr
84050769Sdfrstatic void
84150769Sdfrisa_set_config_callback(device_t dev, device_t child,
84250769Sdfr			isa_config_cb *fn, void *arg)
84350769Sdfr{
84450769Sdfr	struct isa_device* idev = DEVTOISA(child);
84550769Sdfr
84650769Sdfr	idev->id_config_cb = fn;
84750769Sdfr	idev->id_config_arg = arg;
84850769Sdfr}
84950769Sdfr
85050769Sdfrstatic int
85150769Sdfrisa_pnp_probe(device_t dev, device_t child, struct isa_pnp_id *ids)
85250769Sdfr{
85350769Sdfr	struct isa_device* idev = DEVTOISA(child);
85450769Sdfr
85550769Sdfr	if (!idev->id_vendorid)
85650769Sdfr		return ENOENT;
85750769Sdfr
85850769Sdfr	while (ids->ip_id) {
85950769Sdfr		/*
86050769Sdfr		 * Really ought to support >1 compat id per device.
86150769Sdfr		 */
86250769Sdfr		if (idev->id_logicalid == ids->ip_id
86350769Sdfr		    || idev->id_compatid == ids->ip_id) {
86450910Sdfr			if (ids->ip_desc)
86550910Sdfr				device_set_desc(child, ids->ip_desc);
86650769Sdfr			return 0;
86750769Sdfr		}
86850769Sdfr		ids++;
86950769Sdfr	}
87050769Sdfr
87150769Sdfr	return ENXIO;
87250769Sdfr}
87350769Sdfr
87447398Sdfrstatic device_method_t isa_methods[] = {
87547398Sdfr	/* Device interface */
87647398Sdfr	DEVMETHOD(device_probe,		isa_probe),
87747398Sdfr	DEVMETHOD(device_attach,	isa_attach),
87847398Sdfr	DEVMETHOD(device_detach,	bus_generic_detach),
87947398Sdfr	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
88047398Sdfr	DEVMETHOD(device_suspend,	bus_generic_suspend),
88147398Sdfr	DEVMETHOD(device_resume,	bus_generic_resume),
88247398Sdfr
88347398Sdfr	/* Bus interface */
88447398Sdfr	DEVMETHOD(bus_add_child,	isa_add_child),
88547398Sdfr	DEVMETHOD(bus_print_child,	isa_print_child),
88647398Sdfr	DEVMETHOD(bus_read_ivar,	isa_read_ivar),
88747398Sdfr	DEVMETHOD(bus_write_ivar,	isa_write_ivar),
88850769Sdfr	DEVMETHOD(bus_child_detached,	isa_child_detached),
88947398Sdfr	DEVMETHOD(bus_alloc_resource,	isa_alloc_resource),
89047398Sdfr	DEVMETHOD(bus_release_resource,	isa_release_resource),
89147398Sdfr	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
89247398Sdfr	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
89347398Sdfr	DEVMETHOD(bus_setup_intr,	isa_setup_intr),
89447398Sdfr	DEVMETHOD(bus_teardown_intr,	isa_teardown_intr),
89547398Sdfr
89647398Sdfr	/* ISA interface */
89747398Sdfr	DEVMETHOD(isa_set_resource,	isa_set_resource),
89847398Sdfr	DEVMETHOD(isa_get_resource,	isa_get_resource),
89947613Sdfr	DEVMETHOD(isa_delete_resource,	isa_delete_resource),
90050769Sdfr	DEVMETHOD(isa_add_config,	isa_add_config),
90150769Sdfr	DEVMETHOD(isa_set_config_callback, isa_set_config_callback),
90250769Sdfr	DEVMETHOD(isa_pnp_probe,	isa_pnp_probe),
90347398Sdfr
90447398Sdfr	{ 0, 0 }
90547398Sdfr};
90647398Sdfr
90747398Sdfrstatic driver_t isa_driver = {
90847398Sdfr	"isa",
90947398Sdfr	isa_methods,
91047398Sdfr	1,			/* no softc */
91147398Sdfr};
91247398Sdfr
91347398Sdfr/*
91447398Sdfr * ISA can be attached to a PCI-ISA bridge or directly to the nexus.
91547398Sdfr */
91647398SdfrDRIVER_MODULE(isa, isab, isa_driver, isa_devclass, 0, 0);
91747398Sdfr#ifdef __i386__
91847398SdfrDRIVER_MODULE(isa, nexus, isa_driver, isa_devclass, 0, 0);
91947398Sdfr#endif
92050769Sdfr
92150769Sdfr/*
92250769Sdfr * A fallback driver for reporting un-matched pnp devices.
92350769Sdfr */
92450769Sdfr
92550769Sdfrstatic int
92650769Sdfrunknown_probe(device_t dev)
92750769Sdfr{
92850769Sdfr	/*
92950769Sdfr	 * Only match pnp devices.
93050769Sdfr	 */
93150769Sdfr	if (isa_get_vendorid(dev) != 0)
93250769Sdfr		return -100;
93350769Sdfr	return ENXIO;
93450769Sdfr}
93550769Sdfr
93650769Sdfrstatic int
93750769Sdfrunknown_attach(device_t dev)
93850769Sdfr{
93950769Sdfr	return 0;
94050769Sdfr}
94150769Sdfr
94250769Sdfrstatic int
94350769Sdfrunknown_detach(device_t dev)
94450769Sdfr{
94550769Sdfr	return 0;
94650769Sdfr}
94750769Sdfr
94850769Sdfrstatic device_method_t unknown_methods[] = {
94950769Sdfr	/* Device interface */
95050769Sdfr	DEVMETHOD(device_probe,		unknown_probe),
95150769Sdfr	DEVMETHOD(device_attach,	unknown_attach),
95250769Sdfr	DEVMETHOD(device_detach,	unknown_detach),
95350769Sdfr
95450769Sdfr	{ 0, 0 }
95550769Sdfr};
95650769Sdfr
95750769Sdfrstatic driver_t unknown_driver = {
95850769Sdfr	"unknown",
95950769Sdfr	unknown_methods,
96050769Sdfr	1,			/* no softc */
96150769Sdfr};
96250769Sdfr
96350769Sdfrstatic devclass_t unknown_devclass;
96450769Sdfr
96550769SdfrDRIVER_MODULE(unknown, isa, unknown_driver, unknown_devclass, 0, 0);
966