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/cdefs.h>
3329bfcb3aSEmmanuel Vadot
3429bfcb3aSEmmanuel Vadot #include <sys/param.h>
3529bfcb3aSEmmanuel Vadot #include <sys/systm.h>
3629bfcb3aSEmmanuel Vadot #include <sys/bus.h>
3729bfcb3aSEmmanuel Vadot #include <sys/rman.h>
3829bfcb3aSEmmanuel Vadot #include <sys/kernel.h>
3929bfcb3aSEmmanuel Vadot #include <sys/module.h>
4029bfcb3aSEmmanuel Vadot #include <sys/gpio.h>
4129bfcb3aSEmmanuel Vadot #include <machine/bus.h>
4229bfcb3aSEmmanuel Vadot
4329bfcb3aSEmmanuel Vadot #include <dev/fdt/simplebus.h>
4429bfcb3aSEmmanuel Vadot
4529bfcb3aSEmmanuel Vadot #include <dev/fdt/fdt_common.h>
4629bfcb3aSEmmanuel Vadot #include <dev/ofw/ofw_bus.h>
4729bfcb3aSEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h>
4829bfcb3aSEmmanuel Vadot #include <dev/ofw/ofw_subr.h>
4929bfcb3aSEmmanuel Vadot
50be82b3a0SEmmanuel Vadot #include <dev/clk/clk.h>
511f469a9fSEmmanuel Vadot #include <dev/hwreset/hwreset.h>
52950a6087SEmmanuel Vadot #include <dev/phy/phy_usb.h>
5362e8ccc3SEmmanuel Vadot #include <dev/syscon/syscon.h>
5429bfcb3aSEmmanuel Vadot
5529bfcb3aSEmmanuel Vadot static struct ofw_compat_data compat_data[] = {
5629bfcb3aSEmmanuel Vadot { "xlnx,zynqmp-dwc3", 1 },
5729bfcb3aSEmmanuel Vadot { NULL, 0 }
5829bfcb3aSEmmanuel Vadot };
5929bfcb3aSEmmanuel Vadot
6029bfcb3aSEmmanuel Vadot struct xlnx_dwc3_softc {
6129bfcb3aSEmmanuel Vadot struct simplebus_softc sc;
6229bfcb3aSEmmanuel Vadot device_t dev;
6329bfcb3aSEmmanuel Vadot hwreset_t rst_crst;
6429bfcb3aSEmmanuel Vadot hwreset_t rst_hibrst;
6529bfcb3aSEmmanuel Vadot hwreset_t rst_apbrst;
6629bfcb3aSEmmanuel Vadot };
6729bfcb3aSEmmanuel Vadot
6829bfcb3aSEmmanuel Vadot static int
xlnx_dwc3_probe(device_t dev)6929bfcb3aSEmmanuel Vadot xlnx_dwc3_probe(device_t dev)
7029bfcb3aSEmmanuel Vadot {
7129bfcb3aSEmmanuel Vadot phandle_t node;
7229bfcb3aSEmmanuel Vadot
7329bfcb3aSEmmanuel Vadot if (!ofw_bus_status_okay(dev))
7429bfcb3aSEmmanuel Vadot return (ENXIO);
7529bfcb3aSEmmanuel Vadot
7629bfcb3aSEmmanuel Vadot if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
7729bfcb3aSEmmanuel Vadot return (ENXIO);
7829bfcb3aSEmmanuel Vadot
7929bfcb3aSEmmanuel Vadot /* Binding says that we need a child node for the actual dwc3 controller */
8029bfcb3aSEmmanuel Vadot node = ofw_bus_get_node(dev);
8129bfcb3aSEmmanuel Vadot if (OF_child(node) <= 0)
8229bfcb3aSEmmanuel Vadot return (ENXIO);
8329bfcb3aSEmmanuel Vadot
8429bfcb3aSEmmanuel Vadot device_set_desc(dev, "Xilinx ZYNQMP DWC3");
8529bfcb3aSEmmanuel Vadot return (BUS_PROBE_DEFAULT);
8629bfcb3aSEmmanuel Vadot }
8729bfcb3aSEmmanuel Vadot
8829bfcb3aSEmmanuel Vadot static int
xlnx_dwc3_attach(device_t dev)8929bfcb3aSEmmanuel Vadot xlnx_dwc3_attach(device_t dev)
9029bfcb3aSEmmanuel Vadot {
9129bfcb3aSEmmanuel Vadot struct xlnx_dwc3_softc *sc;
9229bfcb3aSEmmanuel Vadot device_t cdev;
9329bfcb3aSEmmanuel Vadot phandle_t node, child;
9429bfcb3aSEmmanuel Vadot
9529bfcb3aSEmmanuel Vadot sc = device_get_softc(dev);
9629bfcb3aSEmmanuel Vadot sc->dev = dev;
9729bfcb3aSEmmanuel Vadot node = ofw_bus_get_node(dev);
9829bfcb3aSEmmanuel Vadot
9929bfcb3aSEmmanuel Vadot /*
10029bfcb3aSEmmanuel Vadot * Put module out of reset
10129bfcb3aSEmmanuel Vadot * Based on the bindings this should be mandatory to have
10229bfcb3aSEmmanuel Vadot * but reality shows that they aren't always there.
10329bfcb3aSEmmanuel Vadot * This is the case on the DTB in the AVnet Ultra96
10429bfcb3aSEmmanuel Vadot */
10529bfcb3aSEmmanuel Vadot if (hwreset_get_by_ofw_name(dev, node, "usb_crst", &sc->rst_crst) == 0) {
10629bfcb3aSEmmanuel Vadot if (hwreset_deassert(sc->rst_crst) != 0) {
10729bfcb3aSEmmanuel Vadot device_printf(dev, "Cannot deassert reset\n");
10829bfcb3aSEmmanuel Vadot return (ENXIO);
10929bfcb3aSEmmanuel Vadot }
11029bfcb3aSEmmanuel Vadot }
11129bfcb3aSEmmanuel Vadot if (hwreset_get_by_ofw_name(dev, node, "usb_hibrst", &sc->rst_hibrst) == 0) {
1125fb94d0eSEmmanuel Vadot if (hwreset_deassert(sc->rst_hibrst) != 0) {
11329bfcb3aSEmmanuel Vadot device_printf(dev, "Cannot deassert reset\n");
11429bfcb3aSEmmanuel Vadot return (ENXIO);
11529bfcb3aSEmmanuel Vadot }
11629bfcb3aSEmmanuel Vadot }
11729bfcb3aSEmmanuel Vadot if (hwreset_get_by_ofw_name(dev, node, "usb_apbrst", &sc->rst_apbrst) == 0) {
1185fb94d0eSEmmanuel Vadot if (hwreset_deassert(sc->rst_apbrst) != 0) {
11929bfcb3aSEmmanuel Vadot device_printf(dev, "Cannot deassert reset\n");
12029bfcb3aSEmmanuel Vadot return (ENXIO);
12129bfcb3aSEmmanuel Vadot }
12229bfcb3aSEmmanuel Vadot }
12329bfcb3aSEmmanuel Vadot
12429bfcb3aSEmmanuel Vadot simplebus_init(dev, node);
12529bfcb3aSEmmanuel Vadot if (simplebus_fill_ranges(node, &sc->sc) < 0) {
12629bfcb3aSEmmanuel Vadot device_printf(dev, "could not get ranges\n");
12729bfcb3aSEmmanuel Vadot return (ENXIO);
12829bfcb3aSEmmanuel Vadot }
12929bfcb3aSEmmanuel Vadot
13029bfcb3aSEmmanuel Vadot for (child = OF_child(node); child > 0; child = OF_peer(child)) {
13129bfcb3aSEmmanuel Vadot cdev = simplebus_add_device(dev, child, 0, NULL, -1, NULL);
13229bfcb3aSEmmanuel Vadot if (cdev != NULL)
13329bfcb3aSEmmanuel Vadot device_probe_and_attach(cdev);
13429bfcb3aSEmmanuel Vadot }
13529bfcb3aSEmmanuel Vadot
136*18250ec6SJohn Baldwin bus_attach_children(dev);
137*18250ec6SJohn Baldwin return (0);
13829bfcb3aSEmmanuel Vadot }
13929bfcb3aSEmmanuel Vadot
14029bfcb3aSEmmanuel Vadot static device_method_t xlnx_dwc3_methods[] = {
14129bfcb3aSEmmanuel Vadot /* Device interface */
14229bfcb3aSEmmanuel Vadot DEVMETHOD(device_probe, xlnx_dwc3_probe),
14329bfcb3aSEmmanuel Vadot DEVMETHOD(device_attach, xlnx_dwc3_attach),
14429bfcb3aSEmmanuel Vadot
14529bfcb3aSEmmanuel Vadot DEVMETHOD_END
14629bfcb3aSEmmanuel Vadot };
14729bfcb3aSEmmanuel Vadot
14829bfcb3aSEmmanuel Vadot DEFINE_CLASS_1(xlnx_dwc3, xlnx_dwc3_driver, xlnx_dwc3_methods,
14929bfcb3aSEmmanuel Vadot sizeof(struct xlnx_dwc3_softc), simplebus_driver);
15029bfcb3aSEmmanuel Vadot DRIVER_MODULE(xlnx_dwc3, simplebus, xlnx_dwc3_driver, 0, 0);
151