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 * Because the main ehci device cannot access registers in a range that's 87 * defined in the fdt data as belonging to another device, we implement a teeny 88 * little "usbmisc" driver which exists only to provide access to the usbmisc 89 * control register for each of the 4 usb controller instances. That little 90 * driver is implemented here in this file, before the main driver. 91 * 92 * In addition to the single usbmisc device, the existing FDT data defines a 93 * separate device for each of the OTG or EHCI cores within the USBOH3. Each of 94 * those devices has a set of core registers described by the reg property. 95 * 96 * The core registers for each of the four cores in the USBOH3 are divided into 97 * two parts: a set of imx-specific registers at an offset of 0 from the 98 * beginning of the register range, and the standard USB (EHCI or OTG) registers 99 * at an offset of 0x100 from the beginning of the register range. The FreeBSD 100 * way of dealing with this might be to map out two ranges in the reg property, 101 * but that's not what the alternate universe has done. To work with existing 102 * FDT data, we acquire the resource that maps all the core registers, then use 103 * bus_space_subregion() to create another resource that maps just the standard 104 * USB registers, which we provide to the standard USB code in the ehci_softc. 105 * 106 * The following compat strings have been seen for the OTG and EHCI cores. The 107 * FDT compat table in this driver contains all these strings, but as of this 108 * writing, not all of these SoCs have been tested with the driver. The fact 109 * that imx27 is common to all of them gives some hope that the driver will work 110 * on all these SoCs. 111 * - "fsl,imx23-usb", "fsl,imx27-usb"; 112 * - "fsl,imx25-usb", "fsl,imx27-usb"; 113 * - "fsl,imx28-usb", "fsl,imx27-usb"; 114 * - "fsl,imx51-usb", "fsl,imx27-usb"; 115 * - "fsl,imx53-usb", "fsl,imx27-usb"; 116 * - "fsl,imx6q-usb", "fsl,imx27-usb"; 117 * 118 * The FDT data for some SoCs contains the following properties, which we don't 119 * currently do anything with: 120 * - fsl,usbmisc = <&usbmisc 0>; 121 * - fsl,usbphy = <&usbphy0>; 122 * 123 * Some imx SoCs have FDT data related to USB PHY, some don't. We have separate 124 * usbphy drivers where needed; this data is mentioned here just to keep all the 125 * imx-FDT-usb-related info in one place. Here are the usbphy compat strings 126 * known to exist: 127 * - "nop-usbphy" 128 * - "usb-nop-xceiv"; 129 * - "fsl,imx23-usbphy" 130 * - "fsl,imx28-usbphy", "fsl,imx23-usbphy"; 131 * - "fsl,imx6q-usbphy", "fsl,imx23-usbphy"; 132 * 133 */ 134 135 /*----------------------------------------------------------------------------- 136 * imx_usbmisc driver 137 *---------------------------------------------------------------------------*/ 138 139 #define USBNC_OVER_CUR_POL (1u << 8) 140 #define USBNC_OVER_CUR_DIS (1u << 7) 141 142 struct imx_usbmisc_softc { 143 device_t dev; 144 struct resource *mmio; 145 }; 146 147 static struct ofw_compat_data usbmisc_compat_data[] = { 148 {"fsl,imx6q-usbmisc", true}, 149 {"fsl,imx51-usbmisc", true}, 150 {"fsl,imx25-usbmisc", true}, 151 {NULL, false}, 152 }; 153 154 static void 155 imx_usbmisc_set_ctrl(device_t dev, u_int index, uint32_t bits) 156 { 157 struct imx_usbmisc_softc *sc; 158 uint32_t reg; 159 160 sc = device_get_softc(dev); 161 reg = bus_read_4(sc->mmio, index * sizeof(uint32_t)); 162 bus_write_4(sc->mmio, index * sizeof(uint32_t), reg | bits); 163 } 164 165 #ifdef notyet 166 static void 167 imx_usbmisc_clr_ctrl(device_t dev, u_int index, uint32_t bits) 168 { 169 struct imx_usbmisc_softc *sc; 170 uint32_t reg; 171 172 sc = device_get_softc(dev); 173 reg = bus_read_4(sc->mmio, index * sizeof(uint32_t)); 174 bus_write_4(sc->mmio, index * sizeof(uint32_t), reg & ~bits); 175 } 176 #endif 177 178 static int 179 imx_usbmisc_probe(device_t dev) 180 { 181 182 if (!ofw_bus_status_okay(dev)) 183 return (ENXIO); 184 185 if (ofw_bus_search_compatible(dev, usbmisc_compat_data)->ocd_data) { 186 device_set_desc(dev, "i.MX USB Misc Control"); 187 return (BUS_PROBE_DEFAULT); 188 } 189 return (ENXIO); 190 } 191 192 static int 193 imx_usbmisc_detach(device_t dev) 194 { 195 struct imx_usbmisc_softc *sc; 196 197 sc = device_get_softc(dev); 198 199 if (sc->mmio != NULL) 200 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->mmio); 201 202 return (0); 203 } 204 205 static int 206 imx_usbmisc_attach(device_t dev) 207 { 208 struct imx_usbmisc_softc *sc; 209 int err, rid; 210 211 sc = device_get_softc(dev); 212 err = 0; 213 214 /* Allocate bus_space resources. */ 215 rid = 0; 216 sc->mmio = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 217 RF_ACTIVE); 218 if (sc->mmio == NULL) { 219 device_printf(dev, "Cannot allocate memory resources\n"); 220 return (ENXIO); 221 } 222 223 OF_device_register_xref(OF_xref_from_node(ofw_bus_get_node(dev)), dev); 224 225 return (0); 226 } 227 228 static device_method_t imx_usbmisc_methods[] = { 229 /* Device interface */ 230 DEVMETHOD(device_probe, imx_usbmisc_probe), 231 DEVMETHOD(device_attach, imx_usbmisc_attach), 232 DEVMETHOD(device_detach, imx_usbmisc_detach), 233 234 DEVMETHOD_END 235 }; 236 237 static driver_t imx_usbmisc_driver = { 238 "imx_usbmisc", 239 imx_usbmisc_methods, 240 sizeof(struct imx_usbmisc_softc) 241 }; 242 243 static devclass_t imx_usbmisc_devclass; 244 245 /* 246 * This driver needs to start before the ehci driver, but later than the usual 247 * "special" drivers like clocks and cpu. Ehci starts at DEFAULT so 248 * DEFAULT-1000 seems good. 249 */ 250 EARLY_DRIVER_MODULE(imx_usbmisc, simplebus, imx_usbmisc_driver, 251 imx_usbmisc_devclass, 0, 0, BUS_PASS_DEFAULT - 1000); 252 253 /*----------------------------------------------------------------------------- 254 * imx_ehci driver... 255 *---------------------------------------------------------------------------*/ 256 257 /* 258 * Each EHCI device in the SoC has some SoC-specific per-device registers at an 259 * offset of 0, then the standard EHCI registers begin at an offset of 0x100. 260 */ 261 #define IMX_EHCI_REG_OFF 0x100 262 #define IMX_EHCI_REG_SIZE 0x100 263 264 struct imx_ehci_softc { 265 ehci_softc_t ehci_softc; 266 device_t dev; 267 struct resource *ehci_mem_res; /* EHCI core regs. */ 268 struct resource *ehci_irq_res; /* EHCI core IRQ. */ 269 }; 270 271 static struct ofw_compat_data compat_data[] = { 272 {"fsl,imx6q-usb", 1}, 273 {"fsl,imx53-usb", 1}, 274 {"fsl,imx51-usb", 1}, 275 {"fsl,imx28-usb", 1}, 276 {"fsl,imx27-usb", 1}, 277 {"fsl,imx25-usb", 1}, 278 {"fsl,imx23-usb", 1}, 279 {NULL, 0}, 280 }; 281 282 static void 283 imx_ehci_post_reset(struct ehci_softc *ehci_softc) 284 { 285 uint32_t usbmode; 286 287 /* Force HOST mode */ 288 usbmode = EOREAD4(ehci_softc, EHCI_USBMODE_NOLPM); 289 usbmode &= ~EHCI_UM_CM; 290 usbmode |= EHCI_UM_CM_HOST; 291 EOWRITE4(ehci_softc, EHCI_USBMODE_NOLPM, usbmode); 292 } 293 294 static int 295 imx_ehci_probe(device_t dev) 296 { 297 298 if (!ofw_bus_status_okay(dev)) 299 return (ENXIO); 300 301 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) { 302 device_set_desc(dev, "Freescale i.MX integrated USB controller"); 303 return (BUS_PROBE_DEFAULT); 304 } 305 return (ENXIO); 306 } 307 308 static int 309 imx_ehci_detach(device_t dev) 310 { 311 struct imx_ehci_softc *sc; 312 ehci_softc_t *esc; 313 314 sc = device_get_softc(dev); 315 316 esc = &sc->ehci_softc; 317 318 if (esc->sc_bus.bdev != NULL) 319 device_delete_child(dev, esc->sc_bus.bdev); 320 if (esc->sc_flags & EHCI_SCFLG_DONEINIT) 321 ehci_detach(esc); 322 if (esc->sc_intr_hdl != NULL) 323 bus_teardown_intr(dev, esc->sc_irq_res, 324 esc->sc_intr_hdl); 325 if (sc->ehci_irq_res != NULL) 326 bus_release_resource(dev, SYS_RES_IRQ, 0, 327 sc->ehci_irq_res); 328 if (sc->ehci_mem_res != NULL) 329 bus_release_resource(dev, SYS_RES_MEMORY, 0, 330 sc->ehci_mem_res); 331 332 usb_bus_mem_free_all(&esc->sc_bus, &ehci_iterate_hw_softc); 333 334 /* During module unload there are lots of children leftover */ 335 device_delete_children(dev); 336 337 return (0); 338 } 339 340 static void 341 imx_ehci_disable_oc(struct imx_ehci_softc *sc) 342 { 343 device_t usbmdev; 344 pcell_t usbmprops[2]; 345 phandle_t node; 346 ssize_t size; 347 int index; 348 349 /* Get the reference to the usbmisc driver from the fdt data */ 350 node = ofw_bus_get_node(sc->dev); 351 size = OF_getencprop(node, "fsl,usbmisc", usbmprops, 352 sizeof(usbmprops)); 353 if (size < sizeof(usbmprops)) { 354 device_printf(sc->dev, "failed to retrieve fsl,usbmisc " 355 "property, cannot disable overcurrent protection"); 356 return; 357 } 358 /* Retrieve the device_t via the xref handle. */ 359 usbmdev = OF_device_from_xref(usbmprops[0]); 360 if (usbmdev == NULL) { 361 device_printf(sc->dev, "usbmisc device not found, " 362 "cannot disable overcurrent protection"); 363 return; 364 } 365 /* Call the device routine to set the overcurrent disable bit. */ 366 index = usbmprops[1]; 367 imx_usbmisc_set_ctrl(usbmdev, index, USBNC_OVER_CUR_DIS); 368 } 369 370 static int 371 imx_ehci_attach(device_t dev) 372 { 373 struct imx_ehci_softc *sc; 374 ehci_softc_t *esc; 375 int err, rid; 376 377 sc = device_get_softc(dev); 378 sc->dev = dev; 379 esc = &sc->ehci_softc; 380 err = 0; 381 382 /* Allocate bus_space resources. */ 383 rid = 0; 384 sc->ehci_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 385 RF_ACTIVE); 386 if (sc->ehci_mem_res == NULL) { 387 device_printf(dev, "Cannot allocate memory resources\n"); 388 err = ENXIO; 389 goto out; 390 } 391 392 rid = 0; 393 sc->ehci_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 394 RF_ACTIVE); 395 if (sc->ehci_irq_res == NULL) { 396 device_printf(dev, "Cannot allocate IRQ resources\n"); 397 err = ENXIO; 398 goto out; 399 } 400 401 esc->sc_io_tag = rman_get_bustag(sc->ehci_mem_res); 402 esc->sc_bus.parent = dev; 403 esc->sc_bus.devices = esc->sc_devices; 404 esc->sc_bus.devices_max = EHCI_MAX_DEVICES; 405 esc->sc_bus.dma_bits = 32; 406 407 /* allocate all DMA memory */ 408 if (usb_bus_mem_alloc_all(&esc->sc_bus, USB_GET_DMA_TAG(dev), 409 &ehci_iterate_hw_softc) != 0) { 410 device_printf(dev, "usb_bus_mem_alloc_all() failed\n"); 411 err = ENOMEM; 412 goto out; 413 } 414 415 /* 416 * Set handle to USB related registers subregion used by 417 * generic EHCI driver. 418 */ 419 err = bus_space_subregion(esc->sc_io_tag, 420 rman_get_bushandle(sc->ehci_mem_res), 421 IMX_EHCI_REG_OFF, IMX_EHCI_REG_SIZE, &esc->sc_io_hdl); 422 if (err != 0) { 423 device_printf(dev, "bus_space_subregion() failed\n"); 424 err = ENXIO; 425 goto out; 426 } 427 428 /* Setup interrupt handler. */ 429 err = bus_setup_intr(dev, sc->ehci_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 430 NULL, (driver_intr_t *)ehci_interrupt, esc, &esc->sc_intr_hdl); 431 if (err != 0) { 432 device_printf(dev, "Could not setup IRQ\n"); 433 goto out; 434 } 435 436 /* Turn on clocks. */ 437 imx_ccm_usb_enable(dev); 438 439 /* Disable overcurrent detection, if configured to do so. */ 440 if (OF_hasprop(ofw_bus_get_node(sc->dev), "disable-over-current")) 441 imx_ehci_disable_oc(sc); 442 443 /* Add USB bus device. */ 444 esc->sc_bus.bdev = device_add_child(dev, "usbus", -1); 445 if (esc->sc_bus.bdev == NULL) { 446 device_printf(dev, "Could not add USB device\n"); 447 goto out; 448 } 449 device_set_ivars(esc->sc_bus.bdev, &esc->sc_bus); 450 451 esc->sc_id_vendor = USB_VENDOR_FREESCALE; 452 strlcpy(esc->sc_vendor, "Freescale", sizeof(esc->sc_vendor)); 453 454 /* 455 * Set flags that affect ehci_init() behavior, and hook our post-reset 456 * code into the standard controller code. 457 */ 458 esc->sc_flags |= EHCI_SCFLG_NORESTERM | EHCI_SCFLG_TT; 459 esc->sc_vendor_post_reset = imx_ehci_post_reset; 460 esc->sc_vendor_get_port_speed = ehci_get_port_speed_portsc; 461 462 err = ehci_init(esc); 463 if (err != 0) { 464 device_printf(dev, "USB init failed, usb_err_t=%d\n", 465 err); 466 goto out; 467 } 468 esc->sc_flags |= EHCI_SCFLG_DONEINIT; 469 470 /* Probe the bus. */ 471 err = device_probe_and_attach(esc->sc_bus.bdev); 472 if (err != 0) { 473 device_printf(dev, 474 "device_probe_and_attach() failed\n"); 475 goto out; 476 } 477 478 err = 0; 479 480 out: 481 482 if (err != 0) 483 imx_ehci_detach(dev); 484 485 return (err); 486 } 487 488 static device_method_t ehci_methods[] = { 489 /* Device interface */ 490 DEVMETHOD(device_probe, imx_ehci_probe), 491 DEVMETHOD(device_attach, imx_ehci_attach), 492 DEVMETHOD(device_detach, imx_ehci_detach), 493 DEVMETHOD(device_suspend, bus_generic_suspend), 494 DEVMETHOD(device_resume, bus_generic_resume), 495 DEVMETHOD(device_shutdown, bus_generic_shutdown), 496 497 /* Bus interface */ 498 DEVMETHOD(bus_print_child, bus_generic_print_child), 499 500 DEVMETHOD_END 501 }; 502 503 static driver_t ehci_driver = { 504 "ehci", 505 ehci_methods, 506 sizeof(struct imx_ehci_softc) 507 }; 508 509 static devclass_t ehci_devclass; 510 511 DRIVER_MODULE(ehci, simplebus, ehci_driver, ehci_devclass, 0, 0); 512 MODULE_DEPEND(ehci, usb, 1, 1, 1); 513