isa_common.c revision 51052
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 51052 1999-09-07 08:42:49Z 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	/*
43850769Sdfr	 * First probe all non-pnp devices so that they claim their
43950769Sdfr	 * resources first.
44050769Sdfr	 */
44150769Sdfr	for (i = 0; i < nchildren; i++) {
44250769Sdfr		device_t child = children[i];
44350769Sdfr		struct isa_device *idev = DEVTOISA(child);
44450769Sdfr
44550769Sdfr		if (TAILQ_FIRST(&idev->id_configs))
44650769Sdfr			continue;
44750769Sdfr
44850769Sdfr		device_probe_and_attach(child);
44950769Sdfr	}
45050769Sdfr
45150769Sdfr	/*
45250769Sdfr	 * Next assign resource to pnp devices and probe them.
45350769Sdfr	 */
45450769Sdfr	for (i = 0; i < nchildren; i++) {
45550769Sdfr		device_t child = children[i];
45650769Sdfr		struct isa_device* idev = DEVTOISA(child);
45750769Sdfr
45850769Sdfr		if (!TAILQ_FIRST(&idev->id_configs))
45950769Sdfr			continue;
46050769Sdfr
46150769Sdfr		if (isa_assign_resources(child)) {
46250769Sdfr			struct resource_list_entry *rle;
46350769Sdfr
46450769Sdfr			device_probe_and_attach(child);
46550769Sdfr
46650769Sdfr			/*
46750769Sdfr			 * Claim any unallocated resources to keep other
46850769Sdfr			 * devices from using them.
46950769Sdfr			 */
47050769Sdfr			SLIST_FOREACH(rle, &idev->id_resources, link) {
47150769Sdfr				if (!rle->res) {
47250769Sdfr					int rid = rle->rid;
47350769Sdfr					resource_list_alloc(dev, child,
47450769Sdfr							    rle->type,
47550769Sdfr							    &rid,
47650769Sdfr							    0, ~0, 1,
47750769Sdfr							    RF_ACTIVE);
47850769Sdfr				}
47950769Sdfr			}
48050769Sdfr		}
48150769Sdfr	}
48250769Sdfr
48350769Sdfr	free(children, M_TEMP);
48450769Sdfr}
48550769Sdfr
48650769Sdfr/*
48747398Sdfr * Add a new child with default ivars.
48847398Sdfr */
48947398Sdfrstatic device_t
49047578Sdfrisa_add_child(device_t dev, int order, const char *name, int unit)
49147398Sdfr{
49247398Sdfr	struct	isa_device *idev;
49347398Sdfr
49447398Sdfr	idev = malloc(sizeof(struct isa_device), M_ISADEV, M_NOWAIT);
49547398Sdfr	if (!idev)
49647398Sdfr		return 0;
49747398Sdfr	bzero(idev, sizeof *idev);
49847398Sdfr
49947398Sdfr	resource_list_init(&idev->id_resources);
50050769Sdfr	TAILQ_INIT(&idev->id_configs);
50147398Sdfr
50247578Sdfr	return device_add_child_ordered(dev, order, name, unit, idev);
50347398Sdfr}
50447398Sdfr
50547398Sdfrstatic void
50647398Sdfrisa_print_resources(struct resource_list *rl, const char *name, int type,
50747398Sdfr		    const char *format)
50847398Sdfr{
50950769Sdfr	struct resource_list_entry *rle;
51050769Sdfr	int printed;
51150769Sdfr	int i;
51247398Sdfr
51350769Sdfr	printed = 0;
51450769Sdfr	for (i = 0; i < 16; i++) {
51550769Sdfr		rle = resource_list_find(rl, type, i);
51650769Sdfr		if (rle) {
51750769Sdfr			if (printed == 0)
51850769Sdfr				printf(" %s ", name);
51950769Sdfr			else if (printed > 0)
52047398Sdfr				printf(",");
52150769Sdfr			printed++;
52250769Sdfr			printf(format, rle->start);
52350769Sdfr			if (rle->count > 1) {
52447398Sdfr				printf("-");
52550769Sdfr				printf(format, rle->start + rle->count - 1);
52647398Sdfr			}
52750769Sdfr		} else if (i > 3) {
52850769Sdfr			/* check the first few regardless */
52950769Sdfr			break;
53047398Sdfr		}
53147398Sdfr	}
53247398Sdfr}
53347398Sdfr
53449195Smdoddstatic int
53547398Sdfrisa_print_child(device_t bus, device_t dev)
53647398Sdfr{
53747398Sdfr	struct	isa_device *idev = DEVTOISA(dev);
53847398Sdfr	struct resource_list *rl = &idev->id_resources;
53949195Smdodd	int retval = 0;
54047398Sdfr
54149195Smdodd	retval += bus_print_child_header(bus, dev);
54249195Smdodd
54351052Sdfr	if (SLIST_FIRST(rl) || device_get_flags(dev))
54449195Smdodd		retval += printf(" at");
54547398Sdfr
54649198Smdodd	isa_print_resources(rl, "port", SYS_RES_IOPORT, "%#lx");
54749198Smdodd	isa_print_resources(rl, "iomem", SYS_RES_MEMORY, "%#lx");
54849198Smdodd	isa_print_resources(rl, "irq", SYS_RES_IRQ, "%ld");
54949198Smdodd	isa_print_resources(rl, "drq", SYS_RES_DRQ, "%ld");
55051052Sdfr	if (device_get_flags(dev))
55151052Sdfr		retval += printf(" flags %#x", device_get_flags(dev));
55247398Sdfr
55349195Smdodd	retval += bus_print_child_footer(bus, dev);
55449195Smdodd
55549195Smdodd	return (retval);
55647398Sdfr}
55747398Sdfr
55847398Sdfrstatic int
55947398Sdfrisa_read_ivar(device_t bus, device_t dev, int index, uintptr_t * result)
56047398Sdfr{
56147398Sdfr	struct isa_device* idev = DEVTOISA(dev);
56247398Sdfr	struct resource_list *rl = &idev->id_resources;
56347398Sdfr	struct resource_list_entry *rle;
56447398Sdfr
56547398Sdfr	switch (index) {
56647398Sdfr	case ISA_IVAR_PORT_0:
56747398Sdfr		rle = resource_list_find(rl, SYS_RES_IOPORT, 0);
56847398Sdfr		if (rle)
56947398Sdfr			*result = rle->start;
57047398Sdfr		else
57147398Sdfr			*result = -1;
57247398Sdfr		break;
57347398Sdfr
57447398Sdfr	case ISA_IVAR_PORT_1:
57547398Sdfr		rle = resource_list_find(rl, SYS_RES_IOPORT, 1);
57647398Sdfr		if (rle)
57747398Sdfr			*result = rle->start;
57847398Sdfr		else
57947398Sdfr			*result = -1;
58047398Sdfr		break;
58147398Sdfr
58247398Sdfr	case ISA_IVAR_PORTSIZE_0:
58347398Sdfr		rle = resource_list_find(rl, SYS_RES_IOPORT, 0);
58447398Sdfr		if (rle)
58547398Sdfr			*result = rle->count;
58647398Sdfr		else
58747398Sdfr			*result = 0;
58847398Sdfr		break;
58947398Sdfr
59047398Sdfr	case ISA_IVAR_PORTSIZE_1:
59147398Sdfr		rle = resource_list_find(rl, SYS_RES_IOPORT, 1);
59247398Sdfr		if (rle)
59347398Sdfr			*result = rle->count;
59447398Sdfr		else
59547398Sdfr			*result = 0;
59647398Sdfr		break;
59747398Sdfr
59847398Sdfr	case ISA_IVAR_MADDR_0:
59947398Sdfr		rle = resource_list_find(rl, SYS_RES_MEMORY, 0);
60047398Sdfr		if (rle)
60147398Sdfr			*result = rle->start;
60247398Sdfr		else
60347398Sdfr			*result = -1;
60447398Sdfr		break;
60547398Sdfr
60647398Sdfr	case ISA_IVAR_MADDR_1:
60747398Sdfr		rle = resource_list_find(rl, SYS_RES_MEMORY, 1);
60847398Sdfr		if (rle)
60947398Sdfr			*result = rle->start;
61047398Sdfr		else
61147398Sdfr			*result = -1;
61247398Sdfr		break;
61347398Sdfr
61447398Sdfr	case ISA_IVAR_MSIZE_0:
61547398Sdfr		rle = resource_list_find(rl, SYS_RES_MEMORY, 0);
61647398Sdfr		if (rle)
61747398Sdfr			*result = rle->count;
61847398Sdfr		else
61947398Sdfr			*result = 0;
62047398Sdfr		break;
62147398Sdfr
62247398Sdfr	case ISA_IVAR_MSIZE_1:
62347398Sdfr		rle = resource_list_find(rl, SYS_RES_MEMORY, 1);
62447398Sdfr		if (rle)
62547398Sdfr			*result = rle->count;
62647398Sdfr		else
62747398Sdfr			*result = 0;
62847398Sdfr		break;
62947398Sdfr
63047398Sdfr	case ISA_IVAR_IRQ_0:
63147398Sdfr		rle = resource_list_find(rl, SYS_RES_IRQ, 0);
63247398Sdfr		if (rle)
63347398Sdfr			*result = rle->start;
63447398Sdfr		else
63547398Sdfr			*result = -1;
63647398Sdfr		break;
63747398Sdfr
63847398Sdfr	case ISA_IVAR_IRQ_1:
63947398Sdfr		rle = resource_list_find(rl, SYS_RES_IRQ, 1);
64047398Sdfr		if (rle)
64147398Sdfr			*result = rle->start;
64247398Sdfr		else
64347398Sdfr			*result = -1;
64447398Sdfr		break;
64547398Sdfr
64647398Sdfr	case ISA_IVAR_DRQ_0:
64747398Sdfr		rle = resource_list_find(rl, SYS_RES_DRQ, 0);
64847398Sdfr		if (rle)
64947398Sdfr			*result = rle->start;
65047398Sdfr		else
65147398Sdfr			*result = -1;
65247398Sdfr		break;
65347398Sdfr
65447398Sdfr	case ISA_IVAR_DRQ_1:
65547398Sdfr		rle = resource_list_find(rl, SYS_RES_DRQ, 1);
65647398Sdfr		if (rle)
65747398Sdfr			*result = rle->start;
65847398Sdfr		else
65947398Sdfr			*result = -1;
66047398Sdfr		break;
66147398Sdfr
66247613Sdfr	case ISA_IVAR_VENDORID:
66347613Sdfr		*result = idev->id_vendorid;
66447613Sdfr		break;
66547613Sdfr
66647613Sdfr	case ISA_IVAR_SERIAL:
66747613Sdfr		*result = idev->id_serial;
66847613Sdfr		break;
66947613Sdfr
67047613Sdfr	case ISA_IVAR_LOGICALID:
67147613Sdfr		*result = idev->id_logicalid;
67247613Sdfr		break;
67347613Sdfr
67447613Sdfr	case ISA_IVAR_COMPATID:
67547613Sdfr		*result = idev->id_compatid;
67647613Sdfr		break;
67747613Sdfr
67847613Sdfr	default:
67947613Sdfr		return ENOENT;
68047398Sdfr	}
68147613Sdfr
68247613Sdfr	return 0;
68347398Sdfr}
68447398Sdfr
68547398Sdfrstatic int
68647398Sdfrisa_write_ivar(device_t bus, device_t dev,
68747398Sdfr	       int index, uintptr_t value)
68847398Sdfr{
68947398Sdfr	struct isa_device* idev = DEVTOISA(dev);
69047398Sdfr
69147398Sdfr	switch (index) {
69247398Sdfr	case ISA_IVAR_PORT_0:
69347398Sdfr	case ISA_IVAR_PORT_1:
69447398Sdfr	case ISA_IVAR_PORTSIZE_0:
69547398Sdfr	case ISA_IVAR_PORTSIZE_1:
69647398Sdfr	case ISA_IVAR_MADDR_0:
69747398Sdfr	case ISA_IVAR_MADDR_1:
69847398Sdfr	case ISA_IVAR_MSIZE_0:
69947398Sdfr	case ISA_IVAR_MSIZE_1:
70047398Sdfr	case ISA_IVAR_IRQ_0:
70147398Sdfr	case ISA_IVAR_IRQ_1:
70247398Sdfr	case ISA_IVAR_DRQ_0:
70347398Sdfr	case ISA_IVAR_DRQ_1:
70447398Sdfr		return EINVAL;
70547398Sdfr
70647613Sdfr	case ISA_IVAR_VENDORID:
70747613Sdfr		idev->id_vendorid = value;
70847613Sdfr		break;
70947613Sdfr
71047613Sdfr	case ISA_IVAR_SERIAL:
71147613Sdfr		idev->id_serial = value;
71247613Sdfr		break;
71347613Sdfr
71447613Sdfr	case ISA_IVAR_LOGICALID:
71547613Sdfr		idev->id_logicalid = value;
71647613Sdfr		break;
71747613Sdfr
71847613Sdfr	case ISA_IVAR_COMPATID:
71947613Sdfr		idev->id_compatid = value;
72047613Sdfr		break;
72147613Sdfr
72247398Sdfr	default:
72347398Sdfr		return (ENOENT);
72447398Sdfr	}
72547613Sdfr
72647398Sdfr	return (0);
72747398Sdfr}
72847398Sdfr
72950769Sdfr/*
73050769Sdfr * Free any resources which the driver missed or which we were holding for
73150769Sdfr * it (see isa_probe_children).
73250769Sdfr */
73350769Sdfrstatic void
73450769Sdfrisa_child_detached(device_t dev, device_t child)
73550769Sdfr{
73650769Sdfr	struct isa_device* idev = DEVTOISA(child);
73750769Sdfr	struct resource_list_entry *rle;
73850769Sdfr
73950769Sdfr	SLIST_FOREACH(rle, &idev->id_resources, link) {
74050769Sdfr		if (rle->res)
74150769Sdfr			resource_list_release(dev, child,
74250769Sdfr					      rle->type,
74350769Sdfr					      rle->rid,
74450769Sdfr					      rle->res);
74550769Sdfr	}
74650769Sdfr}
74750769Sdfr
74847398Sdfrstatic int
74947398Sdfrisa_set_resource(device_t dev, device_t child, int type, int rid,
75047398Sdfr		 u_long start, u_long count)
75147398Sdfr{
75247398Sdfr	struct isa_device* idev = DEVTOISA(child);
75347398Sdfr	struct resource_list *rl = &idev->id_resources;
75447398Sdfr
75547398Sdfr	if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
75647398Sdfr	    && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
75747398Sdfr		return EINVAL;
75850769Sdfr	if (rid < 0)
75947398Sdfr		return EINVAL;
76050769Sdfr	if (type == SYS_RES_IOPORT && rid >= ISA_NPORT)
76150769Sdfr		return EINVAL;
76250769Sdfr	if (type == SYS_RES_MEMORY && rid >= ISA_NMEM)
76350769Sdfr		return EINVAL;
76450769Sdfr	if (type == SYS_RES_IRQ && rid >= ISA_NIRQ)
76550769Sdfr		return EINVAL;
76650769Sdfr	if (type == SYS_RES_DRQ && rid >= ISA_NDRQ)
76750769Sdfr		return EINVAL;
76847398Sdfr
76947398Sdfr	resource_list_add(rl, type, rid, start, start + count - 1, count);
77047398Sdfr
77147398Sdfr	return 0;
77247398Sdfr}
77347398Sdfr
77447398Sdfrstatic int
77547398Sdfrisa_get_resource(device_t dev, device_t child, int type, int rid,
77647398Sdfr		 u_long *startp, u_long *countp)
77747398Sdfr{
77847398Sdfr	struct isa_device* idev = DEVTOISA(child);
77947398Sdfr	struct resource_list *rl = &idev->id_resources;
78047398Sdfr	struct resource_list_entry *rle;
78147398Sdfr
78247398Sdfr	rle = resource_list_find(rl, type, rid);
78347398Sdfr	if (!rle)
78447398Sdfr		return ENOENT;
78547398Sdfr
78647398Sdfr	*startp = rle->start;
78747398Sdfr	*countp = rle->count;
78847398Sdfr
78947398Sdfr	return 0;
79047398Sdfr}
79147398Sdfr
79247613Sdfrstatic void
79347613Sdfrisa_delete_resource(device_t dev, device_t child, int type, int rid)
79447613Sdfr{
79547613Sdfr	struct isa_device* idev = DEVTOISA(child);
79647613Sdfr	struct resource_list *rl = &idev->id_resources;
79747613Sdfr	resource_list_delete(rl, type, rid);
79847613Sdfr}
79947613Sdfr
80050769Sdfrstatic int
80150769Sdfrisa_add_config(device_t dev, device_t child,
80250769Sdfr	       int priority, struct isa_config *config)
80350769Sdfr{
80450769Sdfr	struct isa_device* idev = DEVTOISA(child);
80550769Sdfr	struct isa_config_entry *newice, *ice;
80650769Sdfr
80750769Sdfr	newice = malloc(sizeof *ice, M_DEVBUF, M_NOWAIT);
80850769Sdfr	if (!newice)
80950769Sdfr		return ENOMEM;
81050769Sdfr
81150769Sdfr	newice->ice_priority = priority;
81250769Sdfr	newice->ice_config = *config;
81350769Sdfr
81450769Sdfr	TAILQ_FOREACH(ice, &idev->id_configs, ice_link) {
81550769Sdfr		if (ice->ice_priority > priority)
81650769Sdfr			break;
81750769Sdfr	}
81850769Sdfr	if (ice)
81950769Sdfr		TAILQ_INSERT_BEFORE(ice, newice, ice_link);
82050769Sdfr	else
82150769Sdfr		TAILQ_INSERT_TAIL(&idev->id_configs, newice, ice_link);
82250769Sdfr
82350769Sdfr	return 0;
82450769Sdfr}
82550769Sdfr
82650769Sdfrstatic void
82750769Sdfrisa_set_config_callback(device_t dev, device_t child,
82850769Sdfr			isa_config_cb *fn, void *arg)
82950769Sdfr{
83050769Sdfr	struct isa_device* idev = DEVTOISA(child);
83150769Sdfr
83250769Sdfr	idev->id_config_cb = fn;
83350769Sdfr	idev->id_config_arg = arg;
83450769Sdfr}
83550769Sdfr
83650769Sdfrstatic int
83750769Sdfrisa_pnp_probe(device_t dev, device_t child, struct isa_pnp_id *ids)
83850769Sdfr{
83950769Sdfr	struct isa_device* idev = DEVTOISA(child);
84050769Sdfr
84150769Sdfr	if (!idev->id_vendorid)
84250769Sdfr		return ENOENT;
84350769Sdfr
84450769Sdfr	while (ids->ip_id) {
84550769Sdfr		/*
84650769Sdfr		 * Really ought to support >1 compat id per device.
84750769Sdfr		 */
84850769Sdfr		if (idev->id_logicalid == ids->ip_id
84950769Sdfr		    || idev->id_compatid == ids->ip_id) {
85050910Sdfr			if (ids->ip_desc)
85150910Sdfr				device_set_desc(child, ids->ip_desc);
85250769Sdfr			return 0;
85350769Sdfr		}
85450769Sdfr		ids++;
85550769Sdfr	}
85650769Sdfr
85750769Sdfr	return ENXIO;
85850769Sdfr}
85950769Sdfr
86047398Sdfrstatic device_method_t isa_methods[] = {
86147398Sdfr	/* Device interface */
86247398Sdfr	DEVMETHOD(device_probe,		isa_probe),
86347398Sdfr	DEVMETHOD(device_attach,	isa_attach),
86447398Sdfr	DEVMETHOD(device_detach,	bus_generic_detach),
86547398Sdfr	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
86647398Sdfr	DEVMETHOD(device_suspend,	bus_generic_suspend),
86747398Sdfr	DEVMETHOD(device_resume,	bus_generic_resume),
86847398Sdfr
86947398Sdfr	/* Bus interface */
87047398Sdfr	DEVMETHOD(bus_add_child,	isa_add_child),
87147398Sdfr	DEVMETHOD(bus_print_child,	isa_print_child),
87247398Sdfr	DEVMETHOD(bus_read_ivar,	isa_read_ivar),
87347398Sdfr	DEVMETHOD(bus_write_ivar,	isa_write_ivar),
87450769Sdfr	DEVMETHOD(bus_child_detached,	isa_child_detached),
87547398Sdfr	DEVMETHOD(bus_alloc_resource,	isa_alloc_resource),
87647398Sdfr	DEVMETHOD(bus_release_resource,	isa_release_resource),
87747398Sdfr	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
87847398Sdfr	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
87947398Sdfr	DEVMETHOD(bus_setup_intr,	isa_setup_intr),
88047398Sdfr	DEVMETHOD(bus_teardown_intr,	isa_teardown_intr),
88147398Sdfr
88247398Sdfr	/* ISA interface */
88347398Sdfr	DEVMETHOD(isa_set_resource,	isa_set_resource),
88447398Sdfr	DEVMETHOD(isa_get_resource,	isa_get_resource),
88547613Sdfr	DEVMETHOD(isa_delete_resource,	isa_delete_resource),
88650769Sdfr	DEVMETHOD(isa_add_config,	isa_add_config),
88750769Sdfr	DEVMETHOD(isa_set_config_callback, isa_set_config_callback),
88850769Sdfr	DEVMETHOD(isa_pnp_probe,	isa_pnp_probe),
88947398Sdfr
89047398Sdfr	{ 0, 0 }
89147398Sdfr};
89247398Sdfr
89347398Sdfrstatic driver_t isa_driver = {
89447398Sdfr	"isa",
89547398Sdfr	isa_methods,
89647398Sdfr	1,			/* no softc */
89747398Sdfr};
89847398Sdfr
89947398Sdfr/*
90047398Sdfr * ISA can be attached to a PCI-ISA bridge or directly to the nexus.
90147398Sdfr */
90247398SdfrDRIVER_MODULE(isa, isab, isa_driver, isa_devclass, 0, 0);
90347398Sdfr#ifdef __i386__
90447398SdfrDRIVER_MODULE(isa, nexus, isa_driver, isa_devclass, 0, 0);
90547398Sdfr#endif
90650769Sdfr
90750769Sdfr/*
90850769Sdfr * A fallback driver for reporting un-matched pnp devices.
90950769Sdfr */
91050769Sdfr
91150769Sdfrstatic int
91250769Sdfrunknown_probe(device_t dev)
91350769Sdfr{
91450769Sdfr	/*
91550769Sdfr	 * Only match pnp devices.
91650769Sdfr	 */
91750769Sdfr	if (isa_get_vendorid(dev) != 0)
91850769Sdfr		return -100;
91950769Sdfr	return ENXIO;
92050769Sdfr}
92150769Sdfr
92250769Sdfrstatic int
92350769Sdfrunknown_attach(device_t dev)
92450769Sdfr{
92550769Sdfr	return 0;
92650769Sdfr}
92750769Sdfr
92850769Sdfrstatic int
92950769Sdfrunknown_detach(device_t dev)
93050769Sdfr{
93150769Sdfr	return 0;
93250769Sdfr}
93350769Sdfr
93450769Sdfrstatic device_method_t unknown_methods[] = {
93550769Sdfr	/* Device interface */
93650769Sdfr	DEVMETHOD(device_probe,		unknown_probe),
93750769Sdfr	DEVMETHOD(device_attach,	unknown_attach),
93850769Sdfr	DEVMETHOD(device_detach,	unknown_detach),
93950769Sdfr
94050769Sdfr	{ 0, 0 }
94150769Sdfr};
94250769Sdfr
94350769Sdfrstatic driver_t unknown_driver = {
94450769Sdfr	"unknown",
94550769Sdfr	unknown_methods,
94650769Sdfr	1,			/* no softc */
94750769Sdfr};
94850769Sdfr
94950769Sdfrstatic devclass_t unknown_devclass;
95050769Sdfr
95150769SdfrDRIVER_MODULE(unknown, isa, unknown_driver, unknown_devclass, 0, 0);
952