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