isa.c revision 58762
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 * $FreeBSD: head/sys/i386/isa/isa.c 58762 2000-03-29 03:15:22Z kato $
27 */
28
29/*
30 * Modifications for Intel architecture by Garrett A. Wollman.
31 * Copyright 1998 Massachusetts Institute of Technology
32 *
33 * Permission to use, copy, modify, and distribute this software and
34 * its documentation for any purpose and without fee is hereby
35 * granted, provided that both the above copyright notice and this
36 * permission notice appear in all copies, that both the above
37 * copyright notice and this permission notice appear in all
38 * supporting documentation, and that the name of M.I.T. not be used
39 * in advertising or publicity pertaining to distribution of the
40 * software without specific, written prior permission.  M.I.T. makes
41 * no representations about the suitability of this software for any
42 * purpose.  It is provided "as is" without express or implied
43 * warranty.
44 *
45 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
46 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
47 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
48 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
49 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
51 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
52 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
53 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
54 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
55 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE.
57 */
58
59#include "opt_compat_oldisa.h"
60
61#include <sys/param.h>
62#include <sys/bus.h>
63#include <sys/malloc.h>
64#include <machine/bus.h>
65#include <sys/rman.h>
66
67#include <machine/resource.h>
68
69#include <isa/isavar.h>
70#include <isa/isa_common.h>
71
72void
73isa_init(void)
74{
75#ifdef COMPAT_OLDISA
76	isa_wrap_old_drivers();
77#endif
78}
79
80/*
81 * This implementation simply passes the request up to the parent
82 * bus, which in our case is the special i386 nexus, substituting any
83 * configured values if the caller defaulted.  We can get away with
84 * this because there is no special mapping for ISA resources on an Intel
85 * platform.  When porting this code to another architecture, it may be
86 * necessary to interpose a mapping layer here.
87 */
88struct resource *
89isa_alloc_resource(device_t bus, device_t child, int type, int *rid,
90		   u_long start, u_long end, u_long count, u_int flags)
91{
92	/*
93	 * Consider adding a resource definition. We allow rid 0-1 for
94	 * irq and drq, 0-3 for memory and 0-7 for ports which is
95	 * sufficient for isapnp.
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	ioport;
150	struct resource *re;
151	struct resource	**bsre;
152	int		i, j, k, linear_cnt, ressz, bsrid;
153
154	ioport = bus_get_resource_start(child, SYS_RES_IOPORT, 0);
155
156	linear_cnt = count;
157	ressz = 1;
158	for (i = 1; i < count; ++i) {
159		if (res[i] > res[i - 1] + 1) {
160			if (i < linear_cnt)
161				linear_cnt = i;
162			++ressz;
163		}
164	}
165
166	re = isa_alloc_resource(bus, child, type, rid, ioport + res[0],
167			 ioport + res[linear_cnt - 1], linear_cnt, flags);
168	if (re == NULL)
169		return NULL;
170
171	bsre = malloc(sizeof (struct resource *) * ressz, M_DEVBUF, M_NOWAIT);
172	if (bsre == NULL) {
173		resource_list_release(rl, bus, child, type, *rid, re);
174		return NULL;
175	}
176	bsre[0] = re;
177
178	for (i = linear_cnt, k = 1; i < count; i = j, k++) {
179		for (j = i + 1; j < count; j++) {
180			if (res[j] > res[j - 1] + 1)
181				break;
182		}
183		bsrid = *rid + k;
184		bsre[k] = isa_alloc_resource(bus, child, type, &bsrid,
185			ioport + res[i], ioport + res[j - 1], j - i, flags);
186		if (bsre[k] == NULL) {
187			for (k--; k >= 0; k--)
188				resource_list_release(rl, bus, child, type,
189						      *rid + k, bsre[k]);
190			free(bsre, M_DEVBUF);
191			return NULL;
192		}
193	}
194
195	re->r_bushandle.bsh_res = bsre;
196	re->r_bushandle.bsh_ressz = ressz;
197
198	return re;
199}
200
201int
202isa_load_resourcev(struct resource *re, bus_addr_t *res, bus_size_t count)
203{
204	bus_addr_t	*addr;
205	int		i;
206
207	addr = malloc(sizeof (bus_addr_t) * count, M_DEVBUF, M_NOWAIT);
208	if (addr == NULL)
209		return 1;
210
211	for (i = 0; i < count; i++)
212		addr[i] = rman_get_start(re) + res[i];
213
214	rman_set_bustag(re, I386_BUS_SPACE_IO_IND);
215	re->r_bushandle.bsh_iat = addr;
216	re->r_bushandle.bsh_iatsz = count;
217
218	return 0;
219}
220#endif	/* PC98 */
221
222int
223isa_release_resource(device_t bus, device_t child, int type, int rid,
224		     struct resource *r)
225{
226	struct isa_device* idev = DEVTOISA(child);
227	struct resource_list *rl = &idev->id_resources;
228#ifdef PC98
229	/*
230	 * Indirection support.  The type of bus_space_handle_t is
231	 * defined in sys/i386/include/bus_pc98.h.
232	 */
233	int	i;
234
235	for (i = 1; i < r->r_bushandle.bsh_ressz; i++)
236		resource_list_release(rl, bus, child, type, rid + i,
237				      r->r_bushandle.bsh_res[i]);
238	if (r->r_bushandle.bsh_res != NULL)
239		free(r->r_bushandle.bsh_res, M_DEVBUF);
240	if (r->r_bushandle.bsh_iat != NULL)
241		free(r->r_bushandle.bsh_iat, M_DEVBUF);
242#endif
243	return resource_list_release(rl, bus, child, type, rid, r);
244}
245
246/*
247 * We can't use the bus_generic_* versions of these methods because those
248 * methods always pass the bus param as the requesting device, and we need
249 * to pass the child (the i386 nexus knows about this and is prepared to
250 * deal).
251 */
252int
253isa_setup_intr(device_t bus, device_t child, struct resource *r, int flags,
254	       void (*ihand)(void *), void *arg, void **cookiep)
255{
256	return (BUS_SETUP_INTR(device_get_parent(bus), child, r, flags,
257			       ihand, arg, cookiep));
258}
259
260int
261isa_teardown_intr(device_t bus, device_t child, struct resource *r,
262		  void *cookie)
263{
264	return (BUS_TEARDOWN_INTR(device_get_parent(bus), child, r, cookie));
265}
266