isa.c revision 131534
1306196Sjkim/*-
2238405Sjkim * Copyright (c) 1998 Doug Rabson
3238405Sjkim * All rights reserved.
4238405Sjkim *
5238405Sjkim * Redistribution and use in source and binary forms, with or without
6238405Sjkim * modification, are permitted provided that the following conditions
7238405Sjkim * are met:
8238405Sjkim * 1. Redistributions of source code must retain the above copyright
9238405Sjkim *    notice, this list of conditions and the following disclaimer.
10238405Sjkim * 2. Redistributions in binary form must reproduce the above copyright
11238405Sjkim *    notice, this list of conditions and the following disclaimer in the
12238405Sjkim *    documentation and/or other materials provided with the distribution.
13238405Sjkim *
14238405Sjkim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15238405Sjkim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16238405Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17238405Sjkim * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18238405Sjkim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19238405Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20238405Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21238405Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22238405Sjkim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23238405Sjkim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24238405Sjkim * SUCH DAMAGE.
25238405Sjkim */
26238405Sjkim
27238405Sjkim#include <sys/cdefs.h>
28238405Sjkim__FBSDID("$FreeBSD: head/sys/i386/isa/isa.c 131534 2004-07-03 20:11:49Z imp $");
29238405Sjkim
30238405Sjkim/*
31238405Sjkim * Modifications for Intel architecture by Garrett A. Wollman.
32238405Sjkim * Copyright 1998 Massachusetts Institute of Technology
33238405Sjkim *
34238405Sjkim * Permission to use, copy, modify, and distribute this software and
35238405Sjkim * its documentation for any purpose and without fee is hereby
36238405Sjkim * granted, provided that both the above copyright notice and this
37238405Sjkim * permission notice appear in all copies, that both the above
38238405Sjkim * copyright notice and this permission notice appear in all
39238405Sjkim * supporting documentation, and that the name of M.I.T. not be used
40238405Sjkim * in advertising or publicity pertaining to distribution of the
41276864Sjkim * software without specific, written prior permission.  M.I.T. makes
42276864Sjkim * no representations about the suitability of this software for any
43238405Sjkim * purpose.  It is provided "as is" without express or implied
44238405Sjkim * warranty.
45238405Sjkim *
46238405Sjkim * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
47238405Sjkim * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
48238405Sjkim * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
49238405Sjkim * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
50238405Sjkim * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
51238405Sjkim * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
52238405Sjkim * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
53276864Sjkim * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
54276864Sjkim * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
55276864Sjkim * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
56238405Sjkim * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57276864Sjkim * SUCH DAMAGE.
58276864Sjkim */
59276864Sjkim
60276864Sjkim#ifdef PC98
61276864Sjkim#define __RMAN_RESOURCE_VISIBLE
62276864Sjkim#endif
63238405Sjkim#include <sys/param.h>
64276864Sjkim#include <sys/bus.h>
65276864Sjkim#include <sys/malloc.h>
66276864Sjkim#include <machine/bus.h>
67276864Sjkim#include <sys/rman.h>
68276864Sjkim#ifdef PC98
69238405Sjkim#include <sys/systm.h>
70276864Sjkim#endif
71238405Sjkim
72238405Sjkim#include <machine/resource.h>
73238405Sjkim
74238405Sjkim#include <isa/isavar.h>
75238405Sjkim#include <isa/isa_common.h>
76238405Sjkim
77238405Sjkimvoid
78238405Sjkimisa_init(device_t dev)
79238405Sjkim{
80238405Sjkim}
81238405Sjkim
82238405Sjkim/*
83238405Sjkim * This implementation simply passes the request up to the parent
84238405Sjkim * bus, which in our case is the special i386 nexus, substituting any
85238405Sjkim * configured values if the caller defaulted.  We can get away with
86238405Sjkim * this because there is no special mapping for ISA resources on an Intel
87238405Sjkim * platform.  When porting this code to another architecture, it may be
88238405Sjkim * necessary to interpose a mapping layer here.
89238405Sjkim */
90238405Sjkimstruct resource *
91238405Sjkimisa_alloc_resource(device_t bus, device_t child, int type, int *rid,
92238405Sjkim		   u_long start, u_long end, u_long count, u_int flags)
93238405Sjkim{
94238405Sjkim	/*
95238405Sjkim	 * Consider adding a resource definition.
96238405Sjkim	 */
97238405Sjkim	int passthrough = (device_get_parent(child) != bus);
98238405Sjkim	int isdefault = (start == 0UL && end == ~0UL);
99238405Sjkim	struct isa_device* idev = DEVTOISA(child);
100238405Sjkim	struct resource_list *rl = &idev->id_resources;
101238405Sjkim	struct resource_list_entry *rle;
102238405Sjkim
103238405Sjkim	if (!passthrough && !isdefault) {
104238405Sjkim		rle = resource_list_find(rl, type, *rid);
105238405Sjkim		if (!rle) {
106238405Sjkim			if (*rid < 0)
107238405Sjkim				return 0;
108238405Sjkim			switch (type) {
109238405Sjkim			case SYS_RES_IRQ:
110238405Sjkim				if (*rid >= ISA_NIRQ)
111238405Sjkim					return 0;
112238405Sjkim				break;
113238405Sjkim			case SYS_RES_DRQ:
114238405Sjkim				if (*rid >= ISA_NDRQ)
115238405Sjkim					return 0;
116238405Sjkim				break;
117238405Sjkim			case SYS_RES_MEMORY:
118238405Sjkim				if (*rid >= ISA_NMEM)
119238405Sjkim					return 0;
120238405Sjkim				break;
121238405Sjkim			case SYS_RES_IOPORT:
122238405Sjkim				if (*rid >= ISA_NPORT)
123238405Sjkim					return 0;
124238405Sjkim				break;
125238405Sjkim			default:
126238405Sjkim				return 0;
127238405Sjkim			}
128238405Sjkim			resource_list_add(rl, type, *rid, start, end, count);
129238405Sjkim		}
130238405Sjkim	}
131238405Sjkim
132238405Sjkim	return resource_list_alloc(rl, bus, child, type, rid,
133238405Sjkim				   start, end, count, flags);
134238405Sjkim}
135238405Sjkim
136306196Sjkim#ifdef PC98
137238405Sjkim/*
138238405Sjkim * Indirection support.  The type of bus_space_handle_t is
139238405Sjkim * defined in sys/i386/include/bus_pc98.h.
140238405Sjkim */
141238405Sjkimstruct resource *
142238405Sjkimisa_alloc_resourcev(device_t child, int type, int *rid,
143238405Sjkim		    bus_addr_t *res, bus_size_t count, u_int flags)
144238405Sjkim{
145238405Sjkim	struct isa_device* idev = DEVTOISA(child);
146238405Sjkim	struct resource_list *rl = &idev->id_resources;
147238405Sjkim
148238405Sjkim	device_t	bus = device_get_parent(child);
149238405Sjkim	bus_addr_t	start;
150238405Sjkim	bus_space_handle_t bh;
151238405Sjkim	struct resource *re;
152238405Sjkim	struct resource	**bsre;
153238405Sjkim	int		i, j, k, linear_cnt, ressz, bsrid;
154238405Sjkim
155238405Sjkim	start = bus_get_resource_start(child, type, *rid);
156238405Sjkim
157238405Sjkim	linear_cnt = count;
158238405Sjkim	ressz = 1;
159238405Sjkim	for (i = 1; i < count; ++i) {
160238405Sjkim		if (res[i] != res[i - 1] + 1) {
161238405Sjkim			if (i < linear_cnt)
162238405Sjkim				linear_cnt = i;
163238405Sjkim			++ressz;
164238405Sjkim		}
165276864Sjkim	}
166238405Sjkim
167238405Sjkim	re = isa_alloc_resource(bus, child, type, rid,
168238405Sjkim				start + res[0], start + res[linear_cnt - 1],
169238405Sjkim				linear_cnt, flags);
170238405Sjkim	if (re == NULL)
171238405Sjkim		return NULL;
172238405Sjkim
173238405Sjkim	bsre = malloc(sizeof (struct resource *) * ressz, M_DEVBUF, M_NOWAIT);
174238405Sjkim	if (bsre == NULL) {
175238405Sjkim		resource_list_release(rl, bus, child, type, *rid, re);
176238405Sjkim		return NULL;
177238405Sjkim	}
178238405Sjkim	bsre[0] = re;
179238405Sjkim
180238405Sjkim	for (i = linear_cnt, k = 1; i < count; i = j, k++) {
181238405Sjkim		for (j = i + 1; j < count; j++) {
182238405Sjkim			if (res[j] != res[j - 1] + 1)
183238405Sjkim				break;
184238405Sjkim		}
185238405Sjkim		bsrid = *rid + k;
186238405Sjkim		bsre[k] = isa_alloc_resource(bus, child, type, &bsrid,
187238405Sjkim			start + res[i], start + res[j - 1], j - i, flags);
188238405Sjkim		if (bsre[k] == NULL) {
189238405Sjkim			for (k--; k >= 0; k--)
190238405Sjkim				resource_list_release(rl, bus, child, type,
191238405Sjkim						      *rid + k, bsre[k]);
192238405Sjkim			free(bsre, M_DEVBUF);
193238405Sjkim			return NULL;
194238405Sjkim		}
195238405Sjkim	}
196238405Sjkim
197238405Sjkim	bh = rman_get_bushandle(re);
198238405Sjkim	bh->bsh_res = bsre;
199	bh->bsh_ressz = ressz;
200
201	return re;
202}
203
204int
205isa_load_resourcev(struct resource *re, bus_addr_t *res, bus_size_t count)
206{
207	bus_addr_t	start;
208	bus_space_handle_t bh;
209	int		i;
210
211	bh = rman_get_bushandle(re);
212	if (count > bh->bsh_maxiatsz) {
213		printf("isa_load_resourcev: map size too large\n");
214		return EINVAL;
215	}
216
217	start = rman_get_start(re);
218	for (i = 0; i < bh->bsh_maxiatsz; i++) {
219		if (i < count)
220			bh->bsh_iat[i] = start + res[i];
221		else
222			bh->bsh_iat[i] = start;
223	}
224
225	bh->bsh_iatsz = count;
226	bh->bsh_bam = rman_get_bustag(re)->bs_ra;	/* relocate access */
227
228	return 0;
229}
230#endif	/* PC98 */
231
232int
233isa_release_resource(device_t bus, device_t child, int type, int rid,
234		     struct resource *r)
235{
236	struct isa_device* idev = DEVTOISA(child);
237	struct resource_list *rl = &idev->id_resources;
238#ifdef PC98
239	/*
240	 * Indirection support.  The type of bus_space_handle_t is
241	 * defined in sys/i386/include/bus_pc98.h.
242	 */
243	int	i;
244	bus_space_handle_t bh;
245
246	if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
247		bh = rman_get_bushandle(r);
248		for (i = 1; i < bh->bsh_ressz; i++)
249			resource_list_release(rl, bus, child, type, rid + i,
250					      bh->bsh_res[i]);
251		if (bh->bsh_res != NULL)
252			free(bh->bsh_res, M_DEVBUF);
253	}
254#endif
255	return resource_list_release(rl, bus, child, type, rid, r);
256}
257
258/*
259 * We can't use the bus_generic_* versions of these methods because those
260 * methods always pass the bus param as the requesting device, and we need
261 * to pass the child (the i386 nexus knows about this and is prepared to
262 * deal).
263 */
264int
265isa_setup_intr(device_t bus, device_t child, struct resource *r, int flags,
266	       void (*ihand)(void *), void *arg, void **cookiep)
267{
268	return (BUS_SETUP_INTR(device_get_parent(bus), child, r, flags,
269			       ihand, arg, cookiep));
270}
271
272int
273isa_teardown_intr(device_t bus, device_t child, struct resource *r,
274		  void *cookie)
275{
276	return (BUS_TEARDOWN_INTR(device_get_parent(bus), child, r, cookie));
277}
278