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