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