xref: /freebsd/sys/arm/ti/usb/omap_tll.c (revision 1d386b48)
1 /*-
2  * Copyright (c) 2011
3  *	Ben Gray <ben.r.gray@gmail.com>.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, 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 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/conf.h>
32 #include <sys/kernel.h>
33 #include <sys/rman.h>
34 #include <sys/module.h>
35 
36 #include <dev/ofw/ofw_bus_subr.h>
37 
38 #include <machine/bus.h>
39 
40 #include <arm/ti/ti_sysc.h>
41 #include <arm/ti/usb/omap_usb.h>
42 
43 /*
44  * USB TLL Module
45  */
46 #define	OMAP_USBTLL_REVISION                        0x0000
47 #define	OMAP_USBTLL_SYSCONFIG                       0x0010
48 #define	OMAP_USBTLL_SYSSTATUS                       0x0014
49 #define	OMAP_USBTLL_IRQSTATUS                       0x0018
50 #define	OMAP_USBTLL_IRQENABLE                       0x001C
51 #define	OMAP_USBTLL_TLL_SHARED_CONF                 0x0030
52 #define	OMAP_USBTLL_TLL_CHANNEL_CONF(i)             (0x0040 + (0x04 * (i)))
53 #define	OMAP_USBTLL_SAR_CNTX(i)                     (0x0400 + (0x04 * (i)))
54 #define	OMAP_USBTLL_ULPI_VENDOR_ID_LO(i)            (0x0800 + (0x100 * (i)))
55 #define	OMAP_USBTLL_ULPI_VENDOR_ID_HI(i)            (0x0801 + (0x100 * (i)))
56 #define	OMAP_USBTLL_ULPI_PRODUCT_ID_LO(i)           (0x0802 + (0x100 * (i)))
57 #define	OMAP_USBTLL_ULPI_PRODUCT_ID_HI(i)           (0x0803 + (0x100 * (i)))
58 #define	OMAP_USBTLL_ULPI_FUNCTION_CTRL(i)           (0x0804 + (0x100 * (i)))
59 #define	OMAP_USBTLL_ULPI_FUNCTION_CTRL_SET(i)       (0x0805 + (0x100 * (i)))
60 #define	OMAP_USBTLL_ULPI_FUNCTION_CTRL_CLR(i)       (0x0806 + (0x100 * (i)))
61 #define	OMAP_USBTLL_ULPI_INTERFACE_CTRL(i)          (0x0807 + (0x100 * (i)))
62 #define	OMAP_USBTLL_ULPI_INTERFACE_CTRL_SET(i)      (0x0808 + (0x100 * (i)))
63 #define	OMAP_USBTLL_ULPI_INTERFACE_CTRL_CLR(i)      (0x0809 + (0x100 * (i)))
64 #define	OMAP_USBTLL_ULPI_OTG_CTRL(i)                (0x080A + (0x100 * (i)))
65 #define	OMAP_USBTLL_ULPI_OTG_CTRL_SET(i)            (0x080B + (0x100 * (i)))
66 #define	OMAP_USBTLL_ULPI_OTG_CTRL_CLR(i)            (0x080C + (0x100 * (i)))
67 #define	OMAP_USBTLL_ULPI_USB_INT_EN_RISE(i)         (0x080D + (0x100 * (i)))
68 #define	OMAP_USBTLL_ULPI_USB_INT_EN_RISE_SET(i)     (0x080E + (0x100 * (i)))
69 #define	OMAP_USBTLL_ULPI_USB_INT_EN_RISE_CLR(i)     (0x080F + (0x100 * (i)))
70 #define	OMAP_USBTLL_ULPI_USB_INT_EN_FALL(i)         (0x0810 + (0x100 * (i)))
71 #define	OMAP_USBTLL_ULPI_USB_INT_EN_FALL_SET(i)     (0x0811 + (0x100 * (i)))
72 #define	OMAP_USBTLL_ULPI_USB_INT_EN_FALL_CLR(i)     (0x0812 + (0x100 * (i)))
73 #define	OMAP_USBTLL_ULPI_USB_INT_STATUS(i)          (0x0813 + (0x100 * (i)))
74 #define	OMAP_USBTLL_ULPI_USB_INT_LATCH(i)           (0x0814 + (0x100 * (i)))
75 #define	OMAP_USBTLL_ULPI_DEBUG(i)                   (0x0815 + (0x100 * (i)))
76 #define	OMAP_USBTLL_ULPI_SCRATCH_REGISTER(i)        (0x0816 + (0x100 * (i)))
77 #define	OMAP_USBTLL_ULPI_SCRATCH_REGISTER_SET(i)    (0x0817 + (0x100 * (i)))
78 #define	OMAP_USBTLL_ULPI_SCRATCH_REGISTER_CLR(i)    (0x0818 + (0x100 * (i)))
79 #define	OMAP_USBTLL_ULPI_EXTENDED_SET_ACCESS(i)     (0x082F + (0x100 * (i)))
80 #define	OMAP_USBTLL_ULPI_UTMI_VCONTROL_EN(i)        (0x0830 + (0x100 * (i)))
81 #define	OMAP_USBTLL_ULPI_UTMI_VCONTROL_EN_SET(i)    (0x0831 + (0x100 * (i)))
82 #define	OMAP_USBTLL_ULPI_UTMI_VCONTROL_EN_CLR(i)    (0x0832 + (0x100 * (i)))
83 #define	OMAP_USBTLL_ULPI_UTMI_VCONTROL_STATUS(i)    (0x0833 + (0x100 * (i)))
84 #define	OMAP_USBTLL_ULPI_UTMI_VCONTROL_LATCH(i)     (0x0834 + (0x100 * (i)))
85 #define	OMAP_USBTLL_ULPI_UTMI_VSTATUS(i)            (0x0835 + (0x100 * (i)))
86 #define	OMAP_USBTLL_ULPI_UTMI_VSTATUS_SET(i)        (0x0836 + (0x100 * (i)))
87 #define	OMAP_USBTLL_ULPI_UTMI_VSTATUS_CLR(i)        (0x0837 + (0x100 * (i)))
88 #define	OMAP_USBTLL_ULPI_USB_INT_LATCH_NOCLR(i)     (0x0838 + (0x100 * (i)))
89 #define	OMAP_USBTLL_ULPI_VENDOR_INT_EN(i)           (0x083B + (0x100 * (i)))
90 #define	OMAP_USBTLL_ULPI_VENDOR_INT_EN_SET(i)       (0x083C + (0x100 * (i)))
91 #define	OMAP_USBTLL_ULPI_VENDOR_INT_EN_CLR(i)       (0x083D + (0x100 * (i)))
92 #define	OMAP_USBTLL_ULPI_VENDOR_INT_STATUS(i)       (0x083E + (0x100 * (i)))
93 #define	OMAP_USBTLL_ULPI_VENDOR_INT_LATCH(i)        (0x083F + (0x100 * (i)))
94 
95 /* TLL Register Set */
96 #define	TLL_SYSCONFIG_CACTIVITY                 (1UL << 8)
97 #define	TLL_SYSCONFIG_SIDLE_SMART_IDLE          (2UL << 3)
98 #define	TLL_SYSCONFIG_SIDLE_NO_IDLE             (1UL << 3)
99 #define	TLL_SYSCONFIG_SIDLE_FORCED_IDLE         (0UL << 3)
100 #define	TLL_SYSCONFIG_ENAWAKEUP                 (1UL << 2)
101 #define	TLL_SYSCONFIG_SOFTRESET                 (1UL << 1)
102 #define	TLL_SYSCONFIG_AUTOIDLE                  (1UL << 0)
103 
104 #define	TLL_SYSSTATUS_RESETDONE                 (1UL << 0)
105 
106 #define TLL_SHARED_CONF_USB_90D_DDR_EN          (1UL << 6)
107 #define TLL_SHARED_CONF_USB_180D_SDR_EN         (1UL << 5)
108 #define TLL_SHARED_CONF_USB_DIVRATIO_MASK       (7UL << 2)
109 #define TLL_SHARED_CONF_USB_DIVRATIO_128        (7UL << 2)
110 #define TLL_SHARED_CONF_USB_DIVRATIO_64         (6UL << 2)
111 #define TLL_SHARED_CONF_USB_DIVRATIO_32         (5UL << 2)
112 #define TLL_SHARED_CONF_USB_DIVRATIO_16         (4UL << 2)
113 #define TLL_SHARED_CONF_USB_DIVRATIO_8          (3UL << 2)
114 #define TLL_SHARED_CONF_USB_DIVRATIO_4          (2UL << 2)
115 #define TLL_SHARED_CONF_USB_DIVRATIO_2          (1UL << 2)
116 #define TLL_SHARED_CONF_USB_DIVRATIO_1          (0UL << 2)
117 #define TLL_SHARED_CONF_FCLK_REQ                (1UL << 1)
118 #define TLL_SHARED_CONF_FCLK_IS_ON              (1UL << 0)
119 
120 #define TLL_CHANNEL_CONF_DRVVBUS                (1UL << 16)
121 #define TLL_CHANNEL_CONF_CHRGVBUS               (1UL << 15)
122 #define TLL_CHANNEL_CONF_ULPINOBITSTUFF         (1UL << 11)
123 #define TLL_CHANNEL_CONF_ULPIAUTOIDLE           (1UL << 10)
124 #define TLL_CHANNEL_CONF_UTMIAUTOIDLE           (1UL << 9)
125 #define TLL_CHANNEL_CONF_ULPIDDRMODE            (1UL << 8)
126 #define TLL_CHANNEL_CONF_ULPIOUTCLKMODE         (1UL << 7)
127 #define TLL_CHANNEL_CONF_TLLFULLSPEED           (1UL << 6)
128 #define TLL_CHANNEL_CONF_TLLCONNECT             (1UL << 5)
129 #define TLL_CHANNEL_CONF_TLLATTACH              (1UL << 4)
130 #define TLL_CHANNEL_CONF_UTMIISADEV             (1UL << 3)
131 #define TLL_CHANNEL_CONF_CHANEN                 (1UL << 0)
132 
133 struct omap_tll_softc {
134 	device_t		sc_dev;
135 
136 	/* TLL register set */
137 	struct resource*	tll_mem_res;
138 	int			tll_mem_rid;
139 };
140 
141 static struct omap_tll_softc *omap_tll_sc;
142 
143 static int omap_tll_attach(device_t dev);
144 static int omap_tll_detach(device_t dev);
145 
146 static inline uint32_t
147 omap_tll_read_4(struct omap_tll_softc *sc, bus_size_t off)
148 {
149 	return bus_read_4(sc->tll_mem_res, off);
150 }
151 
152 static inline void
153 omap_tll_write_4(struct omap_tll_softc *sc, bus_size_t off, uint32_t val)
154 {
155 	bus_write_4(sc->tll_mem_res, off, val);
156 }
157 
158 void
159 omap_tll_utmi_enable(unsigned int en_mask)
160 {
161 	struct omap_tll_softc *sc;
162 	unsigned int i;
163 	uint32_t reg;
164 
165 	sc = omap_tll_sc;
166 	if (sc == NULL)
167 		return;
168 
169 	/* There are 3 TLL channels, one per USB controller so set them all up the
170 	 * same, SDR mode, bit stuffing and no autoidle.
171 	 */
172 	for (i=0; i<3; i++) {
173 		reg = omap_tll_read_4(sc, OMAP_USBTLL_TLL_CHANNEL_CONF(i));
174 
175 		reg &= ~(TLL_CHANNEL_CONF_UTMIAUTOIDLE
176 				 | TLL_CHANNEL_CONF_ULPINOBITSTUFF
177 				 | TLL_CHANNEL_CONF_ULPIDDRMODE);
178 
179 		omap_tll_write_4(sc, OMAP_USBTLL_TLL_CHANNEL_CONF(i), reg);
180 	}
181 
182 	/* Program the common TLL register */
183 	reg = omap_tll_read_4(sc, OMAP_USBTLL_TLL_SHARED_CONF);
184 
185 	reg &= ~( TLL_SHARED_CONF_USB_90D_DDR_EN
186 			| TLL_SHARED_CONF_USB_DIVRATIO_MASK);
187 	reg |=  ( TLL_SHARED_CONF_FCLK_IS_ON
188 			| TLL_SHARED_CONF_USB_DIVRATIO_2
189 			| TLL_SHARED_CONF_USB_180D_SDR_EN);
190 
191 	omap_tll_write_4(sc, OMAP_USBTLL_TLL_SHARED_CONF, reg);
192 
193 	/* Enable channels now */
194 	for (i = 0; i < 3; i++) {
195 		reg = omap_tll_read_4(sc, OMAP_USBTLL_TLL_CHANNEL_CONF(i));
196 
197 		/* Enable only the reg that is needed */
198 		if ((en_mask & (1 << i)) == 0)
199 			continue;
200 
201 		reg |= TLL_CHANNEL_CONF_CHANEN;
202 		omap_tll_write_4(sc, OMAP_USBTLL_TLL_CHANNEL_CONF(i), reg);
203 	}
204 }
205 
206 static int
207 omap_tll_init(struct omap_tll_softc *sc)
208 {
209 	unsigned long timeout;
210 	int ret = 0;
211 
212 	/* Enable the USB TLL */
213 	ti_sysc_clock_enable(device_get_parent(sc->sc_dev));
214 
215 	/* Perform TLL soft reset, and wait until reset is complete */
216 	omap_tll_write_4(sc, OMAP_USBTLL_SYSCONFIG, TLL_SYSCONFIG_SOFTRESET);
217 
218 	/* Set the timeout to 100ms*/
219 	timeout = (hz < 10) ? 1 : ((100 * hz) / 1000);
220 
221 	/* Wait for TLL reset to complete */
222 	while ((omap_tll_read_4(sc, OMAP_USBTLL_SYSSTATUS) &
223 	        TLL_SYSSTATUS_RESETDONE) == 0x00) {
224 		/* Sleep for a tick */
225 		pause("USBRESET", 1);
226 
227 		if (timeout-- == 0) {
228 			device_printf(sc->sc_dev, "TLL reset operation timed out\n");
229 			ret = EINVAL;
230 			goto err_sys_status;
231 		}
232 	}
233 
234 	/* CLOCKACTIVITY = 1 : OCP-derived internal clocks ON during idle
235 	 * SIDLEMODE = 2     : Smart-idle mode. Sidleack asserted after Idlereq
236 	 *                     assertion when no more activity on the USB.
237 	 * ENAWAKEUP = 1     : Wakeup generation enabled
238 	 */
239 	omap_tll_write_4(sc, OMAP_USBTLL_SYSCONFIG, TLL_SYSCONFIG_ENAWAKEUP |
240 	                                            TLL_SYSCONFIG_AUTOIDLE |
241 	                                            TLL_SYSCONFIG_SIDLE_SMART_IDLE |
242 	                                            TLL_SYSCONFIG_CACTIVITY);
243 
244 	return(0);
245 
246 err_sys_status:
247 	/* Disable the TLL clocks */
248 	ti_sysc_clock_disable(device_get_parent(sc->sc_dev));
249 
250 	return(ret);
251 }
252 
253 static void
254 omap_tll_disable(struct omap_tll_softc *sc)
255 {
256 	unsigned long timeout;
257 
258 	timeout = (hz < 10) ? 1 : ((100 * hz) / 1000);
259 
260 	/* Reset the TLL module */
261 	omap_tll_write_4(sc, OMAP_USBTLL_SYSCONFIG, 0x0002);
262 	while ((omap_tll_read_4(sc, OMAP_USBTLL_SYSSTATUS) & (0x01)) == 0x00) {
263 		/* Sleep for a tick */
264 		pause("USBRESET", 1);
265 
266 		if (timeout-- == 0) {
267 			device_printf(sc->sc_dev, "operation timed out\n");
268 			break;
269 		}
270 	}
271 
272 	/* Disable functional and interface clocks for the TLL and HOST modules */
273 	ti_sysc_clock_disable(device_get_parent(sc->sc_dev));
274 }
275 
276 static int
277 omap_tll_probe(device_t dev)
278 {
279 
280 	if (!ofw_bus_status_okay(dev))
281 		return (ENXIO);
282 
283 	if (!ofw_bus_is_compatible(dev, "ti,usbhs-tll"))
284 		return (ENXIO);
285 
286 	device_set_desc(dev, "TI OMAP USB 2.0 TLL module");
287 
288 	return (BUS_PROBE_DEFAULT);
289 }
290 
291 static int
292 omap_tll_attach(device_t dev)
293 {
294 	struct omap_tll_softc *sc;
295 
296 	sc = device_get_softc(dev);
297 	/* save the device */
298 	sc->sc_dev = dev;
299 
300 	/* Allocate resource for the TLL register set */
301 	sc->tll_mem_rid = 0;
302 	sc->tll_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
303 	    &sc->tll_mem_rid, RF_ACTIVE);
304 	if (!sc->tll_mem_res) {
305 		device_printf(dev, "Error: Could not map TLL memory\n");
306 		goto error;
307 	}
308 
309 	omap_tll_init(sc);
310 
311 	omap_tll_sc = sc;
312 
313 	return (0);
314 
315 error:
316 	omap_tll_detach(dev);
317 	return (ENXIO);
318 }
319 
320 static int
321 omap_tll_detach(device_t dev)
322 {
323 	struct omap_tll_softc *sc;
324 
325 	sc = device_get_softc(dev);
326 	omap_tll_disable(sc);
327 
328 	/* Release the other register set memory maps */
329 	if (sc->tll_mem_res) {
330 		bus_release_resource(dev, SYS_RES_MEMORY,
331 		    sc->tll_mem_rid, sc->tll_mem_res);
332 		sc->tll_mem_res = NULL;
333 	}
334 
335 	omap_tll_sc = NULL;
336 
337 	return (0);
338 }
339 
340 static device_method_t omap_tll_methods[] = {
341 	/* Device interface */
342 	DEVMETHOD(device_probe, omap_tll_probe),
343 	DEVMETHOD(device_attach, omap_tll_attach),
344 	DEVMETHOD(device_detach, omap_tll_detach),
345 	DEVMETHOD(device_suspend, bus_generic_suspend),
346 	DEVMETHOD(device_resume, bus_generic_resume),
347 	DEVMETHOD(device_shutdown, bus_generic_shutdown),
348 	{0, 0}
349 };
350 
351 static driver_t omap_tll_driver = {
352 	"omap_tll",
353 	omap_tll_methods,
354 	sizeof(struct omap_tll_softc),
355 };
356 
357 DRIVER_MODULE(omap_tll, simplebus, omap_tll_driver, 0, 0);
358