129bfcb3aSEmmanuel Vadot /*-
229bfcb3aSEmmanuel Vadot * SPDX-License-Identifier: BSD-2-Clause
329bfcb3aSEmmanuel Vadot *
429bfcb3aSEmmanuel Vadot * Copyright (c) 2022 Beckhoff Automation GmbH & Co. KG
529bfcb3aSEmmanuel Vadot *
629bfcb3aSEmmanuel Vadot * Redistribution and use in source and binary forms, with or without
729bfcb3aSEmmanuel Vadot * modification, are permitted provided that the following conditions
829bfcb3aSEmmanuel Vadot * are met:
929bfcb3aSEmmanuel Vadot * 1. Redistributions of source code must retain the above copyright
1029bfcb3aSEmmanuel Vadot * notice, this list of conditions and the following disclaimer.
1129bfcb3aSEmmanuel Vadot * 2. Redistributions in binary form must reproduce the above copyright
1229bfcb3aSEmmanuel Vadot * notice, this list of conditions and the following disclaimer in the
1329bfcb3aSEmmanuel Vadot * documentation and/or other materials provided with the distribution.
1429bfcb3aSEmmanuel Vadot *
1529bfcb3aSEmmanuel Vadot * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1629bfcb3aSEmmanuel Vadot * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1729bfcb3aSEmmanuel Vadot * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1829bfcb3aSEmmanuel Vadot * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1929bfcb3aSEmmanuel Vadot * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2029bfcb3aSEmmanuel Vadot * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2129bfcb3aSEmmanuel Vadot * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2229bfcb3aSEmmanuel Vadot * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2329bfcb3aSEmmanuel Vadot * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2429bfcb3aSEmmanuel Vadot * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2529bfcb3aSEmmanuel Vadot * SUCH DAMAGE.
2629bfcb3aSEmmanuel Vadot */
2729bfcb3aSEmmanuel Vadot
2829bfcb3aSEmmanuel Vadot /*
2929bfcb3aSEmmanuel Vadot * Xilinx DWC3 glue
3029bfcb3aSEmmanuel Vadot */
3129bfcb3aSEmmanuel Vadot
3229bfcb3aSEmmanuel Vadot #include <sys/param.h>
3329bfcb3aSEmmanuel Vadot #include <sys/systm.h>
3429bfcb3aSEmmanuel Vadot #include <sys/bus.h>
3529bfcb3aSEmmanuel Vadot #include <sys/rman.h>
3629bfcb3aSEmmanuel Vadot #include <sys/kernel.h>
3729bfcb3aSEmmanuel Vadot #include <sys/module.h>
3829bfcb3aSEmmanuel Vadot #include <sys/gpio.h>
3929bfcb3aSEmmanuel Vadot #include <machine/bus.h>
4029bfcb3aSEmmanuel Vadot
4129bfcb3aSEmmanuel Vadot #include <dev/fdt/simplebus.h>
4229bfcb3aSEmmanuel Vadot
4329bfcb3aSEmmanuel Vadot #include <dev/fdt/fdt_common.h>
4429bfcb3aSEmmanuel Vadot #include <dev/ofw/ofw_bus.h>
4529bfcb3aSEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h>
4629bfcb3aSEmmanuel Vadot #include <dev/ofw/ofw_subr.h>
4729bfcb3aSEmmanuel Vadot
48be82b3a0SEmmanuel Vadot #include <dev/clk/clk.h>
491f469a9fSEmmanuel Vadot #include <dev/hwreset/hwreset.h>
50950a6087SEmmanuel Vadot #include <dev/phy/phy_usb.h>
5162e8ccc3SEmmanuel Vadot #include <dev/syscon/syscon.h>
5229bfcb3aSEmmanuel Vadot
5329bfcb3aSEmmanuel Vadot static struct ofw_compat_data compat_data[] = {
5429bfcb3aSEmmanuel Vadot { "xlnx,zynqmp-dwc3", 1 },
5529bfcb3aSEmmanuel Vadot { NULL, 0 }
5629bfcb3aSEmmanuel Vadot };
5729bfcb3aSEmmanuel Vadot
5829bfcb3aSEmmanuel Vadot struct xlnx_dwc3_softc {
5929bfcb3aSEmmanuel Vadot struct simplebus_softc sc;
6029bfcb3aSEmmanuel Vadot device_t dev;
6129bfcb3aSEmmanuel Vadot hwreset_t rst_crst;
6229bfcb3aSEmmanuel Vadot hwreset_t rst_hibrst;
6329bfcb3aSEmmanuel Vadot hwreset_t rst_apbrst;
6429bfcb3aSEmmanuel Vadot };
6529bfcb3aSEmmanuel Vadot
6629bfcb3aSEmmanuel Vadot static int
xlnx_dwc3_probe(device_t dev)6729bfcb3aSEmmanuel Vadot xlnx_dwc3_probe(device_t dev)
6829bfcb3aSEmmanuel Vadot {
6929bfcb3aSEmmanuel Vadot phandle_t node;
7029bfcb3aSEmmanuel Vadot
7129bfcb3aSEmmanuel Vadot if (!ofw_bus_status_okay(dev))
7229bfcb3aSEmmanuel Vadot return (ENXIO);
7329bfcb3aSEmmanuel Vadot
7429bfcb3aSEmmanuel Vadot if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
7529bfcb3aSEmmanuel Vadot return (ENXIO);
7629bfcb3aSEmmanuel Vadot
7729bfcb3aSEmmanuel Vadot /* Binding says that we need a child node for the actual dwc3 controller */
7829bfcb3aSEmmanuel Vadot node = ofw_bus_get_node(dev);
7929bfcb3aSEmmanuel Vadot if (OF_child(node) <= 0)
8029bfcb3aSEmmanuel Vadot return (ENXIO);
8129bfcb3aSEmmanuel Vadot
8229bfcb3aSEmmanuel Vadot device_set_desc(dev, "Xilinx ZYNQMP DWC3");
8329bfcb3aSEmmanuel Vadot return (BUS_PROBE_DEFAULT);
8429bfcb3aSEmmanuel Vadot }
8529bfcb3aSEmmanuel Vadot
8629bfcb3aSEmmanuel Vadot static int
xlnx_dwc3_attach(device_t dev)8729bfcb3aSEmmanuel Vadot xlnx_dwc3_attach(device_t dev)
8829bfcb3aSEmmanuel Vadot {
8929bfcb3aSEmmanuel Vadot struct xlnx_dwc3_softc *sc;
9029bfcb3aSEmmanuel Vadot device_t cdev;
9129bfcb3aSEmmanuel Vadot phandle_t node, child;
9229bfcb3aSEmmanuel Vadot
9329bfcb3aSEmmanuel Vadot sc = device_get_softc(dev);
9429bfcb3aSEmmanuel Vadot sc->dev = dev;
9529bfcb3aSEmmanuel Vadot node = ofw_bus_get_node(dev);
9629bfcb3aSEmmanuel Vadot
9729bfcb3aSEmmanuel Vadot /*
9829bfcb3aSEmmanuel Vadot * Put module out of reset
9929bfcb3aSEmmanuel Vadot * Based on the bindings this should be mandatory to have
10029bfcb3aSEmmanuel Vadot * but reality shows that they aren't always there.
10129bfcb3aSEmmanuel Vadot * This is the case on the DTB in the AVnet Ultra96
10229bfcb3aSEmmanuel Vadot */
10329bfcb3aSEmmanuel Vadot if (hwreset_get_by_ofw_name(dev, node, "usb_crst", &sc->rst_crst) == 0) {
10429bfcb3aSEmmanuel Vadot if (hwreset_deassert(sc->rst_crst) != 0) {
10529bfcb3aSEmmanuel Vadot device_printf(dev, "Cannot deassert reset\n");
10629bfcb3aSEmmanuel Vadot return (ENXIO);
10729bfcb3aSEmmanuel Vadot }
10829bfcb3aSEmmanuel Vadot }
10929bfcb3aSEmmanuel Vadot if (hwreset_get_by_ofw_name(dev, node, "usb_hibrst", &sc->rst_hibrst) == 0) {
1105fb94d0eSEmmanuel Vadot if (hwreset_deassert(sc->rst_hibrst) != 0) {
11129bfcb3aSEmmanuel Vadot device_printf(dev, "Cannot deassert reset\n");
11229bfcb3aSEmmanuel Vadot return (ENXIO);
11329bfcb3aSEmmanuel Vadot }
11429bfcb3aSEmmanuel Vadot }
11529bfcb3aSEmmanuel Vadot if (hwreset_get_by_ofw_name(dev, node, "usb_apbrst", &sc->rst_apbrst) == 0) {
1165fb94d0eSEmmanuel Vadot if (hwreset_deassert(sc->rst_apbrst) != 0) {
11729bfcb3aSEmmanuel Vadot device_printf(dev, "Cannot deassert reset\n");
11829bfcb3aSEmmanuel Vadot return (ENXIO);
11929bfcb3aSEmmanuel Vadot }
12029bfcb3aSEmmanuel Vadot }
12129bfcb3aSEmmanuel Vadot
12229bfcb3aSEmmanuel Vadot simplebus_init(dev, node);
12329bfcb3aSEmmanuel Vadot if (simplebus_fill_ranges(node, &sc->sc) < 0) {
12429bfcb3aSEmmanuel Vadot device_printf(dev, "could not get ranges\n");
12529bfcb3aSEmmanuel Vadot return (ENXIO);
12629bfcb3aSEmmanuel Vadot }
12729bfcb3aSEmmanuel Vadot
12829bfcb3aSEmmanuel Vadot for (child = OF_child(node); child > 0; child = OF_peer(child)) {
12929bfcb3aSEmmanuel Vadot cdev = simplebus_add_device(dev, child, 0, NULL, -1, NULL);
13029bfcb3aSEmmanuel Vadot if (cdev != NULL)
13129bfcb3aSEmmanuel Vadot device_probe_and_attach(cdev);
13229bfcb3aSEmmanuel Vadot }
13329bfcb3aSEmmanuel Vadot
134*18250ec6SJohn Baldwin bus_attach_children(dev);
135*18250ec6SJohn Baldwin return (0);
13629bfcb3aSEmmanuel Vadot }
13729bfcb3aSEmmanuel Vadot
13829bfcb3aSEmmanuel Vadot static device_method_t xlnx_dwc3_methods[] = {
13929bfcb3aSEmmanuel Vadot /* Device interface */
14029bfcb3aSEmmanuel Vadot DEVMETHOD(device_probe, xlnx_dwc3_probe),
14129bfcb3aSEmmanuel Vadot DEVMETHOD(device_attach, xlnx_dwc3_attach),
14229bfcb3aSEmmanuel Vadot
14329bfcb3aSEmmanuel Vadot DEVMETHOD_END
14429bfcb3aSEmmanuel Vadot };
14529bfcb3aSEmmanuel Vadot
14629bfcb3aSEmmanuel Vadot DEFINE_CLASS_1(xlnx_dwc3, xlnx_dwc3_driver, xlnx_dwc3_methods,
14729bfcb3aSEmmanuel Vadot sizeof(struct xlnx_dwc3_softc), simplebus_driver);
14829bfcb3aSEmmanuel Vadot DRIVER_MODULE(xlnx_dwc3, simplebus, xlnx_dwc3_driver, 0, 0);
149