1/*- 2 * Copyright (c) 2013-2014 Ruslan Bukin <br@bsdpad.com> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#ifdef USB_GLOBAL_INCLUDE_FILE 28#include USB_GLOBAL_INCLUDE_FILE 29#else 30#include <sys/cdefs.h> 31__FBSDID("$FreeBSD: stable/11/sys/arm/samsung/exynos/exynos5_ehci.c 346524 2019-04-22 04:56:41Z ian $"); 32 33#include "opt_bus.h" 34 35#include <sys/param.h> 36#include <sys/systm.h> 37#include <sys/kernel.h> 38#include <sys/module.h> 39#include <sys/bus.h> 40#include <sys/condvar.h> 41#include <sys/rman.h> 42#include <sys/gpio.h> 43 44#include <dev/ofw/ofw_bus.h> 45#include <dev/ofw/ofw_bus_subr.h> 46 47#include <dev/usb/usb.h> 48#include <dev/usb/usbdi.h> 49#include <dev/usb/usb_busdma.h> 50#include <dev/usb/usb_process.h> 51#include <dev/usb/usb_controller.h> 52#include <dev/usb/usb_bus.h> 53#include <dev/usb/controller/ehci.h> 54#include <dev/usb/controller/ehcireg.h> 55 56#include <machine/bus.h> 57#include <machine/resource.h> 58 59#include <arm/samsung/exynos/exynos5_common.h> 60#include <arm/samsung/exynos/exynos5_pmu.h> 61 62#include "gpio_if.h" 63 64#include "opt_platform.h" 65#endif 66 67/* GPIO control */ 68#define GPIO_OUTPUT 1 69#define GPIO_INPUT 0 70#define PIN_USB 161 71 72/* SYSREG */ 73#define EXYNOS5_SYSREG_USB2_PHY 0x0 74#define USB2_MODE_HOST 0x1 75 76/* USB HOST */ 77#define HOST_CTRL_CLK_24MHZ (5 << 16) 78#define HOST_CTRL_CLK_MASK (7 << 16) 79#define HOST_CTRL_SIDDQ (1 << 6) 80#define HOST_CTRL_SLEEP (1 << 5) 81#define HOST_CTRL_SUSPEND (1 << 4) 82#define HOST_CTRL_RESET_LINK (1 << 1) 83#define HOST_CTRL_RESET_PHY (1 << 0) 84#define HOST_CTRL_RESET_PHY_ALL (1U << 31) 85 86/* Forward declarations */ 87static int exynos_ehci_attach(device_t dev); 88static int exynos_ehci_detach(device_t dev); 89static int exynos_ehci_probe(device_t dev); 90 91struct exynos_ehci_softc { 92 device_t dev; 93 ehci_softc_t base; 94 struct resource *res[4]; 95 bus_space_tag_t host_bst; 96 bus_space_tag_t sysreg_bst; 97 bus_space_handle_t host_bsh; 98 bus_space_handle_t sysreg_bsh; 99 100}; 101 102static struct resource_spec exynos_ehci_spec[] = { 103 { SYS_RES_MEMORY, 0, RF_ACTIVE }, 104 { SYS_RES_MEMORY, 1, RF_ACTIVE }, 105 { SYS_RES_MEMORY, 2, RF_ACTIVE }, 106 { SYS_RES_IRQ, 0, RF_ACTIVE }, 107 { -1, 0 } 108}; 109 110static device_method_t ehci_methods[] = { 111 /* Device interface */ 112 DEVMETHOD(device_probe, exynos_ehci_probe), 113 DEVMETHOD(device_attach, exynos_ehci_attach), 114 DEVMETHOD(device_detach, exynos_ehci_detach), 115 DEVMETHOD(device_suspend, bus_generic_suspend), 116 DEVMETHOD(device_resume, bus_generic_resume), 117 DEVMETHOD(device_shutdown, bus_generic_shutdown), 118 119 /* Bus interface */ 120 DEVMETHOD(bus_print_child, bus_generic_print_child), 121 122 { 0, 0 } 123}; 124 125/* kobj_class definition */ 126static driver_t ehci_driver = { 127 "ehci", 128 ehci_methods, 129 sizeof(struct exynos_ehci_softc) 130}; 131 132static devclass_t ehci_devclass; 133 134DRIVER_MODULE(exynos_ehci, simplebus, ehci_driver, ehci_devclass, 0, 0); 135MODULE_DEPEND(exynos_ehci, usb, 1, 1, 1); 136 137/* 138 * Public methods 139 */ 140static int 141exynos_ehci_probe(device_t dev) 142{ 143 144 if (!ofw_bus_status_okay(dev)) 145 return (ENXIO); 146 147 if (ofw_bus_is_compatible(dev, "exynos,usb-ehci") == 0) 148 return (ENXIO); 149 150 device_set_desc(dev, "Exynos integrated USB controller"); 151 return (BUS_PROBE_DEFAULT); 152} 153 154static int 155gpio_ctrl(struct exynos_ehci_softc *esc, int dir, int power) 156{ 157 device_t gpio_dev; 158 159 /* Get the GPIO device, we need this to give power to USB */ 160 gpio_dev = devclass_get_device(devclass_find("gpio"), 0); 161 if (gpio_dev == NULL) { 162 device_printf(esc->dev, "cant find gpio_dev\n"); 163 return (1); 164 } 165 166 if (power) 167 GPIO_PIN_SET(gpio_dev, PIN_USB, GPIO_PIN_HIGH); 168 else 169 GPIO_PIN_SET(gpio_dev, PIN_USB, GPIO_PIN_LOW); 170 171 if (dir) 172 GPIO_PIN_SETFLAGS(gpio_dev, PIN_USB, GPIO_PIN_OUTPUT); 173 else 174 GPIO_PIN_SETFLAGS(gpio_dev, PIN_USB, GPIO_PIN_INPUT); 175 176 return (0); 177} 178 179static int 180reset_hsic_hub(struct exynos_ehci_softc *esc, phandle_t hub) 181{ 182 device_t gpio_dev; 183 pcell_t pin; 184 185 /* TODO: check that hub is compatible with "smsc,usb3503" */ 186 if (!OF_hasprop(hub, "freebsd,reset-gpio")) { 187 return (1); 188 } 189 190 if (OF_getencprop(hub, "freebsd,reset-gpio", &pin, sizeof(pin)) < 0) { 191 device_printf(esc->dev, 192 "failed to decode reset GPIO pin number for HSIC hub\n"); 193 return (1); 194 } 195 196 /* Get the GPIO device, we need this to give power to USB */ 197 gpio_dev = devclass_get_device(devclass_find("gpio"), 0); 198 if (gpio_dev == NULL) { 199 device_printf(esc->dev, "Cant find gpio device\n"); 200 return (1); 201 } 202 203 GPIO_PIN_SET(gpio_dev, pin, GPIO_PIN_LOW); 204 DELAY(100); 205 GPIO_PIN_SET(gpio_dev, pin, GPIO_PIN_HIGH); 206 207 return (0); 208} 209 210static int 211phy_init(struct exynos_ehci_softc *esc) 212{ 213 int reg; 214 phandle_t hub; 215 216 gpio_ctrl(esc, GPIO_INPUT, 1); 217 218 /* set USB HOST mode */ 219 bus_space_write_4(esc->sysreg_bst, esc->sysreg_bsh, 220 EXYNOS5_SYSREG_USB2_PHY, USB2_MODE_HOST); 221 222 /* Power ON phy */ 223 usb2_phy_power_on(); 224 225 reg = bus_space_read_4(esc->host_bst, esc->host_bsh, 0x0); 226 reg &= ~(HOST_CTRL_CLK_MASK | 227 HOST_CTRL_RESET_PHY | 228 HOST_CTRL_RESET_PHY_ALL | 229 HOST_CTRL_SIDDQ | 230 HOST_CTRL_SUSPEND | 231 HOST_CTRL_SLEEP); 232 233 reg |= (HOST_CTRL_CLK_24MHZ | 234 HOST_CTRL_RESET_LINK); 235 bus_space_write_4(esc->host_bst, esc->host_bsh, 0x0, reg); 236 237 DELAY(10); 238 239 reg = bus_space_read_4(esc->host_bst, esc->host_bsh, 0x0); 240 reg &= ~(HOST_CTRL_RESET_LINK); 241 bus_space_write_4(esc->host_bst, esc->host_bsh, 0x0, reg); 242 243 if ((hub = OF_finddevice("/hsichub")) != 0) { 244 reset_hsic_hub(esc, hub); 245 } 246 247 gpio_ctrl(esc, GPIO_OUTPUT, 1); 248 249 return (0); 250} 251 252static int 253exynos_ehci_attach(device_t dev) 254{ 255 struct exynos_ehci_softc *esc; 256 ehci_softc_t *sc; 257 bus_space_handle_t bsh; 258 int err; 259 260 esc = device_get_softc(dev); 261 esc->dev = dev; 262 sc = &esc->base; 263 sc->sc_bus.parent = dev; 264 sc->sc_bus.devices = sc->sc_devices; 265 sc->sc_bus.devices_max = EHCI_MAX_DEVICES; 266 sc->sc_bus.dma_bits = 32; 267 268 if (bus_alloc_resources(dev, exynos_ehci_spec, esc->res)) { 269 device_printf(dev, "could not allocate resources\n"); 270 return (ENXIO); 271 } 272 273 /* EHCI registers */ 274 sc->sc_io_tag = rman_get_bustag(esc->res[0]); 275 bsh = rman_get_bushandle(esc->res[0]); 276 sc->sc_io_size = rman_get_size(esc->res[0]); 277 278 /* EHCI HOST ctrl registers */ 279 esc->host_bst = rman_get_bustag(esc->res[1]); 280 esc->host_bsh = rman_get_bushandle(esc->res[1]); 281 282 /* SYSREG */ 283 esc->sysreg_bst = rman_get_bustag(esc->res[2]); 284 esc->sysreg_bsh = rman_get_bushandle(esc->res[2]); 285 286 /* get all DMA memory */ 287 if (usb_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(dev), 288 &ehci_iterate_hw_softc)) 289 return (ENXIO); 290 291 /* 292 * Set handle to USB related registers subregion used by 293 * generic EHCI driver. 294 */ 295 err = bus_space_subregion(sc->sc_io_tag, bsh, 0x0, 296 sc->sc_io_size, &sc->sc_io_hdl); 297 if (err != 0) 298 return (ENXIO); 299 300 phy_init(esc); 301 302 /* Setup interrupt handler */ 303 err = bus_setup_intr(dev, esc->res[3], INTR_TYPE_BIO | INTR_MPSAFE, 304 NULL, (driver_intr_t *)ehci_interrupt, sc, 305 &sc->sc_intr_hdl); 306 if (err) { 307 device_printf(dev, "Could not setup irq, " 308 "%d\n", err); 309 return (1); 310 } 311 312 /* Add USB device */ 313 sc->sc_bus.bdev = device_add_child(dev, "usbus", -1); 314 if (!sc->sc_bus.bdev) { 315 device_printf(dev, "Could not add USB device\n"); 316 err = bus_teardown_intr(dev, esc->res[3], 317 sc->sc_intr_hdl); 318 if (err) 319 device_printf(dev, "Could not tear down irq," 320 " %d\n", err); 321 return (1); 322 } 323 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus); 324 325 strlcpy(sc->sc_vendor, "Samsung", sizeof(sc->sc_vendor)); 326 327 err = ehci_init(sc); 328 if (!err) { 329 sc->sc_flags |= EHCI_SCFLG_DONEINIT; 330 err = device_probe_and_attach(sc->sc_bus.bdev); 331 } else { 332 device_printf(dev, "USB init failed err=%d\n", err); 333 334 device_delete_child(dev, sc->sc_bus.bdev); 335 sc->sc_bus.bdev = NULL; 336 337 err = bus_teardown_intr(dev, esc->res[3], 338 sc->sc_intr_hdl); 339 if (err) 340 device_printf(dev, "Could not tear down irq," 341 " %d\n", err); 342 return (1); 343 } 344 return (0); 345} 346 347static int 348exynos_ehci_detach(device_t dev) 349{ 350 struct exynos_ehci_softc *esc; 351 ehci_softc_t *sc; 352 int err; 353 354 esc = device_get_softc(dev); 355 sc = &esc->base; 356 357 if (sc->sc_flags & EHCI_SCFLG_DONEINIT) 358 return (0); 359 360 /* 361 * only call ehci_detach() after ehci_init() 362 */ 363 if (sc->sc_flags & EHCI_SCFLG_DONEINIT) { 364 ehci_detach(sc); 365 sc->sc_flags &= ~EHCI_SCFLG_DONEINIT; 366 } 367 368 /* 369 * Disable interrupts that might have been switched on in 370 * ehci_init. 371 */ 372 if (sc->sc_io_tag && sc->sc_io_hdl) 373 bus_space_write_4(sc->sc_io_tag, sc->sc_io_hdl, 374 EHCI_USBINTR, 0); 375 376 if (esc->res[3] && sc->sc_intr_hdl) { 377 err = bus_teardown_intr(dev, esc->res[3], 378 sc->sc_intr_hdl); 379 if (err) { 380 device_printf(dev, "Could not tear down irq," 381 " %d\n", err); 382 return (err); 383 } 384 sc->sc_intr_hdl = NULL; 385 } 386 387 if (sc->sc_bus.bdev) { 388 device_delete_child(dev, sc->sc_bus.bdev); 389 sc->sc_bus.bdev = NULL; 390 } 391 392 /* During module unload there are lots of children leftover */ 393 device_delete_children(dev); 394 395 bus_release_resources(dev, exynos_ehci_spec, esc->res); 396 397 return (0); 398} 399