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