isa.c revision 139790
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: head/sys/i386/isa/isa.c 139790 2005-01-06 22:18:23Z imp $");
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
60131405Snyan#ifdef PC98
61131405Snyan#define __RMAN_RESOURCE_VISIBLE
62131405Snyan#endif
632056Swollman#include <sys/param.h>
6445720Speter#include <sys/bus.h>
65139279Simp#include <sys/kernel.h>
662056Swollman#include <sys/malloc.h>
67139279Simp#include <sys/module.h>
6845720Speter#include <machine/bus.h>
6958762Skato#include <sys/rman.h>
7090466Snyan#ifdef PC98
7190466Snyan#include <sys/systm.h>
7290466Snyan#endif
734Srgrimes
7445720Speter#include <machine/resource.h>
7526157Sse
7645720Speter#include <isa/isavar.h>
7747398Sdfr#include <isa/isa_common.h>
7829613Sjmg
7947398Sdfrvoid
8088376Stmmisa_init(device_t dev)
8131457Sjmg{
823258Sdg}
838876Srgrimes
8447178Sdfr/*
8545720Speter * This implementation simply passes the request up to the parent
8645720Speter * bus, which in our case is the special i386 nexus, substituting any
8745720Speter * configured values if the caller defaulted.  We can get away with
8845720Speter * this because there is no special mapping for ISA resources on an Intel
8945720Speter * platform.  When porting this code to another architecture, it may be
9045720Speter * necessary to interpose a mapping layer here.
9115147Ssmpatel */
9247398Sdfrstruct resource *
9345720Speterisa_alloc_resource(device_t bus, device_t child, int type, int *rid,
9445720Speter		   u_long start, u_long end, u_long count, u_int flags)
9515147Ssmpatel{
9645720Speter	/*
97127139Sjmg	 * Consider adding a resource definition.
9845720Speter	 */
9947398Sdfr	int passthrough = (device_get_parent(child) != bus);
10047398Sdfr	int isdefault = (start == 0UL && end == ~0UL);
10152174Sdfr	struct isa_device* idev = DEVTOISA(child);
10252174Sdfr	struct resource_list *rl = &idev->id_resources;
10347398Sdfr	struct resource_list_entry *rle;
10447398Sdfr
10547398Sdfr	if (!passthrough && !isdefault) {
10647398Sdfr		rle = resource_list_find(rl, type, *rid);
10747398Sdfr		if (!rle) {
10847398Sdfr			if (*rid < 0)
10947398Sdfr				return 0;
11052174Sdfr			switch (type) {
11152174Sdfr			case SYS_RES_IRQ:
11252174Sdfr				if (*rid >= ISA_NIRQ)
11352174Sdfr					return 0;
11452174Sdfr				break;
11552174Sdfr			case SYS_RES_DRQ:
11652174Sdfr				if (*rid >= ISA_NDRQ)
11752174Sdfr					return 0;
11852174Sdfr				break;
11952174Sdfr			case SYS_RES_MEMORY:
12052174Sdfr				if (*rid >= ISA_NMEM)
12152174Sdfr					return 0;
12252174Sdfr				break;
12352174Sdfr			case SYS_RES_IOPORT:
12452174Sdfr				if (*rid >= ISA_NPORT)
12552174Sdfr					return 0;
12652174Sdfr				break;
12752174Sdfr			default:
12847398Sdfr				return 0;
12952174Sdfr			}
13047398Sdfr			resource_list_add(rl, type, *rid, start, end, count);
13145720Speter		}
13245720Speter	}
13347398Sdfr
13452174Sdfr	return resource_list_alloc(rl, bus, child, type, rid,
13547398Sdfr				   start, end, count, flags);
13645720Speter}
13715147Ssmpatel
13858762Skato#ifdef PC98
13958762Skato/*
14058762Skato * Indirection support.  The type of bus_space_handle_t is
14158762Skato * defined in sys/i386/include/bus_pc98.h.
14258762Skato */
14358762Skatostruct resource *
14458762Skatoisa_alloc_resourcev(device_t child, int type, int *rid,
14558762Skato		    bus_addr_t *res, bus_size_t count, u_int flags)
14658762Skato{
14758762Skato	struct isa_device* idev = DEVTOISA(child);
14858762Skato	struct resource_list *rl = &idev->id_resources;
14958762Skato
15058762Skato	device_t	bus = device_get_parent(child);
15178391Snyan	bus_addr_t	start;
152131534Simp	bus_space_handle_t bh;
15358762Skato	struct resource *re;
15458762Skato	struct resource	**bsre;
15558762Skato	int		i, j, k, linear_cnt, ressz, bsrid;
15658762Skato
15778391Snyan	start = bus_get_resource_start(child, type, *rid);
15858762Skato
15958762Skato	linear_cnt = count;
16058762Skato	ressz = 1;
16158762Skato	for (i = 1; i < count; ++i) {
16265570Snyan		if (res[i] != res[i - 1] + 1) {
16358762Skato			if (i < linear_cnt)
16458762Skato				linear_cnt = i;
16558762Skato			++ressz;
16658762Skato		}
16758762Skato	}
16858762Skato
16978391Snyan	re = isa_alloc_resource(bus, child, type, rid,
17078391Snyan				start + res[0], start + res[linear_cnt - 1],
17178391Snyan				linear_cnt, flags);
17258762Skato	if (re == NULL)
17358762Skato		return NULL;
17458762Skato
17558762Skato	bsre = malloc(sizeof (struct resource *) * ressz, M_DEVBUF, M_NOWAIT);
17658762Skato	if (bsre == NULL) {
17758762Skato		resource_list_release(rl, bus, child, type, *rid, re);
17858762Skato		return NULL;
17958762Skato	}
18058762Skato	bsre[0] = re;
18158762Skato
18258762Skato	for (i = linear_cnt, k = 1; i < count; i = j, k++) {
18358762Skato		for (j = i + 1; j < count; j++) {
18465570Snyan			if (res[j] != res[j - 1] + 1)
18558762Skato				break;
18658762Skato		}
18758762Skato		bsrid = *rid + k;
18858762Skato		bsre[k] = isa_alloc_resource(bus, child, type, &bsrid,
18978391Snyan			start + res[i], start + res[j - 1], j - i, flags);
19058762Skato		if (bsre[k] == NULL) {
19158762Skato			for (k--; k >= 0; k--)
19258762Skato				resource_list_release(rl, bus, child, type,
19358762Skato						      *rid + k, bsre[k]);
19458762Skato			free(bsre, M_DEVBUF);
19558762Skato			return NULL;
19658762Skato		}
19758762Skato	}
19858762Skato
199131534Simp	bh = rman_get_bushandle(re);
200131534Simp	bh->bsh_res = bsre;
201131534Simp	bh->bsh_ressz = ressz;
20258762Skato
20358762Skato	return re;
20458762Skato}
20558762Skato
20647398Sdfrint
20758762Skatoisa_load_resourcev(struct resource *re, bus_addr_t *res, bus_size_t count)
20858762Skato{
20984615Snyan	bus_addr_t	start;
210131534Simp	bus_space_handle_t bh;
21158762Skato	int		i;
21258762Skato
213131534Simp	bh = rman_get_bushandle(re);
214131534Simp	if (count > bh->bsh_maxiatsz) {
21584615Snyan		printf("isa_load_resourcev: map size too large\n");
21684615Snyan		return EINVAL;
21784615Snyan	}
21858762Skato
21984615Snyan	start = rman_get_start(re);
220131534Simp	for (i = 0; i < bh->bsh_maxiatsz; i++) {
22184615Snyan		if (i < count)
222131534Simp			bh->bsh_iat[i] = start + res[i];
22384615Snyan		else
224131534Simp			bh->bsh_iat[i] = start;
22584615Snyan	}
22658762Skato
227131534Simp	bh->bsh_iatsz = count;
228131534Simp	bh->bsh_bam = rman_get_bustag(re)->bs_ra;	/* relocate access */
22958762Skato
23058762Skato	return 0;
23158762Skato}
23258762Skato#endif	/* PC98 */
23358762Skato
23458762Skatoint
23545720Speterisa_release_resource(device_t bus, device_t child, int type, int rid,
23645720Speter		     struct resource *r)
23745720Speter{
23852174Sdfr	struct isa_device* idev = DEVTOISA(child);
23952174Sdfr	struct resource_list *rl = &idev->id_resources;
24058762Skato#ifdef PC98
24158762Skato	/*
24258762Skato	 * Indirection support.  The type of bus_space_handle_t is
24358762Skato	 * defined in sys/i386/include/bus_pc98.h.
24458762Skato	 */
24558762Skato	int	i;
246131534Simp	bus_space_handle_t bh;
24758762Skato
24890762Snyan	if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
249131534Simp		bh = rman_get_bushandle(r);
250131534Simp		for (i = 1; i < bh->bsh_ressz; i++)
25190762Snyan			resource_list_release(rl, bus, child, type, rid + i,
252131534Simp					      bh->bsh_res[i]);
253131534Simp		if (bh->bsh_res != NULL)
254131534Simp			free(bh->bsh_res, M_DEVBUF);
25590762Snyan	}
25658762Skato#endif
25752174Sdfr	return resource_list_release(rl, bus, child, type, rid, r);
2584Srgrimes}
2594Srgrimes
2604Srgrimes/*
26145720Speter * We can't use the bus_generic_* versions of these methods because those
26245720Speter * methods always pass the bus param as the requesting device, and we need
26345720Speter * to pass the child (the i386 nexus knows about this and is prepared to
26445720Speter * deal).
2654Srgrimes */
26647398Sdfrint
26746743Sdfrisa_setup_intr(device_t bus, device_t child, struct resource *r, int flags,
26845720Speter	       void (*ihand)(void *), void *arg, void **cookiep)
26931457Sjmg{
27046743Sdfr	return (BUS_SETUP_INTR(device_get_parent(bus), child, r, flags,
27146743Sdfr			       ihand, arg, cookiep));
2724Srgrimes}
2734Srgrimes
27447398Sdfrint
27545720Speterisa_teardown_intr(device_t bus, device_t child, struct resource *r,
27645720Speter		  void *cookie)
27727639Smsmith{
27845720Speter	return (BUS_TEARDOWN_INTR(device_get_parent(bus), child, r, cookie));
27927639Smsmith}
280139279Simp
281139279Simp/*
282139279Simp * On this platform, isa can also attach to the legacy bus.
283139279Simp */
284139279SimpDRIVER_MODULE(isa, legacy, isa_driver, isa_devclass, 0, 0);
285