ehci_imx.c revision 314513
1248557Sray/*- 2248557Sray * Copyright (c) 2010-2012 Semihalf 3248557Sray * Copyright (c) 2012 The FreeBSD Foundation 4257393Sian * Copyright (c) 2013 Ian Lepore <ian@freebsd.org> 5248557Sray * All rights reserved. 6248557Sray * 7248557Sray * Portions of this software were developed by Oleksandr Rybalko 8248557Sray * under sponsorship from the FreeBSD Foundation. 9248557Sray * 10248557Sray * Redistribution and use in source and binary forms, with or without 11248557Sray * modification, are permitted provided that the following conditions 12248557Sray * are met: 13248557Sray * 1. Redistributions of source code must retain the above copyright 14248557Sray * notice, this list of conditions and the following disclaimer. 15248557Sray * 2. Redistributions in binary form must reproduce the above copyright 16248557Sray * notice, this list of conditions and the following disclaimer in the 17248557Sray * documentation and/or other materials provided with the distribution. 18248557Sray * 19248557Sray * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20248557Sray * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21248557Sray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22248557Sray * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23248557Sray * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24248557Sray * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25248557Sray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26248557Sray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27248557Sray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28248557Sray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29248557Sray * SUCH DAMAGE. 30248557Sray */ 31248557Sray 32248557Sray#include <sys/cdefs.h> 33248557Sray__FBSDID("$FreeBSD: stable/11/sys/dev/usb/controller/ehci_imx.c 314513 2017-03-01 21:12:50Z ian $"); 34248557Sray 35257393Sian/* 36257393Sian * EHCI driver for Freescale i.MX SoCs which incorporate the USBOH3 controller. 37257393Sian */ 38248557Sray 39248557Sray#include <sys/param.h> 40248557Sray#include <sys/systm.h> 41248557Sray#include <sys/kernel.h> 42248557Sray#include <sys/module.h> 43248557Sray#include <sys/bus.h> 44248557Sray#include <sys/condvar.h> 45248557Sray#include <sys/rman.h> 46248557Sray 47248557Sray#include <dev/ofw/ofw_bus.h> 48248557Sray#include <dev/ofw/ofw_bus_subr.h> 49248557Sray 50248557Sray#include <dev/usb/usb.h> 51248557Sray#include <dev/usb/usbdi.h> 52248557Sray#include <dev/usb/usb_busdma.h> 53248557Sray#include <dev/usb/usb_process.h> 54248557Sray#include <dev/usb/usb_controller.h> 55248557Sray#include <dev/usb/usb_bus.h> 56248557Sray#include <dev/usb/controller/ehci.h> 57248557Sray#include <dev/usb/controller/ehcireg.h> 58257393Sian#include "usbdevs.h" 59248557Sray 60248557Sray#include <machine/bus.h> 61248557Sray#include <machine/resource.h> 62248557Sray 63264977Sian#include <arm/freescale/imx/imx_ccmvar.h> 64257393Sian 65248557Sray#include "opt_platform.h" 66248557Sray 67257393Sian/* 68257393Sian * Notes on the hardware and related FDT data seen in the wild. 69257393Sian * 70257393Sian * There are two sets of registers in the USBOH3 implementation; documentation 71257393Sian * refers to them as "core" and "non-core" registers. A set of core register 72257393Sian * exists for each OTG or EHCI device. There is a single set of non-core 73257393Sian * registers per USBOH3, and they control aspects of operation not directly 74257393Sian * related to the USB specs, such as whether interrupts from each of the core 75257393Sian * devices are able to generate a SoC wakeup event. 76257393Sian * 77257393Sian * In the FreeBSD universe we might be inclined to describe the core and 78257393Sian * non-core registers by using a pair of resource address/size values (two 79257393Sian * entries in the reg property for each core). However, we have to work with 80257393Sian * existing FDT data (which mostly comes from the linux universe), and the way 81257393Sian * they've chosen to represent this is with an entry for a "usbmisc" device 82257393Sian * whose reg property describes the non-core registers. The way we handle FDT 83257393Sian * data, this means that the resources (memory-mapped register range) for the 84257393Sian * non-core registers belongs to a device other than the echi devices. 85257393Sian * 86257393Sian * At the moment we have no need to access the non-core registers, so all of 87257393Sian * this amounts to documenting what's known. The following compat strings have 88257393Sian * been seen in existing FDT data: 89257393Sian * - "fsl,imx25-usbmisc" 90257393Sian * - "fsl,imx51-usbmisc"; 91257393Sian * - "fsl,imx6q-usbmisc"; 92257393Sian * 93257393Sian * In addition to the single usbmisc device, the existing FDT data defines a 94257393Sian * separate device for each of the OTG or EHCI cores within the USBOH3. Each of 95257393Sian * those devices has a set of core registers described by the reg property. 96257393Sian * 97257393Sian * The core registers for each of the four cores in the USBOH3 are divided into 98257393Sian * two parts: a set of imx-specific registers at an offset of 0 from the 99257393Sian * beginning of the register range, and the standard USB (EHCI or OTG) registers 100257393Sian * at an offset of 0x100 from the beginning of the register range. The FreeBSD 101257393Sian * way of dealing with this might be to map out two ranges in the reg property, 102257393Sian * but that's not what the alternate universe has done. To work with existing 103257393Sian * FDT data, we acquire the resource that maps all the core registers, then use 104257393Sian * bus_space_subregion() to create another resource that maps just the standard 105257393Sian * USB registers, which we provide to the standard USB code in the ehci_softc. 106257393Sian * 107257393Sian * The following compat strings have been seen for the OTG and EHCI cores. The 108257393Sian * FDT compat table in this driver contains all these strings, but as of this 109257393Sian * writing, not all of these SoCs have been tested with the driver. The fact 110257393Sian * that imx27 is common to all of them gives some hope that the driver will work 111257393Sian * on all these SoCs. 112257393Sian * - "fsl,imx23-usb", "fsl,imx27-usb"; 113257393Sian * - "fsl,imx25-usb", "fsl,imx27-usb"; 114257393Sian * - "fsl,imx28-usb", "fsl,imx27-usb"; 115257393Sian * - "fsl,imx51-usb", "fsl,imx27-usb"; 116257393Sian * - "fsl,imx53-usb", "fsl,imx27-usb"; 117257393Sian * - "fsl,imx6q-usb", "fsl,imx27-usb"; 118257393Sian * 119257393Sian * The FDT data for some SoCs contains the following properties, which we don't 120257393Sian * currently do anything with: 121257393Sian * - fsl,usbmisc = <&usbmisc 0>; 122257393Sian * - fsl,usbphy = <&usbphy0>; 123257393Sian * 124257393Sian * Some imx SoCs have FDT data related to USB PHY, some don't. We have separate 125257393Sian * usbphy drivers where needed; this data is mentioned here just to keep all the 126257393Sian * imx-FDT-usb-related info in one place. Here are the usbphy compat strings 127257393Sian * known to exist: 128257393Sian * - "nop-usbphy" 129257393Sian * - "usb-nop-xceiv"; 130257393Sian * - "fsl,imx23-usbphy" 131257393Sian * - "fsl,imx28-usbphy", "fsl,imx23-usbphy"; 132257393Sian * - "fsl,imx6q-usbphy", "fsl,imx23-usbphy"; 133257393Sian * 134257393Sian */ 135248557Sray 136257393Sianstatic struct ofw_compat_data compat_data[] = { 137257393Sian {"fsl,imx6q-usb", 1}, 138257393Sian {"fsl,imx53-usb", 1}, 139257393Sian {"fsl,imx51-usb", 1}, 140257393Sian {"fsl,imx28-usb", 1}, 141257393Sian {"fsl,imx27-usb", 1}, 142257393Sian {"fsl,imx25-usb", 1}, 143257393Sian {"fsl,imx23-usb", 1}, 144257393Sian {NULL, 0}, 145248557Sray}; 146248557Sray 147257393Sian/* 148257393Sian * Each EHCI device in the SoC has some SoC-specific per-device registers at an 149257393Sian * offset of 0, then the standard EHCI registers begin at an offset of 0x100. 150257393Sian */ 151257393Sian#define IMX_EHCI_REG_OFF 0x100 152257393Sian#define IMX_EHCI_REG_SIZE 0x100 153257393Sian 154257393Sianstruct imx_ehci_softc { 155257393Sian ehci_softc_t ehci_softc; 156257393Sian struct resource *ehci_mem_res; /* EHCI core regs. */ 157257393Sian struct resource *ehci_irq_res; /* EHCI core IRQ. */ 158248557Sray}; 159248557Sray 160313649Sianstatic void 161313649Sianimx_ehci_post_reset(struct ehci_softc *ehci_softc) 162313649Sian{ 163313649Sian uint32_t usbmode; 164313649Sian 165313649Sian /* Force HOST mode */ 166313649Sian usbmode = EOREAD4(ehci_softc, EHCI_USBMODE_NOLPM); 167313649Sian usbmode &= ~EHCI_UM_CM; 168313649Sian usbmode |= EHCI_UM_CM_HOST; 169313649Sian EOWRITE4(ehci_softc, EHCI_USBMODE_NOLPM, usbmode); 170313649Sian} 171313649Sian 172257393Sianstatic int 173257393Sianimx_ehci_probe(device_t dev) 174257393Sian{ 175248557Sray 176261410Sian if (!ofw_bus_status_okay(dev)) 177261410Sian return (ENXIO); 178261410Sian 179257393Sian if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) { 180257393Sian device_set_desc(dev, "Freescale i.MX integrated USB controller"); 181257393Sian return (BUS_PROBE_DEFAULT); 182257393Sian } 183257393Sian return (ENXIO); 184257393Sian} 185248557Sray 186257393Sianstatic int 187257393Sianimx_ehci_detach(device_t dev) 188257393Sian{ 189257393Sian struct imx_ehci_softc *sc; 190257393Sian ehci_softc_t *esc; 191248557Sray 192257393Sian sc = device_get_softc(dev); 193248557Sray 194257393Sian esc = &sc->ehci_softc; 195248557Sray 196257393Sian if (esc->sc_bus.bdev != NULL) 197257393Sian device_delete_child(dev, esc->sc_bus.bdev); 198257393Sian if (esc->sc_flags & EHCI_SCFLG_DONEINIT) 199257393Sian ehci_detach(esc); 200257393Sian if (esc->sc_intr_hdl != NULL) 201257393Sian bus_teardown_intr(dev, esc->sc_irq_res, 202257393Sian esc->sc_intr_hdl); 203257393Sian if (sc->ehci_irq_res != NULL) 204257393Sian bus_release_resource(dev, SYS_RES_IRQ, 0, 205257393Sian sc->ehci_irq_res); 206257393Sian if (sc->ehci_mem_res != NULL) 207257393Sian bus_release_resource(dev, SYS_RES_MEMORY, 0, 208257393Sian sc->ehci_mem_res); 209248557Sray 210257393Sian usb_bus_mem_free_all(&esc->sc_bus, &ehci_iterate_hw_softc); 211248557Sray 212257393Sian /* During module unload there are lots of children leftover */ 213257393Sian device_delete_children(dev); 214248557Sray 215257393Sian return (0); 216248557Sray} 217248557Sray 218248557Sraystatic int 219257393Sianimx_ehci_attach(device_t dev) 220248557Sray{ 221248557Sray struct imx_ehci_softc *sc; 222248557Sray ehci_softc_t *esc; 223257393Sian int err, rid; 224248557Sray 225257393Sian sc = device_get_softc(dev); 226257393Sian esc = &sc->ehci_softc; 227257393Sian err = 0; 228257393Sian 229257393Sian /* Allocate bus_space resources. */ 230248557Sray rid = 0; 231257393Sian sc->ehci_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 232257393Sian RF_ACTIVE); 233257393Sian if (sc->ehci_mem_res == NULL) { 234257393Sian device_printf(dev, "Cannot allocate memory resources\n"); 235257393Sian err = ENXIO; 236257393Sian goto out; 237257393Sian } 238248557Sray 239257393Sian rid = 0; 240257393Sian sc->ehci_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 241257393Sian RF_ACTIVE); 242257393Sian if (sc->ehci_irq_res == NULL) { 243257393Sian device_printf(dev, "Cannot allocate IRQ resources\n"); 244257393Sian err = ENXIO; 245257393Sian goto out; 246248557Sray } 247248557Sray 248257393Sian esc->sc_io_tag = rman_get_bustag(sc->ehci_mem_res); 249257393Sian esc->sc_bus.parent = dev; 250257393Sian esc->sc_bus.devices = esc->sc_devices; 251257393Sian esc->sc_bus.devices_max = EHCI_MAX_DEVICES; 252276717Shselasky esc->sc_bus.dma_bits = 32; 253248557Sray 254276717Shselasky /* allocate all DMA memory */ 255257393Sian if (usb_bus_mem_alloc_all(&esc->sc_bus, USB_GET_DMA_TAG(dev), 256257393Sian &ehci_iterate_hw_softc) != 0) { 257257393Sian device_printf(dev, "usb_bus_mem_alloc_all() failed\n"); 258257393Sian err = ENOMEM; 259257393Sian goto out; 260257393Sian } 261248557Sray 262257393Sian /* 263257393Sian * Set handle to USB related registers subregion used by 264257393Sian * generic EHCI driver. 265257393Sian */ 266257393Sian err = bus_space_subregion(esc->sc_io_tag, 267257393Sian rman_get_bushandle(sc->ehci_mem_res), 268257393Sian IMX_EHCI_REG_OFF, IMX_EHCI_REG_SIZE, &esc->sc_io_hdl); 269257393Sian if (err != 0) { 270257393Sian device_printf(dev, "bus_space_subregion() failed\n"); 271257393Sian err = ENXIO; 272257393Sian goto out; 273257393Sian } 274248557Sray 275257393Sian /* Setup interrupt handler. */ 276297579Smmel err = bus_setup_intr(dev, sc->ehci_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 277297579Smmel NULL, (driver_intr_t *)ehci_interrupt, esc, &esc->sc_intr_hdl); 278257393Sian if (err != 0) { 279257393Sian device_printf(dev, "Could not setup IRQ\n"); 280257393Sian goto out; 281257393Sian } 282248557Sray 283257393Sian /* Turn on clocks. */ 284257393Sian imx_ccm_usb_enable(dev); 285248557Sray 286257393Sian /* Add USB bus device. */ 287257393Sian esc->sc_bus.bdev = device_add_child(dev, "usbus", -1); 288257393Sian if (esc->sc_bus.bdev == NULL) { 289257393Sian device_printf(dev, "Could not add USB device\n"); 290257393Sian goto out; 291257393Sian } 292257393Sian device_set_ivars(esc->sc_bus.bdev, &esc->sc_bus); 293248557Sray 294257393Sian esc->sc_id_vendor = USB_VENDOR_FREESCALE; 295257393Sian strlcpy(esc->sc_vendor, "Freescale", sizeof(esc->sc_vendor)); 296248557Sray 297313649Sian /* 298313649Sian * Set flags that affect ehci_init() behavior, and hook our post-reset 299313649Sian * code into the standard controller code. 300313649Sian */ 301314513Sian esc->sc_flags |= EHCI_SCFLG_NORESTERM | EHCI_SCFLG_TT; 302313649Sian esc->sc_vendor_post_reset = imx_ehci_post_reset; 303314513Sian esc->sc_vendor_get_port_speed = ehci_get_port_speed_portsc; 304313649Sian 305257393Sian err = ehci_init(esc); 306257393Sian if (err != 0) { 307257393Sian device_printf(dev, "USB init failed, usb_err_t=%d\n", 308257393Sian err); 309257393Sian goto out; 310257393Sian } 311257393Sian esc->sc_flags |= EHCI_SCFLG_DONEINIT; 312248557Sray 313257393Sian /* Probe the bus. */ 314257393Sian err = device_probe_and_attach(esc->sc_bus.bdev); 315257393Sian if (err != 0) { 316257393Sian device_printf(dev, 317257393Sian "device_probe_and_attach() failed\n"); 318257393Sian goto out; 319257393Sian } 320248557Sray 321257393Sian err = 0; 322248557Sray 323257393Sianout: 324248557Sray 325257393Sian if (err != 0) 326257393Sian imx_ehci_detach(dev); 327248557Sray 328257393Sian return (err); 329248557Sray} 330248557Sray 331257393Sianstatic device_method_t ehci_methods[] = { 332257393Sian /* Device interface */ 333257393Sian DEVMETHOD(device_probe, imx_ehci_probe), 334257393Sian DEVMETHOD(device_attach, imx_ehci_attach), 335257393Sian DEVMETHOD(device_detach, imx_ehci_detach), 336257393Sian DEVMETHOD(device_suspend, bus_generic_suspend), 337257393Sian DEVMETHOD(device_resume, bus_generic_resume), 338257393Sian DEVMETHOD(device_shutdown, bus_generic_shutdown), 339248557Sray 340257393Sian /* Bus interface */ 341257393Sian DEVMETHOD(bus_print_child, bus_generic_print_child), 342248557Sray 343257393Sian DEVMETHOD_END 344257393Sian}; 345248557Sray 346257393Sianstatic driver_t ehci_driver = { 347257393Sian "ehci", 348257393Sian ehci_methods, 349257393Sian sizeof(struct imx_ehci_softc) 350257393Sian}; 351248557Sray 352257393Sianstatic devclass_t ehci_devclass; 353248557Sray 354257393SianDRIVER_MODULE(ehci, simplebus, ehci_driver, ehci_devclass, 0, 0); 355257393SianMODULE_DEPEND(ehci, usb, 1, 1, 1); 356