2 * Copyright (c) 2015-2016 Landon Fuller <landon@landonf.org>
3 * Copyright (c) 2017 The FreeBSD Foundation
6 * Portions of this software were developed by Landon Fuller
7 * under sponsorship from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
16 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
17 * redistribution must be conditioned upon including a substantially
18 * similar Disclaimer requirement for further binary redistribution.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
24 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
25 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
26 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
29 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGES.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/kernel.h>
40 #include <sys/module.h>
41 #include <sys/systm.h>
43 #include <dev/bhnd/bhnd_ids.h>
44 #include <dev/bhnd/bhndb/bhndbvar.h>
45 #include <dev/bhnd/bhndb/bhndb_hwdata.h>
51 * Supports attachment of siba(4) bus devices via a bhndb bridge.
54 struct siba_bhndb_softc;
56 static int siba_bhndb_wars_hwup(struct siba_bhndb_softc *sc);
58 /* siba_bhndb per-instance state */
59 struct siba_bhndb_softc {
60 struct siba_softc siba; /**< common siba per-instance state */
61 uint32_t quirks; /**< bus-level quirks */
64 /* siba_bhndb quirks */
66 /** When PCIe-bridged, the D11 core's initiator request
67 * timeout must be disabled to prevent D11 from entering a
68 * RESP_TIMEOUT error state. */
69 SIBA_QUIRK_PCIE_D11_SB_TIMEOUT = (1<<0)
72 /* Bus-level quirks when bridged via a PCI host bridge core */
73 static struct bhnd_device_quirk pci_bridge_quirks[] = {
77 /* Bus-level quirks when bridged via a PCIe host bridge core */
78 static struct bhnd_device_quirk pcie_bridge_quirks[] = {
79 BHND_CHIP_QUIRK (4311, HWREV_EQ(2), SIBA_QUIRK_PCIE_D11_SB_TIMEOUT),
80 BHND_CHIP_QUIRK (4312, HWREV_ANY, SIBA_QUIRK_PCIE_D11_SB_TIMEOUT),
84 /* Bus-level quirks specific to a particular host bridge core */
85 static struct bhnd_device bridge_devs[] = {
86 BHND_DEVICE(BCM, PCI, NULL, pci_bridge_quirks),
87 BHND_DEVICE(BCM, PCIE, NULL, pcie_bridge_quirks),
92 siba_bhndb_probe(device_t dev)
94 const struct bhnd_chipid *cid;
97 /* Defer to default probe implementation */
98 if ((error = siba_probe(dev)) > 0)
102 cid = BHNDB_GET_CHIPID(device_get_parent(dev), dev);
103 if (cid->chip_type != BHND_CHIPTYPE_SIBA)
106 /* Set device description */
107 bhnd_set_default_bus_desc(dev, cid);
113 siba_bhndb_attach(device_t dev)
115 struct siba_bhndb_softc *sc;
119 sc = device_get_softc(dev);
122 /* Perform initial attach and enumerate our children. */
123 if ((error = siba_attach(dev)))
126 /* Fetch bus-level quirks required by the host bridge core */
127 if ((hostb = bhnd_bus_find_hostb_device(dev)) != NULL) {
128 sc->quirks |= bhnd_device_quirks(hostb, bridge_devs,
129 sizeof(bridge_devs[0]));
132 /* Apply attach/resume workarounds before any child drivers attach */
133 if ((error = siba_bhndb_wars_hwup(sc)))
136 /* Delegate remainder to standard bhnd method implementation */
137 if ((error = bhnd_generic_attach(dev)))
148 siba_bhndb_resume(device_t dev)
150 struct siba_bhndb_softc *sc;
153 sc = device_get_softc(dev);
155 /* Apply attach/resume work-arounds */
156 if ((error = siba_bhndb_wars_hwup(sc)))
159 /* Call our superclass' implementation */
160 return (siba_resume(dev));
163 /* Suspend all references to the device's cfg register blocks */
165 siba_bhndb_suspend_cfgblocks(device_t dev, struct siba_devinfo *dinfo) {
166 for (u_int i = 0; i < dinfo->core_id.num_cfg_blocks; i++) {
167 if (dinfo->cfg_res[i] == NULL)
170 BHNDB_SUSPEND_RESOURCE(device_get_parent(dev), dev,
171 SYS_RES_MEMORY, dinfo->cfg_res[i]->res);
176 siba_bhndb_suspend_child(device_t dev, device_t child)
178 struct siba_devinfo *dinfo;
181 if (device_get_parent(child) != dev)
182 BUS_SUSPEND_CHILD(device_get_parent(dev), child);
184 dinfo = device_get_ivars(child);
186 /* Suspend the child */
187 if ((error = bhnd_generic_br_suspend_child(dev, child)))
190 /* Suspend resource references to the child's config registers */
191 siba_bhndb_suspend_cfgblocks(dev, dinfo);
197 siba_bhndb_resume_child(device_t dev, device_t child)
199 struct siba_devinfo *dinfo;
202 if (device_get_parent(child) != dev)
203 BUS_SUSPEND_CHILD(device_get_parent(dev), child);
205 if (!device_is_suspended(child))
208 dinfo = device_get_ivars(child);
210 /* Resume all resource references to the child's config registers */
211 for (u_int i = 0; i < dinfo->core_id.num_cfg_blocks; i++) {
212 if (dinfo->cfg_res[i] == NULL)
215 error = BHNDB_RESUME_RESOURCE(device_get_parent(dev), dev,
216 SYS_RES_MEMORY, dinfo->cfg_res[i]->res);
218 siba_bhndb_suspend_cfgblocks(dev, dinfo);
223 /* Resume the child */
224 if ((error = bhnd_generic_br_resume_child(dev, child))) {
225 siba_bhndb_suspend_cfgblocks(dev, dinfo);
232 /* Work-around implementation for SIBA_QUIRK_PCIE_D11_SB_TIMEOUT */
234 siba_bhndb_wars_pcie_clear_d11_timeout(struct siba_bhndb_softc *sc)
236 struct siba_devinfo *dinfo;
240 /* Only applicable if there's a D11 core */
241 d11 = bhnd_bus_match_child(sc->siba.dev, &(struct bhnd_core_match) {
242 BHND_MATCH_CORE(BHND_MFGID_BCM, BHND_COREID_D11),
243 BHND_MATCH_CORE_UNIT(0)
248 /* Clear initiator timeout in D11's CFG0 block */
249 dinfo = device_get_ivars(d11);
250 KASSERT(dinfo->cfg_res[0] != NULL, ("missing core config mapping"));
252 imcfg = bhnd_bus_read_4(dinfo->cfg_res[0], SIBA_CFG0_IMCONFIGLOW);
253 imcfg &= ~(SIBA_IMCL_RTO_MASK|SIBA_IMCL_STO_MASK);
255 bhnd_bus_write_4(dinfo->cfg_res[0], SIBA_CFG0_IMCONFIGLOW, imcfg);
261 * Apply any hardware workarounds that are required upon attach or resume
265 siba_bhndb_wars_hwup(struct siba_bhndb_softc *sc)
269 if (sc->quirks & SIBA_QUIRK_PCIE_D11_SB_TIMEOUT) {
270 if ((error = siba_bhndb_wars_pcie_clear_d11_timeout(sc)))
277 static device_method_t siba_bhndb_methods[] = {
278 /* Device interface */
279 DEVMETHOD(device_probe, siba_bhndb_probe),
280 DEVMETHOD(device_attach, siba_bhndb_attach),
281 DEVMETHOD(device_resume, siba_bhndb_resume),
284 DEVMETHOD(bus_suspend_child, siba_bhndb_suspend_child),
285 DEVMETHOD(bus_resume_child, siba_bhndb_resume_child),
290 DEFINE_CLASS_2(bhnd, siba_bhndb_driver, siba_bhndb_methods,
291 sizeof(struct siba_softc), bhnd_bhndb_driver, siba_driver);
293 DRIVER_MODULE(siba_bhndb, bhndb, siba_bhndb_driver, bhnd_devclass, NULL, NULL);
295 MODULE_VERSION(siba_bhndb, 1);
296 MODULE_DEPEND(siba_bhndb, siba, 1, 1, 1);
297 MODULE_DEPEND(siba_bhndb, bhnd, 1, 1, 1);
298 MODULE_DEPEND(siba_bhndb, bhndb, 1, 1, 1);