2 * Copyright (c) 2013-2014 Ruslan Bukin <br@bsdpad.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #ifdef USB_GLOBAL_INCLUDE_FILE
28 #include USB_GLOBAL_INCLUDE_FILE
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
40 #include <sys/condvar.h>
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
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>
56 #include <machine/bus.h>
57 #include <machine/resource.h>
59 #include <arm/samsung/exynos/exynos5_common.h>
60 #include <arm/samsung/exynos/exynos5_pmu.h>
64 #include "opt_platform.h"
73 #define EXYNOS5_SYSREG_USB2_PHY 0x0
74 #define USB2_MODE_HOST 0x1
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)
86 /* Forward declarations */
87 static int exynos_ehci_attach(device_t dev);
88 static int exynos_ehci_detach(device_t dev);
89 static int exynos_ehci_probe(device_t dev);
91 struct exynos_ehci_softc {
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;
102 static 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 },
110 static 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),
120 DEVMETHOD(bus_print_child, bus_generic_print_child),
125 /* kobj_class definition */
126 static driver_t ehci_driver = {
129 sizeof(struct exynos_ehci_softc)
132 static devclass_t ehci_devclass;
134 DRIVER_MODULE(exynos_ehci, simplebus, ehci_driver, ehci_devclass, 0, 0);
135 MODULE_DEPEND(exynos_ehci, usb, 1, 1, 1);
141 exynos_ehci_probe(device_t dev)
144 if (!ofw_bus_status_okay(dev))
147 if (ofw_bus_is_compatible(dev, "exynos,usb-ehci") == 0)
150 device_set_desc(dev, "Exynos integrated USB controller");
151 return (BUS_PROBE_DEFAULT);
155 gpio_ctrl(struct exynos_ehci_softc *esc, int dir, int power)
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");
167 GPIO_PIN_SET(gpio_dev, PIN_USB, GPIO_PIN_HIGH);
169 GPIO_PIN_SET(gpio_dev, PIN_USB, GPIO_PIN_LOW);
172 GPIO_PIN_SETFLAGS(gpio_dev, PIN_USB, GPIO_PIN_OUTPUT);
174 GPIO_PIN_SETFLAGS(gpio_dev, PIN_USB, GPIO_PIN_INPUT);
180 reset_hsic_hub(struct exynos_ehci_softc *esc, phandle_t hub)
185 /* TODO: check that hub is compatible with "smsc,usb3503" */
186 if (!OF_hasprop(hub, "freebsd,reset-gpio")) {
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");
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");
203 GPIO_PIN_SET(gpio_dev, pin, GPIO_PIN_LOW);
205 GPIO_PIN_SET(gpio_dev, pin, GPIO_PIN_HIGH);
211 phy_init(struct exynos_ehci_softc *esc)
216 gpio_ctrl(esc, GPIO_INPUT, 1);
218 /* set USB HOST mode */
219 bus_space_write_4(esc->sysreg_bst, esc->sysreg_bsh,
220 EXYNOS5_SYSREG_USB2_PHY, USB2_MODE_HOST);
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 |
233 reg |= (HOST_CTRL_CLK_24MHZ |
234 HOST_CTRL_RESET_LINK);
235 bus_space_write_4(esc->host_bst, esc->host_bsh, 0x0, reg);
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);
243 if ((hub = OF_finddevice("/hsichub")) != -1) {
244 reset_hsic_hub(esc, hub);
247 gpio_ctrl(esc, GPIO_OUTPUT, 1);
253 exynos_ehci_attach(device_t dev)
255 struct exynos_ehci_softc *esc;
257 bus_space_handle_t bsh;
260 esc = device_get_softc(dev);
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;
268 if (bus_alloc_resources(dev, exynos_ehci_spec, esc->res)) {
269 device_printf(dev, "could not allocate resources\n");
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]);
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]);
283 esc->sysreg_bst = rman_get_bustag(esc->res[2]);
284 esc->sysreg_bsh = rman_get_bushandle(esc->res[2]);
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))
292 * Set handle to USB related registers subregion used by
293 * generic EHCI driver.
295 err = bus_space_subregion(sc->sc_io_tag, bsh, 0x0,
296 sc->sc_io_size, &sc->sc_io_hdl);
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,
307 device_printf(dev, "Could not setup irq, "
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],
319 device_printf(dev, "Could not tear down irq,"
323 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
325 strlcpy(sc->sc_vendor, "Samsung", sizeof(sc->sc_vendor));
329 sc->sc_flags |= EHCI_SCFLG_DONEINIT;
330 err = device_probe_and_attach(sc->sc_bus.bdev);
332 device_printf(dev, "USB init failed err=%d\n", err);
334 device_delete_child(dev, sc->sc_bus.bdev);
335 sc->sc_bus.bdev = NULL;
337 err = bus_teardown_intr(dev, esc->res[3],
340 device_printf(dev, "Could not tear down irq,"
348 exynos_ehci_detach(device_t dev)
350 struct exynos_ehci_softc *esc;
354 esc = device_get_softc(dev);
357 if (sc->sc_flags & EHCI_SCFLG_DONEINIT)
361 * only call ehci_detach() after ehci_init()
363 if (sc->sc_flags & EHCI_SCFLG_DONEINIT) {
365 sc->sc_flags &= ~EHCI_SCFLG_DONEINIT;
369 * Disable interrupts that might have been switched on in
372 if (sc->sc_io_tag && sc->sc_io_hdl)
373 bus_space_write_4(sc->sc_io_tag, sc->sc_io_hdl,
376 if (esc->res[3] && sc->sc_intr_hdl) {
377 err = bus_teardown_intr(dev, esc->res[3],
380 device_printf(dev, "Could not tear down irq,"
384 sc->sc_intr_hdl = NULL;
387 if (sc->sc_bus.bdev) {
388 device_delete_child(dev, sc->sc_bus.bdev);
389 sc->sc_bus.bdev = NULL;
392 /* During module unload there are lots of children leftover */
393 device_delete_children(dev);
395 bus_release_resources(dev, exynos_ehci_spec, esc->res);