xref: /freebsd/sys/dev/usb/controller/ehci_imx.c (revision ca48e43ba9ee73a07cdbad8365117793b01273bb)
1a2c472e7SAleksandr Rybalko /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
4a2c472e7SAleksandr Rybalko  * Copyright (c) 2010-2012 Semihalf
5a2c472e7SAleksandr Rybalko  * Copyright (c) 2012 The FreeBSD Foundation
6f26c8105SIan Lepore  * Copyright (c) 2013 Ian Lepore <ian@freebsd.org>
7a2c472e7SAleksandr Rybalko  * All rights reserved.
8a2c472e7SAleksandr Rybalko  *
9a2c472e7SAleksandr Rybalko  * Portions of this software were developed by Oleksandr Rybalko
10a2c472e7SAleksandr Rybalko  * under sponsorship from the FreeBSD Foundation.
11a2c472e7SAleksandr Rybalko  *
12a2c472e7SAleksandr Rybalko  * Redistribution and use in source and binary forms, with or without
13a2c472e7SAleksandr Rybalko  * modification, are permitted provided that the following conditions
14a2c472e7SAleksandr Rybalko  * are met:
15a2c472e7SAleksandr Rybalko  * 1. Redistributions of source code must retain the above copyright
16a2c472e7SAleksandr Rybalko  *    notice, this list of conditions and the following disclaimer.
17a2c472e7SAleksandr Rybalko  * 2. Redistributions in binary form must reproduce the above copyright
18a2c472e7SAleksandr Rybalko  *    notice, this list of conditions and the following disclaimer in the
19a2c472e7SAleksandr Rybalko  *    documentation and/or other materials provided with the distribution.
20a2c472e7SAleksandr Rybalko  *
21a2c472e7SAleksandr Rybalko  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22a2c472e7SAleksandr Rybalko  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23a2c472e7SAleksandr Rybalko  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24a2c472e7SAleksandr Rybalko  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25a2c472e7SAleksandr Rybalko  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26a2c472e7SAleksandr Rybalko  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27a2c472e7SAleksandr Rybalko  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28a2c472e7SAleksandr Rybalko  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29a2c472e7SAleksandr Rybalko  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30a2c472e7SAleksandr Rybalko  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31a2c472e7SAleksandr Rybalko  * SUCH DAMAGE.
32a2c472e7SAleksandr Rybalko  */
33a2c472e7SAleksandr Rybalko 
34f26c8105SIan Lepore /*
35f26c8105SIan Lepore  * EHCI driver for Freescale i.MX SoCs which incorporate the USBOH3 controller.
36f26c8105SIan Lepore  */
37a2c472e7SAleksandr Rybalko 
38a2c472e7SAleksandr Rybalko #include <sys/param.h>
39a2c472e7SAleksandr Rybalko #include <sys/systm.h>
40a2c472e7SAleksandr Rybalko #include <sys/kernel.h>
41a2c472e7SAleksandr Rybalko #include <sys/module.h>
42a2c472e7SAleksandr Rybalko #include <sys/bus.h>
43a2c472e7SAleksandr Rybalko #include <sys/condvar.h>
44a2c472e7SAleksandr Rybalko #include <sys/rman.h>
45a2c472e7SAleksandr Rybalko 
46a2c472e7SAleksandr Rybalko #include <dev/ofw/ofw_bus.h>
47a2c472e7SAleksandr Rybalko #include <dev/ofw/ofw_bus_subr.h>
48a2c472e7SAleksandr Rybalko 
49a2c472e7SAleksandr Rybalko #include <dev/usb/usb.h>
50a2c472e7SAleksandr Rybalko #include <dev/usb/usbdi.h>
51a2c472e7SAleksandr Rybalko #include <dev/usb/usb_busdma.h>
52a2c472e7SAleksandr Rybalko #include <dev/usb/usb_process.h>
53a2c472e7SAleksandr Rybalko #include <dev/usb/usb_controller.h>
54a2c472e7SAleksandr Rybalko #include <dev/usb/usb_bus.h>
55a2c472e7SAleksandr Rybalko #include <dev/usb/controller/ehci.h>
56a2c472e7SAleksandr Rybalko #include <dev/usb/controller/ehcireg.h>
57f26c8105SIan Lepore #include "usbdevs.h"
58a2c472e7SAleksandr Rybalko 
59a2c472e7SAleksandr Rybalko #include <machine/bus.h>
60a2c472e7SAleksandr Rybalko #include <machine/resource.h>
61a2c472e7SAleksandr Rybalko 
62a7fa939bSIan Lepore #include <arm/freescale/imx/imx_ccmvar.h>
63f26c8105SIan Lepore 
64a2c472e7SAleksandr Rybalko #include "opt_platform.h"
65a2c472e7SAleksandr Rybalko 
66f26c8105SIan Lepore /*
67f26c8105SIan Lepore  * Notes on the hardware and related FDT data seen in the wild.
68f26c8105SIan Lepore  *
69f26c8105SIan Lepore  * There are two sets of registers in the USBOH3 implementation; documentation
70f26c8105SIan Lepore  * refers to them as "core" and "non-core" registers.  A set of core register
71f26c8105SIan Lepore  * exists for each OTG or EHCI device.  There is a single set of non-core
72f26c8105SIan Lepore  * registers per USBOH3, and they control aspects of operation not directly
73f26c8105SIan Lepore  * related to the USB specs, such as whether interrupts from each of the core
74f26c8105SIan Lepore  * devices are able to generate a SoC wakeup event.
75f26c8105SIan Lepore  *
76f26c8105SIan Lepore  * In the FreeBSD universe we might be inclined to describe the core and
77f26c8105SIan Lepore  * non-core registers by using a pair of resource address/size values (two
78f26c8105SIan Lepore  * entries in the reg property for each core).  However, we have to work with
79f26c8105SIan Lepore  * existing FDT data (which mostly comes from the linux universe), and the way
80f26c8105SIan Lepore  * they've chosen to represent this is with an entry for a "usbmisc" device
81f26c8105SIan Lepore  * whose reg property describes the non-core registers. The way we handle FDT
82f26c8105SIan Lepore  * data, this means that the resources (memory-mapped register range) for the
83f26c8105SIan Lepore  * non-core registers belongs to a device other than the echi devices.
84f26c8105SIan Lepore  *
857164f27dSIan Lepore  * Because the main ehci device cannot access registers in a range that's
867164f27dSIan Lepore  * defined in the fdt data as belonging to another device, we implement a teeny
877164f27dSIan Lepore  * little "usbmisc" driver which exists only to provide access to the usbmisc
887164f27dSIan Lepore  * control register for each of the 4 usb controller instances.  That little
897164f27dSIan Lepore  * driver is implemented here in this file, before the main driver.
90f26c8105SIan Lepore  *
91f26c8105SIan Lepore  * In addition to the single usbmisc device, the existing FDT data defines a
92f26c8105SIan Lepore  * separate device for each of the OTG or EHCI cores within the USBOH3.  Each of
93f26c8105SIan Lepore  * those devices has a set of core registers described by the reg property.
94f26c8105SIan Lepore  *
95f26c8105SIan Lepore  * The core registers for each of the four cores in the USBOH3 are divided into
96f26c8105SIan Lepore  * two parts: a set of imx-specific registers at an offset of 0 from the
97f26c8105SIan Lepore  * beginning of the register range, and the standard USB (EHCI or OTG) registers
98f26c8105SIan Lepore  * at an offset of 0x100 from the beginning of the register range.  The FreeBSD
99f26c8105SIan Lepore  * way of dealing with this might be to map out two ranges in the reg property,
100f26c8105SIan Lepore  * but that's not what the alternate universe has done.  To work with existing
101f26c8105SIan Lepore  * FDT data, we acquire the resource that maps all the core registers, then use
102f26c8105SIan Lepore  * bus_space_subregion() to create another resource that maps just the standard
103f26c8105SIan Lepore  * USB registers, which we provide to the standard USB code in the ehci_softc.
104f26c8105SIan Lepore  *
105f26c8105SIan Lepore  * The following compat strings have been seen for the OTG and EHCI cores.  The
106f26c8105SIan Lepore  * FDT compat table in this driver contains all these strings, but as of this
107f26c8105SIan Lepore  * writing, not all of these SoCs have been tested with the driver.  The fact
108f26c8105SIan Lepore  * that imx27 is common to all of them gives some hope that the driver will work
109f26c8105SIan Lepore  * on all these SoCs.
110f26c8105SIan Lepore  *   - "fsl,imx23-usb", "fsl,imx27-usb";
111f26c8105SIan Lepore  *   - "fsl,imx25-usb", "fsl,imx27-usb";
112f26c8105SIan Lepore  *   - "fsl,imx28-usb", "fsl,imx27-usb";
113f26c8105SIan Lepore  *   - "fsl,imx51-usb", "fsl,imx27-usb";
114f26c8105SIan Lepore  *   - "fsl,imx53-usb", "fsl,imx27-usb";
115f26c8105SIan Lepore  *   - "fsl,imx6q-usb", "fsl,imx27-usb";
116f26c8105SIan Lepore  *
117f26c8105SIan Lepore  * The FDT data for some SoCs contains the following properties, which we don't
118f26c8105SIan Lepore  * currently do anything with:
119f26c8105SIan Lepore  *   - fsl,usbmisc = <&usbmisc 0>;
120f26c8105SIan Lepore  *   - fsl,usbphy = <&usbphy0>;
121f26c8105SIan Lepore  *
122f26c8105SIan Lepore  * Some imx SoCs have FDT data related to USB PHY, some don't.  We have separate
123f26c8105SIan Lepore  * usbphy drivers where needed; this data is mentioned here just to keep all the
124f26c8105SIan Lepore  * imx-FDT-usb-related info in one place.  Here are the usbphy compat strings
125f26c8105SIan Lepore  * known to exist:
126f26c8105SIan Lepore  *   - "nop-usbphy"
127f26c8105SIan Lepore  *   - "usb-nop-xceiv";
128f26c8105SIan Lepore  *   - "fsl,imx23-usbphy"
129f26c8105SIan Lepore  *   - "fsl,imx28-usbphy", "fsl,imx23-usbphy";
130f26c8105SIan Lepore  *   - "fsl,imx6q-usbphy", "fsl,imx23-usbphy";
131f26c8105SIan Lepore  *
132f26c8105SIan Lepore  */
133f26c8105SIan Lepore 
1347164f27dSIan Lepore /*-----------------------------------------------------------------------------
1357164f27dSIan Lepore  * imx_usbmisc driver
1367164f27dSIan Lepore  *---------------------------------------------------------------------------*/
1377164f27dSIan Lepore 
1387164f27dSIan Lepore #define	USBNC_OVER_CUR_POL	  (1u << 8)
1397164f27dSIan Lepore #define	USBNC_OVER_CUR_DIS	  (1u << 7)
1407164f27dSIan Lepore 
1417164f27dSIan Lepore struct imx_usbmisc_softc {
1427164f27dSIan Lepore 	device_t	dev;
1437164f27dSIan Lepore 	struct resource	*mmio;
144f26c8105SIan Lepore };
145f26c8105SIan Lepore 
1467164f27dSIan Lepore static struct ofw_compat_data usbmisc_compat_data[] = {
1477164f27dSIan Lepore 	{"fsl,imx6q-usbmisc",	true},
1487164f27dSIan Lepore 	{"fsl,imx51-usbmisc",	true},
1497164f27dSIan Lepore 	{"fsl,imx25-usbmisc",	true},
1507164f27dSIan Lepore 	{NULL, 			false},
1517164f27dSIan Lepore };
1527164f27dSIan Lepore 
1537164f27dSIan Lepore static void
imx_usbmisc_set_ctrl(device_t dev,u_int index,uint32_t bits)1547164f27dSIan Lepore imx_usbmisc_set_ctrl(device_t dev, u_int index, uint32_t bits)
1557164f27dSIan Lepore {
1567164f27dSIan Lepore 	struct imx_usbmisc_softc *sc;
1577164f27dSIan Lepore 	uint32_t reg;
1587164f27dSIan Lepore 
1597164f27dSIan Lepore 	sc = device_get_softc(dev);
1607164f27dSIan Lepore 	reg = bus_read_4(sc->mmio, index * sizeof(uint32_t));
1617164f27dSIan Lepore 	bus_write_4(sc->mmio, index * sizeof(uint32_t), reg | bits);
1627164f27dSIan Lepore }
1637164f27dSIan Lepore 
1642d5f51fbSIan Lepore #ifdef notyet
1657164f27dSIan Lepore static void
imx_usbmisc_clr_ctrl(device_t dev,u_int index,uint32_t bits)1667164f27dSIan Lepore imx_usbmisc_clr_ctrl(device_t dev, u_int index, uint32_t bits)
1677164f27dSIan Lepore {
1687164f27dSIan Lepore 	struct imx_usbmisc_softc *sc;
1697164f27dSIan Lepore 	uint32_t reg;
1707164f27dSIan Lepore 
1717164f27dSIan Lepore 	sc = device_get_softc(dev);
1727164f27dSIan Lepore 	reg = bus_read_4(sc->mmio, index * sizeof(uint32_t));
1737164f27dSIan Lepore 	bus_write_4(sc->mmio, index * sizeof(uint32_t), reg & ~bits);
1747164f27dSIan Lepore }
1752d5f51fbSIan Lepore #endif
1767164f27dSIan Lepore 
1777164f27dSIan Lepore static int
imx_usbmisc_probe(device_t dev)1787164f27dSIan Lepore imx_usbmisc_probe(device_t dev)
1797164f27dSIan Lepore {
1807164f27dSIan Lepore 
1817164f27dSIan Lepore 	if (!ofw_bus_status_okay(dev))
1827164f27dSIan Lepore 		return (ENXIO);
1837164f27dSIan Lepore 
1847164f27dSIan Lepore 	if (ofw_bus_search_compatible(dev, usbmisc_compat_data)->ocd_data) {
1857164f27dSIan Lepore 		device_set_desc(dev, "i.MX USB Misc Control");
1867164f27dSIan Lepore 		return (BUS_PROBE_DEFAULT);
1877164f27dSIan Lepore 	}
1887164f27dSIan Lepore 	return (ENXIO);
1897164f27dSIan Lepore }
1907164f27dSIan Lepore 
1917164f27dSIan Lepore static int
imx_usbmisc_detach(device_t dev)1927164f27dSIan Lepore imx_usbmisc_detach(device_t dev)
1937164f27dSIan Lepore {
1947164f27dSIan Lepore 	struct imx_usbmisc_softc *sc;
1957164f27dSIan Lepore 
1967164f27dSIan Lepore 	sc = device_get_softc(dev);
1977164f27dSIan Lepore 
1987164f27dSIan Lepore 	if (sc->mmio != NULL)
1997164f27dSIan Lepore 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->mmio);
2007164f27dSIan Lepore 
2017164f27dSIan Lepore 	return (0);
2027164f27dSIan Lepore }
2037164f27dSIan Lepore 
2047164f27dSIan Lepore static int
imx_usbmisc_attach(device_t dev)2057164f27dSIan Lepore imx_usbmisc_attach(device_t dev)
2067164f27dSIan Lepore {
2077164f27dSIan Lepore 	struct imx_usbmisc_softc *sc;
20843854830SJohn Baldwin 	int rid;
2097164f27dSIan Lepore 
2107164f27dSIan Lepore 	sc = device_get_softc(dev);
2117164f27dSIan Lepore 
2127164f27dSIan Lepore 	/* Allocate bus_space resources. */
2137164f27dSIan Lepore 	rid = 0;
2147164f27dSIan Lepore 	sc->mmio = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
2157164f27dSIan Lepore 	    RF_ACTIVE);
2167164f27dSIan Lepore 	if (sc->mmio == NULL) {
2177164f27dSIan Lepore 		device_printf(dev, "Cannot allocate memory resources\n");
2187164f27dSIan Lepore 		return (ENXIO);
2197164f27dSIan Lepore 	}
2207164f27dSIan Lepore 
2217164f27dSIan Lepore 	OF_device_register_xref(OF_xref_from_node(ofw_bus_get_node(dev)), dev);
2227164f27dSIan Lepore 
2237164f27dSIan Lepore 	return (0);
2247164f27dSIan Lepore }
2257164f27dSIan Lepore 
2267164f27dSIan Lepore static device_method_t imx_usbmisc_methods[] = {
2277164f27dSIan Lepore 	/* Device interface */
2287164f27dSIan Lepore 	DEVMETHOD(device_probe, 	imx_usbmisc_probe),
2297164f27dSIan Lepore 	DEVMETHOD(device_attach,	imx_usbmisc_attach),
2307164f27dSIan Lepore 	DEVMETHOD(device_detach,	imx_usbmisc_detach),
2317164f27dSIan Lepore 
2327164f27dSIan Lepore 	DEVMETHOD_END
2337164f27dSIan Lepore };
2347164f27dSIan Lepore 
2357164f27dSIan Lepore static driver_t imx_usbmisc_driver = {
2367164f27dSIan Lepore 	"imx_usbmisc",
2377164f27dSIan Lepore 	imx_usbmisc_methods,
2387164f27dSIan Lepore 	sizeof(struct imx_usbmisc_softc)
2397164f27dSIan Lepore };
2407164f27dSIan Lepore 
2417164f27dSIan Lepore /*
2427164f27dSIan Lepore  * This driver needs to start before the ehci driver, but later than the usual
2437164f27dSIan Lepore  * "special" drivers like clocks and cpu.  Ehci starts at DEFAULT so
2447164f27dSIan Lepore  * DEFAULT-1000 seems good.
2457164f27dSIan Lepore  */
2467164f27dSIan Lepore EARLY_DRIVER_MODULE(imx_usbmisc, simplebus, imx_usbmisc_driver,
247bc9372d7SJohn Baldwin     0, 0, BUS_PASS_DEFAULT - 1000);
2487164f27dSIan Lepore 
2497164f27dSIan Lepore /*-----------------------------------------------------------------------------
2507164f27dSIan Lepore  * imx_ehci driver...
2517164f27dSIan Lepore  *---------------------------------------------------------------------------*/
2527164f27dSIan Lepore 
253f26c8105SIan Lepore /*
254f26c8105SIan Lepore  * Each EHCI device in the SoC has some SoC-specific per-device registers at an
255f26c8105SIan Lepore  * offset of 0, then the standard EHCI registers begin at an offset of 0x100.
256f26c8105SIan Lepore  */
257f26c8105SIan Lepore #define	IMX_EHCI_REG_OFF	0x100
258f26c8105SIan Lepore #define	IMX_EHCI_REG_SIZE	0x100
259a2c472e7SAleksandr Rybalko 
260a2c472e7SAleksandr Rybalko struct imx_ehci_softc {
261f26c8105SIan Lepore 	ehci_softc_t	ehci_softc;
2627164f27dSIan Lepore 	device_t	dev;
263f26c8105SIan Lepore 	struct resource	*ehci_mem_res;	/* EHCI core regs. */
264f26c8105SIan Lepore 	struct resource	*ehci_irq_res;	/* EHCI core IRQ. */
265a2c472e7SAleksandr Rybalko };
266a2c472e7SAleksandr Rybalko 
2677164f27dSIan Lepore static struct ofw_compat_data compat_data[] = {
2687164f27dSIan Lepore 	{"fsl,imx6q-usb",	1},
2697164f27dSIan Lepore 	{"fsl,imx53-usb",	1},
2707164f27dSIan Lepore 	{"fsl,imx51-usb",	1},
2717164f27dSIan Lepore 	{"fsl,imx28-usb",	1},
2727164f27dSIan Lepore 	{"fsl,imx27-usb",	1},
2737164f27dSIan Lepore 	{"fsl,imx25-usb",	1},
2747164f27dSIan Lepore 	{"fsl,imx23-usb",	1},
2757164f27dSIan Lepore 	{NULL,		 	0},
2767164f27dSIan Lepore };
2777164f27dSIan Lepore 
278cfed2e63SIan Lepore static void
imx_ehci_post_reset(struct ehci_softc * ehci_softc)279cfed2e63SIan Lepore imx_ehci_post_reset(struct ehci_softc *ehci_softc)
280cfed2e63SIan Lepore {
281cfed2e63SIan Lepore         uint32_t usbmode;
282cfed2e63SIan Lepore 
283cfed2e63SIan Lepore         /* Force HOST mode */
284cfed2e63SIan Lepore         usbmode = EOREAD4(ehci_softc, EHCI_USBMODE_NOLPM);
285cfed2e63SIan Lepore         usbmode &= ~EHCI_UM_CM;
286cfed2e63SIan Lepore         usbmode |= EHCI_UM_CM_HOST;
287cfed2e63SIan Lepore         EOWRITE4(ehci_softc, EHCI_USBMODE_NOLPM, usbmode);
288cfed2e63SIan Lepore }
289cfed2e63SIan Lepore 
290f26c8105SIan Lepore static int
imx_ehci_probe(device_t dev)291f26c8105SIan Lepore imx_ehci_probe(device_t dev)
292f26c8105SIan Lepore {
293a2c472e7SAleksandr Rybalko 
294add35ed5SIan Lepore 	if (!ofw_bus_status_okay(dev))
295add35ed5SIan Lepore 		return (ENXIO);
296add35ed5SIan Lepore 
297f26c8105SIan Lepore 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) {
298f26c8105SIan Lepore 		device_set_desc(dev, "Freescale i.MX integrated USB controller");
299f26c8105SIan Lepore 		return (BUS_PROBE_DEFAULT);
300f26c8105SIan Lepore 	}
301f26c8105SIan Lepore 	return (ENXIO);
302f26c8105SIan Lepore }
303f26c8105SIan Lepore 
304f26c8105SIan Lepore static int
imx_ehci_detach(device_t dev)305f26c8105SIan Lepore imx_ehci_detach(device_t dev)
306f26c8105SIan Lepore {
307f26c8105SIan Lepore 	struct imx_ehci_softc *sc;
308f26c8105SIan Lepore 	ehci_softc_t *esc;
309a6dae562SIan Lepore 	int err;
310f26c8105SIan Lepore 
311f26c8105SIan Lepore 	sc = device_get_softc(dev);
312f26c8105SIan Lepore 
313f26c8105SIan Lepore 	esc = &sc->ehci_softc;
314f26c8105SIan Lepore 
315a6dae562SIan Lepore 	/* First detach all children; we can't detach if that fails. */
316*3ddaf820SJohn Baldwin 	if ((err = bus_generic_detach(dev)) != 0)
317a6dae562SIan Lepore 		return (err);
318a6dae562SIan Lepore 
319f26c8105SIan Lepore 	if (esc->sc_flags & EHCI_SCFLG_DONEINIT)
320f26c8105SIan Lepore 		ehci_detach(esc);
321f26c8105SIan Lepore 	if (esc->sc_intr_hdl != NULL)
322f26c8105SIan Lepore 		bus_teardown_intr(dev, esc->sc_irq_res,
323f26c8105SIan Lepore 		    esc->sc_intr_hdl);
324f26c8105SIan Lepore 	if (sc->ehci_irq_res != NULL)
325f26c8105SIan Lepore 		bus_release_resource(dev, SYS_RES_IRQ, 0,
326f26c8105SIan Lepore 		    sc->ehci_irq_res);
327f26c8105SIan Lepore 	if (sc->ehci_mem_res != NULL)
328f26c8105SIan Lepore 		bus_release_resource(dev, SYS_RES_MEMORY, 0,
329f26c8105SIan Lepore 		    sc->ehci_mem_res);
330f26c8105SIan Lepore 
331f26c8105SIan Lepore 	usb_bus_mem_free_all(&esc->sc_bus, &ehci_iterate_hw_softc);
332f26c8105SIan Lepore 
333f26c8105SIan Lepore 	return (0);
334f26c8105SIan Lepore }
335f26c8105SIan Lepore 
3367164f27dSIan Lepore static void
imx_ehci_disable_oc(struct imx_ehci_softc * sc)3377164f27dSIan Lepore imx_ehci_disable_oc(struct imx_ehci_softc *sc)
3387164f27dSIan Lepore {
3397164f27dSIan Lepore 	device_t usbmdev;
3407164f27dSIan Lepore 	pcell_t usbmprops[2];
3417164f27dSIan Lepore 	phandle_t node;
3427164f27dSIan Lepore 	ssize_t size;
3437164f27dSIan Lepore 	int index;
3447164f27dSIan Lepore 
3457164f27dSIan Lepore 	/* Get the reference to the usbmisc driver from the fdt data */
3467164f27dSIan Lepore 	node = ofw_bus_get_node(sc->dev);
3477164f27dSIan Lepore 	size = OF_getencprop(node, "fsl,usbmisc", usbmprops,
3487164f27dSIan Lepore 	    sizeof(usbmprops));
3497164f27dSIan Lepore 	if (size < sizeof(usbmprops)) {
3507164f27dSIan Lepore 		device_printf(sc->dev, "failed to retrieve fsl,usbmisc "
3517164f27dSIan Lepore 		   "property, cannot disable overcurrent protection");
3527164f27dSIan Lepore 		return;
3537164f27dSIan Lepore 	}
3547164f27dSIan Lepore 	/* Retrieve the device_t via the xref handle. */
3557164f27dSIan Lepore 	usbmdev = OF_device_from_xref(usbmprops[0]);
3567164f27dSIan Lepore 	if (usbmdev == NULL) {
3577164f27dSIan Lepore 		device_printf(sc->dev, "usbmisc device not found, "
3587164f27dSIan Lepore 		    "cannot disable overcurrent protection");
3597164f27dSIan Lepore 		return;
3607164f27dSIan Lepore 	}
3617164f27dSIan Lepore 	/* Call the device routine to set the overcurrent disable bit. */
3627164f27dSIan Lepore 	index = usbmprops[1];
3637164f27dSIan Lepore 	imx_usbmisc_set_ctrl(usbmdev, index, USBNC_OVER_CUR_DIS);
3647164f27dSIan Lepore }
3657164f27dSIan Lepore 
366f26c8105SIan Lepore static int
imx_ehci_attach(device_t dev)367f26c8105SIan Lepore imx_ehci_attach(device_t dev)
368f26c8105SIan Lepore {
369f26c8105SIan Lepore 	struct imx_ehci_softc *sc;
370f26c8105SIan Lepore 	ehci_softc_t *esc;
371f26c8105SIan Lepore 	int err, rid;
372f26c8105SIan Lepore 
373f26c8105SIan Lepore 	sc = device_get_softc(dev);
3747164f27dSIan Lepore 	sc->dev = dev;
375f26c8105SIan Lepore 	esc = &sc->ehci_softc;
376f26c8105SIan Lepore 	err = 0;
377f26c8105SIan Lepore 
378f26c8105SIan Lepore 	/* Allocate bus_space resources. */
379f26c8105SIan Lepore 	rid = 0;
380f26c8105SIan Lepore 	sc->ehci_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
381f26c8105SIan Lepore 	    RF_ACTIVE);
382f26c8105SIan Lepore 	if (sc->ehci_mem_res == NULL) {
383f26c8105SIan Lepore 		device_printf(dev, "Cannot allocate memory resources\n");
384f26c8105SIan Lepore 		err = ENXIO;
385f26c8105SIan Lepore 		goto out;
386f26c8105SIan Lepore 	}
387f26c8105SIan Lepore 
388f26c8105SIan Lepore 	rid = 0;
389f26c8105SIan Lepore 	sc->ehci_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
390f26c8105SIan Lepore 	    RF_ACTIVE);
391f26c8105SIan Lepore 	if (sc->ehci_irq_res == NULL) {
392f26c8105SIan Lepore 		device_printf(dev, "Cannot allocate IRQ resources\n");
393f26c8105SIan Lepore 		err = ENXIO;
394f26c8105SIan Lepore 		goto out;
395f26c8105SIan Lepore 	}
396f26c8105SIan Lepore 
397f26c8105SIan Lepore 	esc->sc_io_tag = rman_get_bustag(sc->ehci_mem_res);
398f26c8105SIan Lepore 	esc->sc_bus.parent = dev;
399f26c8105SIan Lepore 	esc->sc_bus.devices = esc->sc_devices;
400f26c8105SIan Lepore 	esc->sc_bus.devices_max = EHCI_MAX_DEVICES;
401b217d184SHans Petter Selasky 	esc->sc_bus.dma_bits = 32;
402f26c8105SIan Lepore 
403b217d184SHans Petter Selasky 	/* allocate all DMA memory */
404f26c8105SIan Lepore 	if (usb_bus_mem_alloc_all(&esc->sc_bus, USB_GET_DMA_TAG(dev),
405f26c8105SIan Lepore 	    &ehci_iterate_hw_softc) != 0) {
406f26c8105SIan Lepore 		device_printf(dev, "usb_bus_mem_alloc_all() failed\n");
407f26c8105SIan Lepore 		err = ENOMEM;
408f26c8105SIan Lepore 		goto out;
409f26c8105SIan Lepore 	}
410f26c8105SIan Lepore 
411f26c8105SIan Lepore 	/*
412f26c8105SIan Lepore 	 * Set handle to USB related registers subregion used by
413f26c8105SIan Lepore 	 * generic EHCI driver.
414f26c8105SIan Lepore 	 */
415f26c8105SIan Lepore 	err = bus_space_subregion(esc->sc_io_tag,
416f26c8105SIan Lepore 	    rman_get_bushandle(sc->ehci_mem_res),
417f26c8105SIan Lepore 	    IMX_EHCI_REG_OFF, IMX_EHCI_REG_SIZE, &esc->sc_io_hdl);
418f26c8105SIan Lepore 	if (err != 0) {
419f26c8105SIan Lepore 		device_printf(dev, "bus_space_subregion() failed\n");
420f26c8105SIan Lepore 		err = ENXIO;
421f26c8105SIan Lepore 		goto out;
422f26c8105SIan Lepore 	}
423f26c8105SIan Lepore 
424f26c8105SIan Lepore 	/* Setup interrupt handler. */
425c520cb4fSMichal Meloun 	err = bus_setup_intr(dev, sc->ehci_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
426c520cb4fSMichal Meloun 	    NULL, (driver_intr_t *)ehci_interrupt, esc, &esc->sc_intr_hdl);
427f26c8105SIan Lepore 	if (err != 0) {
428f26c8105SIan Lepore 		device_printf(dev, "Could not setup IRQ\n");
429f26c8105SIan Lepore 		goto out;
430f26c8105SIan Lepore 	}
431f26c8105SIan Lepore 
432f26c8105SIan Lepore 	/* Turn on clocks. */
433f26c8105SIan Lepore 	imx_ccm_usb_enable(dev);
434f26c8105SIan Lepore 
4357164f27dSIan Lepore 	/* Disable overcurrent detection, if configured to do so. */
4367164f27dSIan Lepore 	if (OF_hasprop(ofw_bus_get_node(sc->dev), "disable-over-current"))
4377164f27dSIan Lepore 		imx_ehci_disable_oc(sc);
4387164f27dSIan Lepore 
439f26c8105SIan Lepore 	/* Add USB bus device. */
4405b56413dSWarner Losh 	esc->sc_bus.bdev = device_add_child(dev, "usbus", DEVICE_UNIT_ANY);
441f26c8105SIan Lepore 	if (esc->sc_bus.bdev == NULL) {
442f26c8105SIan Lepore 		device_printf(dev, "Could not add USB device\n");
443f26c8105SIan Lepore 		goto out;
444f26c8105SIan Lepore 	}
445f26c8105SIan Lepore 	device_set_ivars(esc->sc_bus.bdev, &esc->sc_bus);
446f26c8105SIan Lepore 
447f26c8105SIan Lepore 	esc->sc_id_vendor = USB_VENDOR_FREESCALE;
448f26c8105SIan Lepore 	strlcpy(esc->sc_vendor, "Freescale", sizeof(esc->sc_vendor));
449f26c8105SIan Lepore 
450cfed2e63SIan Lepore 	/*
451cfed2e63SIan Lepore 	 * Set flags that affect ehci_init() behavior, and hook our post-reset
452cfed2e63SIan Lepore 	 * code into the standard controller code.
453cfed2e63SIan Lepore 	 */
4548148f4f3SIan Lepore 	esc->sc_flags |= EHCI_SCFLG_NORESTERM | EHCI_SCFLG_TT;
455cfed2e63SIan Lepore 	esc->sc_vendor_post_reset = imx_ehci_post_reset;
4568148f4f3SIan Lepore 	esc->sc_vendor_get_port_speed = ehci_get_port_speed_portsc;
457cfed2e63SIan Lepore 
458f26c8105SIan Lepore 	err = ehci_init(esc);
459f26c8105SIan Lepore 	if (err != 0) {
460f26c8105SIan Lepore 		device_printf(dev, "USB init failed, usb_err_t=%d\n",
461f26c8105SIan Lepore 		    err);
462f26c8105SIan Lepore 		goto out;
463f26c8105SIan Lepore 	}
464f26c8105SIan Lepore 	esc->sc_flags |= EHCI_SCFLG_DONEINIT;
465f26c8105SIan Lepore 
466f26c8105SIan Lepore 	/* Probe the bus. */
467f26c8105SIan Lepore 	err = device_probe_and_attach(esc->sc_bus.bdev);
468f26c8105SIan Lepore 	if (err != 0) {
469f26c8105SIan Lepore 		device_printf(dev,
470f26c8105SIan Lepore 		    "device_probe_and_attach() failed\n");
471f26c8105SIan Lepore 		goto out;
472f26c8105SIan Lepore 	}
473f26c8105SIan Lepore 
474f26c8105SIan Lepore 	err = 0;
475f26c8105SIan Lepore 
476f26c8105SIan Lepore out:
477f26c8105SIan Lepore 
478f26c8105SIan Lepore 	if (err != 0)
479f26c8105SIan Lepore 		imx_ehci_detach(dev);
480f26c8105SIan Lepore 
481f26c8105SIan Lepore 	return (err);
482f26c8105SIan Lepore }
483a2c472e7SAleksandr Rybalko 
484a2c472e7SAleksandr Rybalko static device_method_t ehci_methods[] = {
485a2c472e7SAleksandr Rybalko 	/* Device interface */
486f26c8105SIan Lepore 	DEVMETHOD(device_probe, imx_ehci_probe),
487f26c8105SIan Lepore 	DEVMETHOD(device_attach, imx_ehci_attach),
488f26c8105SIan Lepore 	DEVMETHOD(device_detach, imx_ehci_detach),
489a2c472e7SAleksandr Rybalko 	DEVMETHOD(device_suspend, bus_generic_suspend),
490a2c472e7SAleksandr Rybalko 	DEVMETHOD(device_resume, bus_generic_resume),
491a2c472e7SAleksandr Rybalko 	DEVMETHOD(device_shutdown, bus_generic_shutdown),
492a2c472e7SAleksandr Rybalko 
493a2c472e7SAleksandr Rybalko 	/* Bus interface */
494a2c472e7SAleksandr Rybalko 	DEVMETHOD(bus_print_child, bus_generic_print_child),
495a2c472e7SAleksandr Rybalko 
496f26c8105SIan Lepore 	DEVMETHOD_END
497a2c472e7SAleksandr Rybalko };
498a2c472e7SAleksandr Rybalko 
499a2c472e7SAleksandr Rybalko static driver_t ehci_driver = {
500a2c472e7SAleksandr Rybalko 	"ehci",
501a2c472e7SAleksandr Rybalko 	ehci_methods,
502a2c472e7SAleksandr Rybalko 	sizeof(struct imx_ehci_softc)
503a2c472e7SAleksandr Rybalko };
504a2c472e7SAleksandr Rybalko 
505bc9372d7SJohn Baldwin DRIVER_MODULE(imx_ehci, simplebus, ehci_driver, 0, 0);
506f943f61cSEmmanuel Vadot MODULE_DEPEND(imx_ehci, usb, 1, 1, 1);
507