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