dwc2_fdt.c revision 1.2.2.4 1 1.2.2.4 pgoyette /* $NetBSD: dwc2_fdt.c,v 1.2.2.4 2019/01/26 22:00:06 pgoyette Exp $ */
2 1.2.2.2 pgoyette
3 1.2.2.2 pgoyette /*-
4 1.2.2.2 pgoyette * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.2.2.2 pgoyette * All rights reserved.
6 1.2.2.2 pgoyette *
7 1.2.2.2 pgoyette * This code is derived from software contributed to The NetBSD Foundation
8 1.2.2.2 pgoyette * by Nick Hudson
9 1.2.2.2 pgoyette *
10 1.2.2.2 pgoyette * Redistribution and use in source and binary forms, with or without
11 1.2.2.2 pgoyette * modification, are permitted provided that the following conditions
12 1.2.2.2 pgoyette * are met:
13 1.2.2.2 pgoyette * 1. Redistributions of source code must retain the above copyright
14 1.2.2.2 pgoyette * notice, this list of conditions and the following disclaimer.
15 1.2.2.2 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.2.2 pgoyette * notice, this list of conditions and the following disclaimer in the
17 1.2.2.2 pgoyette * documentation and/or other materials provided with the distribution.
18 1.2.2.2 pgoyette *
19 1.2.2.2 pgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2.2.2 pgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2.2.2 pgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2.2.2 pgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2.2.2 pgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2.2.2 pgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2.2.2 pgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2.2.2 pgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2.2.2 pgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2.2.2 pgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2.2.2 pgoyette * POSSIBILITY OF SUCH DAMAGE.
30 1.2.2.2 pgoyette */
31 1.2.2.2 pgoyette
32 1.2.2.2 pgoyette #include <sys/cdefs.h>
33 1.2.2.4 pgoyette __KERNEL_RCSID(0, "$NetBSD: dwc2_fdt.c,v 1.2.2.4 2019/01/26 22:00:06 pgoyette Exp $");
34 1.2.2.2 pgoyette
35 1.2.2.2 pgoyette #include <sys/param.h>
36 1.2.2.2 pgoyette #include <sys/systm.h>
37 1.2.2.2 pgoyette #include <sys/device.h>
38 1.2.2.2 pgoyette #include <sys/mutex.h>
39 1.2.2.2 pgoyette #include <sys/bus.h>
40 1.2.2.2 pgoyette #include <sys/workqueue.h>
41 1.2.2.2 pgoyette
42 1.2.2.2 pgoyette #include <dev/fdt/fdtvar.h>
43 1.2.2.2 pgoyette
44 1.2.2.2 pgoyette #include <dev/usb/usb.h>
45 1.2.2.2 pgoyette #include <dev/usb/usbdi.h>
46 1.2.2.2 pgoyette #include <dev/usb/usbdivar.h>
47 1.2.2.2 pgoyette #include <dev/usb/usb_mem.h>
48 1.2.2.2 pgoyette
49 1.2.2.2 pgoyette #include <dwc2/dwc2var.h>
50 1.2.2.2 pgoyette
51 1.2.2.2 pgoyette #include <dwc2/dwc2.h>
52 1.2.2.2 pgoyette #include "dwc2_core.h"
53 1.2.2.2 pgoyette
54 1.2.2.2 pgoyette struct dwc2_fdt_softc {
55 1.2.2.2 pgoyette struct dwc2_softc sc_dwc2;
56 1.2.2.2 pgoyette
57 1.2.2.2 pgoyette struct dwc2_core_params sc_params;
58 1.2.2.2 pgoyette
59 1.2.2.2 pgoyette void *sc_ih;
60 1.2.2.2 pgoyette int sc_phandle;
61 1.2.2.2 pgoyette };
62 1.2.2.2 pgoyette
63 1.2.2.2 pgoyette static int dwc2_fdt_match(device_t, struct cfdata *, void *);
64 1.2.2.2 pgoyette static void dwc2_fdt_attach(device_t, device_t, void *);
65 1.2.2.2 pgoyette static void dwc2_fdt_deferred(device_t);
66 1.2.2.2 pgoyette
67 1.2.2.4 pgoyette static void dwc2_fdt_amlogic_params(struct dwc2_fdt_softc *, struct dwc2_core_params *);
68 1.2.2.2 pgoyette static void dwc2_fdt_rockchip_params(struct dwc2_fdt_softc *, struct dwc2_core_params *);
69 1.2.2.2 pgoyette
70 1.2.2.2 pgoyette struct dwc2_fdt_config {
71 1.2.2.2 pgoyette void (*params)(struct dwc2_fdt_softc *, struct dwc2_core_params *);
72 1.2.2.2 pgoyette };
73 1.2.2.2 pgoyette
74 1.2.2.2 pgoyette static const struct dwc2_fdt_config dwc2_fdt_rk3066_config = {
75 1.2.2.2 pgoyette .params = dwc2_fdt_rockchip_params,
76 1.2.2.2 pgoyette };
77 1.2.2.2 pgoyette
78 1.2.2.4 pgoyette static const struct dwc2_fdt_config dwc2_fdt_meson8b_config = {
79 1.2.2.4 pgoyette .params = dwc2_fdt_amlogic_params,
80 1.2.2.4 pgoyette };
81 1.2.2.4 pgoyette
82 1.2.2.2 pgoyette static const struct dwc2_fdt_config dwc2_fdt_generic_config = {
83 1.2.2.2 pgoyette };
84 1.2.2.2 pgoyette
85 1.2.2.2 pgoyette static const struct of_compat_data compat_data[] = {
86 1.2.2.4 pgoyette { "amlogic,meson8b-usb", (uintptr_t)&dwc2_fdt_meson8b_config },
87 1.2.2.2 pgoyette { "rockchip,rk3066-usb", (uintptr_t)&dwc2_fdt_rk3066_config },
88 1.2.2.2 pgoyette { "snps,dwc2", (uintptr_t)&dwc2_fdt_generic_config },
89 1.2.2.2 pgoyette { NULL }
90 1.2.2.2 pgoyette };
91 1.2.2.2 pgoyette
92 1.2.2.2 pgoyette CFATTACH_DECL_NEW(dwc2_fdt, sizeof(struct dwc2_fdt_softc),
93 1.2.2.2 pgoyette dwc2_fdt_match, dwc2_fdt_attach, NULL, NULL);
94 1.2.2.2 pgoyette
95 1.2.2.2 pgoyette /* ARGSUSED */
96 1.2.2.2 pgoyette static int
97 1.2.2.2 pgoyette dwc2_fdt_match(device_t parent, struct cfdata *match, void *aux)
98 1.2.2.2 pgoyette {
99 1.2.2.2 pgoyette struct fdt_attach_args * const faa = aux;
100 1.2.2.2 pgoyette
101 1.2.2.2 pgoyette return of_match_compat_data(faa->faa_phandle, compat_data);
102 1.2.2.2 pgoyette }
103 1.2.2.2 pgoyette
104 1.2.2.2 pgoyette /* ARGSUSED */
105 1.2.2.2 pgoyette static void
106 1.2.2.2 pgoyette dwc2_fdt_attach(device_t parent, device_t self, void *aux)
107 1.2.2.2 pgoyette {
108 1.2.2.2 pgoyette struct dwc2_fdt_softc *sc = device_private(self);
109 1.2.2.2 pgoyette struct fdt_attach_args * const faa = aux;
110 1.2.2.2 pgoyette const int phandle = faa->faa_phandle;
111 1.2.2.2 pgoyette const struct dwc2_fdt_config *conf =
112 1.2.2.2 pgoyette (void *)of_search_compatible(phandle, compat_data)->data;
113 1.2.2.2 pgoyette char intrstr[128];
114 1.2.2.2 pgoyette struct fdtbus_phy *phy;
115 1.2.2.2 pgoyette struct clk *clk;
116 1.2.2.2 pgoyette bus_addr_t addr;
117 1.2.2.2 pgoyette bus_size_t size;
118 1.2.2.2 pgoyette int error;
119 1.2.2.2 pgoyette
120 1.2.2.2 pgoyette const char *dr_mode = fdtbus_get_string(phandle, "dr_mode");
121 1.2.2.2 pgoyette if (dr_mode == NULL || strcmp(dr_mode, "host") != 0) {
122 1.2.2.2 pgoyette aprint_error(": mode '%s' not supported\n", dr_mode);
123 1.2.2.2 pgoyette return;
124 1.2.2.2 pgoyette }
125 1.2.2.2 pgoyette
126 1.2.2.2 pgoyette if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
127 1.2.2.2 pgoyette aprint_error(": couldn't get registers\n");
128 1.2.2.2 pgoyette return;
129 1.2.2.2 pgoyette }
130 1.2.2.2 pgoyette
131 1.2.2.2 pgoyette clk = fdtbus_clock_get(phandle, "otg");
132 1.2.2.2 pgoyette if (clk == NULL || clk_enable(clk) != 0) {
133 1.2.2.2 pgoyette aprint_error(": couldn't enable otg clock\n");
134 1.2.2.2 pgoyette return;
135 1.2.2.2 pgoyette }
136 1.2.2.2 pgoyette
137 1.2.2.2 pgoyette /* Enable optional phy */
138 1.2.2.2 pgoyette phy = fdtbus_phy_get(phandle, "usb2-phy");
139 1.2.2.2 pgoyette if (phy && fdtbus_phy_enable(phy, true) != 0) {
140 1.2.2.2 pgoyette aprint_error(": couldn't enable phy\n");
141 1.2.2.2 pgoyette return;
142 1.2.2.2 pgoyette }
143 1.2.2.2 pgoyette
144 1.2.2.2 pgoyette sc->sc_phandle = phandle;
145 1.2.2.2 pgoyette sc->sc_dwc2.sc_dev = self;
146 1.2.2.2 pgoyette sc->sc_dwc2.sc_iot = faa->faa_bst;
147 1.2.2.2 pgoyette sc->sc_dwc2.sc_bus.ub_dmatag = faa->faa_dmat;
148 1.2.2.2 pgoyette
149 1.2.2.2 pgoyette error = bus_space_map(faa->faa_bst, addr, size, 0, &sc->sc_dwc2.sc_ioh);
150 1.2.2.2 pgoyette if (error) {
151 1.2.2.2 pgoyette aprint_error(": couldn't map device\n");
152 1.2.2.2 pgoyette return;
153 1.2.2.2 pgoyette }
154 1.2.2.2 pgoyette
155 1.2.2.2 pgoyette if (conf->params) {
156 1.2.2.2 pgoyette conf->params(sc, &sc->sc_params);
157 1.2.2.2 pgoyette sc->sc_dwc2.sc_params = &sc->sc_params;
158 1.2.2.2 pgoyette }
159 1.2.2.2 pgoyette
160 1.2.2.2 pgoyette if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
161 1.2.2.2 pgoyette aprint_error(": failed to decode interrupt\n");
162 1.2.2.2 pgoyette return;
163 1.2.2.2 pgoyette }
164 1.2.2.2 pgoyette
165 1.2.2.2 pgoyette aprint_naive("\n");
166 1.2.2.2 pgoyette aprint_normal(": DesignWare USB2 OTG\n");
167 1.2.2.2 pgoyette
168 1.2.2.2 pgoyette sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_VM, FDT_INTR_MPSAFE,
169 1.2.2.2 pgoyette dwc2_intr, &sc->sc_dwc2);
170 1.2.2.2 pgoyette
171 1.2.2.2 pgoyette if (sc->sc_ih == NULL) {
172 1.2.2.2 pgoyette aprint_error_dev(self, "failed to establish interrupt %s\n",
173 1.2.2.2 pgoyette intrstr);
174 1.2.2.2 pgoyette goto fail;
175 1.2.2.2 pgoyette }
176 1.2.2.2 pgoyette aprint_normal_dev(self, "interrupting on %s\n", intrstr);
177 1.2.2.2 pgoyette config_interrupts(self, dwc2_fdt_deferred);
178 1.2.2.2 pgoyette
179 1.2.2.2 pgoyette return;
180 1.2.2.2 pgoyette
181 1.2.2.2 pgoyette fail:
182 1.2.2.2 pgoyette if (sc->sc_ih) {
183 1.2.2.2 pgoyette fdtbus_intr_disestablish(sc->sc_phandle, sc->sc_ih);
184 1.2.2.2 pgoyette sc->sc_ih = NULL;
185 1.2.2.2 pgoyette }
186 1.2.2.2 pgoyette bus_space_unmap(sc->sc_dwc2.sc_iot, sc->sc_dwc2.sc_ioh, size);
187 1.2.2.2 pgoyette }
188 1.2.2.2 pgoyette
189 1.2.2.2 pgoyette static void
190 1.2.2.2 pgoyette dwc2_fdt_deferred(device_t self)
191 1.2.2.2 pgoyette {
192 1.2.2.2 pgoyette struct dwc2_fdt_softc *sc = device_private(self);
193 1.2.2.2 pgoyette int error;
194 1.2.2.2 pgoyette
195 1.2.2.2 pgoyette error = dwc2_init(&sc->sc_dwc2);
196 1.2.2.2 pgoyette if (error != 0) {
197 1.2.2.2 pgoyette aprint_error_dev(self, "couldn't initialize host, error=%d\n",
198 1.2.2.2 pgoyette error);
199 1.2.2.2 pgoyette return;
200 1.2.2.2 pgoyette }
201 1.2.2.2 pgoyette sc->sc_dwc2.sc_child = config_found(sc->sc_dwc2.sc_dev,
202 1.2.2.2 pgoyette &sc->sc_dwc2.sc_bus, usbctlprint);
203 1.2.2.2 pgoyette }
204 1.2.2.2 pgoyette
205 1.2.2.2 pgoyette static void
206 1.2.2.4 pgoyette dwc2_fdt_amlogic_params(struct dwc2_fdt_softc *sc, struct dwc2_core_params *params)
207 1.2.2.4 pgoyette {
208 1.2.2.4 pgoyette dwc2_set_all_params(params, -1);
209 1.2.2.4 pgoyette
210 1.2.2.4 pgoyette params->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
211 1.2.2.4 pgoyette params->speed = DWC2_SPEED_PARAM_HIGH;
212 1.2.2.4 pgoyette params->host_rx_fifo_size = 512;
213 1.2.2.4 pgoyette params->host_nperio_tx_fifo_size = 500;
214 1.2.2.4 pgoyette params->host_perio_tx_fifo_size = 500;
215 1.2.2.4 pgoyette params->host_channels = 16;
216 1.2.2.4 pgoyette params->phy_type = DWC2_PHY_TYPE_PARAM_UTMI;
217 1.2.2.4 pgoyette params->ahbcfg = GAHBCFG_HBSTLEN_INCR8 << GAHBCFG_HBSTLEN_SHIFT;
218 1.2.2.4 pgoyette #ifdef DWC2_POWER_DOWN_PARAM_NONE
219 1.2.2.4 pgoyette params->power_down = DWC2_POWER_DOWN_PARAM_NONE;
220 1.2.2.4 pgoyette #endif
221 1.2.2.4 pgoyette }
222 1.2.2.4 pgoyette
223 1.2.2.4 pgoyette static void
224 1.2.2.2 pgoyette dwc2_fdt_rockchip_params(struct dwc2_fdt_softc *sc, struct dwc2_core_params *params)
225 1.2.2.2 pgoyette {
226 1.2.2.2 pgoyette dwc2_set_all_params(params, -1);
227 1.2.2.2 pgoyette
228 1.2.2.2 pgoyette params->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
229 1.2.2.2 pgoyette params->host_rx_fifo_size = 525;
230 1.2.2.2 pgoyette params->host_nperio_tx_fifo_size = 128;
231 1.2.2.2 pgoyette params->host_perio_tx_fifo_size = 256;
232 1.2.2.2 pgoyette params->ahbcfg = GAHBCFG_HBSTLEN_INCR16 << GAHBCFG_HBSTLEN_SHIFT;
233 1.2.2.2 pgoyette }
234