xref: /netbsd-src/sys/arch/arm/nvidia/tegra_ehci.c (revision 796c32c94f6e154afc9de0f63da35c91bb739b45)
1 /* $NetBSD: tegra_ehci.c,v 1.15 2017/05/25 23:45:04 jmcneill Exp $ */
2 
3 /*-
4  * Copyright (c) 2015 Jared D. McNeill <jmcneill@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: tegra_ehci.c,v 1.15 2017/05/25 23:45:04 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/ehcireg.h>
44 #include <dev/usb/ehcivar.h>
45 
46 #include <arm/nvidia/tegra_reg.h>
47 #include <arm/nvidia/tegra_var.h>
48 #include <arm/nvidia/tegra_usbreg.h>
49 
50 #include <dev/fdt/fdtvar.h>
51 
52 #define TEGRA_EHCI_REG_OFFSET	0x100
53 
54 static int	tegra_ehci_match(device_t, cfdata_t, void *);
55 static void	tegra_ehci_attach(device_t, device_t, void *);
56 
57 static void	tegra_ehci_init(struct ehci_softc *);
58 
59 struct tegra_ehci_softc {
60 	struct ehci_softc	sc;
61 	bus_space_tag_t		sc_bst;
62 	bus_space_handle_t	sc_bsh;
63 	void			*sc_ih;
64 };
65 
66 static int	tegra_ehci_port_status(struct ehci_softc *sc, uint32_t v,
67 		    int i);
68 
69 CFATTACH_DECL2_NEW(tegra_ehci, sizeof(struct tegra_ehci_softc),
70 	tegra_ehci_match, tegra_ehci_attach, NULL,
71 	ehci_activate, NULL, ehci_childdet);
72 
73 static int
74 tegra_ehci_match(device_t parent, cfdata_t cf, void *aux)
75 {
76 	const char * const compatible[] = {
77 		"nvidia,tegra210-ehci",
78 		"nvidia,tegra124-ehci",
79 		"nvidia,tegra30-ehci",
80 		NULL
81 	};
82 	struct fdt_attach_args * const faa = aux;
83 
84 	return of_match_compatible(faa->faa_phandle, compatible);
85 }
86 
87 static void
88 tegra_ehci_attach(device_t parent, device_t self, void *aux)
89 {
90 	struct tegra_ehci_softc * const sc = device_private(self);
91 	struct fdt_attach_args * const faa = aux;
92 	char intrstr[128];
93 	bus_addr_t addr;
94 	bus_size_t size;
95 	int error;
96 
97 	if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
98 		aprint_error(": couldn't get registers\n");
99 		return;
100 	}
101 
102 	sc->sc_bst = faa->faa_bst;
103 	error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
104 	if (error) {
105 		aprint_error(": couldn't map USB\n");
106 		return;
107 	}
108 
109 	sc->sc.sc_dev = self;
110 	sc->sc.sc_bus.ub_hcpriv = &sc->sc;
111 	sc->sc.sc_bus.ub_dmatag = faa->faa_dmat;
112 	sc->sc.sc_bus.ub_revision = USBREV_2_0;
113 	sc->sc.sc_ncomp = 0;
114 	sc->sc.sc_flags = EHCIF_ETTF;
115 	sc->sc.sc_id_vendor = 0x10de;
116 	strlcpy(sc->sc.sc_vendor, "Tegra", sizeof(sc->sc.sc_vendor));
117 	sc->sc.sc_size = size - TEGRA_EHCI_REG_OFFSET;
118 	sc->sc.iot = sc->sc_bst;
119 	bus_space_subregion(sc->sc_bst, sc->sc_bsh, TEGRA_EHCI_REG_OFFSET,
120 	    sc->sc.sc_size, &sc->sc.ioh);
121 	sc->sc.sc_vendor_init = tegra_ehci_init;
122 	sc->sc.sc_vendor_port_status = tegra_ehci_port_status;
123 
124 	aprint_naive("\n");
125 	aprint_normal(": USB\n");
126 
127 	sc->sc.sc_offs = EREAD1(&sc->sc, EHCI_CAPLENGTH);
128 
129 	if (!fdtbus_intr_str(faa->faa_phandle, 0, intrstr, sizeof(intrstr))) {
130 		aprint_error_dev(self, "failed to decode interrupt\n");
131 		return;
132 	}
133 
134 	sc->sc_ih = fdtbus_intr_establish(faa->faa_phandle, 0, IPL_USB,
135 	    FDT_INTR_MPSAFE, ehci_intr, &sc->sc);
136 	if (sc->sc_ih == NULL) {
137 		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
138 		    intrstr);
139 		return;
140 	}
141 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
142 
143 	error = ehci_init(&sc->sc);
144 	if (error) {
145 		aprint_error_dev(self, "init failed, error = %d\n", error);
146 		return;
147 	}
148 
149 	sc->sc.sc_child = config_found(self, &sc->sc.sc_bus, usbctlprint);
150 }
151 
152 static void
153 tegra_ehci_init(struct ehci_softc *esc)
154 {
155 	struct tegra_ehci_softc * const sc = device_private(esc->sc_dev);
156 	uint32_t usbmode;
157 
158 	usbmode = bus_space_read_4(sc->sc_bst, sc->sc_bsh,
159 	    TEGRA_EHCI_USBMODE_REG);
160 
161 	const u_int cm = __SHIFTOUT(usbmode, TEGRA_EHCI_USBMODE_CM);
162 	if (cm != TEGRA_EHCI_USBMODE_CM_HOST) {
163 		aprint_verbose_dev(esc->sc_dev, "switching to host mode\n");
164 		usbmode &= ~TEGRA_EHCI_USBMODE_CM;
165 		usbmode |= __SHIFTIN(TEGRA_EHCI_USBMODE_CM_HOST,
166 				     TEGRA_EHCI_USBMODE_CM);
167 		bus_space_write_4(sc->sc_bst, sc->sc_bsh,
168 		    TEGRA_EHCI_USBMODE_REG, usbmode);
169 	}
170 
171 	/* Parallel transceiver select */
172 	tegra_reg_set_clear(sc->sc_bst, sc->sc_bsh,
173 	    TEGRA_EHCI_HOSTPC1_DEVLC_REG,
174 	    __SHIFTIN(TEGRA_EHCI_HOSTPC1_DEVLC_PTS_UTMI,
175 		      TEGRA_EHCI_HOSTPC1_DEVLC_PTS),
176 	    TEGRA_EHCI_HOSTPC1_DEVLC_PTS |
177 	    TEGRA_EHCI_HOSTPC1_DEVLC_STS);
178 
179 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, TEGRA_EHCI_TXFILLTUNING_REG,
180 	    __SHIFTIN(0x10, TEGRA_EHCI_TXFILLTUNING_TXFIFOTHRES));
181 }
182 
183 static int
184 tegra_ehci_port_status(struct ehci_softc *ehci_sc, uint32_t v, int i)
185 {
186 	struct tegra_ehci_softc * const sc = device_private(ehci_sc->sc_dev);
187 	bus_space_tag_t iot = sc->sc_bst;
188 	bus_space_handle_t ioh = sc->sc_bsh;
189 
190 	i &= ~(UPS_HIGH_SPEED|UPS_LOW_SPEED);
191 
192 	uint32_t val = bus_space_read_4(iot, ioh,
193 	    TEGRA_EHCI_HOSTPC1_DEVLC_REG);
194 
195 	switch (__SHIFTOUT(val, TEGRA_EHCI_HOSTPC1_DEVLC_PSPD)) {
196 	case TEGRA_EHCI_HOSTPC1_DEVLC_PSPD_FS:
197 		i |= UPS_FULL_SPEED;
198 		break;
199 	case TEGRA_EHCI_HOSTPC1_DEVLC_PSPD_LS:
200 		i |= UPS_LOW_SPEED;
201 		break;
202 	case TEGRA_EHCI_HOSTPC1_DEVLC_PSPD_HS:
203 	default:
204 		i |= UPS_HIGH_SPEED;
205 		break;
206 	}
207 	return i;
208 }
209