omap_ehci.c revision 239281
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/**
29 * Driver for the High Speed USB EHCI module on the TI OMAP3530 SoC.
30 *
31 * WARNING: I've only tried this driver on a limited number of USB peripherals,
32 * it is still very raw and bound to have numerous bugs in it.
33 *
34 * This driver is based on the FreeBSD IXP4xx EHCI driver with a lot of the
35 * setup sequence coming from the Linux community and their EHCI driver for
36 * OMAP.  Without these as a base I don't think I would have been able to get
37 * this driver working.
38 *
39 * The driver only contains the EHCI parts, the module also supports OHCI and
40 * USB on-the-go (OTG), currently neither are supported.
41 *
42 * CAUTION: This driver was written to run on the beaglebaord dev board, so I
43 * have made some assumptions about the type of PHY used and some of the other
44 * settings.  Bare that in mind if you intend to use this driver on another
45 * platform.
46 *
47 * NOTE: This module uses a few different clocks, one being a 60Mhz clock for
48 * the TTL part of the module.  This clock is derived from DPPL5 which must be
49 * configured prior to loading this driver - it is not configured by the
50 * bootloader.  It took me a long time to figure this out, and caused much
51 * frustration.  This PLL is now setup in the timer/clocks part of the BSP,
52 * check out the omap_prcm_setup_dpll5() function in omap_prcm.c for more info.
53 *
54 */
55
56#include <sys/cdefs.h>
57__FBSDID("$FreeBSD: head/sys/arm/ti/usb/omap_ehci.c 239281 2012-08-15 06:31:32Z gonzo $");
58
59#include "opt_bus.h"
60
61#include <sys/stdint.h>
62#include <sys/stddef.h>
63#include <sys/param.h>
64#include <sys/queue.h>
65#include <sys/types.h>
66#include <sys/systm.h>
67#include <sys/kernel.h>
68#include <sys/bus.h>
69#include <sys/rman.h>
70#include <sys/linker_set.h>
71#include <sys/module.h>
72#include <sys/lock.h>
73#include <sys/mutex.h>
74#include <sys/condvar.h>
75#include <sys/sysctl.h>
76#include <sys/sx.h>
77#include <sys/unistd.h>
78#include <sys/callout.h>
79#include <sys/malloc.h>
80#include <sys/priv.h>
81#include <sys/gpio.h>
82
83#include <dev/fdt/fdt_common.h>
84#include <dev/ofw/openfirm.h>
85#include <dev/ofw/ofw_bus.h>
86#include <dev/ofw/ofw_bus_subr.h>
87
88#include <dev/usb/usb.h>
89#include <dev/usb/usbdi.h>
90
91#include <dev/usb/usb_core.h>
92#include <dev/usb/usb_busdma.h>
93#include <dev/usb/usb_process.h>
94#include <dev/usb/usb_util.h>
95
96#include <dev/usb/usb_controller.h>
97#include <dev/usb/usb_bus.h>
98#include <dev/usb/controller/ehci.h>
99#include <dev/usb/controller/ehcireg.h>
100
101#include <arm/ti/tivar.h>
102#include <arm/ti/ti_prcm.h>
103#include <arm/ti/ti_scm.h>
104
105#include <arm/ti/usb/omap_usb.h>
106
107#include "gpio_if.h"
108
109struct omap_ehci_softc {
110	ehci_softc_t        base;	/* storage for EHCI code */
111
112	device_t            sc_dev;
113	device_t            sc_gpio_dev;
114
115	/* TLL register set */
116	struct resource*    tll_mem_res;
117
118	/* UHH register set */
119	struct resource*    uhh_mem_res;
120
121	/* The revision of the HS USB HOST read from UHH_REVISION */
122	uint32_t            ehci_rev;
123
124	/* The following details are provided by conf hints */
125	int                 port_mode[3];
126	int                 phy_reset[3];
127	int                 reset_gpio_pin[3];
128};
129
130static device_attach_t omap_ehci_attach;
131static device_detach_t omap_ehci_detach;
132static device_shutdown_t omap_ehci_shutdown;
133static device_suspend_t omap_ehci_suspend;
134static device_resume_t omap_ehci_resume;
135
136/**
137 *	omap_tll_read_4 - read a 32-bit value from the USBTLL registers
138 *	omap_tll_write_4 - write a 32-bit value from the USBTLL registers
139 *	omap_tll_readb - read an 8-bit value from the USBTLL registers
140 *	omap_tll_writeb - write an 8-bit value from the USBTLL registers
141 *	@sc: omap ehci device context
142 *	@off: byte offset within the register set to read from
143 *	@val: the value to write into the register
144 *
145 *
146 *	LOCKING:
147 *	None
148 *
149 *	RETURNS:
150 *	nothing in case of write function, if read function returns the value read.
151 */
152static inline uint32_t
153omap_tll_read_4(struct omap_ehci_softc *sc, bus_size_t off)
154{
155	return bus_read_4(sc->tll_mem_res, off);
156}
157
158static inline void
159omap_tll_write_4(struct omap_ehci_softc *sc, bus_size_t off, uint32_t val)
160{
161	bus_write_4(sc->tll_mem_res, off, val);
162}
163
164static inline uint8_t
165omap_tll_readb(struct omap_ehci_softc *sc, bus_size_t off)
166{
167	return bus_read_1(sc->tll_mem_res, off);
168}
169
170static inline void
171omap_tll_writeb(struct omap_ehci_softc *sc, bus_size_t off, uint8_t val)
172{
173	bus_write_1(sc->tll_mem_res, off, val);
174}
175
176/**
177 *	omap_ehci_read_4 - read a 32-bit value from the EHCI registers
178 *	omap_ehci_write_4 - write a 32-bit value from the EHCI registers
179 *	@sc: omap ehci device context
180 *	@off: byte offset within the register set to read from
181 *	@val: the value to write into the register
182 *
183 *
184 *	LOCKING:
185 *	None
186 *
187 *	RETURNS:
188 *	nothing in case of write function, if read function returns the value read.
189 */
190static inline uint32_t
191omap_ehci_read_4(struct omap_ehci_softc *sc, bus_size_t off)
192{
193	return (bus_read_4(sc->base.sc_io_res, off));
194}
195static inline void
196omap_ehci_write_4(struct omap_ehci_softc *sc, bus_size_t off, uint32_t val)
197{
198	bus_write_4(sc->base.sc_io_res, off, val);
199}
200
201/**
202 *	omap_uhh_read_4 - read a 32-bit value from the UHH registers
203 *	omap_uhh_write_4 - write a 32-bit value from the UHH registers
204 *	@sc: omap ehci device context
205 *	@off: byte offset within the register set to read from
206 *	@val: the value to write into the register
207 *
208 *
209 *	LOCKING:
210 *	None
211 *
212 *	RETURNS:
213 *	nothing in case of write function, if read function returns the value read.
214 */
215static inline uint32_t
216omap_uhh_read_4(struct omap_ehci_softc *sc, bus_size_t off)
217{
218	return bus_read_4(sc->uhh_mem_res, off);
219}
220static inline void
221omap_uhh_write_4(struct omap_ehci_softc *sc, bus_size_t off, uint32_t val)
222{
223	bus_write_4(sc->uhh_mem_res, off, val);
224}
225
226/**
227 *	omap_ehci_utmi_init - initialises the UTMI part of the controller
228 *	@isc: omap ehci device context
229 *
230 *
231 *
232 *	LOCKING:
233 *	none
234 *
235 *	RETURNS:
236 *	nothing
237 */
238static void
239omap_ehci_utmi_init(struct omap_ehci_softc *isc, unsigned int en_mask)
240{
241	unsigned int i;
242	uint32_t reg;
243
244	/* There are 3 TLL channels, one per USB controller so set them all up the
245	 * same, SDR mode, bit stuffing and no autoidle.
246	 */
247	for (i=0; i<3; i++) {
248		reg = omap_tll_read_4(isc, OMAP_USBTLL_TLL_CHANNEL_CONF(i));
249
250		reg &= ~(TLL_CHANNEL_CONF_UTMIAUTOIDLE
251				 | TLL_CHANNEL_CONF_ULPINOBITSTUFF
252				 | TLL_CHANNEL_CONF_ULPIDDRMODE);
253
254		omap_tll_write_4(isc, OMAP_USBTLL_TLL_CHANNEL_CONF(i), reg);
255	}
256
257	/* Program the common TLL register */
258	reg = omap_tll_read_4(isc, OMAP_USBTLL_TLL_SHARED_CONF);
259
260	reg &= ~( TLL_SHARED_CONF_USB_90D_DDR_EN
261			| TLL_SHARED_CONF_USB_DIVRATIO_MASK);
262	reg |=  ( TLL_SHARED_CONF_FCLK_IS_ON
263			| TLL_SHARED_CONF_USB_DIVRATIO_2
264			| TLL_SHARED_CONF_USB_180D_SDR_EN);
265
266	omap_tll_write_4(isc, OMAP_USBTLL_TLL_SHARED_CONF, reg);
267
268	/* Enable channels now */
269	for (i = 0; i < 3; i++) {
270		reg = omap_tll_read_4(isc, OMAP_USBTLL_TLL_CHANNEL_CONF(i));
271
272		/* Enable only the reg that is needed */
273		if ((en_mask & (1 << i)) == 0)
274			continue;
275
276		reg |= TLL_CHANNEL_CONF_CHANEN;
277		omap_tll_write_4(isc, OMAP_USBTLL_TLL_CHANNEL_CONF(i), reg);
278	}
279}
280
281/**
282 *	omap_ehci_soft_phy_reset - resets the phy using the reset command
283 *	@isc: omap ehci device context
284 *	@port: port to send the reset over
285 *
286 *
287 *	LOCKING:
288 *	none
289 *
290 *	RETURNS:
291 *	nothing
292 */
293static void
294omap_ehci_soft_phy_reset(struct omap_ehci_softc *isc, unsigned int port)
295{
296	unsigned long timeout = (hz < 10) ? 1 : ((100 * hz) / 1000);
297	uint32_t reg;
298
299	reg = ULPI_FUNC_CTRL_RESET
300		/* FUNCTION_CTRL_SET register */
301		| (ULPI_SET(ULPI_FUNC_CTRL) << OMAP_USBHOST_INSNREG05_ULPI_REGADD_SHIFT)
302		/* Write */
303		| (2 << OMAP_USBHOST_INSNREG05_ULPI_OPSEL_SHIFT)
304		/* PORTn */
305		| ((port + 1) << OMAP_USBHOST_INSNREG05_ULPI_PORTSEL_SHIFT)
306		/* start ULPI access*/
307		| (1 << OMAP_USBHOST_INSNREG05_ULPI_CONTROL_SHIFT);
308
309	omap_ehci_write_4(isc, OMAP_USBHOST_INSNREG05_ULPI, reg);
310
311	/* Wait for ULPI access completion */
312	while ((omap_ehci_read_4(isc, OMAP_USBHOST_INSNREG05_ULPI)
313	       & (1 << OMAP_USBHOST_INSNREG05_ULPI_CONTROL_SHIFT))) {
314
315		/* Sleep for a tick */
316		pause("USBPHY_RESET", 1);
317
318		if (timeout-- == 0) {
319			device_printf(isc->sc_dev, "PHY reset operation timed out\n");
320			break;
321		}
322	}
323}
324
325
326/**
327 *	omap_ehci_init - initialises the USB host EHCI controller
328 *	@isc: omap ehci device context
329 *
330 *	This initialisation routine is quite heavily based on the work done by the
331 *	OMAP Linux team (for which I thank them very much).  The init sequence is
332 *	almost identical, diverging only for the FreeBSD specifics.
333 *
334 *	LOCKING:
335 *	none
336 *
337 *	RETURNS:
338 *	0 on success, a negative error code on failure.
339 */
340static int
341omap_ehci_init(struct omap_ehci_softc *isc)
342{
343	unsigned long timeout;
344	int ret = 0;
345	uint8_t tll_ch_mask = 0;
346	uint32_t reg = 0;
347	int reset_performed = 0;
348	int i;
349
350	device_printf(isc->sc_dev, "Starting TI EHCI USB Controller\n");
351
352
353	/* Enable Clocks for high speed USBHOST */
354	ti_prcm_clk_enable(USBHSHOST_CLK);
355
356	/* Hold the PHY in reset while configuring */
357	for (int i = 0; i < 3; i++) {
358		if (isc->phy_reset[i]) {
359			/* Configure the GPIO to drive low (hold in reset) */
360			if ((isc->reset_gpio_pin[i] != -1) && (isc->sc_gpio_dev != NULL)) {
361				GPIO_PIN_SETFLAGS(isc->sc_gpio_dev, isc->reset_gpio_pin[i],
362				    GPIO_PIN_OUTPUT);
363				GPIO_PIN_SET(isc->sc_gpio_dev, isc->reset_gpio_pin[i],
364				    GPIO_PIN_LOW);
365				reset_performed = 1;
366			}
367		}
368	}
369
370	/* Hold the PHY in RESET for enough time till DIR is high */
371	if (reset_performed)
372		DELAY(10);
373
374	/* Read the UHH revision */
375	isc->ehci_rev = omap_uhh_read_4(isc, OMAP_USBHOST_UHH_REVISION);
376	device_printf(isc->sc_dev, "UHH revision 0x%08x\n", isc->ehci_rev);
377
378	/* Initilise the low level interface module(s) */
379	if (isc->ehci_rev == OMAP_EHCI_REV1) {
380
381		/* Enable the USB TLL */
382		ti_prcm_clk_enable(USBTLL_CLK);
383
384		/* Perform TLL soft reset, and wait until reset is complete */
385		omap_tll_write_4(isc, OMAP_USBTLL_SYSCONFIG, TLL_SYSCONFIG_SOFTRESET);
386
387		/* Set the timeout to 100ms*/
388		timeout = (hz < 10) ? 1 : ((100 * hz) / 1000);
389
390		/* Wait for TLL reset to complete */
391		while ((omap_tll_read_4(isc, OMAP_USBTLL_SYSSTATUS) &
392		        TLL_SYSSTATUS_RESETDONE) == 0x00) {
393
394			/* Sleep for a tick */
395			pause("USBRESET", 1);
396
397			if (timeout-- == 0) {
398				device_printf(isc->sc_dev, "TLL reset operation timed out\n");
399				ret = EINVAL;
400				goto err_sys_status;
401			}
402		}
403
404		device_printf(isc->sc_dev, "TLL RESET DONE\n");
405
406		/* CLOCKACTIVITY = 1 : OCP-derived internal clocks ON during idle
407		 * SIDLEMODE = 2     : Smart-idle mode. Sidleack asserted after Idlereq
408		 *                     assertion when no more activity on the USB.
409		 * ENAWAKEUP = 1     : Wakeup generation enabled
410		 */
411		omap_tll_write_4(isc, OMAP_USBTLL_SYSCONFIG, TLL_SYSCONFIG_ENAWAKEUP |
412		                                            TLL_SYSCONFIG_AUTOIDLE |
413		                                            TLL_SYSCONFIG_SIDLE_SMART_IDLE |
414		                                            TLL_SYSCONFIG_CACTIVITY);
415
416	} else if (isc->ehci_rev == OMAP_EHCI_REV2) {
417
418		/* For OMAP44xx devices you have to enable the per-port clocks:
419		 *  PHY_MODE  - External ULPI clock
420		 *  TTL_MODE  - Internal UTMI clock
421		 *  HSIC_MODE - Internal 480Mhz and 60Mhz clocks
422		 */
423		if (isc->ehci_rev == OMAP_EHCI_REV2) {
424			if (isc->port_mode[0] == EHCI_HCD_OMAP_MODE_PHY) {
425				ti_prcm_clk_set_source(USBP1_PHY_CLK, EXT_CLK);
426				ti_prcm_clk_enable(USBP1_PHY_CLK);
427			} else if (isc->port_mode[0] == EHCI_HCD_OMAP_MODE_TLL)
428				ti_prcm_clk_enable(USBP1_UTMI_CLK);
429			else if (isc->port_mode[0] == EHCI_HCD_OMAP_MODE_HSIC)
430				ti_prcm_clk_enable(USBP1_HSIC_CLK);
431
432			if (isc->port_mode[1] == EHCI_HCD_OMAP_MODE_PHY) {
433				ti_prcm_clk_set_source(USBP2_PHY_CLK, EXT_CLK);
434				ti_prcm_clk_enable(USBP2_PHY_CLK);
435			} else if (isc->port_mode[1] == EHCI_HCD_OMAP_MODE_TLL)
436				ti_prcm_clk_enable(USBP2_UTMI_CLK);
437			else if (isc->port_mode[1] == EHCI_HCD_OMAP_MODE_HSIC)
438				ti_prcm_clk_enable(USBP2_HSIC_CLK);
439		}
440	}
441
442	/* Put UHH in SmartIdle/SmartStandby mode */
443	reg = omap_uhh_read_4(isc, OMAP_USBHOST_UHH_SYSCONFIG);
444	if (isc->ehci_rev == OMAP_EHCI_REV1) {
445		reg &= ~(UHH_SYSCONFIG_SIDLEMODE_MASK |
446		         UHH_SYSCONFIG_MIDLEMODE_MASK);
447		reg |= (UHH_SYSCONFIG_ENAWAKEUP |
448		        UHH_SYSCONFIG_AUTOIDLE |
449		        UHH_SYSCONFIG_CLOCKACTIVITY |
450		        UHH_SYSCONFIG_SIDLEMODE_SMARTIDLE |
451		        UHH_SYSCONFIG_MIDLEMODE_SMARTSTANDBY);
452	} else if (isc->ehci_rev == OMAP_EHCI_REV2) {
453		reg &= ~UHH_SYSCONFIG_IDLEMODE_MASK;
454		reg |=  UHH_SYSCONFIG_IDLEMODE_NOIDLE;
455		reg &= ~UHH_SYSCONFIG_STANDBYMODE_MASK;
456		reg |=  UHH_SYSCONFIG_STANDBYMODE_NOSTDBY;
457	}
458	omap_uhh_write_4(isc, OMAP_USBHOST_UHH_SYSCONFIG, reg);
459	device_printf(isc->sc_dev, "OMAP_UHH_SYSCONFIG: 0x%08x\n", reg);
460
461	reg = omap_uhh_read_4(isc, OMAP_USBHOST_UHH_HOSTCONFIG);
462
463	/* Setup ULPI bypass and burst configurations */
464	reg |= (UHH_HOSTCONFIG_ENA_INCR4 |
465			UHH_HOSTCONFIG_ENA_INCR8 |
466			UHH_HOSTCONFIG_ENA_INCR16);
467	reg &= ~UHH_HOSTCONFIG_ENA_INCR_ALIGN;
468
469	if (isc->ehci_rev == OMAP_EHCI_REV1) {
470		if (isc->port_mode[0] == EHCI_HCD_OMAP_MODE_UNKNOWN)
471			reg &= ~UHH_HOSTCONFIG_P1_CONNECT_STATUS;
472		if (isc->port_mode[1] == EHCI_HCD_OMAP_MODE_UNKNOWN)
473			reg &= ~UHH_HOSTCONFIG_P2_CONNECT_STATUS;
474		if (isc->port_mode[2] == EHCI_HCD_OMAP_MODE_UNKNOWN)
475			reg &= ~UHH_HOSTCONFIG_P3_CONNECT_STATUS;
476
477		/* Bypass the TLL module for PHY mode operation */
478		if ((isc->port_mode[0] == EHCI_HCD_OMAP_MODE_PHY) ||
479		    (isc->port_mode[1] == EHCI_HCD_OMAP_MODE_PHY) ||
480		    (isc->port_mode[2] == EHCI_HCD_OMAP_MODE_PHY))
481			reg &= ~UHH_HOSTCONFIG_P1_ULPI_BYPASS;
482		else
483			reg |= UHH_HOSTCONFIG_P1_ULPI_BYPASS;
484
485	} else if (isc->ehci_rev == OMAP_EHCI_REV2) {
486		reg |=  UHH_HOSTCONFIG_APP_START_CLK;
487
488		/* Clear port mode fields for PHY mode*/
489		reg &= ~UHH_HOSTCONFIG_P1_MODE_MASK;
490		reg &= ~UHH_HOSTCONFIG_P2_MODE_MASK;
491
492		if (isc->port_mode[0] == EHCI_HCD_OMAP_MODE_TLL)
493			reg |= UHH_HOSTCONFIG_P1_MODE_UTMI_PHY;
494		else if (isc->port_mode[0] == EHCI_HCD_OMAP_MODE_HSIC)
495			reg |= UHH_HOSTCONFIG_P1_MODE_HSIC;
496
497		if (isc->port_mode[1] == EHCI_HCD_OMAP_MODE_TLL)
498			reg |= UHH_HOSTCONFIG_P2_MODE_UTMI_PHY;
499		else if (isc->port_mode[1] == EHCI_HCD_OMAP_MODE_HSIC)
500			reg |= UHH_HOSTCONFIG_P2_MODE_HSIC;
501	}
502
503	omap_uhh_write_4(isc, OMAP_USBHOST_UHH_HOSTCONFIG, reg);
504	device_printf(isc->sc_dev, "UHH setup done, uhh_hostconfig=0x%08x\n", reg);
505
506
507	/* I found the code and comments in the Linux EHCI driver - thanks guys :)
508	 *
509	 * "An undocumented "feature" in the OMAP3 EHCI controller, causes suspended
510	 * ports to be taken out of suspend when the USBCMD.Run/Stop bit is cleared
511	 * (for example when we do ehci_bus_suspend). This breaks suspend-resume if
512	 * the root-hub is allowed to suspend. Writing 1 to this undocumented
513	 * register bit disables this feature and restores normal behavior."
514	 */
515#if 0
516	omap_ehci_write_4(isc, OMAP_USBHOST_INSNREG04,
517	                 OMAP_USBHOST_INSNREG04_DISABLE_UNSUSPEND);
518#endif
519
520	/* If any of the ports are configured in TLL mode, enable them */
521	if ((isc->port_mode[0] == EHCI_HCD_OMAP_MODE_TLL) ||
522		(isc->port_mode[1] == EHCI_HCD_OMAP_MODE_TLL) ||
523		(isc->port_mode[2] == EHCI_HCD_OMAP_MODE_TLL)) {
524
525		if (isc->port_mode[0] == EHCI_HCD_OMAP_MODE_TLL)
526			tll_ch_mask |= 0x1;
527		if (isc->port_mode[1] == EHCI_HCD_OMAP_MODE_TLL)
528			tll_ch_mask |= 0x2;
529		if (isc->port_mode[2] == EHCI_HCD_OMAP_MODE_TLL)
530			tll_ch_mask |= 0x4;
531
532		/* Enable UTMI mode for required TLL channels */
533		omap_ehci_utmi_init(isc, tll_ch_mask);
534	}
535
536
537	/* Release the PHY reset signal now we have configured everything */
538	if (reset_performed) {
539
540		/* Delay for 10ms */
541		DELAY(10000);
542
543		for (i = 0; i < 3; i++) {
544			/* Release reset */
545
546			if (isc->phy_reset[i] && (isc->reset_gpio_pin[i] != -1)
547			    && (isc->sc_gpio_dev != NULL)) {
548				GPIO_PIN_SET(isc->sc_gpio_dev,
549					isc->reset_gpio_pin[i], GPIO_PIN_HIGH);
550			}
551		}
552	}
553
554	/* Set the interrupt threshold control, it controls the maximum rate at
555	 * which the host controller issues interrupts.  We set it to 1 microframe
556	 * at startup - the default is 8 mircoframes (equates to 1ms).
557	 */
558	reg = omap_ehci_read_4(isc, OMAP_USBHOST_USBCMD);
559	reg &= 0xff00ffff;
560	reg |= (1 << 16);
561	omap_ehci_write_4(isc, OMAP_USBHOST_USBCMD, reg);
562
563	/* Soft reset the PHY using PHY reset command over ULPI */
564	if (isc->port_mode[0] == EHCI_HCD_OMAP_MODE_PHY)
565		omap_ehci_soft_phy_reset(isc, 0);
566	if (isc->port_mode[1] == EHCI_HCD_OMAP_MODE_PHY)
567		omap_ehci_soft_phy_reset(isc, 1);
568
569	return(0);
570
571err_sys_status:
572
573	/* Disable the TLL clocks */
574	ti_prcm_clk_disable(USBTLL_CLK);
575
576	/* Disable Clocks for USBHOST */
577	ti_prcm_clk_disable(USBHSHOST_CLK);
578
579	return(ret);
580}
581
582
583/**
584 *	omap_ehci_fini - shutdown the EHCI controller
585 *	@isc: omap ehci device context
586 *
587 *
588 *
589 *	LOCKING:
590 *	none
591 *
592 *	RETURNS:
593 *	0 on success, a negative error code on failure.
594 */
595static void
596omap_ehci_fini(struct omap_ehci_softc *isc)
597{
598	unsigned long timeout;
599
600	device_printf(isc->sc_dev, "Stopping TI EHCI USB Controller\n");
601
602	/* Set the timeout */
603	if (hz < 10)
604		timeout = 1;
605	else
606		timeout = (100 * hz) / 1000;
607
608	/* Reset the UHH, OHCI and EHCI modules */
609	omap_uhh_write_4(isc, OMAP_USBHOST_UHH_SYSCONFIG, 0x0002);
610	while ((omap_uhh_read_4(isc, OMAP_USBHOST_UHH_SYSSTATUS) & 0x07) == 0x00) {
611		/* Sleep for a tick */
612		pause("USBRESET", 1);
613
614		if (timeout-- == 0) {
615			device_printf(isc->sc_dev, "operation timed out\n");
616			break;
617		}
618	}
619
620
621	/* Set the timeout */
622	if (hz < 10)
623		timeout = 1;
624	else
625		timeout = (100 * hz) / 1000;
626
627	/* Reset the TLL module */
628	omap_tll_write_4(isc, OMAP_USBTLL_SYSCONFIG, 0x0002);
629	while ((omap_tll_read_4(isc, OMAP_USBTLL_SYSSTATUS) & (0x01)) == 0x00) {
630		/* Sleep for a tick */
631		pause("USBRESET", 1);
632
633		if (timeout-- == 0) {
634			device_printf(isc->sc_dev, "operation timed out\n");
635			break;
636		}
637	}
638
639
640	/* Disable functional and interface clocks for the TLL and HOST modules */
641	ti_prcm_clk_disable(USBTLL_CLK);
642	ti_prcm_clk_disable(USBHSHOST_CLK);
643
644	device_printf(isc->sc_dev, "Clock to USB host has been disabled\n");
645
646}
647
648
649
650/**
651 *	omap_ehci_suspend - suspends the bus
652 *	@dev: omap ehci device
653 *
654 *	Effectively boilerplate EHCI suspend code.
655 *
656 *	TODO: There is a lot more we could do here - i.e. force the controller into
657 *	idle mode and disable all the clocks for start.
658 *
659 *	LOCKING:
660 *	none
661 *
662 *	RETURNS:
663 *	0 on success or a positive error code
664 */
665static int
666omap_ehci_suspend(device_t dev)
667{
668	ehci_softc_t *sc = device_get_softc(dev);
669	int err;
670
671	sc = sc;
672	err = bus_generic_suspend(dev);
673	if (err)
674		return (err);
675	return (0);
676}
677
678
679/**
680 *	omap_ehci_resume - resumes a suspended bus
681 *	@dev: omap ehci device
682 *
683 *	Effectively boilerplate EHCI resume code.
684 *
685 *	LOCKING:
686 *	none
687 *
688 *	RETURNS:
689 *	0 on success or a positive error code on failure
690 */
691static int
692omap_ehci_resume(device_t dev)
693{
694	ehci_softc_t *sc = device_get_softc(dev);
695	sc = sc;
696
697	bus_generic_resume(dev);
698
699	return (0);
700}
701
702
703/**
704 *	omap_ehci_shutdown - starts the given command
705 *	@dev:
706 *
707 *	Effectively boilerplate EHCI shutdown code.
708 *
709 *	LOCKING:
710 *	none.
711 *
712 *	RETURNS:
713 *	0 on success or a positive error code on failure
714 */
715static int
716omap_ehci_shutdown(device_t dev)
717{
718	ehci_softc_t *sc = device_get_softc(dev);
719	int err;
720
721	sc = sc;
722	err = bus_generic_shutdown(dev);
723	if (err)
724		return (err);
725
726	return (0);
727}
728
729
730/**
731 *	omap_ehci_probe - starts the given command
732 *	@dev:
733 *
734 *	Effectively boilerplate EHCI resume code.
735 *
736 *	LOCKING:
737 *	Caller should be holding the OMAP3_MMC lock.
738 *
739 *	RETURNS:
740 *	EH_HANDLED or EH_NOT_HANDLED
741 */
742static int
743omap_ehci_probe(device_t dev)
744{
745	if (!ofw_bus_is_compatible(dev, "ti,usb-ehci"))
746		return (ENXIO);
747
748	device_set_desc(dev, OMAP_EHCI_HC_DEVSTR);
749
750	return (BUS_PROBE_DEFAULT);
751}
752
753/**
754 *	omap_ehci_attach - driver entry point, sets up the ECHI controller/driver
755 *	@dev: the new device handle
756 *
757 *	Sets up bus spaces, interrupt handles, etc for the EHCI controller.  It also
758 *	parses the resource hints and calls omap_ehci_init() to initialise the
759 *	H/W.
760 *
761 *	LOCKING:
762 *	none
763 *
764 *	RETURNS:
765 *	0 on success or a positive error code on failure.
766 */
767static int
768omap_ehci_attach(device_t dev)
769{
770	struct omap_ehci_softc *isc = device_get_softc(dev);
771	phandle_t node;
772	/* 3 ports with 3 cells per port */
773	pcell_t phyconf[3 * 3];
774	pcell_t *phyconf_ptr;
775	ehci_softc_t *sc = &isc->base;
776	int err;
777	int rid;
778	int len, tuple_size;
779	int i;
780
781	/* initialise some bus fields */
782	sc->sc_bus.parent = dev;
783	sc->sc_bus.devices = sc->sc_devices;
784	sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
785
786	/* save the device */
787	isc->sc_dev = dev;
788
789	/* get all DMA memory */
790	if (usb_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(dev),
791	                          &ehci_iterate_hw_softc)) {
792		return (ENOMEM);
793	}
794
795	/* When the EHCI driver is added to the tree it is expected that 3
796	 * memory resources and 1 interrupt resource is assigned. The memory
797	 * resources should be:
798	 *   0 => EHCI register range
799	 *   1 => UHH register range
800	 *   2 => TLL register range
801	 *
802	 * The interrupt resource is just the single interupt for the controller.
803	 */
804
805	/* Allocate resource for the EHCI register set */
806	rid = 0;
807	sc->sc_io_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
808	if (!sc->sc_io_res) {
809		device_printf(dev, "Error: Could not map EHCI memory\n");
810		goto error;
811	}
812	/* Request an interrupt resource */
813	rid = 0;
814	sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
815	if (sc->sc_irq_res == NULL) {
816		device_printf(dev, "Error: could not allocate irq\n");
817		goto error;
818	}
819
820	/* Allocate resource for the UHH register set */
821	rid = 1;
822	isc->uhh_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
823	if (!isc->uhh_mem_res) {
824		device_printf(dev, "Error: Could not map UHH memory\n");
825		goto error;
826	}
827	/* Allocate resource for the TLL register set */
828	rid = 2;
829	isc->tll_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
830	if (!isc->tll_mem_res) {
831		device_printf(dev, "Error: Could not map TLL memory\n");
832		goto error;
833	}
834
835	/* Add this device as a child of the USBus device */
836	sc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
837	if (!sc->sc_bus.bdev) {
838		device_printf(dev, "Error: could not add USB device\n");
839		goto error;
840	}
841
842	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
843	device_set_desc(sc->sc_bus.bdev, OMAP_EHCI_HC_DEVSTR);
844
845	/* Set the vendor name */
846	sprintf(sc->sc_vendor, "Texas Instruments");
847
848	/* Get the GPIO device, we may need this if the driver needs to toggle
849	 * some pins for external PHY resets.
850	 */
851	isc->sc_gpio_dev = devclass_get_device(devclass_find("gpio"), 0);
852	if (isc->sc_gpio_dev == NULL) {
853		device_printf(dev, "Error: failed to get the GPIO device\n");
854		goto error;
855	}
856
857	/* Set the defaults for the hints */
858	for (i = 0; i < 3; i++) {
859		isc->phy_reset[i] = 0;
860		isc->port_mode[i] = EHCI_HCD_OMAP_MODE_UNKNOWN;
861		isc->reset_gpio_pin[i] = -1;
862	}
863
864	tuple_size = sizeof(pcell_t) * 3;
865	node = ofw_bus_get_node(dev);
866	len = OF_getprop(node, "phy-config", phyconf, sizeof(phyconf));
867	if (len > 0) {
868		if (len % tuple_size)
869			goto error;
870		if ((len / tuple_size) != 3)
871			goto error;
872
873		phyconf_ptr = phyconf;
874		for (i = 0; i < 3; i++) {
875			isc->port_mode[i] = fdt32_to_cpu(*phyconf_ptr);
876			isc->phy_reset[i] = fdt32_to_cpu(*(phyconf_ptr + 1));
877			isc->reset_gpio_pin[i] = fdt32_to_cpu(*(phyconf_ptr + 2));
878
879			phyconf_ptr += 3;
880		}
881	}
882
883	/* Initialise the ECHI registers */
884	err = omap_ehci_init(isc);
885	if (err) {
886		device_printf(dev, "Error: could not setup OMAP EHCI, %d\n", err);
887		goto error;
888	}
889
890
891	/* Set the tag and size of the register set in the EHCI context */
892	sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
893	sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
894	sc->sc_io_size = rman_get_size(sc->sc_io_res);
895
896
897	/* Setup the interrupt */
898	err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
899						 NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
900	if (err) {
901		device_printf(dev, "Error: could not setup irq, %d\n", err);
902		sc->sc_intr_hdl = NULL;
903		goto error;
904	}
905
906
907	/* Finally we are ready to kick off the ECHI host controller */
908	err = ehci_init(sc);
909	if (err == 0) {
910		err = device_probe_and_attach(sc->sc_bus.bdev);
911	}
912	if (err) {
913		device_printf(dev, "Error: USB init failed err=%d\n", err);
914		goto error;
915	}
916
917	return (0);
918
919error:
920	omap_ehci_detach(dev);
921	return (ENXIO);
922}
923
924/**
925 *	omap_ehci_detach - detach the device and cleanup the driver
926 *	@dev: device handle
927 *
928 *	Clean-up routine where everything initialised in omap_ehci_attach is
929 *	freed and cleaned up.  This function calls omap_ehci_fini() to shutdown
930 *	the on-chip module.
931 *
932 *	LOCKING:
933 *	none
934 *
935 *	RETURNS:
936 *	Always returns 0 (success).
937 */
938static int
939omap_ehci_detach(device_t dev)
940{
941	struct omap_ehci_softc *isc = device_get_softc(dev);
942	ehci_softc_t *sc = &isc->base;
943	device_t bdev;
944	int err;
945
946	if (sc->sc_bus.bdev) {
947		bdev = sc->sc_bus.bdev;
948		device_detach(bdev);
949		device_delete_child(dev, bdev);
950	}
951
952	/* during module unload there are lots of children leftover */
953	device_delete_children(dev);
954
955	/*
956	 * disable interrupts that might have been switched on in ehci_init
957	 */
958	if (sc->sc_io_res) {
959		EWRITE4(sc, EHCI_USBINTR, 0);
960	}
961
962	if (sc->sc_irq_res && sc->sc_intr_hdl) {
963		/*
964		 * only call ehci_detach() after ehci_init()
965		 */
966		ehci_detach(sc);
967
968		err = bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intr_hdl);
969		if (err)
970			device_printf(dev, "Error: could not tear down irq, %d\n", err);
971		sc->sc_intr_hdl = NULL;
972	}
973
974	/* Free the resources stored in the base EHCI handler */
975	if (sc->sc_irq_res) {
976		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
977		sc->sc_irq_res = NULL;
978	}
979	if (sc->sc_io_res) {
980		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_io_res);
981		sc->sc_io_res = NULL;
982	}
983
984	/* Release the other register set memory maps */
985	if (isc->tll_mem_res) {
986		bus_release_resource(dev, SYS_RES_MEMORY, 0, isc->tll_mem_res);
987		isc->tll_mem_res = NULL;
988	}
989	if (isc->uhh_mem_res) {
990		bus_release_resource(dev, SYS_RES_MEMORY, 0, isc->uhh_mem_res);
991		isc->uhh_mem_res = NULL;
992	}
993
994	usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
995
996	omap_ehci_fini(isc);
997
998	return (0);
999}
1000
1001static device_method_t ehci_methods[] = {
1002	/* Device interface */
1003	DEVMETHOD(device_probe, omap_ehci_probe),
1004	DEVMETHOD(device_attach, omap_ehci_attach),
1005	DEVMETHOD(device_detach, omap_ehci_detach),
1006	DEVMETHOD(device_suspend, omap_ehci_suspend),
1007	DEVMETHOD(device_resume, omap_ehci_resume),
1008	DEVMETHOD(device_shutdown, omap_ehci_shutdown),
1009
1010	/* Bus interface */
1011	DEVMETHOD(bus_print_child, bus_generic_print_child),
1012
1013	{0, 0}
1014};
1015
1016static driver_t ehci_driver = {
1017	"ehci",
1018	ehci_methods,
1019	sizeof(struct omap_ehci_softc),
1020};
1021
1022static devclass_t ehci_devclass;
1023
1024DRIVER_MODULE(ehci, simplebus, ehci_driver, ehci_devclass, 0, 0);
1025