xref: /freebsd/sys/dev/usb/controller/dwc3/aw_dwc3.c (revision 18250ec6c089c0c50cbd9fd87d78e03ff89916df)
1fe75646aSEmmanuel Vadot /*-
2fe75646aSEmmanuel Vadot  * SPDX-License-Identifier: BSD-2-Clause
3fe75646aSEmmanuel Vadot  *
4fe75646aSEmmanuel Vadot  * Copyright (c) 2019 Emmanuel Vadot <manu@FreeBSD.Org>
5fe75646aSEmmanuel Vadot  *
6fe75646aSEmmanuel Vadot  * Redistribution and use in source and binary forms, with or without
7fe75646aSEmmanuel Vadot  * modification, are permitted provided that the following conditions
8fe75646aSEmmanuel Vadot  * are met:
9fe75646aSEmmanuel Vadot  * 1. Redistributions of source code must retain the above copyright
10fe75646aSEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer.
11fe75646aSEmmanuel Vadot  * 2. Redistributions in binary form must reproduce the above copyright
12fe75646aSEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer in the
13fe75646aSEmmanuel Vadot  *    documentation and/or other materials provided with the distribution.
14fe75646aSEmmanuel Vadot  *
15fe75646aSEmmanuel Vadot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16fe75646aSEmmanuel Vadot  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17fe75646aSEmmanuel Vadot  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18fe75646aSEmmanuel Vadot  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19fe75646aSEmmanuel Vadot  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20fe75646aSEmmanuel Vadot  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21fe75646aSEmmanuel Vadot  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22fe75646aSEmmanuel Vadot  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23fe75646aSEmmanuel Vadot  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24fe75646aSEmmanuel Vadot  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25fe75646aSEmmanuel Vadot  * SUCH DAMAGE.
26fe75646aSEmmanuel Vadot  */
27fe75646aSEmmanuel Vadot 
28fe75646aSEmmanuel Vadot /*
29fe75646aSEmmanuel Vadot  * Rockchip DWC3 glue
30fe75646aSEmmanuel Vadot  */
31fe75646aSEmmanuel Vadot 
32fe75646aSEmmanuel Vadot #include <sys/param.h>
33fe75646aSEmmanuel Vadot #include <sys/systm.h>
34fe75646aSEmmanuel Vadot #include <sys/bus.h>
35fe75646aSEmmanuel Vadot #include <sys/rman.h>
36fe75646aSEmmanuel Vadot #include <sys/kernel.h>
37fe75646aSEmmanuel Vadot #include <sys/module.h>
38fe75646aSEmmanuel Vadot #include <sys/gpio.h>
39fe75646aSEmmanuel Vadot #include <machine/bus.h>
40fe75646aSEmmanuel Vadot 
41fe75646aSEmmanuel Vadot #include <dev/fdt/simplebus.h>
42fe75646aSEmmanuel Vadot 
43fe75646aSEmmanuel Vadot #include <dev/fdt/fdt_common.h>
44fe75646aSEmmanuel Vadot #include <dev/ofw/ofw_bus.h>
45fe75646aSEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h>
46fe75646aSEmmanuel Vadot #include <dev/ofw/ofw_subr.h>
47fe75646aSEmmanuel Vadot 
48be82b3a0SEmmanuel Vadot #include <dev/clk/clk.h>
491f469a9fSEmmanuel Vadot #include <dev/hwreset/hwreset.h>
50950a6087SEmmanuel Vadot #include <dev/phy/phy_usb.h>
51fe75646aSEmmanuel Vadot 
52fe75646aSEmmanuel Vadot static struct ofw_compat_data compat_data[] = {
53fe75646aSEmmanuel Vadot 	{ "allwinner,sun50i-h6-dwc3",	1 },
54fe75646aSEmmanuel Vadot 	{ NULL,				0 }
55fe75646aSEmmanuel Vadot };
56fe75646aSEmmanuel Vadot 
57fe75646aSEmmanuel Vadot struct aw_dwc3_softc {
58fe75646aSEmmanuel Vadot 	struct simplebus_softc	sc;
59fe75646aSEmmanuel Vadot 	device_t		dev;
60fe75646aSEmmanuel Vadot 	clk_t			clk_bus;
61fe75646aSEmmanuel Vadot 	hwreset_t		rst_bus;
62fe75646aSEmmanuel Vadot };
63fe75646aSEmmanuel Vadot 
64fe75646aSEmmanuel Vadot static int
aw_dwc3_probe(device_t dev)65fe75646aSEmmanuel Vadot aw_dwc3_probe(device_t dev)
66fe75646aSEmmanuel Vadot {
67fe75646aSEmmanuel Vadot 	phandle_t node;
68fe75646aSEmmanuel Vadot 
69fe75646aSEmmanuel Vadot 	if (!ofw_bus_status_okay(dev))
70fe75646aSEmmanuel Vadot 		return (ENXIO);
71fe75646aSEmmanuel Vadot 
72fe75646aSEmmanuel Vadot 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
73fe75646aSEmmanuel Vadot 		return (ENXIO);
74fe75646aSEmmanuel Vadot 
75fe75646aSEmmanuel Vadot 	/* Binding says that we need a child node for the actual dwc3 controller */
76fe75646aSEmmanuel Vadot 	node = ofw_bus_get_node(dev);
77fe75646aSEmmanuel Vadot 	if (OF_child(node) <= 0)
78fe75646aSEmmanuel Vadot 		return (ENXIO);
79fe75646aSEmmanuel Vadot 
80fe75646aSEmmanuel Vadot 	device_set_desc(dev, "Allwinner H6 DWC3");
81fe75646aSEmmanuel Vadot 	return (BUS_PROBE_DEFAULT);
82fe75646aSEmmanuel Vadot }
83fe75646aSEmmanuel Vadot 
84fe75646aSEmmanuel Vadot static int
aw_dwc3_attach(device_t dev)85fe75646aSEmmanuel Vadot aw_dwc3_attach(device_t dev)
86fe75646aSEmmanuel Vadot {
87fe75646aSEmmanuel Vadot 	struct aw_dwc3_softc *sc;
88fe75646aSEmmanuel Vadot 	device_t cdev;
89fe75646aSEmmanuel Vadot 	phandle_t node, child;
90fe75646aSEmmanuel Vadot 	int err;
91fe75646aSEmmanuel Vadot 
92fe75646aSEmmanuel Vadot 	sc = device_get_softc(dev);
93fe75646aSEmmanuel Vadot 	sc->dev = dev;
94fe75646aSEmmanuel Vadot 	node = ofw_bus_get_node(dev);
95fe75646aSEmmanuel Vadot 
96fe75646aSEmmanuel Vadot 	/* Enable the clocks */
97fe75646aSEmmanuel Vadot 	if (clk_get_by_ofw_name(dev, 0, "bus", &sc->clk_bus) != 0) {
98fe75646aSEmmanuel Vadot 		device_printf(dev, "Cannot get bus clock\n");
99fe75646aSEmmanuel Vadot 		return (ENXIO);
100fe75646aSEmmanuel Vadot 	}
101fe75646aSEmmanuel Vadot 	err = clk_enable(sc->clk_bus);
102fe75646aSEmmanuel Vadot 	if (err != 0) {
103fe75646aSEmmanuel Vadot 		device_printf(dev, "Could not enable clock %s\n",
104fe75646aSEmmanuel Vadot 		    clk_get_name(sc->clk_bus));
105fe75646aSEmmanuel Vadot 		return (ENXIO);
106fe75646aSEmmanuel Vadot 	}
107fe75646aSEmmanuel Vadot 
108fe75646aSEmmanuel Vadot 	/* Put module out of reset */
109fe75646aSEmmanuel Vadot 	if (hwreset_get_by_ofw_name(dev, node, "bus", &sc->rst_bus) == 0) {
110fe75646aSEmmanuel Vadot 		if (hwreset_deassert(sc->rst_bus) != 0) {
111fe75646aSEmmanuel Vadot 			device_printf(dev, "Cannot deassert reset\n");
112fe75646aSEmmanuel Vadot 			return (ENXIO);
113fe75646aSEmmanuel Vadot 		}
114fe75646aSEmmanuel Vadot 	}
115fe75646aSEmmanuel Vadot 
116fe75646aSEmmanuel Vadot 	simplebus_init(dev, node);
117fe75646aSEmmanuel Vadot 	if (simplebus_fill_ranges(node, &sc->sc) < 0) {
118fe75646aSEmmanuel Vadot 		device_printf(dev, "could not get ranges\n");
119fe75646aSEmmanuel Vadot 		return (ENXIO);
120fe75646aSEmmanuel Vadot 	}
121fe75646aSEmmanuel Vadot 
122fe75646aSEmmanuel Vadot 	for (child = OF_child(node); child > 0; child = OF_peer(child)) {
123fe75646aSEmmanuel Vadot 		cdev = simplebus_add_device(dev, child, 0, NULL, -1, NULL);
124fe75646aSEmmanuel Vadot 		if (cdev != NULL)
125fe75646aSEmmanuel Vadot 			device_probe_and_attach(cdev);
126fe75646aSEmmanuel Vadot 	}
127fe75646aSEmmanuel Vadot 
128*18250ec6SJohn Baldwin 	bus_attach_children(dev);
129*18250ec6SJohn Baldwin 	return (0);
130fe75646aSEmmanuel Vadot }
131fe75646aSEmmanuel Vadot 
132fe75646aSEmmanuel Vadot static device_method_t aw_dwc3_methods[] = {
133fe75646aSEmmanuel Vadot 	/* Device interface */
134fe75646aSEmmanuel Vadot 	DEVMETHOD(device_probe,		aw_dwc3_probe),
135fe75646aSEmmanuel Vadot 	DEVMETHOD(device_attach,	aw_dwc3_attach),
136fe75646aSEmmanuel Vadot 
137fe75646aSEmmanuel Vadot 	DEVMETHOD_END
138fe75646aSEmmanuel Vadot };
139fe75646aSEmmanuel Vadot 
140fe75646aSEmmanuel Vadot DEFINE_CLASS_1(aw_dwc3, aw_dwc3_driver, aw_dwc3_methods,
141fe75646aSEmmanuel Vadot     sizeof(struct aw_dwc3_softc), simplebus_driver);
142fe75646aSEmmanuel Vadot DRIVER_MODULE(aw_dwc3, simplebus, aw_dwc3_driver, 0, 0);
143