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