isa.c revision 131534
1/*-
2 * Copyright (c) 1998 Doug Rabson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/i386/isa/isa.c 131534 2004-07-03 20:11:49Z imp $");
29
30/*
31 * Modifications for Intel architecture by Garrett A. Wollman.
32 * Copyright 1998 Massachusetts Institute of Technology
33 *
34 * Permission to use, copy, modify, and distribute this software and
35 * its documentation for any purpose and without fee is hereby
36 * granted, provided that both the above copyright notice and this
37 * permission notice appear in all copies, that both the above
38 * copyright notice and this permission notice appear in all
39 * supporting documentation, and that the name of M.I.T. not be used
40 * in advertising or publicity pertaining to distribution of the
41 * software without specific, written prior permission.  M.I.T. makes
42 * no representations about the suitability of this software for any
43 * purpose.  It is provided "as is" without express or implied
44 * warranty.
45 *
46 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
47 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
48 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
49 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
50 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
51 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
52 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
53 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
54 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
55 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
56 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
58 */
59
60#ifdef PC98
61#define __RMAN_RESOURCE_VISIBLE
62#endif
63#include <sys/param.h>
64#include <sys/bus.h>
65#include <sys/malloc.h>
66#include <machine/bus.h>
67#include <sys/rman.h>
68#ifdef PC98
69#include <sys/systm.h>
70#endif
71
72#include <machine/resource.h>
73
74#include <isa/isavar.h>
75#include <isa/isa_common.h>
76
77void
78isa_init(device_t dev)
79{
80}
81
82/*
83 * This implementation simply passes the request up to the parent
84 * bus, which in our case is the special i386 nexus, substituting any
85 * configured values if the caller defaulted.  We can get away with
86 * this because there is no special mapping for ISA resources on an Intel
87 * platform.  When porting this code to another architecture, it may be
88 * necessary to interpose a mapping layer here.
89 */
90struct resource *
91isa_alloc_resource(device_t bus, device_t child, int type, int *rid,
92		   u_long start, u_long end, u_long count, u_int flags)
93{
94	/*
95	 * Consider adding a resource definition.
96	 */
97	int passthrough = (device_get_parent(child) != bus);
98	int isdefault = (start == 0UL && end == ~0UL);
99	struct isa_device* idev = DEVTOISA(child);
100	struct resource_list *rl = &idev->id_resources;
101	struct resource_list_entry *rle;
102
103	if (!passthrough && !isdefault) {
104		rle = resource_list_find(rl, type, *rid);
105		if (!rle) {
106			if (*rid < 0)
107				return 0;
108			switch (type) {
109			case SYS_RES_IRQ:
110				if (*rid >= ISA_NIRQ)
111					return 0;
112				break;
113			case SYS_RES_DRQ:
114				if (*rid >= ISA_NDRQ)
115					return 0;
116				break;
117			case SYS_RES_MEMORY:
118				if (*rid >= ISA_NMEM)
119					return 0;
120				break;
121			case SYS_RES_IOPORT:
122				if (*rid >= ISA_NPORT)
123					return 0;
124				break;
125			default:
126				return 0;
127			}
128			resource_list_add(rl, type, *rid, start, end, count);
129		}
130	}
131
132	return resource_list_alloc(rl, bus, child, type, rid,
133				   start, end, count, flags);
134}
135
136#ifdef PC98
137/*
138 * Indirection support.  The type of bus_space_handle_t is
139 * defined in sys/i386/include/bus_pc98.h.
140 */
141struct resource *
142isa_alloc_resourcev(device_t child, int type, int *rid,
143		    bus_addr_t *res, bus_size_t count, u_int flags)
144{
145	struct isa_device* idev = DEVTOISA(child);
146	struct resource_list *rl = &idev->id_resources;
147
148	device_t	bus = device_get_parent(child);
149	bus_addr_t	start;
150	bus_space_handle_t bh;
151	struct resource *re;
152	struct resource	**bsre;
153	int		i, j, k, linear_cnt, ressz, bsrid;
154
155	start = bus_get_resource_start(child, type, *rid);
156
157	linear_cnt = count;
158	ressz = 1;
159	for (i = 1; i < count; ++i) {
160		if (res[i] != res[i - 1] + 1) {
161			if (i < linear_cnt)
162				linear_cnt = i;
163			++ressz;
164		}
165	}
166
167	re = isa_alloc_resource(bus, child, type, rid,
168				start + res[0], start + res[linear_cnt - 1],
169				linear_cnt, flags);
170	if (re == NULL)
171		return NULL;
172
173	bsre = malloc(sizeof (struct resource *) * ressz, M_DEVBUF, M_NOWAIT);
174	if (bsre == NULL) {
175		resource_list_release(rl, bus, child, type, *rid, re);
176		return NULL;
177	}
178	bsre[0] = re;
179
180	for (i = linear_cnt, k = 1; i < count; i = j, k++) {
181		for (j = i + 1; j < count; j++) {
182			if (res[j] != res[j - 1] + 1)
183				break;
184		}
185		bsrid = *rid + k;
186		bsre[k] = isa_alloc_resource(bus, child, type, &bsrid,
187			start + res[i], start + res[j - 1], j - i, flags);
188		if (bsre[k] == NULL) {
189			for (k--; k >= 0; k--)
190				resource_list_release(rl, bus, child, type,
191						      *rid + k, bsre[k]);
192			free(bsre, M_DEVBUF);
193			return NULL;
194		}
195	}
196
197	bh = rman_get_bushandle(re);
198	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