2 * Copyright (c) 2015 Landon Fuller <landon@landonf.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13 * redistribution must be conditioned upon including a substantially
14 * similar Disclaimer requirement for further binary redistribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGES.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 * Broadcom Common PCIe-G2 Support.
36 * This base driver implementation is shared by the bhnd_pcib_g2 (root complex)
37 * and bhnd_pci_hostb_g2 (host bridge) drivers.
40 #include <sys/param.h>
41 #include <sys/malloc.h>
42 #include <sys/kernel.h>
44 #include <sys/module.h>
45 #include <sys/systm.h>
47 #include <machine/bus.h>
49 #include <machine/resource.h>
51 #include <dev/bhnd/bhnd.h>
52 #include <dev/mdio/mdio.h>
54 #include "bhnd_pcie2_reg.h"
55 #include "bhnd_pcie2_var.h"
57 static struct bhnd_device_quirk bhnd_pcie2_quirks[];
59 #define BHND_PCIE_DEV(_core, _desc, ...) \
60 BHND_DEVICE(BCM, _core, _desc, bhnd_pcie2_quirks, ## __VA_ARGS__)
62 static const struct bhnd_device bhnd_pcie2_devs[] = {
63 BHND_PCIE_DEV(PCIE2, "PCIe-G2 Host-PCI bridge", BHND_DF_HOSTB),
64 BHND_PCIE_DEV(PCIE2, "PCIe-G2 PCI-BHND bridge", BHND_DF_SOC),
69 /* Device quirks tables */
70 static struct bhnd_device_quirk bhnd_pcie2_quirks[] = {
75 bhnd_pcie2_generic_probe(device_t dev)
77 const struct bhnd_device *id;
79 id = bhnd_device_lookup(dev, bhnd_pcie2_devs,
80 sizeof(bhnd_pcie2_devs[0]));
84 bhnd_set_custom_core_desc(dev, id->desc);
85 return (BUS_PROBE_DEFAULT);
89 bhnd_pcie2_generic_attach(device_t dev)
91 struct bhnd_pcie2_softc *sc;
94 sc = device_get_softc(dev);
96 sc->quirks = bhnd_device_quirks(dev, bhnd_pcie2_devs,
97 sizeof(bhnd_pcie2_devs[0]));
99 /* Allocate bus resources */
100 sc->mem_res = bhnd_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
102 if (sc->mem_res == NULL)
105 BHND_PCIE2_LOCK_INIT(sc);
107 /* Probe and attach children */
108 if ((error = bus_generic_attach(dev)))
114 bhnd_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem_res);
115 BHND_PCIE2_LOCK_DESTROY(sc);
121 bhnd_pcie2_generic_detach(device_t dev)
123 struct bhnd_pcie2_softc *sc;
126 sc = device_get_softc(dev);
128 if ((error = bus_generic_detach(dev)))
131 bhnd_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem_res);
133 BHND_PCIE2_LOCK_DESTROY(sc);
138 static struct resource_list *
139 bhnd_pcie2_get_resource_list(device_t dev, device_t child)
141 struct bhnd_pcie2_devinfo *dinfo;
143 if (device_get_parent(child) != dev)
146 dinfo = device_get_ivars(child);
147 return (&dinfo->resources);
151 bhnd_pcie2_add_child(device_t dev, u_int order, const char *name, int unit)
153 struct bhnd_pcie2_devinfo *dinfo;
156 child = device_add_child_ordered(dev, order, name, unit);
160 dinfo = malloc(sizeof(struct bhnd_pcie2_devinfo), M_DEVBUF, M_NOWAIT);
162 device_delete_child(dev, child);
166 resource_list_init(&dinfo->resources);
168 device_set_ivars(child, dinfo);
173 bhnd_pcie2_child_deleted(device_t dev, device_t child)
175 struct bhnd_pcie2_devinfo *dinfo;
177 if (device_get_parent(child) != dev)
180 dinfo = device_get_ivars(child);
182 resource_list_free(&dinfo->resources);
183 free(dinfo, M_DEVBUF);
186 device_set_ivars(child, NULL);
190 bhnd_pcie2_generic_suspend(device_t dev)
192 return (bus_generic_suspend(dev));
196 bhnd_pcie2_generic_resume(device_t dev)
198 return (bus_generic_resume(dev));
202 * Read a 32-bit PCIe TLP/DLLP/PLP protocol register.
204 * @param sc The bhndb_pci driver state.
205 * @param addr The protocol register offset.
208 bhnd_pcie2_read_proto_reg(struct bhnd_pcie2_softc *sc, uint32_t addr)
215 * Write a 32-bit PCIe TLP/DLLP/PLP protocol register value.
217 * @param sc The bhndb_pci driver state.
218 * @param addr The protocol register offset.
219 * @param val The value to write to @p addr.
222 bhnd_pcie2_write_proto_reg(struct bhnd_pcie2_softc *sc, uint32_t addr,
226 panic("unimplemented");
230 bhnd_pcie2_mdio_read(struct bhnd_pcie2_softc *sc, int phy, int reg)
237 bhnd_pcie2_mdio_write(struct bhnd_pcie2_softc *sc, int phy, int reg, int val)
244 bhnd_pcie2_mdio_read_ext(struct bhnd_pcie2_softc *sc, int phy, int devaddr,
252 bhnd_pcie2_mdio_write_ext(struct bhnd_pcie2_softc *sc, int phy, int devaddr,
259 static device_method_t bhnd_pcie2_methods[] = {
260 /* Device interface */
261 DEVMETHOD(device_probe, bhnd_pcie2_generic_probe),
262 DEVMETHOD(device_attach, bhnd_pcie2_generic_attach),
263 DEVMETHOD(device_detach, bhnd_pcie2_generic_detach),
264 DEVMETHOD(device_suspend, bhnd_pcie2_generic_suspend),
265 DEVMETHOD(device_resume, bhnd_pcie2_generic_resume),
268 DEVMETHOD(bus_add_child, bhnd_pcie2_add_child),
269 DEVMETHOD(bus_child_deleted, bhnd_pcie2_child_deleted),
270 DEVMETHOD(bus_print_child, bus_generic_print_child),
271 DEVMETHOD(bus_get_resource_list, bhnd_pcie2_get_resource_list),
272 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
273 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
274 DEVMETHOD(bus_delete_resource, bus_generic_rl_delete_resource),
276 DEVMETHOD(bus_alloc_resource, bus_generic_rl_alloc_resource),
277 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
278 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
279 DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource),
280 DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
285 DEFINE_CLASS_0(bhnd_pcie2, bhnd_pcie2_driver, bhnd_pcie2_methods,
286 sizeof(struct bhnd_pcie2_softc));
287 MODULE_DEPEND(bhnd_pcie2, bhnd, 1, 1, 1);
288 MODULE_DEPEND(bhnd_pcie2, pci, 1, 1, 1);
289 MODULE_VERSION(bhnd_pcie2, 1);