dwc3_fdt.c revision 1.4 1 /* $NetBSD: dwc3_fdt.c,v 1.4 2018/07/03 08:52:36 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: dwc3_fdt.c,v 1.4 2018/07/03 08:52:36 jmcneill Exp $");
31
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/device.h>
35 #include <sys/intr.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38
39 #include <dev/usb/usb.h>
40 #include <dev/usb/usbdi.h>
41 #include <dev/usb/usbdivar.h>
42 #include <dev/usb/usb_mem.h>
43 #include <dev/usb/xhcireg.h>
44 #include <dev/usb/xhcivar.h>
45
46 #include <dev/fdt/fdtvar.h>
47
48 #define DWC3_GCTL 0xc110
49 #define GCTL_PRTCAP __BITS(13,12)
50 #define GCTL_PRTCAP_HOST 1
51 #define GCTL_PRTCAP_DEVICE 2
52 #define GCTL_PRTCAP_OTG 3
53 #define GCTL_CORESOFTRESET __BIT(11)
54
55 #define DWC3_GUSB2PHYCFG(n) (0xc200 + ((n) * 4))
56 #define GUSB2PHYCFG_PHYSOFTRST __BIT(31)
57 #define GUSB2PHYCFG_U2_FREECLK_EXISTS __BIT(30)
58 #define GUSB2PHYCFG_USBTRDTIM __BITS(13,10)
59 #define GUSB2PHYCFG_SUSPHY __BIT(6)
60 #define GUSB2PHYCFG_PHYIF __BIT(3)
61 #define GUSB2PHYCFG_ENBLSLPM __BIT(0)
62
63 #define DWC3_GUSB3PIPECTL(n) (0xc2c0 + ((n) * 4))
64 #define GUSB3PIPECTL_PHYSOFTRST __BIT(31)
65
66 #define DWC3_DCFG 0xc700
67 #define DCFG_SPEED __BITS(2,0)
68 #define DCFG_SPEED_HS 0
69 #define DCFG_SPEED_FS 1
70 #define DCFG_SPEED_LS 2
71 #define DCFG_SPEED_SS 4
72 #define DCFG_SPEED_SS_PLUS 5
73
74 static int dwc3_fdt_match(device_t, cfdata_t, void *);
75 static void dwc3_fdt_attach(device_t, device_t, void *);
76
77 CFATTACH_DECL2_NEW(dwc3_fdt, sizeof(struct xhci_softc),
78 dwc3_fdt_match, dwc3_fdt_attach, NULL,
79 xhci_activate, NULL, xhci_childdet);
80
81 #define RD4(sc, reg) \
82 bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg))
83 #define WR4(sc, reg, val) \
84 bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
85 #define SET4(sc, reg, mask) \
86 WR4((sc), (reg), RD4((sc), (reg)) | (mask))
87 #define CLR4(sc, reg, mask) \
88 WR4((sc), (reg), RD4((sc), (reg)) & ~(mask))
89
90 static void
91 dwc3_fdt_soft_reset(struct xhci_softc *sc)
92 {
93 /* Put core in reset */
94 SET4(sc, DWC3_GCTL, GCTL_CORESOFTRESET);
95
96 /* Assert USB3 PHY reset */
97 SET4(sc, DWC3_GUSB3PIPECTL(0), GUSB3PIPECTL_PHYSOFTRST);
98
99 /* Assert USB2 PHY reset */
100 SET4(sc, DWC3_GUSB2PHYCFG(0), GUSB2PHYCFG_PHYSOFTRST);
101
102 delay(100000);
103
104 /* Clear USB3 PHY reset */
105 CLR4(sc, DWC3_GUSB3PIPECTL(0), GUSB3PIPECTL_PHYSOFTRST);
106
107 /* Clear USB2 PHY reset */
108 CLR4(sc, DWC3_GUSB2PHYCFG(0), GUSB2PHYCFG_PHYSOFTRST);
109
110 delay(100000);
111
112 /* Take core out of reset */
113 CLR4(sc, DWC3_GCTL, GCTL_CORESOFTRESET);
114 }
115
116 static void
117 dwc3_fdt_enable_phy(struct xhci_softc *sc, const int phandle)
118 {
119 const char *max_speed;
120 u_int phyif_utmi_bits;
121 uint32_t val;
122
123 val = RD4(sc, DWC3_GUSB2PHYCFG(0));
124 if (of_getprop_uint32(phandle, "snps,phyif-utmi-bits", &phyif_utmi_bits) == 0) {
125 if (phyif_utmi_bits == 16) {
126 val |= GUSB2PHYCFG_PHYIF;
127 val &= ~GUSB2PHYCFG_USBTRDTIM;
128 val |= __SHIFTIN(5, GUSB2PHYCFG_USBTRDTIM);
129 } else if (phyif_utmi_bits == 8) {
130 val &= ~GUSB2PHYCFG_PHYIF;
131 val &= ~GUSB2PHYCFG_USBTRDTIM;
132 val |= __SHIFTIN(9, GUSB2PHYCFG_USBTRDTIM);
133 }
134 }
135 if (of_hasprop(phandle, "snps,dis-enblslpm-quirk"))
136 val &= ~GUSB2PHYCFG_ENBLSLPM;
137 if (of_hasprop(phandle, "snps,dis-u2-freeclk-exists-quirk"))
138 val &= ~GUSB2PHYCFG_U2_FREECLK_EXISTS;
139 if (of_hasprop(phandle, "snps,dis_u2_susphy_quirk"))
140 val &= ~GUSB2PHYCFG_SUSPHY;
141 WR4(sc, DWC3_GUSB2PHYCFG(0), val);
142
143 max_speed = fdtbus_get_string(phandle, "maximum-speed");
144 if (max_speed == NULL)
145 max_speed = "super-speed";
146
147 val = RD4(sc, DWC3_DCFG);
148 val &= ~DCFG_SPEED;
149 if (strcmp(max_speed, "low-speed") == 0)
150 val |= __SHIFTIN(DCFG_SPEED_LS, DCFG_SPEED);
151 else if (strcmp(max_speed, "full-speed") == 0)
152 val |= __SHIFTIN(DCFG_SPEED_FS, DCFG_SPEED);
153 else if (strcmp(max_speed, "high-speed") == 0)
154 val |= __SHIFTIN(DCFG_SPEED_HS, DCFG_SPEED);
155 else if (strcmp(max_speed, "super-speed") == 0)
156 val |= __SHIFTIN(DCFG_SPEED_SS, DCFG_SPEED);
157 else
158 val |= __SHIFTIN(DCFG_SPEED_SS, DCFG_SPEED); /* default to super speed */
159 WR4(sc, DWC3_DCFG, val);
160 }
161
162 static void
163 dwc3_fdt_set_mode(struct xhci_softc *sc, u_int mode)
164 {
165 uint32_t val;
166
167 val = RD4(sc, DWC3_GCTL);
168 val &= ~GCTL_PRTCAP;
169 val |= __SHIFTIN(mode, GCTL_PRTCAP);
170 WR4(sc, DWC3_GCTL, val);
171 }
172
173 static int
174 dwc3_fdt_match(device_t parent, cfdata_t cf, void *aux)
175 {
176 const char * const compatible[] = {
177 "allwinner,sun50i-h6-dwc3",
178 "rockchip,rk3328-dwc3",
179 NULL
180 };
181 struct fdt_attach_args * const faa = aux;
182
183 return of_match_compatible(faa->faa_phandle, compatible);
184 }
185
186 static void
187 dwc3_fdt_attach(device_t parent, device_t self, void *aux)
188 {
189 struct xhci_softc * const sc = device_private(self);
190 struct fdt_attach_args * const faa = aux;
191 const int phandle = faa->faa_phandle;
192 struct fdtbus_reset *rst;
193 struct fdtbus_phy *phy;
194 struct clk *clk;
195 char intrstr[128];
196 bus_addr_t addr;
197 bus_size_t size;
198 int error;
199 void *ih;
200 u_int n;
201
202 /* Find dwc3 sub-node */
203 const int dwc3_phandle = of_find_firstchild_byname(phandle, "dwc3");
204 if (dwc3_phandle <= 0) {
205 aprint_error(": couldn't find dwc3 child node\n");
206 return;
207 }
208
209 /* Only host mode is supported */
210 const char *dr_mode = fdtbus_get_string(dwc3_phandle, "dr_mode");
211 if (dr_mode == NULL || strcmp(dr_mode, "host") != 0) {
212 aprint_error(": '%s' not supported\n", dr_mode);
213 return;
214 }
215
216 /* Enable clocks */
217 for (n = 0; (clk = fdtbus_clock_get_index(phandle, n)) != NULL; n++)
218 if (clk_enable(clk) != 0) {
219 aprint_error(": couldn't enable clock #%d\n", n);
220 return;
221 }
222 /* De-assert resets */
223 for (n = 0; (rst = fdtbus_reset_get_index(phandle, n)) != NULL; n++)
224 if (fdtbus_reset_deassert(rst) != 0) {
225 aprint_error(": couldn't de-assert reset #%d\n", n);
226 return;
227 }
228
229 /* Get resources */
230 if (fdtbus_get_reg(dwc3_phandle, 0, &addr, &size) != 0) {
231 aprint_error(": couldn't get registers\n");
232 return;
233 }
234
235 sc->sc_dev = self;
236 sc->sc_bus.ub_hcpriv = sc;
237 sc->sc_bus.ub_dmatag = faa->faa_dmat;
238 sc->sc_iot = faa->faa_bst;
239 if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh) != 0) {
240 aprint_error(": couldn't map registers\n");
241 return;
242 }
243
244 aprint_naive("\n");
245 aprint_normal(": DesignWare USB3 XHCI\n");
246
247 /* Enable phy */
248 phy = fdtbus_phy_get(dwc3_phandle, "usb3-phy");
249 if (!phy || fdtbus_phy_enable(phy, true) != 0) {
250 aprint_error_dev(self, "couldn't enable usb3-phy\n");
251 }
252
253 dwc3_fdt_soft_reset(sc);
254 dwc3_fdt_enable_phy(sc, phandle);
255 dwc3_fdt_set_mode(sc, GCTL_PRTCAP_HOST);
256
257 if (!fdtbus_intr_str(dwc3_phandle, 0, intrstr, sizeof(intrstr))) {
258 aprint_error_dev(self, "failed to decode interrupt\n");
259 return;
260 }
261
262 ih = fdtbus_intr_establish(dwc3_phandle, 0, IPL_USB, FDT_INTR_MPSAFE,
263 xhci_intr, sc);
264 if (ih == NULL) {
265 aprint_error_dev(self, "couldn't establish interrupt on %s\n",
266 intrstr);
267 return;
268 }
269 aprint_normal_dev(self, "interrupting on %s\n", intrstr);
270
271 sc->sc_bus.ub_revision = USBREV_3_0;
272 error = xhci_init(sc);
273 if (error) {
274 aprint_error_dev(self, "init failed, error = %d\n", error);
275 return;
276 }
277
278 sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint);
279 sc->sc_child2 = config_found(self, &sc->sc_bus2, usbctlprint);
280 }
281