isa.c revision 127139
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 127139 2004-03-17 19:04:58Z jmg $");
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#include <sys/param.h>
61#include <sys/bus.h>
62#include <sys/malloc.h>
63#include <machine/bus.h>
64#include <sys/rman.h>
65#ifdef PC98
66#include <sys/systm.h>
67#endif
68
69#include <machine/resource.h>
70
71#include <isa/isavar.h>
72#include <isa/isa_common.h>
73
74void
75isa_init(device_t dev)
76{
77}
78
79/*
80 * This implementation simply passes the request up to the parent
81 * bus, which in our case is the special i386 nexus, substituting any
82 * configured values if the caller defaulted.  We can get away with
83 * this because there is no special mapping for ISA resources on an Intel
84 * platform.  When porting this code to another architecture, it may be
85 * necessary to interpose a mapping layer here.
86 */
87struct resource *
88isa_alloc_resource(device_t bus, device_t child, int type, int *rid,
89		   u_long start, u_long end, u_long count, u_int flags)
90{
91	/*
92	 * Consider adding a resource definition.
93	 */
94	int passthrough = (device_get_parent(child) != bus);
95	int isdefault = (start == 0UL && end == ~0UL);
96	struct isa_device* idev = DEVTOISA(child);
97	struct resource_list *rl = &idev->id_resources;
98	struct resource_list_entry *rle;
99
100	if (!passthrough && !isdefault) {
101		rle = resource_list_find(rl, type, *rid);
102		if (!rle) {
103			if (*rid < 0)
104				return 0;
105			switch (type) {
106			case SYS_RES_IRQ:
107				if (*rid >= ISA_NIRQ)
108					return 0;
109				break;
110			case SYS_RES_DRQ:
111				if (*rid >= ISA_NDRQ)
112					return 0;
113				break;
114			case SYS_RES_MEMORY:
115				if (*rid >= ISA_NMEM)
116					return 0;
117				break;
118			case SYS_RES_IOPORT:
119				if (*rid >= ISA_NPORT)
120					return 0;
121				break;
122			default:
123				return 0;
124			}
125			resource_list_add(rl, type, *rid, start, end, count);
126		}
127	}
128
129	return resource_list_alloc(rl, bus, child, type, rid,
130				   start, end, count, flags);
131}
132
133#ifdef PC98
134/*
135 * Indirection support.  The type of bus_space_handle_t is
136 * defined in sys/i386/include/bus_pc98.h.
137 */
138struct resource *
139isa_alloc_resourcev(device_t child, int type, int *rid,
140		    bus_addr_t *res, bus_size_t count, u_int flags)
141{
142	struct isa_device* idev = DEVTOISA(child);
143	struct resource_list *rl = &idev->id_resources;
144
145	device_t	bus = device_get_parent(child);
146	bus_addr_t	start;
147	struct resource *re;
148	struct resource	**bsre;
149	int		i, j, k, linear_cnt, ressz, bsrid;
150
151	start = bus_get_resource_start(child, type, *rid);
152
153	linear_cnt = count;
154	ressz = 1;
155	for (i = 1; i < count; ++i) {
156		if (res[i] != res[i - 1] + 1) {
157			if (i < linear_cnt)
158				linear_cnt = i;
159			++ressz;
160		}
161	}
162
163	re = isa_alloc_resource(bus, child, type, rid,
164				start + res[0], start + res[linear_cnt - 1],
165				linear_cnt, flags);
166	if (re == NULL)
167		return NULL;
168
169	bsre = malloc(sizeof (struct resource *) * ressz, M_DEVBUF, M_NOWAIT);
170	if (bsre == NULL) {
171		resource_list_release(rl, bus, child, type, *rid, re);
172		return NULL;
173	}
174	bsre[0] = re;
175
176	for (i = linear_cnt, k = 1; i < count; i = j, k++) {
177		for (j = i + 1; j < count; j++) {
178			if (res[j] != res[j - 1] + 1)
179				break;
180		}
181		bsrid = *rid + k;
182		bsre[k] = isa_alloc_resource(bus, child, type, &bsrid,
183			start + res[i], start + res[j - 1], j - i, flags);
184		if (bsre[k] == NULL) {
185			for (k--; k >= 0; k--)
186				resource_list_release(rl, bus, child, type,
187						      *rid + k, bsre[k]);
188			free(bsre, M_DEVBUF);
189			return NULL;
190		}
191	}
192
193	re->r_bushandle->bsh_res = bsre;
194	re->r_bushandle->bsh_ressz = ressz;
195
196	return re;
197}
198
199int
200isa_load_resourcev(struct resource *re, bus_addr_t *res, bus_size_t count)
201{
202	bus_addr_t	start;
203	int		i;
204
205	if (count > re->r_bushandle->bsh_maxiatsz) {
206		printf("isa_load_resourcev: map size too large\n");
207		return EINVAL;
208	}
209
210	start = rman_get_start(re);
211	for (i = 0; i < re->r_bushandle->bsh_maxiatsz; i++) {
212		if (i < count)
213			re->r_bushandle->bsh_iat[i] = start + res[i];
214		else
215			re->r_bushandle->bsh_iat[i] = start;
216	}
217
218	re->r_bushandle->bsh_iatsz = count;
219	re->r_bushandle->bsh_bam = re->r_bustag->bs_ra;	/* relocate access */
220
221	return 0;
222}
223#endif	/* PC98 */
224
225int
226isa_release_resource(device_t bus, device_t child, int type, int rid,
227		     struct resource *r)
228{
229	struct isa_device* idev = DEVTOISA(child);
230	struct resource_list *rl = &idev->id_resources;
231#ifdef PC98
232	/*
233	 * Indirection support.  The type of bus_space_handle_t is
234	 * defined in sys/i386/include/bus_pc98.h.
235	 */
236	int	i;
237
238	if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
239		for (i = 1; i < r->r_bushandle->bsh_ressz; i++)
240			resource_list_release(rl, bus, child, type, rid + i,
241					      r->r_bushandle->bsh_res[i]);
242		if (r->r_bushandle->bsh_res != NULL)
243			free(r->r_bushandle->bsh_res, M_DEVBUF);
244	}
245#endif
246	return resource_list_release(rl, bus, child, type, rid, r);
247}
248
249/*
250 * We can't use the bus_generic_* versions of these methods because those
251 * methods always pass the bus param as the requesting device, and we need
252 * to pass the child (the i386 nexus knows about this and is prepared to
253 * deal).
254 */
255int
256isa_setup_intr(device_t bus, device_t child, struct resource *r, int flags,
257	       void (*ihand)(void *), void *arg, void **cookiep)
258{
259	return (BUS_SETUP_INTR(device_get_parent(bus), child, r, flags,
260			       ihand, arg, cookiep));
261}
262
263int
264isa_teardown_intr(device_t bus, device_t child, struct resource *r,
265		  void *cookie)
266{
267	return (BUS_TEARDOWN_INTR(device_get_parent(bus), child, r, cookie));
268}
269