14Srgrimes/*-
245720Speter * Copyright (c) 1998 Doug Rabson
34Srgrimes * All rights reserved.
44Srgrimes *
54Srgrimes * Redistribution and use in source and binary forms, with or without
64Srgrimes * modification, are permitted provided that the following conditions
74Srgrimes * are met:
84Srgrimes * 1. Redistributions of source code must retain the above copyright
94Srgrimes *    notice, this list of conditions and the following disclaimer.
104Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
114Srgrimes *    notice, this list of conditions and the following disclaimer in the
124Srgrimes *    documentation and/or other materials provided with the distribution.
134Srgrimes *
1445720Speter * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
154Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
164Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1745720Speter * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
184Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
194Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
204Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
214Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
224Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
234Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
244Srgrimes * SUCH DAMAGE.
254Srgrimes */
264Srgrimes
27115703Sobrien#include <sys/cdefs.h>
28115703Sobrien__FBSDID("$FreeBSD$");
29115703Sobrien
30139790Simp/*-
3145720Speter * Modifications for Intel architecture by Garrett A. Wollman.
3245720Speter * Copyright 1998 Massachusetts Institute of Technology
334Srgrimes *
3445720Speter * Permission to use, copy, modify, and distribute this software and
3545720Speter * its documentation for any purpose and without fee is hereby
3645720Speter * granted, provided that both the above copyright notice and this
3745720Speter * permission notice appear in all copies, that both the above
3845720Speter * copyright notice and this permission notice appear in all
3945720Speter * supporting documentation, and that the name of M.I.T. not be used
4045720Speter * in advertising or publicity pertaining to distribution of the
4145720Speter * software without specific, written prior permission.  M.I.T. makes
4245720Speter * no representations about the suitability of this software for any
4345720Speter * purpose.  It is provided "as is" without express or implied
4445720Speter * warranty.
4545720Speter *
4645720Speter * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
4745720Speter * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
4845720Speter * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
4945720Speter * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
5045720Speter * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
5145720Speter * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
5245720Speter * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
5345720Speter * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
5445720Speter * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
5545720Speter * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
5645720Speter * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5745720Speter * SUCH DAMAGE.
584Srgrimes */
594Srgrimes
602056Swollman#include <sys/param.h>
6145720Speter#include <sys/bus.h>
62139279Simp#include <sys/kernel.h>
632056Swollman#include <sys/malloc.h>
64139279Simp#include <sys/module.h>
6545720Speter#include <machine/bus.h>
6658762Skato#include <sys/rman.h>
6790466Snyan#ifdef PC98
6890466Snyan#include <sys/systm.h>
6990466Snyan#endif
704Srgrimes
7145720Speter#include <machine/resource.h>
7226157Sse
7345720Speter#include <isa/isavar.h>
7447398Sdfr#include <isa/isa_common.h>
7529613Sjmg
7647398Sdfrvoid
7788376Stmmisa_init(device_t dev)
7831457Sjmg{
793258Sdg}
808876Srgrimes
8147178Sdfr/*
8245720Speter * This implementation simply passes the request up to the parent
8345720Speter * bus, which in our case is the special i386 nexus, substituting any
8445720Speter * configured values if the caller defaulted.  We can get away with
8545720Speter * this because there is no special mapping for ISA resources on an Intel
8645720Speter * platform.  When porting this code to another architecture, it may be
8745720Speter * necessary to interpose a mapping layer here.
8815147Ssmpatel */
8947398Sdfrstruct resource *
9045720Speterisa_alloc_resource(device_t bus, device_t child, int type, int *rid,
91294883Sjhibbits		   rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
9215147Ssmpatel{
9345720Speter	/*
94127139Sjmg	 * Consider adding a resource definition.
9545720Speter	 */
9647398Sdfr	int passthrough = (device_get_parent(child) != bus);
97295832Sjhibbits	int isdefault = RMAN_IS_DEFAULT_RANGE(start, end);
9852174Sdfr	struct isa_device* idev = DEVTOISA(child);
9952174Sdfr	struct resource_list *rl = &idev->id_resources;
10047398Sdfr	struct resource_list_entry *rle;
10147398Sdfr
10247398Sdfr	if (!passthrough && !isdefault) {
10347398Sdfr		rle = resource_list_find(rl, type, *rid);
10447398Sdfr		if (!rle) {
10547398Sdfr			if (*rid < 0)
10647398Sdfr				return 0;
10752174Sdfr			switch (type) {
10852174Sdfr			case SYS_RES_IRQ:
10952174Sdfr				if (*rid >= ISA_NIRQ)
11052174Sdfr					return 0;
11152174Sdfr				break;
11252174Sdfr			case SYS_RES_DRQ:
11352174Sdfr				if (*rid >= ISA_NDRQ)
11452174Sdfr					return 0;
11552174Sdfr				break;
11652174Sdfr			case SYS_RES_MEMORY:
11752174Sdfr				if (*rid >= ISA_NMEM)
11852174Sdfr					return 0;
11952174Sdfr				break;
12052174Sdfr			case SYS_RES_IOPORT:
12152174Sdfr				if (*rid >= ISA_NPORT)
12252174Sdfr					return 0;
12352174Sdfr				break;
12452174Sdfr			default:
12547398Sdfr				return 0;
12652174Sdfr			}
12747398Sdfr			resource_list_add(rl, type, *rid, start, end, count);
12845720Speter		}
12945720Speter	}
13047398Sdfr
13152174Sdfr	return resource_list_alloc(rl, bus, child, type, rid,
13247398Sdfr				   start, end, count, flags);
13345720Speter}
13415147Ssmpatel
13558762Skato#ifdef PC98
13658762Skato/*
13758762Skato * Indirection support.  The type of bus_space_handle_t is
13858762Skato * defined in sys/i386/include/bus_pc98.h.
13958762Skato */
14058762Skatostruct resource *
14158762Skatoisa_alloc_resourcev(device_t child, int type, int *rid,
14258762Skato		    bus_addr_t *res, bus_size_t count, u_int flags)
14358762Skato{
14458762Skato	struct isa_device* idev = DEVTOISA(child);
14558762Skato	struct resource_list *rl = &idev->id_resources;
14658762Skato
14758762Skato	device_t	bus = device_get_parent(child);
14878391Snyan	bus_addr_t	start;
149131534Simp	bus_space_handle_t bh;
15058762Skato	struct resource *re;
15158762Skato	struct resource	**bsre;
15258762Skato	int		i, j, k, linear_cnt, ressz, bsrid;
15358762Skato
15478391Snyan	start = bus_get_resource_start(child, type, *rid);
15558762Skato
15658762Skato	linear_cnt = count;
15758762Skato	ressz = 1;
15858762Skato	for (i = 1; i < count; ++i) {
15965570Snyan		if (res[i] != res[i - 1] + 1) {
16058762Skato			if (i < linear_cnt)
16158762Skato				linear_cnt = i;
16258762Skato			++ressz;
16358762Skato		}
16458762Skato	}
16558762Skato
16678391Snyan	re = isa_alloc_resource(bus, child, type, rid,
16778391Snyan				start + res[0], start + res[linear_cnt - 1],
16878391Snyan				linear_cnt, flags);
16958762Skato	if (re == NULL)
17058762Skato		return NULL;
17158762Skato
17258762Skato	bsre = malloc(sizeof (struct resource *) * ressz, M_DEVBUF, M_NOWAIT);
17358762Skato	if (bsre == NULL) {
17458762Skato		resource_list_release(rl, bus, child, type, *rid, re);
17558762Skato		return NULL;
17658762Skato	}
17758762Skato	bsre[0] = re;
17858762Skato
17958762Skato	for (i = linear_cnt, k = 1; i < count; i = j, k++) {
18058762Skato		for (j = i + 1; j < count; j++) {
18165570Snyan			if (res[j] != res[j - 1] + 1)
18258762Skato				break;
18358762Skato		}
18458762Skato		bsrid = *rid + k;
18558762Skato		bsre[k] = isa_alloc_resource(bus, child, type, &bsrid,
18678391Snyan			start + res[i], start + res[j - 1], j - i, flags);
18758762Skato		if (bsre[k] == NULL) {
18858762Skato			for (k--; k >= 0; k--)
18958762Skato				resource_list_release(rl, bus, child, type,
19058762Skato						      *rid + k, bsre[k]);
19158762Skato			free(bsre, M_DEVBUF);
19258762Skato			return NULL;
19358762Skato		}
19458762Skato	}
19558762Skato
196131534Simp	bh = rman_get_bushandle(re);
197131534Simp	bh->bsh_res = bsre;
198131534Simp	bh->bsh_ressz = ressz;
19958762Skato
20058762Skato	return re;
20158762Skato}
20258762Skato
20347398Sdfrint
20458762Skatoisa_load_resourcev(struct resource *re, bus_addr_t *res, bus_size_t count)
20558762Skato{
20658762Skato
207182836Snyan	return bus_space_map_load(rman_get_bustag(re), rman_get_bushandle(re),
208182836Snyan				  count, res, 0);
20958762Skato}
21058762Skato#endif	/* PC98 */
21158762Skato
21258762Skatoint
21345720Speterisa_release_resource(device_t bus, device_t child, int type, int rid,
21445720Speter		     struct resource *r)
21545720Speter{
21652174Sdfr	struct isa_device* idev = DEVTOISA(child);
21752174Sdfr	struct resource_list *rl = &idev->id_resources;
21858762Skato#ifdef PC98
21958762Skato	/*
22058762Skato	 * Indirection support.  The type of bus_space_handle_t is
22158762Skato	 * defined in sys/i386/include/bus_pc98.h.
22258762Skato	 */
22358762Skato	int	i;
224131534Simp	bus_space_handle_t bh;
22558762Skato
22690762Snyan	if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
227131534Simp		bh = rman_get_bushandle(r);
228172032Snyan		if (bh != NULL) {
229172032Snyan			for (i = 1; i < bh->bsh_ressz; i++)
230172032Snyan				resource_list_release(rl, bus, child, type,
231172032Snyan						      rid + i, bh->bsh_res[i]);
232172032Snyan			if (bh->bsh_res != NULL)
233172032Snyan				free(bh->bsh_res, M_DEVBUF);
234172032Snyan		}
23590762Snyan	}
23658762Skato#endif
23752174Sdfr	return resource_list_release(rl, bus, child, type, rid, r);
2384Srgrimes}
2394Srgrimes
2404Srgrimes/*
241139279Simp * On this platform, isa can also attach to the legacy bus.
242139279Simp */
243139279SimpDRIVER_MODULE(isa, legacy, isa_driver, isa_devclass, 0, 0);
244267535Sroyger
245267535Sroyger/*
246267535Sroyger * Attach the ISA bus to the xenpv bus in order to get syscons.
247267535Sroyger */
248267535SroygerDRIVER_MODULE(isa, xenpv, isa_driver, isa_devclass, 0, 0);
249