ofw_isa.c revision 117119
186232Stmm/*
286232Stmm * Copyright (c) 1999, 2000 Matthew R. Green
3117119Stmm * Copyright (c) 2001, 2003 Thomas Moestl <tmm@FreeBSD.org>
486232Stmm * All rights reserved.
586232Stmm *
686232Stmm * Redistribution and use in source and binary forms, with or without
786232Stmm * modification, are permitted provided that the following conditions
886232Stmm * are met:
986232Stmm * 1. Redistributions of source code must retain the above copyright
1086232Stmm *    notice, this list of conditions and the following disclaimer.
1186232Stmm * 2. Redistributions in binary form must reproduce the above copyright
1286232Stmm *    notice, this list of conditions and the following disclaimer in the
1386232Stmm *    documentation and/or other materials provided with the distribution.
1486232Stmm * 3. The name of the author may not be used to endorse or promote products
1586232Stmm *    derived from this software without specific prior written permission.
1686232Stmm *
1786232Stmm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1886232Stmm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1986232Stmm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2086232Stmm * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2186232Stmm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2286232Stmm * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2386232Stmm * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2486232Stmm * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2586232Stmm * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2686232Stmm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2786232Stmm * SUCH DAMAGE.
2886232Stmm *
2986232Stmm *	from: NetBSD: ebus.c,v 1.26 2001/09/10 16:27:53 eeh Exp
3086232Stmm *
3186232Stmm * $FreeBSD: head/sys/sparc64/isa/ofw_isa.c 117119 2003-07-01 14:52:47Z tmm $
3286232Stmm */
3386232Stmm
3486232Stmm/*
3586232Stmm * Helper functions which can be used in both ISA and EBus code.
3686232Stmm */
3786232Stmm
38117119Stmm#include "opt_ofw_pci.h"
39117119Stmm
4086232Stmm#include <sys/param.h>
4191966Stmm#include <sys/systm.h>
4286232Stmm#include <sys/bus.h>
4386232Stmm
4486232Stmm#include <ofw/openfirm.h>
4586232Stmm#include <ofw/ofw_pci.h>
4686232Stmm
47117119Stmm#include <machine/bus.h>
4886232Stmm#include <machine/resource.h>
4986232Stmm#include <machine/ofw_bus.h>
5086232Stmm
51117119Stmm#include <sparc64/pci/ofw_pci.h>
5286232Stmm#include <sparc64/isa/ofw_isa.h>
5386232Stmm
54117119Stmm#include "pcib_if.h"
55117119Stmm
5686232Stmm/* XXX: this only supports PCI as parent bus right now. */
5786232Stmmint
5886232Stmmofw_isa_map_iorange(struct isa_ranges *range, int nrange, u_long *start,
5986232Stmm    u_long *end)
6086232Stmm{
6186232Stmm	u_int64_t offs, cstart, cend;
6286232Stmm	int i;
6386232Stmm
6486232Stmm	for (i = 0; i < nrange; i++) {
6586232Stmm		cstart = ((u_int64_t)range[i].child_hi << 32) |
6686232Stmm		    range[i].child_lo;
6786232Stmm		cend = cstart + range[i].size;
6886232Stmm		if (*start < cstart || *start > cend)
6986232Stmm			continue;
7086232Stmm		if (*end < cstart || *end > cend) {
7186232Stmm			panic("ofw_isa_map_iorange: iorange crosses pci "
7286232Stmm			    "ranges (%#lx not in %#lx - %#lx)", *end, cstart,
7386232Stmm			    cend);
7486232Stmm		}
7586232Stmm		offs = (((u_int64_t)range[i].phys_mid << 32) |
7686232Stmm		    range[i].phys_lo);
7786232Stmm		*start = *start + offs - cstart;
7886232Stmm		*end  = *end + offs - cstart;
7986232Stmm		/* Isolate address space and find the right tag */
8086232Stmm		switch (ISA_RANGE_PS(&range[i])) {
8186232Stmm		case PCI_CS_IO:
8286232Stmm			return (SYS_RES_IOPORT);
8386232Stmm		case PCI_CS_MEM32:
8486232Stmm			return (SYS_RES_MEMORY);
8586232Stmm		default:
8686232Stmm			panic("ofw_isa_map_iorange: illegal space %x",
8786232Stmm			    ISA_RANGE_PS(&range[i]));
8886232Stmm			break;
8986232Stmm		}
9086232Stmm	}
9186232Stmm	panic("ofw_isa_map_iorange: could not map range %#lx - %#lx",
9286232Stmm	    *start, *end);
9386232Stmm}
94117119Stmm
95117119Stmm#ifdef OFW_NEWPCI
96117119Stmmofw_pci_intr_t
97117119Stmmofw_isa_route_intr(device_t bridge, phandle_t node, struct ofw_bus_iinfo *ii,
98117119Stmm    ofw_isa_intr_t intr)
99117119Stmm{
100117119Stmm	struct isa_regs reg;
101117119Stmm	u_int8_t maskbuf[sizeof(reg) + sizeof(intr)];
102117119Stmm	device_t pbridge;
103117119Stmm	ofw_isa_intr_t mintr;
104117119Stmm
105117119Stmm	pbridge = device_get_parent(device_get_parent(bridge));
106117119Stmm	/*
107117119Stmm	 * If we get a match from using the map, the resulting INO is
108117119Stmm	 * fully specified, so we may not continue to map.
109117119Stmm	 */
110117119Stmm	if (!ofw_bus_lookup_imap(node, ii, &reg, sizeof(reg),
111117119Stmm	    &intr, sizeof(intr), &mintr, sizeof(mintr), maskbuf)) {
112117119Stmm		/* Try routing at the parent bridge. */
113117119Stmm		mintr = PCIB_ROUTE_INTERRUPT(pbridge, bridge, intr);
114117119Stmm	}
115117119Stmm	return (mintr);
116117119Stmm}
117117119Stmm#endif /* OFW_NEWPCI */
118