1 /* $NetBSD: ti_usbtll.c,v 1.2 2021/01/27 03:10:20 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2019 Jared 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. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: ti_usbtll.c,v 1.2 2021/01/27 03:10:20 thorpej Exp $");
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/device.h>
34 #include <sys/conf.h>
35 #include <sys/mutex.h>
36 #include <sys/bus.h>
37
38 #include <dev/fdt/fdtvar.h>
39
40 #include <arm/ti/ti_prcm.h>
41
42 #define USBTLL_SYSCONFIG 0x10
43 #define USBTLL_SYSCONFIG_CLOCKACTIVITY 0x00000100
44 #define USBTLL_SYSCONFIG_SIDLEMODE 0x00000018
45 #define USBTLL_SYSCONFIG_ENAWAKEUP 0x00000004
46 #define USBTLL_SYSCONFIG_SOFTRESET 0x00000002
47 #define USBTLL_SYSCONFIG_AUTOIDLE 0x00000001
48
49 #define USBTLL_SYSSTATUS 0x14
50 #define USBTLL_SYSSTATUS_RESETDONE 0x00000001
51
52 #define USBTLL_SHARED_CONF 0x30
53 #define USBTLL_SHARED_CONF_USB_90D_DDR_EN 0x00000040
54 #define USBTLL_SHARED_CONF_USB_180D_SDR_EN 0x00000020
55 #define USBTLL_SHARED_CONF_USB_DIVRATIO 0x0000001c
56 #define USBTLL_SHARED_CONF_FCLK_REQ 0x00000002
57 #define USBTLL_SHARED_CONF_FCLK_IS_ON 0x00000001
58
59 #define USBTLL_CHANNEL_CONF(i) (0x40 + (0x04 * (i)))
60 #define USBTLL_CHANNEL_CONF_FSLSLINESTATE 0x30000000
61 #define USBTLL_CHANNEL_CONF_FSLSMODE 0x0f000000
62 #define USBTLL_CHANNEL_CONF_TESTTXSE0 0x00100000
63 #define USBTLL_CHANNEL_CONF_TESTTXDAT 0x00080000
64 #define USBTLL_CHANNEL_CONF_TESTTXEN 0x00040000
65 #define USBTLL_CHANNEL_CONF_TESTEN 0x00020000
66 #define USBTLL_CHANNEL_CONF_DRVVBUS 0x00010000
67 #define USBTLL_CHANNEL_CONF_CHRGVBUS 0x00008000
68 #define USBTLL_CHANNEL_CONF_ULPINOBITSTUFF 0x00000800
69 #define USBTLL_CHANNEL_CONF_ULPIAUTOIDLE 0x00000400
70 #define USBTLL_CHANNEL_CONF_UTMIAUTOIDLE 0x00000200
71 #define USBTLL_CHANNEL_CONF_ULPIDDRMODE 0x00000100
72 #define USBTLL_CHANNEL_CONF_ULPIOUTCLKMODE 0x00000080
73 #define USBTLL_CHANNEL_CONF_TLLFULLSPEED 0x00000040
74 #define USBTLL_CHANNEL_CONF_TLLCONNECT 0x00000020
75 #define USBTLL_CHANNEL_CONF_TLLATTACH 0x00000010
76 #define USBTLL_CHANNEL_CONF_UTMIISADEV 0x00000008
77 #define USBTLL_CHANNEL_CONF_CHANMODE 0x00000006
78 #define USBTLL_CHANNEL_CONF_CHANEN 0x00000001
79
80 static const struct device_compatible_entry compat_data[] = {
81 { .compat = "ti,usbhs-tll" },
82 DEVICE_COMPAT_EOL
83 };
84
85 struct ti_usbtll_softc {
86 device_t sc_dev;
87 bus_space_tag_t sc_bst;
88 bus_space_handle_t sc_bsh;
89 };
90
91 static int ti_usbtll_match(device_t, cfdata_t, void *);
92 static void ti_usbtll_attach(device_t, device_t, void *);
93
94 CFATTACH_DECL_NEW(ti_usbtll, sizeof(struct ti_usbtll_softc),
95 ti_usbtll_match, ti_usbtll_attach, NULL, NULL);
96
97 #define RD4(sc, reg) \
98 bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
99 #define WR4(sc, reg, val) \
100 bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
101
102 static struct ti_usbtll_softc *ti_usbtll_sc = NULL;
103
104 void tl_usbtll_enable_port(u_int);
105
106 void
tl_usbtll_enable_port(u_int port)107 tl_usbtll_enable_port(u_int port)
108 {
109 struct ti_usbtll_softc *sc = ti_usbtll_sc;
110 uint32_t val;
111
112 if (sc == NULL) {
113 printf("%s: driver not loaded\n", __func__);
114 return;
115 }
116
117 val = RD4(sc, USBTLL_CHANNEL_CONF(port));
118 val &= ~(USBTLL_CHANNEL_CONF_ULPINOBITSTUFF|
119 USBTLL_CHANNEL_CONF_ULPIAUTOIDLE|
120 USBTLL_CHANNEL_CONF_ULPIDDRMODE);
121 val |= USBTLL_CHANNEL_CONF_CHANEN;
122 WR4(sc, USBTLL_CHANNEL_CONF(port), val);
123 }
124
125 static void
ti_usbtll_reset(struct ti_usbtll_softc * sc)126 ti_usbtll_reset(struct ti_usbtll_softc *sc)
127 {
128 uint32_t val;
129 int retry = 5000;
130
131 WR4(sc, USBTLL_SYSCONFIG, USBTLL_SYSCONFIG_SOFTRESET);
132 do {
133 val = RD4(sc, USBTLL_SYSSTATUS);
134 if (val & USBTLL_SYSSTATUS_RESETDONE)
135 break;
136 delay(10);
137 } while (--retry > 0);
138 if (retry == 0)
139 aprint_error_dev(sc->sc_dev, "reset timeout\n");
140 }
141
142 static void
ti_usbtll_init(struct ti_usbtll_softc * sc)143 ti_usbtll_init(struct ti_usbtll_softc *sc)
144 {
145 uint32_t val;
146
147 ti_usbtll_reset(sc);
148
149 val = USBTLL_SYSCONFIG_ENAWAKEUP |
150 USBTLL_SYSCONFIG_AUTOIDLE |
151 USBTLL_SYSCONFIG_SIDLEMODE |
152 USBTLL_SYSCONFIG_CLOCKACTIVITY;
153 WR4(sc, USBTLL_SYSCONFIG, val);
154
155 val = RD4(sc, USBTLL_SHARED_CONF);
156 val |= (USBTLL_SHARED_CONF_FCLK_IS_ON | (1 << 2));
157 val &= ~USBTLL_SHARED_CONF_USB_90D_DDR_EN;
158 val &= ~USBTLL_SHARED_CONF_USB_180D_SDR_EN;
159 WR4(sc, USBTLL_SHARED_CONF, val);
160 }
161
162 static int
ti_usbtll_match(device_t parent,cfdata_t match,void * aux)163 ti_usbtll_match(device_t parent, cfdata_t match, void *aux)
164 {
165 struct fdt_attach_args * const faa = aux;
166
167 return of_compatible_match(faa->faa_phandle, compat_data);
168 }
169
170 static void
ti_usbtll_attach(device_t parent,device_t self,void * aux)171 ti_usbtll_attach(device_t parent, device_t self, void *aux)
172 {
173 struct ti_usbtll_softc *sc = device_private(self);
174 struct fdt_attach_args * const faa = aux;
175 const int phandle = faa->faa_phandle;
176 bus_addr_t addr;
177 bus_size_t size;
178
179 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
180 aprint_error(": couldn't get registers\n");
181 return;
182 }
183
184 if (ti_prcm_enable_hwmod(phandle, 0) != 0) {
185 aprint_error(": couldn't enable module\n");
186 return;
187 }
188
189 sc->sc_dev = self;
190 sc->sc_bst = faa->faa_bst;
191 if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
192 aprint_error(": couldn't map registers\n");
193 return;
194 }
195
196 aprint_naive("\n");
197 aprint_normal(": OMAP HS USB Host TLL\n");
198
199 ti_usbtll_init(sc);
200
201 if (ti_usbtll_sc == NULL)
202 ti_usbtll_sc = sc;
203 }
204