xref: /freebsd/sys/dev/usb/controller/dwc3/rk_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>
5162e8ccc3SEmmanuel Vadot #include <dev/syscon/syscon.h>
52fe75646aSEmmanuel Vadot 
53fe75646aSEmmanuel Vadot enum rk_dwc3_type {
54fe75646aSEmmanuel Vadot 	RK3399 = 1,
55fe75646aSEmmanuel Vadot };
56fe75646aSEmmanuel Vadot 
57fe75646aSEmmanuel Vadot static struct ofw_compat_data compat_data[] = {
58fe75646aSEmmanuel Vadot 	{ "rockchip,rk3399-dwc3",	RK3399 },
59fe75646aSEmmanuel Vadot 	{ NULL,				0 }
60fe75646aSEmmanuel Vadot };
61fe75646aSEmmanuel Vadot 
62fe75646aSEmmanuel Vadot struct rk_dwc3_softc {
63fe75646aSEmmanuel Vadot 	struct simplebus_softc	sc;
64fe75646aSEmmanuel Vadot 	device_t		dev;
65fe75646aSEmmanuel Vadot 	clk_t			clk_ref;
66fe75646aSEmmanuel Vadot 	clk_t			clk_suspend;
67fe75646aSEmmanuel Vadot 	clk_t			clk_bus;
68fe75646aSEmmanuel Vadot 	clk_t			clk_axi_perf;
69fe75646aSEmmanuel Vadot 	clk_t			clk_usb3;
70fe75646aSEmmanuel Vadot 	clk_t			clk_grf;
71fe75646aSEmmanuel Vadot 	hwreset_t		rst_usb3;
72fe75646aSEmmanuel Vadot 	enum rk_dwc3_type	type;
73fe75646aSEmmanuel Vadot };
74fe75646aSEmmanuel Vadot 
75fe75646aSEmmanuel Vadot static int
rk_dwc3_probe(device_t dev)76fe75646aSEmmanuel Vadot rk_dwc3_probe(device_t dev)
77fe75646aSEmmanuel Vadot {
78fe75646aSEmmanuel Vadot 	phandle_t node;
79fe75646aSEmmanuel Vadot 
80fe75646aSEmmanuel Vadot 	if (!ofw_bus_status_okay(dev))
81fe75646aSEmmanuel Vadot 		return (ENXIO);
82fe75646aSEmmanuel Vadot 
83fe75646aSEmmanuel Vadot 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
84fe75646aSEmmanuel Vadot 		return (ENXIO);
85fe75646aSEmmanuel Vadot 
86fe75646aSEmmanuel Vadot 	/* Binding says that we need a child node for the actual dwc3 controller */
87fe75646aSEmmanuel Vadot 	node = ofw_bus_get_node(dev);
88fe75646aSEmmanuel Vadot 	if (OF_child(node) <= 0)
89fe75646aSEmmanuel Vadot 		return (ENXIO);
90fe75646aSEmmanuel Vadot 
91fe75646aSEmmanuel Vadot 	device_set_desc(dev, "Rockchip RK3399 DWC3");
92fe75646aSEmmanuel Vadot 	return (BUS_PROBE_DEFAULT);
93fe75646aSEmmanuel Vadot }
94fe75646aSEmmanuel Vadot 
95fe75646aSEmmanuel Vadot static int
rk_dwc3_attach(device_t dev)96fe75646aSEmmanuel Vadot rk_dwc3_attach(device_t dev)
97fe75646aSEmmanuel Vadot {
98fe75646aSEmmanuel Vadot 	struct rk_dwc3_softc *sc;
99fe75646aSEmmanuel Vadot 	device_t cdev;
100fe75646aSEmmanuel Vadot 	phandle_t node, child;
101fe75646aSEmmanuel Vadot 	int err;
102fe75646aSEmmanuel Vadot 
103fe75646aSEmmanuel Vadot 	sc = device_get_softc(dev);
104fe75646aSEmmanuel Vadot 	sc->dev = dev;
105fe75646aSEmmanuel Vadot 	node = ofw_bus_get_node(dev);
106fe75646aSEmmanuel Vadot 	sc->type = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
107fe75646aSEmmanuel Vadot 
108fe75646aSEmmanuel Vadot 	/* Mandatory clocks */
109fe75646aSEmmanuel Vadot 	if (clk_get_by_ofw_name(dev, 0, "ref_clk", &sc->clk_ref) != 0) {
110fe75646aSEmmanuel Vadot 		device_printf(dev, "Cannot get ref_clk clock\n");
111fe75646aSEmmanuel Vadot 		return (ENXIO);
112fe75646aSEmmanuel Vadot 	}
113fe75646aSEmmanuel Vadot 	err = clk_enable(sc->clk_ref);
114fe75646aSEmmanuel Vadot 	if (err != 0) {
115fe75646aSEmmanuel Vadot 		device_printf(dev, "Could not enable clock %s\n",
116fe75646aSEmmanuel Vadot 		    clk_get_name(sc->clk_ref));
117fe75646aSEmmanuel Vadot 		return (ENXIO);
118fe75646aSEmmanuel Vadot 	}
119fe75646aSEmmanuel Vadot 	if (clk_get_by_ofw_name(dev, 0, "suspend_clk", &sc->clk_suspend) != 0) {
120fe75646aSEmmanuel Vadot 		device_printf(dev, "Cannot get suspend_clk clock\n");
121fe75646aSEmmanuel Vadot 		return (ENXIO);
122fe75646aSEmmanuel Vadot 	}
123fe75646aSEmmanuel Vadot 	err = clk_enable(sc->clk_suspend);
124fe75646aSEmmanuel Vadot 	if (err != 0) {
125fe75646aSEmmanuel Vadot 		device_printf(dev, "Could not enable clock %s\n",
126fe75646aSEmmanuel Vadot 		    clk_get_name(sc->clk_suspend));
127fe75646aSEmmanuel Vadot 		return (ENXIO);
128fe75646aSEmmanuel Vadot 	}
129fe75646aSEmmanuel Vadot 	if (clk_get_by_ofw_name(dev, 0, "bus_clk", &sc->clk_bus) != 0) {
130fe75646aSEmmanuel Vadot 		device_printf(dev, "Cannot get bus_clk clock\n");
131fe75646aSEmmanuel Vadot 		return (ENXIO);
132fe75646aSEmmanuel Vadot 	}
133fe75646aSEmmanuel Vadot 	err = clk_enable(sc->clk_bus);
134fe75646aSEmmanuel Vadot 	if (err != 0) {
135fe75646aSEmmanuel Vadot 		device_printf(dev, "Could not enable clock %s\n",
136fe75646aSEmmanuel Vadot 		    clk_get_name(sc->clk_bus));
137fe75646aSEmmanuel Vadot 		return (ENXIO);
138fe75646aSEmmanuel Vadot 	}
139fe75646aSEmmanuel Vadot 	if (clk_get_by_ofw_name(dev, 0, "grf_clk", &sc->clk_grf) == 0) {
140fe75646aSEmmanuel Vadot 		err = clk_enable(sc->clk_grf);
141fe75646aSEmmanuel Vadot 		if (err != 0) {
142fe75646aSEmmanuel Vadot 			device_printf(dev, "Could not enable clock %s\n",
143fe75646aSEmmanuel Vadot 			    clk_get_name(sc->clk_grf));
144fe75646aSEmmanuel Vadot 			return (ENXIO);
145fe75646aSEmmanuel Vadot 		}
146fe75646aSEmmanuel Vadot 	}
147fe75646aSEmmanuel Vadot 	/* Optional clocks */
148fe75646aSEmmanuel Vadot 	if (clk_get_by_ofw_name(dev, 0, "aclk_usb3_rksoc_axi_perf", &sc->clk_axi_perf) == 0) {
149fe75646aSEmmanuel Vadot 		err = clk_enable(sc->clk_axi_perf);
150fe75646aSEmmanuel Vadot 		if (err != 0) {
151fe75646aSEmmanuel Vadot 			device_printf(dev, "Could not enable clock %s\n",
152fe75646aSEmmanuel Vadot 			  clk_get_name(sc->clk_axi_perf));
153fe75646aSEmmanuel Vadot 			return (ENXIO);
154fe75646aSEmmanuel Vadot 		}
155fe75646aSEmmanuel Vadot 	}
156fe75646aSEmmanuel Vadot 	if (clk_get_by_ofw_name(dev, 0, "aclk_usb3", &sc->clk_usb3) == 0) {
157fe75646aSEmmanuel Vadot 		err = clk_enable(sc->clk_usb3);
158fe75646aSEmmanuel Vadot 		if (err != 0) {
159fe75646aSEmmanuel Vadot 			device_printf(dev, "Could not enable clock %s\n",
160fe75646aSEmmanuel Vadot 			  clk_get_name(sc->clk_usb3));
161fe75646aSEmmanuel Vadot 			return (ENXIO);
162fe75646aSEmmanuel Vadot 		}
163fe75646aSEmmanuel Vadot 	}
164fe75646aSEmmanuel Vadot 
165fe75646aSEmmanuel Vadot 	/* Put module out of reset */
166fe75646aSEmmanuel Vadot 	if (hwreset_get_by_ofw_name(dev, node, "usb3-otg", &sc->rst_usb3) == 0) {
167fe75646aSEmmanuel Vadot 		if (hwreset_deassert(sc->rst_usb3) != 0) {
168fe75646aSEmmanuel Vadot 			device_printf(dev, "Cannot deassert reset\n");
169fe75646aSEmmanuel Vadot 			return (ENXIO);
170fe75646aSEmmanuel Vadot 		}
171fe75646aSEmmanuel Vadot 	}
172fe75646aSEmmanuel Vadot 
173fe75646aSEmmanuel Vadot 	simplebus_init(dev, node);
174fe75646aSEmmanuel Vadot 	if (simplebus_fill_ranges(node, &sc->sc) < 0) {
175fe75646aSEmmanuel Vadot 		device_printf(dev, "could not get ranges\n");
176fe75646aSEmmanuel Vadot 		return (ENXIO);
177fe75646aSEmmanuel Vadot 	}
178fe75646aSEmmanuel Vadot 
179fe75646aSEmmanuel Vadot 	for (child = OF_child(node); child > 0; child = OF_peer(child)) {
180fe75646aSEmmanuel Vadot 		cdev = simplebus_add_device(dev, child, 0, NULL, -1, NULL);
181fe75646aSEmmanuel Vadot 		if (cdev != NULL)
182fe75646aSEmmanuel Vadot 			device_probe_and_attach(cdev);
183fe75646aSEmmanuel Vadot 	}
184fe75646aSEmmanuel Vadot 
185*18250ec6SJohn Baldwin 	bus_attach_children(dev);
186*18250ec6SJohn Baldwin 	return (0);
187fe75646aSEmmanuel Vadot }
188fe75646aSEmmanuel Vadot 
189fe75646aSEmmanuel Vadot static device_method_t rk_dwc3_methods[] = {
190fe75646aSEmmanuel Vadot 	/* Device interface */
191fe75646aSEmmanuel Vadot 	DEVMETHOD(device_probe,		rk_dwc3_probe),
192fe75646aSEmmanuel Vadot 	DEVMETHOD(device_attach,	rk_dwc3_attach),
193fe75646aSEmmanuel Vadot 
194fe75646aSEmmanuel Vadot 	DEVMETHOD_END
195fe75646aSEmmanuel Vadot };
196fe75646aSEmmanuel Vadot 
197fe75646aSEmmanuel Vadot DEFINE_CLASS_1(rk_dwc3, rk_dwc3_driver, rk_dwc3_methods,
198fe75646aSEmmanuel Vadot     sizeof(struct rk_dwc3_softc), simplebus_driver);
199fe75646aSEmmanuel Vadot DRIVER_MODULE(rk_dwc3, simplebus, rk_dwc3_driver, 0, 0);
200