2 * Copyright (c) 2010-2012 Semihalf
3 * Copyright (c) 2012 The FreeBSD Foundation
4 * Copyright (c) 2013 Ian Lepore <ian@freebsd.org>
7 * Portions of this software were developed by Oleksandr Rybalko
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 * EHCI driver for Freescale i.MX SoCs which incorporate the USBOH3 controller.
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
44 #include <sys/condvar.h>
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/ofw_bus_subr.h>
50 #include <dev/usb/usb.h>
51 #include <dev/usb/usbdi.h>
52 #include <dev/usb/usb_busdma.h>
53 #include <dev/usb/usb_process.h>
54 #include <dev/usb/usb_controller.h>
55 #include <dev/usb/usb_bus.h>
56 #include <dev/usb/controller/ehci.h>
57 #include <dev/usb/controller/ehcireg.h>
60 #include <machine/bus.h>
61 #include <machine/resource.h>
63 #include <arm/freescale/imx/imx_ccmvar.h>
65 #include "opt_platform.h"
68 * Notes on the hardware and related FDT data seen in the wild.
70 * There are two sets of registers in the USBOH3 implementation; documentation
71 * refers to them as "core" and "non-core" registers. A set of core register
72 * exists for each OTG or EHCI device. There is a single set of non-core
73 * registers per USBOH3, and they control aspects of operation not directly
74 * related to the USB specs, such as whether interrupts from each of the core
75 * devices are able to generate a SoC wakeup event.
77 * In the FreeBSD universe we might be inclined to describe the core and
78 * non-core registers by using a pair of resource address/size values (two
79 * entries in the reg property for each core). However, we have to work with
80 * existing FDT data (which mostly comes from the linux universe), and the way
81 * they've chosen to represent this is with an entry for a "usbmisc" device
82 * whose reg property describes the non-core registers. The way we handle FDT
83 * data, this means that the resources (memory-mapped register range) for the
84 * non-core registers belongs to a device other than the echi devices.
86 * At the moment we have no need to access the non-core registers, so all of
87 * this amounts to documenting what's known. The following compat strings have
88 * been seen in existing FDT data:
89 * - "fsl,imx25-usbmisc"
90 * - "fsl,imx51-usbmisc";
91 * - "fsl,imx6q-usbmisc";
93 * In addition to the single usbmisc device, the existing FDT data defines a
94 * separate device for each of the OTG or EHCI cores within the USBOH3. Each of
95 * those devices has a set of core registers described by the reg property.
97 * The core registers for each of the four cores in the USBOH3 are divided into
98 * two parts: a set of imx-specific registers at an offset of 0 from the
99 * beginning of the register range, and the standard USB (EHCI or OTG) registers
100 * at an offset of 0x100 from the beginning of the register range. The FreeBSD
101 * way of dealing with this might be to map out two ranges in the reg property,
102 * but that's not what the alternate universe has done. To work with existing
103 * FDT data, we acquire the resource that maps all the core registers, then use
104 * bus_space_subregion() to create another resource that maps just the standard
105 * USB registers, which we provide to the standard USB code in the ehci_softc.
107 * The following compat strings have been seen for the OTG and EHCI cores. The
108 * FDT compat table in this driver contains all these strings, but as of this
109 * writing, not all of these SoCs have been tested with the driver. The fact
110 * that imx27 is common to all of them gives some hope that the driver will work
112 * - "fsl,imx23-usb", "fsl,imx27-usb";
113 * - "fsl,imx25-usb", "fsl,imx27-usb";
114 * - "fsl,imx28-usb", "fsl,imx27-usb";
115 * - "fsl,imx51-usb", "fsl,imx27-usb";
116 * - "fsl,imx53-usb", "fsl,imx27-usb";
117 * - "fsl,imx6q-usb", "fsl,imx27-usb";
119 * The FDT data for some SoCs contains the following properties, which we don't
120 * currently do anything with:
121 * - fsl,usbmisc = <&usbmisc 0>;
122 * - fsl,usbphy = <&usbphy0>;
124 * Some imx SoCs have FDT data related to USB PHY, some don't. We have separate
125 * usbphy drivers where needed; this data is mentioned here just to keep all the
126 * imx-FDT-usb-related info in one place. Here are the usbphy compat strings
130 * - "fsl,imx23-usbphy"
131 * - "fsl,imx28-usbphy", "fsl,imx23-usbphy";
132 * - "fsl,imx6q-usbphy", "fsl,imx23-usbphy";
136 static struct ofw_compat_data compat_data[] = {
137 {"fsl,imx6q-usb", 1},
138 {"fsl,imx53-usb", 1},
139 {"fsl,imx51-usb", 1},
140 {"fsl,imx28-usb", 1},
141 {"fsl,imx27-usb", 1},
142 {"fsl,imx25-usb", 1},
143 {"fsl,imx23-usb", 1},
148 * Each EHCI device in the SoC has some SoC-specific per-device registers at an
149 * offset of 0, then the standard EHCI registers begin at an offset of 0x100.
151 #define IMX_EHCI_REG_OFF 0x100
152 #define IMX_EHCI_REG_SIZE 0x100
154 struct imx_ehci_softc {
155 ehci_softc_t ehci_softc;
156 struct resource *ehci_mem_res; /* EHCI core regs. */
157 struct resource *ehci_irq_res; /* EHCI core IRQ. */
161 imx_ehci_probe(device_t dev)
164 if (!ofw_bus_status_okay(dev))
167 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) {
168 device_set_desc(dev, "Freescale i.MX integrated USB controller");
169 return (BUS_PROBE_DEFAULT);
175 imx_ehci_detach(device_t dev)
177 struct imx_ehci_softc *sc;
180 sc = device_get_softc(dev);
182 esc = &sc->ehci_softc;
184 if (esc->sc_bus.bdev != NULL)
185 device_delete_child(dev, esc->sc_bus.bdev);
186 if (esc->sc_flags & EHCI_SCFLG_DONEINIT)
188 if (esc->sc_intr_hdl != NULL)
189 bus_teardown_intr(dev, esc->sc_irq_res,
191 if (sc->ehci_irq_res != NULL)
192 bus_release_resource(dev, SYS_RES_IRQ, 0,
194 if (sc->ehci_mem_res != NULL)
195 bus_release_resource(dev, SYS_RES_MEMORY, 0,
198 usb_bus_mem_free_all(&esc->sc_bus, &ehci_iterate_hw_softc);
200 /* During module unload there are lots of children leftover */
201 device_delete_children(dev);
207 imx_ehci_attach(device_t dev)
209 struct imx_ehci_softc *sc;
213 sc = device_get_softc(dev);
214 esc = &sc->ehci_softc;
217 /* Allocate bus_space resources. */
219 sc->ehci_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
221 if (sc->ehci_mem_res == NULL) {
222 device_printf(dev, "Cannot allocate memory resources\n");
228 sc->ehci_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
230 if (sc->ehci_irq_res == NULL) {
231 device_printf(dev, "Cannot allocate IRQ resources\n");
236 esc->sc_io_tag = rman_get_bustag(sc->ehci_mem_res);
237 esc->sc_bus.parent = dev;
238 esc->sc_bus.devices = esc->sc_devices;
239 esc->sc_bus.devices_max = EHCI_MAX_DEVICES;
240 esc->sc_bus.dma_bits = 32;
242 /* allocate all DMA memory */
243 if (usb_bus_mem_alloc_all(&esc->sc_bus, USB_GET_DMA_TAG(dev),
244 &ehci_iterate_hw_softc) != 0) {
245 device_printf(dev, "usb_bus_mem_alloc_all() failed\n");
251 * Set handle to USB related registers subregion used by
252 * generic EHCI driver.
254 err = bus_space_subregion(esc->sc_io_tag,
255 rman_get_bushandle(sc->ehci_mem_res),
256 IMX_EHCI_REG_OFF, IMX_EHCI_REG_SIZE, &esc->sc_io_hdl);
258 device_printf(dev, "bus_space_subregion() failed\n");
263 /* Setup interrupt handler. */
264 err = bus_setup_intr(dev, sc->ehci_irq_res, INTR_TYPE_BIO, NULL,
265 (driver_intr_t *)ehci_interrupt, esc, &esc->sc_intr_hdl);
267 device_printf(dev, "Could not setup IRQ\n");
271 /* Turn on clocks. */
272 imx_ccm_usb_enable(dev);
274 /* Add USB bus device. */
275 esc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
276 if (esc->sc_bus.bdev == NULL) {
277 device_printf(dev, "Could not add USB device\n");
280 device_set_ivars(esc->sc_bus.bdev, &esc->sc_bus);
282 esc->sc_id_vendor = USB_VENDOR_FREESCALE;
283 strlcpy(esc->sc_vendor, "Freescale", sizeof(esc->sc_vendor));
285 /* Set flags that affect ehci_init() behavior. */
286 esc->sc_flags |= EHCI_SCFLG_DONTRESET | EHCI_SCFLG_NORESTERM;
287 err = ehci_init(esc);
289 device_printf(dev, "USB init failed, usb_err_t=%d\n",
293 esc->sc_flags |= EHCI_SCFLG_DONEINIT;
296 err = device_probe_and_attach(esc->sc_bus.bdev);
299 "device_probe_and_attach() failed\n");
308 imx_ehci_detach(dev);
313 static device_method_t ehci_methods[] = {
314 /* Device interface */
315 DEVMETHOD(device_probe, imx_ehci_probe),
316 DEVMETHOD(device_attach, imx_ehci_attach),
317 DEVMETHOD(device_detach, imx_ehci_detach),
318 DEVMETHOD(device_suspend, bus_generic_suspend),
319 DEVMETHOD(device_resume, bus_generic_resume),
320 DEVMETHOD(device_shutdown, bus_generic_shutdown),
323 DEVMETHOD(bus_print_child, bus_generic_print_child),
328 static driver_t ehci_driver = {
331 sizeof(struct imx_ehci_softc)
334 static devclass_t ehci_devclass;
336 DRIVER_MODULE(ehci, simplebus, ehci_driver, ehci_devclass, 0, 0);
337 MODULE_DEPEND(ehci, usb, 1, 1, 1);