isa.c revision 86232
1/*-
2 * Copyright (c) 1998 Doug Rabson
3 * Copyright (c) 2001 Thomas Moestl <tmm@FreeBSD.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 *	from: FreeBSD: src/sys/alpha/isa/isa.c,v 1.26 2001/07/11
28 *
29 * $FreeBSD: head/sys/sparc64/isa/isa.c 86232 2001-11-09 20:21:21Z tmm $
30 */
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/kernel.h>
35#include <sys/module.h>
36#include <sys/bus.h>
37#include <machine/bus.h>
38#include <sys/malloc.h>
39#include <sys/proc.h>
40#include <sys/rman.h>
41#include <sys/interrupt.h>
42
43#include <isa/isareg.h>
44#include <isa/isavar.h>
45#include <isa/isa_common.h>
46
47#include <pci/pcireg.h>
48#include <pci/pcivar.h>
49
50#include <ofw/ofw_pci.h>
51#include <ofw/openfirm.h>
52
53#include <machine/intr_machdep.h>
54#include <machine/resource.h>
55
56#include <sparc64/pci/ofw_pci.h>
57#include <sparc64/isa/ofw_isa.h>
58
59#include "sparcbus_if.h"
60
61/* There can be only one ISA bus, so it is safe to use globals. */
62bus_space_tag_t isa_io_bt = NULL;
63bus_space_handle_t isa_io_hdl;
64bus_space_tag_t isa_mem_bt = NULL;
65bus_space_handle_t isa_mem_hdl;
66
67u_int64_t isa_io_base;
68u_int64_t isa_io_limit;
69u_int64_t isa_mem_base;
70u_int64_t isa_mem_limit;
71
72device_t isa_bus_device;
73
74static struct ofw_pci_register isab_reg;
75static struct ofw_pci_imap *isab_imap;
76static int isab_nimap;
77static struct ofw_pci_imap_msk isab_imap_msk;
78static phandle_t isab_node;
79
80/*
81 * XXX: This is really partly partly PCI-specific, but unfortunately is
82 * differently enough to have to duplicate it here...
83 */
84#define	ISAB_RANGE_PHYS(r)						\
85	(((u_int64_t)(r)->phys_mid << 32) | (u_int64_t)(r)->phys_lo)
86#define	ISAB_RANGE_SPACE(r)	(((r)->phys_hi >> 24) & 0x03)
87
88#define	ISAR_SPACE_IO		0x01
89#define	ISAR_SPACE_MEM		0x02
90
91#define INRANGE(x, start, end)	((x) >= (start) && (x) <= (end))
92
93static int isa_route_intr_res(device_t, u_long, u_long);
94
95static int isa_ino[8];
96
97intrmask_t
98isa_irq_pending(void)
99{
100	intrmask_t pending;
101	int i;
102
103	/* XXX: Is this correct? */
104	for (i = 7, pending = 0; i >= 0; i--) {
105		pending <<= 1;
106		if (isa_ino[i] != 255) {
107			pending |= (SPARCBUS_INTR_PENDING(isa_bus_device,
108			    isa_ino[i]) == 0) ? 0 : 1;
109		}
110	}
111	return (pending);
112}
113
114void
115isa_init(device_t dev)
116{
117	device_t bridge;
118	phandle_t node;
119	struct isa_ranges *br;
120	int nbr, i;
121
122	/* The parent of the bus must be a PCI-ISA bridge. */
123	bridge = device_get_parent(dev);
124	isab_node = ofw_pci_find_node(pci_get_bus(bridge), pci_get_slot(bridge),
125	    pci_get_function(bridge));
126	if (OF_getprop(isab_node, "reg", &isab_reg, sizeof(isab_reg)) < 0)
127		panic("isa_init: cannot get bridge reg property");
128	nbr = OF_getprop_alloc(isab_node, "ranges", sizeof(*br), (void **)&br);
129	if (nbr <= 0)
130		panic("isa_init: cannot get bridge range property");
131	node = isab_node;
132	isab_nimap = ofw_pci_find_imap(node, &isab_imap, &isab_imap_msk);
133	if (isab_nimap == -1)
134		panic("isa_init: could not find interrupt-map");
135	for (i = 0; i < 8; i++) {
136		isa_ino[i] = ofw_pci_route_intr2(i, &isab_reg, isab_imap,
137		    isab_nimap, &isab_imap_msk);
138	}
139
140
141	for (nbr -= 1; nbr >= 0; nbr--) {
142		switch(ISAB_RANGE_SPACE(br + nbr)) {
143		case ISAR_SPACE_IO:
144			/* This is probably always 0. */
145			isa_io_base = ISAB_RANGE_PHYS(&br[nbr]);
146			isa_io_limit = br[nbr].size;
147			isa_io_hdl = SPARCBUS_GET_BUS_HANDLE(bridge, SBBT_IO,
148			    isa_io_base, &isa_io_bt);
149			break;
150		case ISAR_SPACE_MEM:
151			/* This is probably always 0. */
152			isa_mem_base = ISAB_RANGE_PHYS(&br[nbr]);
153			isa_mem_limit = br[nbr].size;
154			isa_mem_hdl = SPARCBUS_GET_BUS_HANDLE(bridge, SBBT_MEM,
155			    isa_mem_base, &isa_mem_bt);
156			break;
157		}
158	}
159	free(br, M_OFWPROP);
160}
161
162static int
163isa_route_intr_res(device_t bus, u_long start, u_long end)
164{
165	int res;
166
167	if (start != end) {
168		panic("isa_route_intr_res: allocation of interrupt range not "
169		    "supported (0x%lx - 0x%lx)", start, end);
170	}
171	res = ofw_pci_route_intr2(start, &isab_reg, isab_imap, isab_nimap,
172	    &isab_imap_msk);
173	if (res == 255)
174		device_printf(bus, "could not map interrupt %d\n", res);
175	return (res);
176}
177
178struct resource *
179isa_alloc_resource(device_t bus, device_t child, int type, int *rid,
180		   u_long start, u_long end, u_long count, u_int flags)
181{
182	/*
183	 * Consider adding a resource definition. We allow rid 0-1 for
184	 * irq and drq, 0-3 for memory and 0-7 for ports which is
185	 * sufficient for isapnp.
186	 */
187	int passthrough = (device_get_parent(child) != bus);
188	int isdefault = (start == 0UL && end == ~0UL);
189	struct isa_device* idev = DEVTOISA(child);
190	struct resource_list *rl = &idev->id_resources;
191	struct resource_list_entry *rle;
192	u_long base, limit;
193
194	if (!passthrough && !isdefault) {
195		rle = resource_list_find(rl, type, *rid);
196		if (!rle) {
197			if (*rid < 0)
198				return 0;
199			switch (type) {
200			case SYS_RES_IRQ:
201				if (*rid >= ISA_NIRQ)
202					return 0;
203				break;
204			case SYS_RES_DRQ:
205				if (*rid >= ISA_NDRQ)
206					return 0;
207				break;
208			case SYS_RES_MEMORY:
209				if (*rid >= ISA_NMEM)
210					return 0;
211				break;
212			case SYS_RES_IOPORT:
213				if (*rid >= ISA_NPORT)
214					return 0;
215				break;
216			default:
217				return 0;
218			}
219			resource_list_add(rl, type, *rid, start, end, count);
220		}
221	}
222
223	/*
224	 * Add the base, change default allocations to be between base and
225	 * limit, and reject allocations if a resource type is not enabled.
226	 */
227	base = limit = 0;
228	switch(type) {
229	case SYS_RES_MEMORY:
230		if (isa_mem_bt == NULL)
231			return (NULL);
232		base = isa_mem_base;
233		limit = base + isa_mem_limit;
234		break;
235	case SYS_RES_IOPORT:
236		if (isa_io_bt == NULL)
237			return (NULL);
238		base = isa_io_base;
239		limit = base + isa_io_limit;
240		break;
241	case SYS_RES_IRQ:
242		if (isdefault && passthrough)
243			panic("isa_alloc_resource: cannot pass through default "
244			    "irq allocation");
245		if (!isdefault) {
246			start = end = isa_route_intr_res(bus, start, end);
247			if (start == 255)
248				return (NULL);
249		}
250		break;
251	default:
252		panic("isa_alloc_resource: unsupported resource type %d", type);
253	}
254	if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
255		start = ulmin(start + base, limit);
256		end = ulmin(end + base, limit);
257	}
258
259	/*
260	 * This inlines a modified resource_list_alloc(); this is needed
261	 * because the resources need to have offsets added to them, whcih
262	 * cannot be done beforehand without patching the resource list entires
263	 * (which is ugly).
264	 */
265	if (passthrough) {
266		return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
267		    type, rid, start, end, count, flags));
268	}
269
270	rle = resource_list_find(rl, type, *rid);
271	if (rle == NULL)
272		return (NULL);		/* no resource of that type/rid */
273
274	if (rle->res != NULL)
275		panic("isa_alloc_resource: resource entry is busy");
276
277	if (isdefault) {
278		start = rle->start;
279		count = ulmax(count, rle->count);
280		end = ulmax(rle->end, start + count - 1);
281		switch (type) {
282		case SYS_RES_MEMORY:
283		case SYS_RES_IOPORT:
284			start += base;
285			end += base;
286			if (!INRANGE(start, base, limit) ||
287			    !INRANGE(end, base, limit)) {
288				panic("isa_alloc_resource: resource list entry "
289				    "out of bus range (0x%lx - 0x%lx not in "
290				    "0x%lx - 0x%lx)", start, end, base, limit);
291			}
292			break;
293		case SYS_RES_IRQ:
294			start = end = isa_route_intr_res(bus, start, end);
295			if (start == 255)
296				return (NULL);
297			break;
298		}
299	}
300
301	rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
302	    type, rid, start, end, count, flags);
303
304	/*
305	 * Record the new range.
306	 */
307	if (rle->res != NULL) {
308		rle->start = rman_get_start(rle->res) - base;
309		rle->end = rman_get_end(rle->res) - base;
310		rle->count = count;
311	}
312
313	return (rle->res);
314}
315
316int
317isa_release_resource(device_t bus, device_t child, int type, int rid,
318		     struct resource *res)
319{
320	struct isa_device* idev = DEVTOISA(child);
321	struct resource_list *rl = &idev->id_resources;
322
323	return (resource_list_release(rl, bus, child, type, rid, res));
324}
325
326int
327isa_setup_intr(device_t dev, device_t child,
328	       struct resource *irq, int flags,
329	       driver_intr_t *intr, void *arg, void **cookiep)
330{
331
332	/*
333	 * Just pass through. This is going to be handled by either one of
334	 * the parent PCI buses or the nexus device.
335	 * The interrupt was routed at allocation time.
336	 */
337	return (BUS_SETUP_INTR(device_get_parent(dev), child, irq, flags, intr,
338	    arg, cookiep));
339}
340
341int
342isa_teardown_intr(device_t dev, device_t child,
343		  struct resource *irq, void *cookie)
344{
345
346	return (BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie));
347}
348