1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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__FBSDID("$FreeBSD$");
34
35/*
36 * Broadcom BHND PCIe-Gen2 PCI-Host Bridge.
37 *
38 * This driver handles all interactions with PCIe-G2 bridge cores operating in
39 * endpoint mode.
40 *
41 * Host-level PCI operations are handled at the bhndb bridge level by the
42 * bhndb_pci driver.
43 */
44
45// TODO
46//
47// A full survey of known quirks/work-arounds has not been completed.
48//
49// Work-arounds for the following are not yet implemented:
50// - BHND_PCIE2_QUIRK_SERDES_TXDRV_DEEMPH
51//   4360 PCIe SerDes Tx amplitude/deemphasis (vendor Apple, boards
52//   BCM94360X51P2, BCM94360X51A)
53
54#include <sys/param.h>
55#include <sys/kernel.h>
56
57#include <sys/malloc.h>
58
59#include <sys/bus.h>
60#include <sys/module.h>
61
62#include <sys/systm.h>
63
64#include <machine/bus.h>
65#include <sys/rman.h>
66#include <machine/resource.h>
67
68#include <dev/bhnd/bhnd.h>
69
70#include <dev/pci/pcireg.h>
71#include <dev/pci/pcivar.h>
72
73#include "bhnd_pcie2_reg.h"
74#include "bhnd_pcie2_hostbvar.h"
75
76static const struct bhnd_device_quirk bhnd_pcie2_quirks[];
77
78
79static int	bhnd_pcie2_wars_early_once(struct bhnd_pcie2hb_softc *sc);
80static int	bhnd_pcie2_wars_hwup(struct bhnd_pcie2hb_softc *sc);
81static int	bhnd_pcie2_wars_hwdown(struct bhnd_pcie2hb_softc *sc);
82
83/*
84 * device/quirk tables
85 */
86
87#define	BHND_PCI_DEV(_core, _quirks)		\
88	BHND_DEVICE(BCM, _core, NULL, _quirks, BHND_DF_HOSTB)
89
90static const struct bhnd_device bhnd_pcie2_devs[] = {
91	BHND_PCI_DEV(PCIE2,	bhnd_pcie2_quirks),
92	BHND_DEVICE_END
93};
94
95static const struct bhnd_device_quirk bhnd_pcie2_quirks[] = {
96	/* Apple BCM4360 boards that require adjusting TX amplitude and
97	 * differential output de-emphasis of the PCIe SerDes */
98	{{ BHND_MATCH_BOARD(PCI_VENDOR_APPLE, BCM94360X51P2), },
99		BHND_PCIE2_QUIRK_SERDES_TXDRV_DEEMPH },
100	{{ BHND_MATCH_BOARD(PCI_VENDOR_APPLE, BCM94360X51A), },
101		BHND_PCIE2_QUIRK_SERDES_TXDRV_DEEMPH },
102
103	BHND_DEVICE_QUIRK_END
104};
105
106static int
107bhnd_pcie2_hostb_attach(device_t dev)
108{
109	struct bhnd_pcie2hb_softc	*sc;
110	int				 error;
111
112	sc = device_get_softc(dev);
113	sc->dev = dev;
114	sc->quirks = bhnd_device_quirks(dev, bhnd_pcie2_devs,
115	    sizeof(bhnd_pcie2_devs[0]));
116
117	/* Find the host PCI bridge device */
118	sc->pci_dev = bhnd_find_bridge_root(dev, devclass_find("pci"));
119	if (sc->pci_dev == NULL) {
120		device_printf(dev, "parent pci bridge device not found\n");
121		return (ENXIO);
122	}
123
124	/* Common setup */
125	if ((error = bhnd_pcie2_generic_attach(dev)))
126		return (error);
127
128
129	/* Apply early single-shot work-arounds */
130	if ((error = bhnd_pcie2_wars_early_once(sc)))
131		goto failed;
132
133
134	/* Apply attach/resume work-arounds */
135	if ((error = bhnd_pcie2_wars_hwup(sc)))
136		goto failed;
137
138
139	return (0);
140
141failed:
142	bhnd_pcie2_generic_detach(dev);
143	return (error);
144}
145
146static int
147bhnd_pcie2_hostb_detach(device_t dev)
148{
149	struct bhnd_pcie2hb_softc	*sc;
150	int				 error;
151
152	sc = device_get_softc(dev);
153
154	/* Apply suspend/detach work-arounds */
155	if ((error = bhnd_pcie2_wars_hwdown(sc)))
156		return (error);
157
158	return (bhnd_pcie2_generic_detach(dev));
159}
160
161static int
162bhnd_pcie2_hostb_suspend(device_t dev)
163{
164	struct bhnd_pcie2hb_softc	*sc;
165	int				 error;
166
167	sc = device_get_softc(dev);
168
169	/* Apply suspend/detach work-arounds */
170	if ((error = bhnd_pcie2_wars_hwdown(sc)))
171		return (error);
172
173	return (bhnd_pcie2_generic_suspend(dev));
174}
175
176static int
177bhnd_pcie2_hostb_resume(device_t dev)
178{
179	struct bhnd_pcie2hb_softc	*sc;
180	int				 error;
181
182	sc = device_get_softc(dev);
183
184	if ((error = bhnd_pcie2_generic_resume(dev)))
185		return (error);
186
187	/* Apply attach/resume work-arounds */
188	if ((error = bhnd_pcie2_wars_hwup(sc))) {
189		bhnd_pcie2_generic_detach(dev);
190		return (error);
191	}
192
193	return (0);
194}
195
196/**
197 * Apply any hardware work-arounds that must be executed exactly once, early in
198 * the attach process.
199 *
200 * This must be called after core enumeration and discovery of all applicable
201 * quirks, but prior to probe/attach of any cores, parsing of
202 * SPROM, etc.
203 */
204static int
205bhnd_pcie2_wars_early_once(struct bhnd_pcie2hb_softc *sc)
206{
207	// TODO
208	return (ENXIO);
209}
210
211/**
212 * Apply any hardware workarounds that are required upon attach or resume
213 * of the bridge device.
214 */
215static int
216bhnd_pcie2_wars_hwup(struct bhnd_pcie2hb_softc *sc)
217{
218	// TODO
219	return (ENXIO);
220}
221
222/**
223 * Apply any hardware workarounds that are required upon detach or suspend
224 * of the bridge device.
225 */
226static int
227bhnd_pcie2_wars_hwdown(struct bhnd_pcie2hb_softc *sc)
228{
229	// TODO
230	return (ENXIO);
231}
232
233static device_method_t bhnd_pcie2_hostb_methods[] = {
234	/* Device interface */
235	DEVMETHOD(device_attach,		bhnd_pcie2_hostb_attach),
236	DEVMETHOD(device_detach,		bhnd_pcie2_hostb_detach),
237	DEVMETHOD(device_suspend,		bhnd_pcie2_hostb_suspend),
238	DEVMETHOD(device_resume,		bhnd_pcie2_hostb_resume),
239
240	DEVMETHOD_END
241};
242
243DEFINE_CLASS_1(bhnd_hostb, bhnd_pcie2_hostb_driver,
244    bhnd_pcie2_hostb_methods, sizeof(struct bhnd_pcie2hb_softc),
245    bhnd_pcie2_driver);
246
247DRIVER_MODULE(bhnd_pcie2_hostb, bhnd, bhnd_pcie2_hostb_driver, bhnd_hostb_devclass, 0, 0);
248
249MODULE_VERSION(bhnd_pcie2_hostb, 1);
250MODULE_DEPEND(bhnd_pcie2_hostb, bhnd, 1, 1, 1);
251MODULE_DEPEND(bhnd_pcie2_hostb, bhnd_pcie2, 1, 1, 1);
252