1 /*- 2 * Copyright (c) 2010-2012 Semihalf 3 * Copyright (c) 2012 The FreeBSD Foundation 4 * Copyright (c) 2013 Ian Lepore <ian@freebsd.org> 5 * All rights reserved. 6 * 7 * Portions of this software were developed by Oleksandr Rybalko 8 * under sponsorship from the FreeBSD Foundation. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 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. 18 * 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 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 /* 36 * EHCI driver for Freescale i.MX SoCs which incorporate the USBOH3 controller. 37 */ 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/kernel.h> 42 #include <sys/module.h> 43 #include <sys/bus.h> 44 #include <sys/condvar.h> 45 #include <sys/rman.h> 46 47 #include <dev/ofw/ofw_bus.h> 48 #include <dev/ofw/ofw_bus_subr.h> 49 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> 58 #include "usbdevs.h" 59 60 #include <machine/bus.h> 61 #include <machine/resource.h> 62 63 #include <arm/freescale/imx/imx_ccmvar.h> 64 65 #include "opt_platform.h" 66 67 /* 68 * Notes on the hardware and related FDT data seen in the wild. 69 * 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. 76 * 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. 85 * 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"; 92 * 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. 96 * 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. 106 * 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 111 * on all these SoCs. 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"; 118 * 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>; 123 * 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 127 * known to exist: 128 * - "nop-usbphy" 129 * - "usb-nop-xceiv"; 130 * - "fsl,imx23-usbphy" 131 * - "fsl,imx28-usbphy", "fsl,imx23-usbphy"; 132 * - "fsl,imx6q-usbphy", "fsl,imx23-usbphy"; 133 * 134 */ 135 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}, 144 {NULL, 0}, 145 }; 146 147 /* 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. 150 */ 151 #define IMX_EHCI_REG_OFF 0x100 152 #define IMX_EHCI_REG_SIZE 0x100 153 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. */ 158 }; 159 160 static int 161 imx_ehci_probe(device_t dev) 162 { 163 164 if (!ofw_bus_status_okay(dev)) 165 return (ENXIO); 166 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); 170 } 171 return (ENXIO); 172 } 173 174 static int 175 imx_ehci_detach(device_t dev) 176 { 177 struct imx_ehci_softc *sc; 178 ehci_softc_t *esc; 179 180 sc = device_get_softc(dev); 181 182 esc = &sc->ehci_softc; 183 184 if (esc->sc_bus.bdev != NULL) 185 device_delete_child(dev, esc->sc_bus.bdev); 186 if (esc->sc_flags & EHCI_SCFLG_DONEINIT) 187 ehci_detach(esc); 188 if (esc->sc_intr_hdl != NULL) 189 bus_teardown_intr(dev, esc->sc_irq_res, 190 esc->sc_intr_hdl); 191 if (sc->ehci_irq_res != NULL) 192 bus_release_resource(dev, SYS_RES_IRQ, 0, 193 sc->ehci_irq_res); 194 if (sc->ehci_mem_res != NULL) 195 bus_release_resource(dev, SYS_RES_MEMORY, 0, 196 sc->ehci_mem_res); 197 198 usb_bus_mem_free_all(&esc->sc_bus, &ehci_iterate_hw_softc); 199 200 /* During module unload there are lots of children leftover */ 201 device_delete_children(dev); 202 203 return (0); 204 } 205 206 static int 207 imx_ehci_attach(device_t dev) 208 { 209 struct imx_ehci_softc *sc; 210 ehci_softc_t *esc; 211 int err, rid; 212 213 sc = device_get_softc(dev); 214 esc = &sc->ehci_softc; 215 err = 0; 216 217 /* Allocate bus_space resources. */ 218 rid = 0; 219 sc->ehci_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 220 RF_ACTIVE); 221 if (sc->ehci_mem_res == NULL) { 222 device_printf(dev, "Cannot allocate memory resources\n"); 223 err = ENXIO; 224 goto out; 225 } 226 227 rid = 0; 228 sc->ehci_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 229 RF_ACTIVE); 230 if (sc->ehci_irq_res == NULL) { 231 device_printf(dev, "Cannot allocate IRQ resources\n"); 232 err = ENXIO; 233 goto out; 234 } 235 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; 241 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"); 246 err = ENOMEM; 247 goto out; 248 } 249 250 /* 251 * Set handle to USB related registers subregion used by 252 * generic EHCI driver. 253 */ 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); 257 if (err != 0) { 258 device_printf(dev, "bus_space_subregion() failed\n"); 259 err = ENXIO; 260 goto out; 261 } 262 263 /* Setup interrupt handler. */ 264 err = bus_setup_intr(dev, sc->ehci_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 265 NULL, (driver_intr_t *)ehci_interrupt, esc, &esc->sc_intr_hdl); 266 if (err != 0) { 267 device_printf(dev, "Could not setup IRQ\n"); 268 goto out; 269 } 270 271 /* Turn on clocks. */ 272 imx_ccm_usb_enable(dev); 273 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"); 278 goto out; 279 } 280 device_set_ivars(esc->sc_bus.bdev, &esc->sc_bus); 281 282 esc->sc_id_vendor = USB_VENDOR_FREESCALE; 283 strlcpy(esc->sc_vendor, "Freescale", sizeof(esc->sc_vendor)); 284 285 /* Set flags that affect ehci_init() behavior. */ 286 esc->sc_flags |= EHCI_SCFLG_DONTRESET | EHCI_SCFLG_NORESTERM; 287 err = ehci_init(esc); 288 if (err != 0) { 289 device_printf(dev, "USB init failed, usb_err_t=%d\n", 290 err); 291 goto out; 292 } 293 esc->sc_flags |= EHCI_SCFLG_DONEINIT; 294 295 /* Probe the bus. */ 296 err = device_probe_and_attach(esc->sc_bus.bdev); 297 if (err != 0) { 298 device_printf(dev, 299 "device_probe_and_attach() failed\n"); 300 goto out; 301 } 302 303 err = 0; 304 305 out: 306 307 if (err != 0) 308 imx_ehci_detach(dev); 309 310 return (err); 311 } 312 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), 321 322 /* Bus interface */ 323 DEVMETHOD(bus_print_child, bus_generic_print_child), 324 325 DEVMETHOD_END 326 }; 327 328 static driver_t ehci_driver = { 329 "ehci", 330 ehci_methods, 331 sizeof(struct imx_ehci_softc) 332 }; 333 334 static devclass_t ehci_devclass; 335 336 DRIVER_MODULE(ehci, simplebus, ehci_driver, ehci_devclass, 0, 0); 337 MODULE_DEPEND(ehci, usb, 1, 1, 1); 338