1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2022 Beckhoff Automation GmbH & Co. KG 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 /* 29 * Xilinx DWC3 glue 30 */ 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/bus.h> 35 #include <sys/rman.h> 36 #include <sys/kernel.h> 37 #include <sys/module.h> 38 #include <sys/gpio.h> 39 #include <machine/bus.h> 40 41 #include <dev/fdt/simplebus.h> 42 43 #include <dev/fdt/fdt_common.h> 44 #include <dev/ofw/ofw_bus.h> 45 #include <dev/ofw/ofw_bus_subr.h> 46 #include <dev/ofw/ofw_subr.h> 47 48 #include <dev/clk/clk.h> 49 #include <dev/hwreset/hwreset.h> 50 #include <dev/phy/phy_usb.h> 51 #include <dev/syscon/syscon.h> 52 53 static struct ofw_compat_data compat_data[] = { 54 { "xlnx,zynqmp-dwc3", 1 }, 55 { NULL, 0 } 56 }; 57 58 struct xlnx_dwc3_softc { 59 device_t dev; 60 hwreset_t rst_crst; 61 hwreset_t rst_hibrst; 62 hwreset_t rst_apbrst; 63 }; 64 65 static int 66 xlnx_dwc3_probe(device_t dev) 67 { 68 phandle_t node; 69 70 if (!ofw_bus_status_okay(dev)) 71 return (ENXIO); 72 73 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 74 return (ENXIO); 75 76 /* Binding says that we need a child node for the actual dwc3 controller */ 77 node = ofw_bus_get_node(dev); 78 if (OF_child(node) <= 0) 79 return (ENXIO); 80 81 device_set_desc(dev, "Xilinx ZYNQMP DWC3"); 82 return (BUS_PROBE_DEFAULT); 83 } 84 85 static int 86 xlnx_dwc3_attach(device_t dev) 87 { 88 struct xlnx_dwc3_softc *sc; 89 phandle_t node; 90 91 sc = device_get_softc(dev); 92 sc->dev = dev; 93 node = ofw_bus_get_node(dev); 94 95 /* 96 * Put module out of reset 97 * Based on the bindings this should be mandatory to have 98 * but reality shows that they aren't always there. 99 * This is the case on the DTB in the AVnet Ultra96 100 */ 101 if (hwreset_get_by_ofw_name(dev, node, "usb_crst", &sc->rst_crst) == 0) { 102 if (hwreset_deassert(sc->rst_crst) != 0) { 103 device_printf(dev, "Cannot deassert reset\n"); 104 return (ENXIO); 105 } 106 } 107 if (hwreset_get_by_ofw_name(dev, node, "usb_hibrst", &sc->rst_hibrst) == 0) { 108 if (hwreset_deassert(sc->rst_hibrst) != 0) { 109 device_printf(dev, "Cannot deassert reset\n"); 110 return (ENXIO); 111 } 112 } 113 if (hwreset_get_by_ofw_name(dev, node, "usb_apbrst", &sc->rst_apbrst) == 0) { 114 if (hwreset_deassert(sc->rst_apbrst) != 0) { 115 device_printf(dev, "Cannot deassert reset\n"); 116 return (ENXIO); 117 } 118 } 119 120 if (simplebus_attach_impl(dev, 0, node) != 0) 121 device_printf(dev, "Cannot attach simplebus\n"); 122 123 bus_attach_children(dev); 124 return (0); 125 } 126 127 static device_method_t xlnx_dwc3_methods[] = { 128 /* Device interface */ 129 DEVMETHOD(device_probe, xlnx_dwc3_probe), 130 DEVMETHOD(device_attach, xlnx_dwc3_attach), 131 132 DEVMETHOD_END 133 }; 134 135 DEFINE_CLASS_1(xlnx_dwc3, xlnx_dwc3_driver, xlnx_dwc3_methods, 136 sizeof(struct xlnx_dwc3_softc), simplebus_driver); 137 DRIVER_MODULE(xlnx_dwc3, simplebus, xlnx_dwc3_driver, 0, 0); 138