1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2019 Emmanuel Vadot <[email protected]>
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/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/bus.h>
38 #include <sys/rman.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
41 #include <sys/gpio.h>
42 #include <machine/bus.h>
43
44 #include <dev/fdt/simplebus.h>
45
46 #include <dev/fdt/fdt_common.h>
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/ofw_bus_subr.h>
49 #include <dev/ofw/ofw_subr.h>
50
51 #include <dev/extres/clk/clk.h>
52 #include <dev/extres/hwreset/hwreset.h>
53 #include <dev/extres/phy/phy_usb.h>
54 #include <dev/extres/syscon/syscon.h>
55
56 enum rk_dwc3_type {
57 RK3328 = 1,
58 RK3399,
59 };
60
61 static struct ofw_compat_data compat_data[] = {
62 { "rockchip,rk3328-dwc3", RK3328 },
63 { "rockchip,rk3399-dwc3", RK3399 },
64 { NULL, 0 }
65 };
66
67 struct rk_dwc3_softc {
68 struct simplebus_softc sc;
69 device_t dev;
70 clk_t clk_ref;
71 clk_t clk_suspend;
72 clk_t clk_bus;
73 clk_t clk_axi_perf;
74 clk_t clk_usb3;
75 clk_t clk_grf;
76 hwreset_t rst_usb3;
77 enum rk_dwc3_type type;
78 };
79
80 static int
rk_dwc3_probe(device_t dev)81 rk_dwc3_probe(device_t dev)
82 {
83 phandle_t node;
84
85 if (!ofw_bus_status_okay(dev))
86 return (ENXIO);
87
88 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
89 return (ENXIO);
90
91 /* Binding says that we need a child node for the actual dwc3 controller */
92 node = ofw_bus_get_node(dev);
93 if (OF_child(node) <= 0)
94 return (ENXIO);
95
96 device_set_desc(dev, "Rockchip RK3399 DWC3");
97 return (BUS_PROBE_DEFAULT);
98 }
99
100 static int
rk_dwc3_attach(device_t dev)101 rk_dwc3_attach(device_t dev)
102 {
103 struct rk_dwc3_softc *sc;
104 device_t cdev;
105 phandle_t node, child;
106 int err;
107
108 sc = device_get_softc(dev);
109 sc->dev = dev;
110 node = ofw_bus_get_node(dev);
111 sc->type = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
112
113 /* Mandatory clocks */
114 if (clk_get_by_ofw_name(dev, 0, "ref_clk", &sc->clk_ref) != 0) {
115 device_printf(dev, "Cannot get ref_clk clock\n");
116 return (ENXIO);
117 }
118 err = clk_enable(sc->clk_ref);
119 if (err != 0) {
120 device_printf(dev, "Could not enable clock %s\n",
121 clk_get_name(sc->clk_ref));
122 return (ENXIO);
123 }
124 if (clk_get_by_ofw_name(dev, 0, "suspend_clk", &sc->clk_suspend) != 0) {
125 device_printf(dev, "Cannot get suspend_clk clock\n");
126 return (ENXIO);
127 }
128 err = clk_enable(sc->clk_suspend);
129 if (err != 0) {
130 device_printf(dev, "Could not enable clock %s\n",
131 clk_get_name(sc->clk_suspend));
132 return (ENXIO);
133 }
134 if (clk_get_by_ofw_name(dev, 0, "bus_clk", &sc->clk_bus) != 0) {
135 device_printf(dev, "Cannot get bus_clk clock\n");
136 return (ENXIO);
137 }
138 err = clk_enable(sc->clk_bus);
139 if (err != 0) {
140 device_printf(dev, "Could not enable clock %s\n",
141 clk_get_name(sc->clk_bus));
142 return (ENXIO);
143 }
144 if (sc->type == RK3399) {
145 if (clk_get_by_ofw_name(dev, 0, "grf_clk", &sc->clk_grf) != 0) {
146 device_printf(dev, "Cannot get grf_clk clock\n");
147 return (ENXIO);
148 }
149 err = clk_enable(sc->clk_grf);
150 if (err != 0) {
151 device_printf(dev, "Could not enable clock %s\n",
152 clk_get_name(sc->clk_grf));
153 return (ENXIO);
154 }
155 }
156 /* Optional clocks */
157 if (clk_get_by_ofw_name(dev, 0, "aclk_usb3_rksoc_axi_perf", &sc->clk_axi_perf) == 0) {
158 err = clk_enable(sc->clk_axi_perf);
159 if (err != 0) {
160 device_printf(dev, "Could not enable clock %s\n",
161 clk_get_name(sc->clk_axi_perf));
162 return (ENXIO);
163 }
164 }
165 if (clk_get_by_ofw_name(dev, 0, "aclk_usb3", &sc->clk_usb3) == 0) {
166 err = clk_enable(sc->clk_usb3);
167 if (err != 0) {
168 device_printf(dev, "Could not enable clock %s\n",
169 clk_get_name(sc->clk_usb3));
170 return (ENXIO);
171 }
172 }
173
174 /* Put module out of reset */
175 if (hwreset_get_by_ofw_name(dev, node, "usb3-otg", &sc->rst_usb3) == 0) {
176 if (hwreset_deassert(sc->rst_usb3) != 0) {
177 device_printf(dev, "Cannot deassert reset\n");
178 return (ENXIO);
179 }
180 }
181
182 simplebus_init(dev, node);
183 if (simplebus_fill_ranges(node, &sc->sc) < 0) {
184 device_printf(dev, "could not get ranges\n");
185 return (ENXIO);
186 }
187
188 for (child = OF_child(node); child > 0; child = OF_peer(child)) {
189 cdev = simplebus_add_device(dev, child, 0, NULL, -1, NULL);
190 if (cdev != NULL)
191 device_probe_and_attach(cdev);
192 }
193
194 return (bus_generic_attach(dev));
195 }
196
197 static device_method_t rk_dwc3_methods[] = {
198 /* Device interface */
199 DEVMETHOD(device_probe, rk_dwc3_probe),
200 DEVMETHOD(device_attach, rk_dwc3_attach),
201
202 DEVMETHOD_END
203 };
204
205 static devclass_t rk_dwc3_devclass;
206
207 DEFINE_CLASS_1(rk_dwc3, rk_dwc3_driver, rk_dwc3_methods,
208 sizeof(struct rk_dwc3_softc), simplebus_driver);
209 DRIVER_MODULE(rk_dwc3, simplebus, rk_dwc3_driver, rk_dwc3_devclass, 0, 0);
210