2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2000 BSDi
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/module.h>
34 #include <sys/malloc.h>
35 #include <sys/kernel.h>
37 #include <dev/ofw/openfirm.h>
38 #include <dev/ofw/ofw_pci.h>
39 #include <dev/ofw/ofw_bus.h>
40 #include <dev/ofw/ofw_bus_subr.h>
42 #include <dev/pci/pcivar.h>
43 #include <dev/pci/pcireg.h>
44 #include <dev/pci/pcib_private.h>
46 #include <machine/intr_machdep.h>
50 static int ofw_pcib_pci_probe(device_t bus);
51 static int ofw_pcib_pci_attach(device_t bus);
52 static phandle_t ofw_pcib_pci_get_node(device_t bus, device_t dev);
53 static int ofw_pcib_pci_route_interrupt(device_t bridge, device_t dev,
56 static device_method_t ofw_pcib_pci_methods[] = {
57 /* Device interface */
58 DEVMETHOD(device_probe, ofw_pcib_pci_probe),
59 DEVMETHOD(device_attach, ofw_pcib_pci_attach),
62 DEVMETHOD(pcib_route_interrupt, ofw_pcib_pci_route_interrupt),
64 /* ofw_bus interface */
65 DEVMETHOD(ofw_bus_get_node, ofw_pcib_pci_get_node),
70 static devclass_t pcib_devclass;
72 struct ofw_pcib_softc {
74 * This is here so that we can use pci bridge methods, too - the
75 * generic routines only need the dev, secbus and subbus members
78 struct pcib_softc ops_pcib_sc;
80 struct ofw_bus_iinfo ops_iinfo;
83 DEFINE_CLASS_1(pcib, ofw_pcib_pci_driver, ofw_pcib_pci_methods,
84 sizeof(struct ofw_pcib_softc), pcib_driver);
85 DRIVER_MODULE(ofw_pcib, pci, ofw_pcib_pci_driver, pcib_devclass, 0, 0);
88 ofw_pcib_pci_probe(device_t dev)
91 if ((pci_get_class(dev) != PCIC_BRIDGE) ||
92 (pci_get_subclass(dev) != PCIS_BRIDGE_PCI)) {
96 if (ofw_bus_get_node(dev) == -1)
99 device_set_desc(dev, "OFW PCI-PCI bridge");
104 ofw_pcib_pci_attach(device_t dev)
106 struct ofw_pcib_softc *sc;
108 sc = device_get_softc(dev);
109 sc->ops_pcib_sc.dev = dev;
110 sc->ops_node = ofw_bus_get_node(dev);
112 ofw_bus_setup_iinfo(sc->ops_node, &sc->ops_iinfo,
115 pcib_attach_common(dev);
117 device_add_child(dev, "pci", -1);
119 return (bus_generic_attach(dev));
123 ofw_pcib_pci_get_node(device_t bridge, device_t dev)
125 /* We have only one child, the PCI bus, so pass it our node */
127 return (ofw_bus_get_node(bridge));
131 ofw_pcib_pci_route_interrupt(device_t bridge, device_t dev, int intpin)
133 struct ofw_pcib_softc *sc;
134 struct ofw_bus_iinfo *ii;
135 struct ofw_pci_register reg;
138 uint8_t maskbuf[sizeof(reg) + sizeof(pintr)];
140 sc = device_get_softc(bridge);
142 if (ii->opi_imapsz > 0) {
144 if (ofw_bus_lookup_imap(ofw_bus_get_node(dev), ii, ®,
145 sizeof(reg), &pintr, sizeof(pintr), &mintr, sizeof(mintr),
146 &iparent, maskbuf)) {
148 * If we've found a mapping, return it and don't map
149 * it again on higher levels - that causes problems
150 * in some cases, and never seems to be required.
152 return (MAP_IRQ(iparent, mintr));
154 } else if (intpin >= 1 && intpin <= 4) {
156 * When an interrupt map is missing, we need to do the
157 * standard PCI swizzle and continue mapping at the parent.
159 return (pcib_route_interrupt(bridge, dev, intpin));
161 return (PCIB_ROUTE_INTERRUPT(device_get_parent(device_get_parent(
162 bridge)), bridge, intpin));