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