ofw_pcib_subr.c revision 129051
1/*-
2 * Copyright (c) 2003 by Thomas Moestl <tmm@FreeBSD.org>
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 ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
23 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 * $FreeBSD: head/sys/sparc64/pci/ofw_pcib_subr.c 129051 2004-05-08 13:53:47Z marius $
26 */
27
28#include "opt_ofw_pci.h"
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/bus.h>
33
34#include <dev/ofw/openfirm.h>
35#include <dev/ofw/ofw_pci.h>
36
37#include <machine/bus.h>
38#include <machine/ofw_bus.h>
39
40#include <dev/pci/pcireg.h>
41#include <dev/pci/pcivar.h>
42#include <dev/pci/pcib_private.h>
43
44#include "pcib_if.h"
45
46#include <sparc64/pci/ofw_pci.h>
47#include <sparc64/pci/ofw_pcib_subr.h>
48
49void
50ofw_pcib_gen_setup(device_t bridge)
51{
52	struct ofw_pcib_gen_softc *sc = device_get_softc(bridge);
53	u_int secbus;
54
55	sc->ops_pcib_sc.dev = bridge;
56	sc->ops_node = ofw_pci_get_node(bridge);
57	KASSERT(sc->ops_node != 0,
58	    ("ofw_pcib_gen_setup: no ofw pci parent bus!"));
59
60	/*
61	 * Setup the secondary bus number register, by allocating a new unique
62	 * bus number for it; the firmware preset does not always seem to be
63	 * correct.
64	 */
65	secbus = ofw_pci_alloc_busno(sc->ops_node);
66	pci_write_config(bridge, PCIR_PRIBUS_1, pci_get_bus(bridge), 1);
67	pci_write_config(bridge, PCIR_SECBUS_1, secbus, 1);
68	pci_write_config(bridge, PCIR_SUBBUS_1, secbus, 1);
69	sc->ops_pcib_sc.subbus = sc->ops_pcib_sc.secbus = secbus;
70	/* Notify parent bridges. */
71	OFW_PCI_ADJUST_BUSRANGE(device_get_parent(bridge), secbus);
72
73	ofw_bus_setup_iinfo(sc->ops_node, &sc->ops_iinfo,
74	    sizeof(ofw_pci_intr_t));
75}
76
77int
78ofw_pcib_gen_route_interrupt(device_t bridge, device_t dev, int intpin)
79{
80	struct ofw_pcib_gen_softc *sc = device_get_softc(bridge);
81	struct ofw_bus_iinfo *ii = &sc->ops_iinfo;
82	struct ofw_pci_register reg;
83	device_t pbridge = device_get_parent(device_get_parent(bridge));
84	phandle_t node = ofw_pci_get_node(dev);
85	ofw_pci_intr_t pintr, mintr;
86	u_int8_t maskbuf[sizeof(reg) + sizeof(pintr)];
87
88	if (ii->opi_imapsz > 0) {
89		pintr = intpin;
90		if (ofw_bus_lookup_imap(node, ii, &reg, sizeof(reg), &pintr,
91		    sizeof(pintr), &mintr, sizeof(mintr), maskbuf)) {
92			/*
93			 * If we've found a mapping, return it and don't map
94			 * it again on higher levels - that causes problems
95			 * in some cases, and never seems to be required.
96			 */
97			return (mintr);
98		}
99	} else if (intpin >= 1 && intpin <= 4) {
100		/*
101		 * When an interrupt map is missing, we need to do the
102		 * standard PCI swizzle and continue mapping at the parent.
103		 */
104		return (pcib_route_interrupt(bridge, dev, intpin));
105	}
106	/* Try at the parent. */
107	return (PCIB_ROUTE_INTERRUPT(pbridge, bridge, intpin));
108}
109
110phandle_t
111ofw_pcib_gen_get_node(device_t bridge, device_t dev)
112{
113	struct ofw_pcib_gen_softc *sc = device_get_softc(bridge);
114
115	return (sc->ops_node);
116}
117
118void
119ofw_pcib_gen_adjust_busrange(device_t bridge, u_int subbus)
120{
121	struct ofw_pcib_gen_softc *sc = device_get_softc(bridge);
122
123	if (subbus > sc->ops_pcib_sc.subbus) {
124#ifdef OFW_PCI_DEBUG
125		device_printf(bridge,
126		    "adjusting secondary bus number from %d to %d\n",
127		    sc->ops_pcib_sc.subbus, subbus);
128#endif
129		pci_write_config(bridge, PCIR_SUBBUS_1, subbus, 1);
130		sc->ops_pcib_sc.subbus = subbus;
131		/* Notify parent bridges. */
132		OFW_PCI_ADJUST_BUSRANGE(device_get_parent(bridge), subbus);
133	}
134}
135