1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2015 Landon Fuller <landon@landonf.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer,
12  *    without modification.
13  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15  *    redistribution must be conditioned upon including a substantially
16  *    similar Disclaimer requirement for further binary redistribution.
17  *
18  * NO WARRANTY
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
22  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
24  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29  * THE POSSIBILITY OF SUCH DAMAGES.
30  */
31 
32 #include <sys/cdefs.h>
33 /*
34  * Broadcom BHND PCIe-Gen2 PCI-Host Bridge.
35  *
36  * This driver handles all interactions with PCIe-G2 bridge cores operating in
37  * endpoint mode.
38  *
39  * Host-level PCI operations are handled at the bhndb bridge level by the
40  * bhndb_pci driver.
41  */
42 
43 // TODO
44 //
45 // A full survey of known quirks/work-arounds has not been completed.
46 //
47 // Work-arounds for the following are not yet implemented:
48 // - BHND_PCIE2_QUIRK_SERDES_TXDRV_DEEMPH
49 //   4360 PCIe SerDes Tx amplitude/deemphasis (vendor Apple, boards
50 //   BCM94360X51P2, BCM94360X51A)
51 
52 #include <sys/param.h>
53 #include <sys/kernel.h>
54 
55 #include <sys/malloc.h>
56 
57 #include <sys/bus.h>
58 #include <sys/module.h>
59 
60 #include <sys/systm.h>
61 
62 #include <machine/bus.h>
63 #include <sys/rman.h>
64 #include <machine/resource.h>
65 
66 #include <dev/bhnd/bhnd.h>
67 
68 #include <dev/pci/pcireg.h>
69 #include <dev/pci/pcivar.h>
70 
71 #include "bhnd_pcie2_reg.h"
72 #include "bhnd_pcie2_hostbvar.h"
73 
74 static const struct bhnd_device_quirk bhnd_pcie2_quirks[];
75 
76 static int	bhnd_pcie2_wars_early_once(struct bhnd_pcie2hb_softc *sc);
77 static int	bhnd_pcie2_wars_hwup(struct bhnd_pcie2hb_softc *sc);
78 static int	bhnd_pcie2_wars_hwdown(struct bhnd_pcie2hb_softc *sc);
79 
80 /*
81  * device/quirk tables
82  */
83 
84 #define	BHND_PCI_DEV(_core, _quirks)		\
85 	BHND_DEVICE(BCM, _core, NULL, _quirks, BHND_DF_HOSTB)
86 
87 static const struct bhnd_device bhnd_pcie2_devs[] = {
88 	BHND_PCI_DEV(PCIE2,	bhnd_pcie2_quirks),
89 	BHND_DEVICE_END
90 };
91 
92 static const struct bhnd_device_quirk bhnd_pcie2_quirks[] = {
93 	/* Apple BCM4360 boards that require adjusting TX amplitude and
94 	 * differential output de-emphasis of the PCIe SerDes */
95 	{{ BHND_MATCH_BOARD(PCI_VENDOR_APPLE, BCM94360X51P2), },
96 		BHND_PCIE2_QUIRK_SERDES_TXDRV_DEEMPH },
97 	{{ BHND_MATCH_BOARD(PCI_VENDOR_APPLE, BCM94360X51A), },
98 		BHND_PCIE2_QUIRK_SERDES_TXDRV_DEEMPH },
99 
100 	BHND_DEVICE_QUIRK_END
101 };
102 
103 static int
104 bhnd_pcie2_hostb_attach(device_t dev)
105 {
106 	struct bhnd_pcie2hb_softc	*sc;
107 	int				 error;
108 
109 	sc = device_get_softc(dev);
110 	sc->dev = dev;
111 	sc->quirks = bhnd_device_quirks(dev, bhnd_pcie2_devs,
112 	    sizeof(bhnd_pcie2_devs[0]));
113 
114 	/* Find the host PCI bridge device */
115 	sc->pci_dev = bhnd_find_bridge_root(dev, devclass_find("pci"));
116 	if (sc->pci_dev == NULL) {
117 		device_printf(dev, "parent pci bridge device not found\n");
118 		return (ENXIO);
119 	}
120 
121 	/* Common setup */
122 	if ((error = bhnd_pcie2_generic_attach(dev)))
123 		return (error);
124 
125 	/* Apply early single-shot work-arounds */
126 	if ((error = bhnd_pcie2_wars_early_once(sc)))
127 		goto failed;
128 
129 	/* Apply attach/resume work-arounds */
130 	if ((error = bhnd_pcie2_wars_hwup(sc)))
131 		goto failed;
132 
133 	return (0);
134 
135 failed:
136 	bhnd_pcie2_generic_detach(dev);
137 	return (error);
138 }
139 
140 static int
141 bhnd_pcie2_hostb_detach(device_t dev)
142 {
143 	struct bhnd_pcie2hb_softc	*sc;
144 	int				 error;
145 
146 	sc = device_get_softc(dev);
147 
148 	/* Apply suspend/detach work-arounds */
149 	if ((error = bhnd_pcie2_wars_hwdown(sc)))
150 		return (error);
151 
152 	return (bhnd_pcie2_generic_detach(dev));
153 }
154 
155 static int
156 bhnd_pcie2_hostb_suspend(device_t dev)
157 {
158 	struct bhnd_pcie2hb_softc	*sc;
159 	int				 error;
160 
161 	sc = device_get_softc(dev);
162 
163 	/* Apply suspend/detach work-arounds */
164 	if ((error = bhnd_pcie2_wars_hwdown(sc)))
165 		return (error);
166 
167 	return (bhnd_pcie2_generic_suspend(dev));
168 }
169 
170 static int
171 bhnd_pcie2_hostb_resume(device_t dev)
172 {
173 	struct bhnd_pcie2hb_softc	*sc;
174 	int				 error;
175 
176 	sc = device_get_softc(dev);
177 
178 	if ((error = bhnd_pcie2_generic_resume(dev)))
179 		return (error);
180 
181 	/* Apply attach/resume work-arounds */
182 	if ((error = bhnd_pcie2_wars_hwup(sc))) {
183 		bhnd_pcie2_generic_detach(dev);
184 		return (error);
185 	}
186 
187 	return (0);
188 }
189 
190 /**
191  * Apply any hardware work-arounds that must be executed exactly once, early in
192  * the attach process.
193  *
194  * This must be called after core enumeration and discovery of all applicable
195  * quirks, but prior to probe/attach of any cores, parsing of
196  * SPROM, etc.
197  */
198 static int
199 bhnd_pcie2_wars_early_once(struct bhnd_pcie2hb_softc *sc)
200 {
201 	// TODO
202 	return (ENXIO);
203 }
204 
205 /**
206  * Apply any hardware workarounds that are required upon attach or resume
207  * of the bridge device.
208  */
209 static int
210 bhnd_pcie2_wars_hwup(struct bhnd_pcie2hb_softc *sc)
211 {
212 	// TODO
213 	return (ENXIO);
214 }
215 
216 /**
217  * Apply any hardware workarounds that are required upon detach or suspend
218  * of the bridge device.
219  */
220 static int
221 bhnd_pcie2_wars_hwdown(struct bhnd_pcie2hb_softc *sc)
222 {
223 	// TODO
224 	return (ENXIO);
225 }
226 
227 static device_method_t bhnd_pcie2_hostb_methods[] = {
228 	/* Device interface */
229 	DEVMETHOD(device_attach,		bhnd_pcie2_hostb_attach),
230 	DEVMETHOD(device_detach,		bhnd_pcie2_hostb_detach),
231 	DEVMETHOD(device_suspend,		bhnd_pcie2_hostb_suspend),
232 	DEVMETHOD(device_resume,		bhnd_pcie2_hostb_resume),
233 
234 	DEVMETHOD_END
235 };
236 
237 DEFINE_CLASS_1(bhnd_hostb, bhnd_pcie2_hostb_driver,
238     bhnd_pcie2_hostb_methods, sizeof(struct bhnd_pcie2hb_softc),
239     bhnd_pcie2_driver);
240 
241 DRIVER_MODULE(bhnd_pcie2_hostb, bhnd, bhnd_pcie2_hostb_driver, 0, 0);
242 
243 MODULE_VERSION(bhnd_pcie2_hostb, 1);
244 MODULE_DEPEND(bhnd_pcie2_hostb, bhnd, 1, 1, 1);
245 MODULE_DEPEND(bhnd_pcie2_hostb, bhnd_pcie2, 1, 1, 1);
246