1117119Stmm/*-
2117119Stmm * Copyright (c) 2003 by Thomas Moestl <tmm@FreeBSD.org>
3117119Stmm * All rights reserved.
4117119Stmm *
5117119Stmm * Redistribution and use in source and binary forms, with or without
6117119Stmm * modification, are permitted provided that the following conditions
7117119Stmm * are met:
8117119Stmm * 1. Redistributions of source code must retain the above copyright
9117119Stmm *    notice, this list of conditions and the following disclaimer.
10117119Stmm * 2. Redistributions in binary form must reproduce the above copyright
11117119Stmm *    notice, this list of conditions and the following disclaimer in the
12117119Stmm *    documentation and/or other materials provided with the distribution.
13117119Stmm *
14117119Stmm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15117119Stmm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16117119Stmm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17117119Stmm * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18117119Stmm * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19117119Stmm * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20117119Stmm * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21117119Stmm * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22117119Stmm * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
23117119Stmm * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24117119Stmm */
25117119Stmm
26153057Smarius#include <sys/cdefs.h>
27153057Smarius__FBSDID("$FreeBSD$");
28153057Smarius
29117119Stmm#include "opt_ofw_pci.h"
30129051Smarius
31117119Stmm#include <sys/param.h>
32117119Stmm#include <sys/systm.h>
33117119Stmm#include <sys/bus.h>
34225931Smarius#include <sys/rman.h>
35117119Stmm
36133589Smarius#include <dev/ofw/ofw_bus.h>
37133589Smarius#include <dev/ofw/ofw_pci.h>
38117119Stmm#include <dev/ofw/openfirm.h>
39117119Stmm
40117119Stmm#include <machine/bus.h>
41117119Stmm
42117119Stmm#include <dev/pci/pcireg.h>
43117119Stmm#include <dev/pci/pcivar.h>
44117119Stmm#include <dev/pci/pcib_private.h>
45117119Stmm
46117119Stmm#include "pcib_if.h"
47117119Stmm
48117119Stmm#include <sparc64/pci/ofw_pci.h>
49117119Stmm#include <sparc64/pci/ofw_pcib_subr.h>
50117119Stmm
51117119Stmmvoid
52117119Stmmofw_pcib_gen_setup(device_t bridge)
53117119Stmm{
54153057Smarius	struct ofw_pcib_gen_softc *sc;
55117119Stmm
56153057Smarius	sc = device_get_softc(bridge);
57117119Stmm	sc->ops_pcib_sc.dev = bridge;
58133589Smarius	sc->ops_node = ofw_bus_get_node(bridge);
59117119Stmm	KASSERT(sc->ops_node != 0,
60117119Stmm	    ("ofw_pcib_gen_setup: no ofw pci parent bus!"));
61117119Stmm
62117119Stmm	ofw_bus_setup_iinfo(sc->ops_node, &sc->ops_iinfo,
63117119Stmm	    sizeof(ofw_pci_intr_t));
64117119Stmm}
65117119Stmm
66117119Stmmint
67117119Stmmofw_pcib_gen_route_interrupt(device_t bridge, device_t dev, int intpin)
68117119Stmm{
69153057Smarius	struct ofw_pcib_gen_softc *sc;
70153057Smarius	struct ofw_bus_iinfo *ii;
71117119Stmm	struct ofw_pci_register reg;
72117119Stmm	ofw_pci_intr_t pintr, mintr;
73117119Stmm
74153057Smarius	sc = device_get_softc(bridge);
75153057Smarius	ii = &sc->ops_iinfo;
76117119Stmm	if (ii->opi_imapsz > 0) {
77117119Stmm		pintr = intpin;
78153057Smarius		if (ofw_bus_lookup_imap(ofw_bus_get_node(dev), ii, &reg,
79153057Smarius		    sizeof(reg), &pintr, sizeof(pintr), &mintr, sizeof(mintr),
80266020Sian		    NULL)) {
81117119Stmm			/*
82117119Stmm			 * If we've found a mapping, return it and don't map
83117119Stmm			 * it again on higher levels - that causes problems
84117119Stmm			 * in some cases, and never seems to be required.
85117119Stmm			 */
86117119Stmm			return (mintr);
87117119Stmm		}
88117119Stmm	} else if (intpin >= 1 && intpin <= 4) {
89117119Stmm		/*
90117119Stmm		 * When an interrupt map is missing, we need to do the
91117119Stmm		 * standard PCI swizzle and continue mapping at the parent.
92117119Stmm		 */
93117119Stmm		return (pcib_route_interrupt(bridge, dev, intpin));
94117119Stmm	}
95117119Stmm	/* Try at the parent. */
96153342Smarius	return (PCIB_ROUTE_INTERRUPT(device_get_parent(device_get_parent(
97153342Smarius	    bridge)), bridge, intpin));
98117119Stmm}
99117119Stmm
100117119Stmmphandle_t
101117119Stmmofw_pcib_gen_get_node(device_t bridge, device_t dev)
102117119Stmm{
103153057Smarius	struct ofw_pcib_gen_softc *sc;
104117119Stmm
105153057Smarius	sc = device_get_softc(bridge);
106117119Stmm	return (sc->ops_node);
107117119Stmm}
108