1 /*- 2 * Copyright (c) 2000 Michael Smith 3 * Copyright (c) 2000 BSDi 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 * 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 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/module.h> 34 #include <sys/bus.h> 35 #include <sys/malloc.h> 36 #include <sys/kernel.h> 37 38 #include <dev/ofw/openfirm.h> 39 #include <dev/ofw/ofw_pci.h> 40 #include <dev/ofw/ofw_bus.h> 41 #include <dev/ofw/ofw_bus_subr.h> 42 43 #include <dev/pci/pcivar.h> 44 #include <dev/pci/pcireg.h> 45 #include <dev/pci/pcib_private.h> 46 47 #include <machine/intr_machdep.h> 48 49 #include "pcib_if.h" 50 51 static int ofw_pcib_pci_probe(device_t bus); 52 static int ofw_pcib_pci_attach(device_t bus); 53 static phandle_t ofw_pcib_pci_get_node(device_t bus, device_t dev); 54 static int ofw_pcib_pci_route_interrupt(device_t bridge, device_t dev, 55 int intpin); 56 57 static device_method_t ofw_pcib_pci_methods[] = { 58 /* Device interface */ 59 DEVMETHOD(device_probe, ofw_pcib_pci_probe), 60 DEVMETHOD(device_attach, ofw_pcib_pci_attach), 61 62 /* pcib interface */ 63 DEVMETHOD(pcib_route_interrupt, ofw_pcib_pci_route_interrupt), 64 65 /* ofw_bus interface */ 66 DEVMETHOD(ofw_bus_get_node, ofw_pcib_pci_get_node), 67 68 DEVMETHOD_END 69 }; 70 71 static devclass_t pcib_devclass; 72 73 struct ofw_pcib_softc { 74 /* 75 * This is here so that we can use pci bridge methods, too - the 76 * generic routines only need the dev, secbus and subbus members 77 * filled. 78 */ 79 struct pcib_softc ops_pcib_sc; 80 phandle_t ops_node; 81 struct ofw_bus_iinfo ops_iinfo; 82 }; 83 84 DEFINE_CLASS_1(pcib, ofw_pcib_pci_driver, ofw_pcib_pci_methods, 85 sizeof(struct ofw_pcib_softc), pcib_driver); 86 DRIVER_MODULE(ofw_pcib, pci, ofw_pcib_pci_driver, pcib_devclass, 0, 0); 87 88 static int 89 ofw_pcib_pci_probe(device_t dev) 90 { 91 92 if ((pci_get_class(dev) != PCIC_BRIDGE) || 93 (pci_get_subclass(dev) != PCIS_BRIDGE_PCI)) { 94 return (ENXIO); 95 } 96 97 if (ofw_bus_get_node(dev) == -1) 98 return (ENXIO); 99 100 device_set_desc(dev, "OFW PCI-PCI bridge"); 101 return (0); 102 } 103 104 static int 105 ofw_pcib_pci_attach(device_t dev) 106 { 107 struct ofw_pcib_softc *sc; 108 109 sc = device_get_softc(dev); 110 sc->ops_pcib_sc.dev = dev; 111 sc->ops_node = ofw_bus_get_node(dev); 112 113 ofw_bus_setup_iinfo(sc->ops_node, &sc->ops_iinfo, 114 sizeof(cell_t)); 115 116 pcib_attach_common(dev); 117 return (pcib_attach_child(dev)); 118 } 119 120 static phandle_t 121 ofw_pcib_pci_get_node(device_t bridge, device_t dev) 122 { 123 /* We have only one child, the PCI bus, so pass it our node */ 124 125 return (ofw_bus_get_node(bridge)); 126 } 127 128 static int 129 ofw_pcib_pci_route_interrupt(device_t bridge, device_t dev, int intpin) 130 { 131 struct ofw_pcib_softc *sc; 132 struct ofw_bus_iinfo *ii; 133 struct ofw_pci_register reg; 134 cell_t pintr, mintr[2]; 135 int intrcells; 136 phandle_t iparent; 137 138 sc = device_get_softc(bridge); 139 ii = &sc->ops_iinfo; 140 if (ii->opi_imapsz > 0) { 141 pintr = intpin; 142 143 /* Fabricate imap information if this isn't an OFW device */ 144 bzero(®, sizeof(reg)); 145 reg.phys_hi = (pci_get_bus(dev) << OFW_PCI_PHYS_HI_BUSSHIFT) | 146 (pci_get_slot(dev) << OFW_PCI_PHYS_HI_DEVICESHIFT) | 147 (pci_get_function(dev) << OFW_PCI_PHYS_HI_FUNCTIONSHIFT); 148 149 intrcells = ofw_bus_lookup_imap(ofw_bus_get_node(dev), ii, ®, 150 sizeof(reg), &pintr, sizeof(pintr), mintr, sizeof(mintr), 151 &iparent); 152 if (intrcells) { 153 /* 154 * If we've found a mapping, return it and don't map 155 * it again on higher levels - that causes problems 156 * in some cases, and never seems to be required. 157 */ 158 mintr[0] = ofw_bus_map_intr(dev, iparent, intrcells, 159 mintr); 160 return (mintr[0]); 161 } 162 } else if (intpin >= 1 && intpin <= 4) { 163 /* 164 * When an interrupt map is missing, we need to do the 165 * standard PCI swizzle and continue mapping at the parent. 166 */ 167 return (pcib_route_interrupt(bridge, dev, intpin)); 168 } 169 return (PCIB_ROUTE_INTERRUPT(device_get_parent(device_get_parent( 170 bridge)), bridge, intpin)); 171 } 172 173