tegra_ehci.c revision 1.4 1 /* $NetBSD: tegra_ehci.c,v 1.4 2015/05/09 18:56:51 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2015 Jared D. 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 "locators.h"
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: tegra_ehci.c,v 1.4 2015/05/09 18:56:51 jmcneill Exp $");
33
34 #include <sys/param.h>
35 #include <sys/bus.h>
36 #include <sys/device.h>
37 #include <sys/intr.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40
41 #include <dev/usb/usb.h>
42 #include <dev/usb/usbdi.h>
43 #include <dev/usb/usbdivar.h>
44 #include <dev/usb/usb_mem.h>
45 #include <dev/usb/ehcireg.h>
46 #include <dev/usb/ehcivar.h>
47
48 #include <arm/nvidia/tegra_var.h>
49 #include <arm/nvidia/tegra_ehcireg.h>
50
51 #define TEGRA_EHCI_REG_OFFSET 0x100
52
53 static int tegra_ehci_match(device_t, cfdata_t, void *);
54 static void tegra_ehci_attach(device_t, device_t, void *);
55
56 static void tegra_ehci_init(struct ehci_softc *);
57
58 struct tegra_ehci_softc {
59 struct ehci_softc sc;
60 bus_space_tag_t sc_bst;
61 bus_space_handle_t sc_bsh;
62 void *sc_ih;
63 u_int sc_port;
64
65 struct tegra_gpio_pin *sc_pin_vbus;
66 };
67
68 static void tegra_ehci_utmip_init(struct tegra_ehci_softc *);
69
70 CFATTACH_DECL2_NEW(tegra_ehci, sizeof(struct tegra_ehci_softc),
71 tegra_ehci_match, tegra_ehci_attach, NULL,
72 ehci_activate, NULL, ehci_childdet);
73
74 static int
75 tegra_ehci_match(device_t parent, cfdata_t cf, void *aux)
76 {
77 return 1;
78 }
79
80 static void
81 tegra_ehci_attach(device_t parent, device_t self, void *aux)
82 {
83 struct tegra_ehci_softc * const sc = device_private(self);
84 struct tegraio_attach_args * const tio = aux;
85 const struct tegra_locators * const loc = &tio->tio_loc;
86 prop_dictionary_t prop = device_properties(self);
87 const char *pin;
88 int error;
89
90 sc->sc_bst = tio->tio_bst;
91 bus_space_subregion(tio->tio_bst, tio->tio_bsh,
92 loc->loc_offset, loc->loc_size, &sc->sc_bsh);
93 sc->sc_port = loc->loc_port;
94
95 sc->sc.sc_dev = self;
96 sc->sc.sc_bus.hci_private = &sc->sc;
97 sc->sc.sc_bus.dmatag = tio->tio_dmat;
98 sc->sc.sc_bus.usbrev = USBREV_2_0;
99 sc->sc.sc_flags = 0; /* XXX EHCIF_ETTF */
100 sc->sc.sc_id_vendor = 0x10de;
101 strlcpy(sc->sc.sc_vendor, "Tegra", sizeof(sc->sc.sc_vendor));
102 sc->sc.sc_size = loc->loc_size;
103 sc->sc.iot = tio->tio_bst;
104 bus_space_subregion(tio->tio_bst, tio->tio_bsh,
105 loc->loc_offset + TEGRA_EHCI_REG_OFFSET,
106 loc->loc_size - TEGRA_EHCI_REG_OFFSET, &sc->sc.ioh);
107 sc->sc.sc_vendor_init = tegra_ehci_init;
108
109 aprint_naive("\n");
110 aprint_normal(": USB%d\n", loc->loc_port + 1);
111
112 tegra_car_periph_usb_enable(sc->sc_port);
113 delay(2);
114
115 tegra_ehci_utmip_init(sc);
116
117 if (prop_dictionary_get_cstring_nocopy(prop, "vbus-gpio", &pin)) {
118 const uint32_t v = bus_space_read_4(sc->sc_bst, sc->sc_bsh,
119 TEGRA_EHCI_PHY_VBUS_SENSORS_REG);
120 if ((v & TEGRA_EHCI_PHY_VBUS_SENSORS_A_VBUS_VLD_STS) == 0) {
121 sc->sc_pin_vbus = tegra_gpio_acquire(pin,
122 GPIO_PIN_OUTPUT | GPIO_PIN_OPENDRAIN);
123 if (sc->sc_pin_vbus)
124 tegra_gpio_write(sc->sc_pin_vbus, 1);
125 } else {
126 aprint_normal_dev(self, "VBUS input active\n");
127 }
128 }
129
130 sc->sc.sc_offs = EREAD1(&sc->sc, EHCI_CAPLENGTH);
131
132 sc->sc_ih = intr_establish(loc->loc_intr, IPL_USB, IST_LEVEL,
133 ehci_intr, &sc->sc);
134 if (sc->sc_ih == NULL) {
135 aprint_error_dev(self, "couldn't establish interrupt %d\n",
136 loc->loc_intr);
137 return;
138 }
139 aprint_normal_dev(self, "interrupting on irq %d\n", loc->loc_intr);
140
141 error = ehci_init(&sc->sc);
142 if (error != USBD_NORMAL_COMPLETION) {
143 aprint_error_dev(self, "init failed, error = %d\n", error);
144 return;
145 }
146
147 sc->sc.sc_child = config_found(self, &sc->sc.sc_bus, usbctlprint);
148 }
149
150 static void
151 tegra_ehci_init(struct ehci_softc *esc)
152 {
153 struct tegra_ehci_softc * const sc = device_private(esc->sc_dev);
154 uint32_t usbmode;
155
156 usbmode = bus_space_read_4(sc->sc_bst, sc->sc_bsh,
157 TEGRA_EHCI_USBMODE_REG);
158
159 const u_int cm = __SHIFTOUT(usbmode, TEGRA_EHCI_USBMODE_CM);
160 if (cm != TEGRA_EHCI_USBMODE_CM_HOST) {
161 aprint_verbose_dev(esc->sc_dev, "switching to host mode\n");
162 usbmode &= ~TEGRA_EHCI_USBMODE_CM;
163 usbmode |= __SHIFTIN(TEGRA_EHCI_USBMODE_CM_HOST,
164 TEGRA_EHCI_USBMODE_CM);
165 bus_space_write_4(sc->sc_bst, sc->sc_bsh,
166 TEGRA_EHCI_USBMODE_REG, usbmode);
167 }
168
169 /* Parallel transceiver select */
170 tegra_reg_set_clear(sc->sc_bst, sc->sc_bsh,
171 TEGRA_EHCI_HOSTPC1_DEVLC_REG,
172 __SHIFTIN(TEGRA_EHCI_HOSTPC1_DEVLC_PTS_UTMI,
173 TEGRA_EHCI_HOSTPC1_DEVLC_PTS),
174 TEGRA_EHCI_HOSTPC1_DEVLC_PTS |
175 TEGRA_EHCI_HOSTPC1_DEVLC_STS);
176
177 bus_space_write_4(sc->sc_bst, sc->sc_bsh, TEGRA_EHCI_TXFILLTUNING_REG,
178 __SHIFTIN(0x10, TEGRA_EHCI_TXFILLTUNING_TXFIFOTHRES));
179 }
180
181 static void
182 tegra_ehci_utmip_init(struct tegra_ehci_softc *sc)
183 {
184 bus_space_tag_t bst = sc->sc_bst;
185 bus_space_handle_t bsh = sc->sc_bsh;
186 int retry;
187
188 /* Put UTMIP PHY into reset before programming UTMIP config registers */
189 tegra_reg_set_clear(bst, bsh, TEGRA_EHCI_SUSP_CTRL_REG,
190 TEGRA_EHCI_SUSP_CTRL_UTMIP_RESET, 0);
191
192 /* Enable UTMIP PHY mode */
193 tegra_reg_set_clear(bst, bsh, TEGRA_EHCI_SUSP_CTRL_REG,
194 TEGRA_EHCI_SUSP_CTRL_UTMIP_PHY_ENB, 0);
195
196 /* Stop crystal clock */
197 tegra_reg_set_clear(bst, bsh, TEGRA_EHCI_UTMIP_MISC_CFG1_REG,
198 0, TEGRA_EHCI_UTMIP_MISC_CFG1_PHY_XTAL_CLOCKEN);
199 delay(1);
200
201 /* Clear session status */
202 tegra_reg_set_clear(bst, bsh, TEGRA_EHCI_PHY_VBUS_SENSORS_REG,
203 0,
204 TEGRA_EHCI_PHY_VBUS_SENSORS_B_VLD_SW_VALUE |
205 TEGRA_EHCI_PHY_VBUS_SENSORS_B_VLD_SW_EN);
206
207 /* PLL configuration */
208 tegra_car_utmip_init();
209
210 /* Transceiver configuration */
211 tegra_reg_set_clear(bst, bsh, TEGRA_EHCI_UTMIP_XCVR_CFG0_REG,
212 __SHIFTIN(4, TEGRA_EHCI_UTMIP_XCVR_CFG0_SETUP) |
213 __SHIFTIN(3, TEGRA_EHCI_UTMIP_XCVR_CFG0_SETUP_MSB) |
214 __SHIFTIN(8, TEGRA_EHCI_UTMIP_XCVR_CFG0_HSSLEW_MSB),
215 TEGRA_EHCI_UTMIP_XCVR_CFG0_SETUP |
216 TEGRA_EHCI_UTMIP_XCVR_CFG0_SETUP_MSB |
217 TEGRA_EHCI_UTMIP_XCVR_CFG0_HSSLEW_MSB);
218 tegra_reg_set_clear(bst, bsh, TEGRA_EHCI_UTMIP_XCVR_CFG1_REG,
219 __SHIFTIN(7, TEGRA_EHCI_UTMIP_XCVR_CFG1_TERM_RANGE_ADJ),
220 TEGRA_EHCI_UTMIP_XCVR_CFG1_TERM_RANGE_ADJ);
221
222 if (sc->sc_port == 0) {
223 tegra_reg_set_clear(bst, bsh, TEGRA_EHCI_UTMIP_BIAS_CFG0_REG,
224 TEGRA_EHCI_UTMIP_BIAS_CFG0_HSDISCON_LEVEL_MSB |
225 __SHIFTIN(2, TEGRA_EHCI_UTMIP_BIAS_CFG0_HSDISCON_LEVEL),
226 TEGRA_EHCI_UTMIP_BIAS_CFG0_HSDISCON_LEVEL);
227 }
228
229 /* Misc config */
230 tegra_reg_set_clear(bst, bsh, TEGRA_EHCI_UTMIP_MISC_CFG0_REG,
231 0,
232 TEGRA_EHCI_UTMIP_MISC_CFG0_SUSPEND_EXIT_ON_EDGE);
233
234 /* BIAS cell power down lag */
235 tegra_reg_set_clear(bst, bsh, TEGRA_EHCI_UTMIP_BIAS_CFG1_REG,
236 __SHIFTIN(6, TEGRA_EHCI_UTMIP_BIAS_CFG1_PDTRK_COUNT),
237 TEGRA_EHCI_UTMIP_BIAS_CFG1_PDTRK_COUNT);
238
239 /* Debounce config */
240 tegra_reg_set_clear(bst, bsh, TEGRA_EHCI_UTMIP_DEBOUNCE_CFG0_REG,
241 __SHIFTIN(0x73f4, TEGRA_EHCI_UTMIP_DEBOUNCE_CFG0_A),
242 TEGRA_EHCI_UTMIP_DEBOUNCE_CFG0_A);
243
244 /* Transmit signal preamble config */
245 tegra_reg_set_clear(bst, bsh, TEGRA_EHCI_UTMIP_TX_CFG0_REG,
246 TEGRA_EHCI_UTMIP_TX_CFG0_FS_PREAMBLE_J, 0);
247
248 /* Power-down battery charger circuit */
249 tegra_reg_set_clear(bst, bsh, TEGRA_EHCI_UTMIP_BAT_CHRG_CFG0_REG,
250 TEGRA_EHCI_UTMIP_BAT_CHRG_CFG0_PD_CHRG, 0);
251
252 /* Select low speed bias method */
253 tegra_reg_set_clear(bst, bsh, TEGRA_EHCI_UTMIP_XCVR_CFG0_REG,
254 0, TEGRA_EHCI_UTMIP_XCVR_CFG0_LSBIAS_SEL);
255
256 /* High speed receive config */
257 tegra_reg_set_clear(bst, bsh, TEGRA_EHCI_UTMIP_HSRX_CFG0_REG,
258 __SHIFTIN(17, TEGRA_EHCI_UTMIP_HSRX_CFG0_IDLE_WAIT) |
259 __SHIFTIN(16, TEGRA_EHCI_UTMIP_HSRX_CFG0_ELASTIC_LIMIT),
260 TEGRA_EHCI_UTMIP_HSRX_CFG0_IDLE_WAIT |
261 TEGRA_EHCI_UTMIP_HSRX_CFG0_ELASTIC_LIMIT);
262 tegra_reg_set_clear(bst, bsh, TEGRA_EHCI_UTMIP_HSRX_CFG1_REG,
263 __SHIFTIN(9, TEGRA_EHCI_UTMIP_HSRX_CFG1_SYNC_START_DLY),
264 TEGRA_EHCI_UTMIP_HSRX_CFG1_SYNC_START_DLY);
265
266 /* Start crystal clock */
267 delay(1);
268 tegra_reg_set_clear(bst, bsh, TEGRA_EHCI_UTMIP_MISC_CFG1_REG,
269 TEGRA_EHCI_UTMIP_MISC_CFG1_PHY_XTAL_CLOCKEN, 0);
270
271 /* Clear port PLL powerdown status */
272 tegra_car_utmip_enable(sc->sc_port);
273
274 /* Bring UTMIP PHY out of reset */
275 tegra_reg_set_clear(bst, bsh, TEGRA_EHCI_SUSP_CTRL_REG,
276 0, TEGRA_EHCI_SUSP_CTRL_UTMIP_RESET);
277 for (retry = 100000; retry > 0; retry--) {
278 const uint32_t susp = bus_space_read_4(bst, bsh,
279 TEGRA_EHCI_SUSP_CTRL_REG);
280 if (susp & TEGRA_EHCI_SUSP_CTRL_PHY_CLK_VALID)
281 break;
282 delay(1);
283 }
284 if (retry == 0) {
285 aprint_error_dev(sc->sc.sc_dev, "PHY clock is not valid\n");
286 return;
287 }
288
289 /* Disable ICUSB transceiver */
290 tegra_reg_set_clear(bst, bsh, TEGRA_EHCI_ICUSB_CTRL_REG,
291 0,
292 TEGRA_EHCI_ICUSB_CTRL_ENB1);
293
294 /* Power up UTMPI transceiver */
295 tegra_reg_set_clear(bst, bsh, TEGRA_EHCI_UTMIP_XCVR_CFG0_REG,
296 0,
297 TEGRA_EHCI_UTMIP_XCVR_CFG0_PD_POWERDOWN |
298 TEGRA_EHCI_UTMIP_XCVR_CFG0_PD2_POWERDOWN |
299 TEGRA_EHCI_UTMIP_XCVR_CFG0_PDZI_POWERDOWN);
300 tegra_reg_set_clear(bst, bsh, TEGRA_EHCI_UTMIP_XCVR_CFG1_REG,
301 0,
302 TEGRA_EHCI_UTMIP_XCVR_CFG1_PDDISC_POWERDOWN |
303 TEGRA_EHCI_UTMIP_XCVR_CFG1_PDCHRP_POWERDOWN |
304 TEGRA_EHCI_UTMIP_XCVR_CFG1_PDDR_POWERDOWN);
305
306 if (sc->sc_port == 0) {
307 tegra_reg_set_clear(bst, bsh, TEGRA_EHCI_UTMIP_BIAS_CFG0_REG,
308 0, TEGRA_EHCI_UTMIP_BIAS_CFG0_BIASPD);
309 delay(25);
310 tegra_reg_set_clear(bst, bsh, TEGRA_EHCI_UTMIP_BIAS_CFG1_REG,
311 0, TEGRA_EHCI_UTMIP_BIAS_CFG1_PDTRK_POWERDOWN);
312 }
313 }
314