1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1994,1995 Stefan Esser, Wolfgang StanglMeier
5  * Copyright (c) 2000 Michael Smith <msmith@freebsd.org>
6  * Copyright (c) 2000 BSDi
7  * Copyright (c) 2001 - 2003 Thomas Moestl <tmm@FreeBSD.org>
8  * Copyright (c) 2009 by Marius Strobl <marius@FreeBSD.org>
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	from: FreeBSD: src/sys/dev/pci/pci_pci.c,v 1.3 2000/12/13
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD: stable/12/sys/sparc64/pci/ofw_pcib.c 326262 2017-11-27 15:10:39Z pfg $");
39 
40 #include "opt_ofw_pci.h"
41 
42 #include <sys/param.h>
43 #include <sys/bus.h>
44 #include <sys/kernel.h>
45 #include <sys/libkern.h>
46 #include <sys/module.h>
47 #include <sys/rman.h>
48 
49 #include <dev/ofw/ofw_bus.h>
50 #include <dev/ofw/openfirm.h>
51 
52 #include <dev/pci/pcireg.h>
53 #include <dev/pci/pcivar.h>
54 #include <dev/pci/pcib_private.h>
55 
56 #include "pcib_if.h"
57 
58 #include <sparc64/pci/ofw_pci.h>
59 #include <sparc64/pci/ofw_pcib_subr.h>
60 
61 #define	PCI_DEVID_ALI_M5249	0x524910b9
62 #define	PCI_VENDOR_PLX		0x10b5
63 
64 static device_probe_t ofw_pcib_probe;
65 static device_attach_t ofw_pcib_attach;
66 static ofw_pci_setup_device_t ofw_pcib_setup_device;
67 
68 static device_method_t ofw_pcib_methods[] = {
69 	/* Device interface */
70 	DEVMETHOD(device_probe,		ofw_pcib_probe),
71 	DEVMETHOD(device_attach,	ofw_pcib_attach),
72 
73 	/* Bus interface */
74 
75 	/* pcib interface */
76 	DEVMETHOD(pcib_route_interrupt, ofw_pcib_gen_route_interrupt),
77 	DEVMETHOD(pcib_request_feature,	pcib_request_feature_allow),
78 
79 	/* ofw_bus interface */
80 	DEVMETHOD(ofw_bus_get_node,	ofw_pcib_gen_get_node),
81 	DEVMETHOD(ofw_pci_setup_device, ofw_pcib_setup_device),
82 
83 	DEVMETHOD_END
84 };
85 
86 static devclass_t pcib_devclass;
87 
88 DEFINE_CLASS_1(pcib, ofw_pcib_driver, ofw_pcib_methods,
89     sizeof(struct ofw_pcib_gen_softc), pcib_driver);
90 EARLY_DRIVER_MODULE(ofw_pcib, pci, ofw_pcib_driver, pcib_devclass, NULL, NULL,
91     BUS_PASS_BUS);
92 MODULE_DEPEND(ofw_pcib, pci, 1, 1, 1);
93 
94 static int
ofw_pcib_probe(device_t dev)95 ofw_pcib_probe(device_t dev)
96 {
97 	char desc[sizeof("OFW PCIe-PCIe bridge")];
98 	const char *dtype, *pbdtype;
99 
100 #define	ISDTYPE(dtype, type)						\
101 	(((dtype) != NULL) && strcmp((dtype), (type)) == 0)
102 
103 	if ((pci_get_class(dev) == PCIC_BRIDGE) &&
104 	    (pci_get_subclass(dev) == PCIS_BRIDGE_PCI) &&
105 	    ofw_bus_get_node(dev) != 0) {
106 		dtype = ofw_bus_get_type(dev);
107 		pbdtype = ofw_bus_get_type(device_get_parent(
108 		    device_get_parent(dev)));
109 		snprintf(desc, sizeof(desc), "OFW PCI%s-PCI%s bridge",
110 		    ISDTYPE(pbdtype, OFW_TYPE_PCIE) ? "e" : "",
111 		    ISDTYPE(dtype, OFW_TYPE_PCIE) ? "e" : "");
112 		device_set_desc_copy(dev, desc);
113 		return (BUS_PROBE_DEFAULT);
114 	}
115 
116 #undef ISDTYPE
117 
118 	return (ENXIO);
119 }
120 
121 static int
ofw_pcib_attach(device_t dev)122 ofw_pcib_attach(device_t dev)
123 {
124 	struct ofw_pcib_gen_softc *sc;
125 
126 	sc = device_get_softc(dev);
127 
128 	switch (pci_get_devid(dev)) {
129 	/*
130 	 * The ALi M5249 found in Fire-based machines by definition must me
131 	 * subtractive as they have a ISA bridge on their secondary side but
132 	 * don't indicate this in the class code although the ISA I/O range
133 	 * isn't included in their bridge decode.
134 	 */
135 	case PCI_DEVID_ALI_M5249:
136 		sc->ops_pcib_sc.flags |= PCIB_SUBTRACTIVE;
137 		break;
138 	}
139 
140 	switch (pci_get_vendor(dev)) {
141 	/*
142 	 * Concurrently write the primary and secondary bus numbers in order
143 	 * to work around a bug in PLX PEX 8114 causing the internal shadow
144 	 * copies of these not to be updated when setting them bytewise.
145 	 */
146 	case PCI_VENDOR_PLX:
147 		pci_write_config(dev, PCIR_PRIBUS_1,
148 		    pci_read_config(dev, PCIR_SECBUS_1, 1) << 8 |
149 		    pci_read_config(dev, PCIR_PRIBUS_1, 1), 2);
150 		break;
151 	}
152 
153 	ofw_pcib_gen_setup(dev);
154 	pcib_attach_common(dev);
155 	return (pcib_attach_child(dev));
156 }
157 
158 static void
ofw_pcib_setup_device(device_t bus,device_t child)159 ofw_pcib_setup_device(device_t bus, device_t child)
160 {
161 	int i;
162 	uint16_t reg;
163 
164 	switch (pci_get_vendor(bus)) {
165 	/*
166 	 * For PLX PEX 8532 issue 64 TLPs to the child from the downstream
167 	 * port to the child device in order to work around a hardware bug.
168 	 */
169 	case PCI_VENDOR_PLX:
170 		for (i = 0, reg = 0; i < 64; i++)
171 			reg |= pci_get_devid(child);
172 		break;
173 	}
174 
175 	OFW_PCI_SETUP_DEVICE(device_get_parent(bus), child);
176 }
177