ofw_isa.c revision 91966
1/*
2 * Copyright (c) 1999, 2000 Matthew R. Green
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 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 *	from: NetBSD: ebus.c,v 1.26 2001/09/10 16:27:53 eeh Exp
30 *
31 * $FreeBSD: head/sys/sparc64/isa/ofw_isa.c 91966 2002-03-09 22:00:30Z tmm $
32 */
33
34/*
35 * Helper functions which can be used in both ISA and EBus code.
36 */
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/bus.h>
41
42#include <ofw/openfirm.h>
43#include <ofw/ofw_pci.h>
44
45#include <machine/resource.h>
46#include <machine/ofw_bus.h>
47
48#include <sparc64/isa/ofw_isa.h>
49#include <sparc64/pci/ofw_pci.h>
50
51/*
52 * This applies only for an ISA/EBus with an own interrupt-map property.
53 */
54int
55ofw_isa_map_intr(struct isa_imap *imap, int nimap, struct isa_imap_msk *imapmsk,
56    int intr, struct isa_regs *regs, int nregs)
57{
58	char regm[8];
59
60	return (ofw_bus_route_intr(intr, regs, sizeof(*regs), 8, nregs,
61	    imap, nimap, imapmsk, regm));
62}
63
64/* XXX: this only supports PCI as parent bus right now. */
65int
66ofw_isa_map_iorange(struct isa_ranges *range, int nrange, u_long *start,
67    u_long *end)
68{
69	u_int64_t offs, cstart, cend;
70	int i;
71
72	for (i = 0; i < nrange; i++) {
73		cstart = ((u_int64_t)range[i].child_hi << 32) |
74		    range[i].child_lo;
75		cend = cstart + range[i].size;
76		if (*start < cstart || *start > cend)
77			continue;
78		if (*end < cstart || *end > cend) {
79			panic("ofw_isa_map_iorange: iorange crosses pci "
80			    "ranges (%#lx not in %#lx - %#lx)", *end, cstart,
81			    cend);
82		}
83		offs = (((u_int64_t)range[i].phys_mid << 32) |
84		    range[i].phys_lo);
85		*start = *start + offs - cstart;
86		*end  = *end + offs - cstart;
87		/* Isolate address space and find the right tag */
88		switch (ISA_RANGE_PS(&range[i])) {
89		case PCI_CS_IO:
90			return (SYS_RES_IOPORT);
91		case PCI_CS_MEM32:
92			return (SYS_RES_MEMORY);
93		default:
94			panic("ofw_isa_map_iorange: illegal space %x",
95			    ISA_RANGE_PS(&range[i]));
96			break;
97		}
98	}
99	panic("ofw_isa_map_iorange: could not map range %#lx - %#lx",
100	    *start, *end);
101}
102