xref: /freebsd/sys/powerpc/ofw/ofw_pcib_pci.c (revision eda14cbc264d6969b02f2b1994cef11148e914f1)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2000 Michael Smith
5  * Copyright (c) 2000 BSDi
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/module.h>
36 #include <sys/bus.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
39 
40 #include <dev/ofw/openfirm.h>
41 #include <dev/ofw/ofw_pci.h>
42 #include <dev/ofw/ofw_bus.h>
43 #include <dev/ofw/ofw_bus_subr.h>
44 
45 #include <dev/pci/pcivar.h>
46 #include <dev/pci/pcireg.h>
47 #include <dev/pci/pcib_private.h>
48 
49 #include <machine/intr_machdep.h>
50 
51 #include "pcib_if.h"
52 
53 static int	ofw_pcib_pci_probe(device_t bus);
54 static int	ofw_pcib_pci_attach(device_t bus);
55 static phandle_t ofw_pcib_pci_get_node(device_t bus, device_t dev);
56 static int	ofw_pcib_pci_route_interrupt(device_t bridge, device_t dev,
57 		    int intpin);
58 
59 static device_method_t ofw_pcib_pci_methods[] = {
60 	/* Device interface */
61 	DEVMETHOD(device_probe,		ofw_pcib_pci_probe),
62 	DEVMETHOD(device_attach,	ofw_pcib_pci_attach),
63 
64 	/* pcib interface */
65 	DEVMETHOD(pcib_route_interrupt,	ofw_pcib_pci_route_interrupt),
66 	DEVMETHOD(pcib_request_feature,	pcib_request_feature_allow),
67 
68 	/* ofw_bus interface */
69 	DEVMETHOD(ofw_bus_get_node,	ofw_pcib_pci_get_node),
70 
71 	DEVMETHOD_END
72 };
73 
74 static devclass_t pcib_devclass;
75 
76 struct ofw_pcib_softc {
77         /*
78          * This is here so that we can use pci bridge methods, too - the
79          * generic routines only need the dev, secbus and subbus members
80          * filled.
81          */
82         struct pcib_softc       ops_pcib_sc;
83 	phandle_t		ops_node;
84         struct ofw_bus_iinfo    ops_iinfo;
85 };
86 
87 DEFINE_CLASS_1(pcib, ofw_pcib_pci_driver, ofw_pcib_pci_methods,
88     sizeof(struct ofw_pcib_softc), pcib_driver);
89 EARLY_DRIVER_MODULE(ofw_pcib, pci, ofw_pcib_pci_driver, pcib_devclass, 0, 0,
90     BUS_PASS_BUS);
91 
92 static int
93 ofw_pcib_pci_probe(device_t dev)
94 {
95 
96 	if ((pci_get_class(dev) != PCIC_BRIDGE) ||
97 	    (pci_get_subclass(dev) != PCIS_BRIDGE_PCI)) {
98 		return (ENXIO);
99 	}
100 
101 	if (ofw_bus_get_node(dev) == -1)
102 		return (ENXIO);
103 
104 	device_set_desc(dev, "OFW PCI-PCI bridge");
105 	return (0);
106 }
107 
108 static int
109 ofw_pcib_pci_attach(device_t dev)
110 {
111 	struct ofw_pcib_softc *sc;
112 
113 	sc = device_get_softc(dev);
114 	sc->ops_pcib_sc.dev = dev;
115 	sc->ops_node = ofw_bus_get_node(dev);
116 
117 	ofw_bus_setup_iinfo(sc->ops_node, &sc->ops_iinfo,
118 	    sizeof(cell_t));
119 
120 	pcib_attach_common(dev);
121 	return (pcib_attach_child(dev));
122 }
123 
124 static phandle_t
125 ofw_pcib_pci_get_node(device_t bridge, device_t dev)
126 {
127 	/* We have only one child, the PCI bus, so pass it our node */
128 
129 	return (ofw_bus_get_node(bridge));
130 }
131 
132 static int
133 ofw_pcib_pci_route_interrupt(device_t bridge, device_t dev, int intpin)
134 {
135 	struct ofw_pcib_softc *sc;
136 	struct ofw_bus_iinfo *ii;
137 	struct ofw_pci_register reg;
138 	cell_t pintr, mintr[2];
139 	int intrcells;
140 	phandle_t iparent;
141 
142 	sc = device_get_softc(bridge);
143 	ii = &sc->ops_iinfo;
144 	if (ii->opi_imapsz > 0) {
145 		pintr = intpin;
146 
147 		/* Fabricate imap information if this isn't an OFW device */
148 		bzero(&reg, sizeof(reg));
149 		reg.phys_hi = (pci_get_bus(dev) << OFW_PCI_PHYS_HI_BUSSHIFT) |
150 		    (pci_get_slot(dev) << OFW_PCI_PHYS_HI_DEVICESHIFT) |
151 		    (pci_get_function(dev) << OFW_PCI_PHYS_HI_FUNCTIONSHIFT);
152 
153 		intrcells = ofw_bus_lookup_imap(ofw_bus_get_node(dev), ii, &reg,
154 		    sizeof(reg), &pintr, sizeof(pintr), mintr, sizeof(mintr),
155 		    &iparent);
156 		if (intrcells) {
157 			/*
158 			 * If we've found a mapping, return it and don't map
159 			 * it again on higher levels - that causes problems
160 			 * in some cases, and never seems to be required.
161 			 */
162 			mintr[0] = ofw_bus_map_intr(dev, iparent, intrcells,
163 			    mintr);
164 			return (mintr[0]);
165 		}
166 	} else if (intpin >= 1 && intpin <= 4) {
167 		/*
168 		 * When an interrupt map is missing, we need to do the
169 		 * standard PCI swizzle and continue mapping at the parent.
170 		 */
171 		return (pcib_route_interrupt(bridge, dev, intpin));
172 	}
173 	return (PCIB_ROUTE_INTERRUPT(device_get_parent(device_get_parent(
174 	    bridge)), bridge, intpin));
175 }
176 
177