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),
124 /* kobj_class definition */
125 static driver_t ehci_driver = {
128 sizeof(struct exynos_ehci_softc)
131 static devclass_t ehci_devclass;
133 DRIVER_MODULE(exynos_ehci, simplebus, ehci_driver, ehci_devclass, 0, 0);
134 MODULE_DEPEND(exynos_ehci, usb, 1, 1, 1);
140 exynos_ehci_probe(device_t dev)
143 if (!ofw_bus_status_okay(dev))
146 if (ofw_bus_is_compatible(dev, "exynos,usb-ehci") == 0)
149 device_set_desc(dev, "Exynos integrated USB controller");
150 return (BUS_PROBE_DEFAULT);
154 gpio_ctrl(struct exynos_ehci_softc *esc, int dir, int power)
158 /* Get the GPIO device, we need this to give power to USB */
159 gpio_dev = devclass_get_device(devclass_find("gpio"), 0);
160 if (gpio_dev == NULL) {
161 device_printf(esc->dev, "cant find gpio_dev\n");
166 GPIO_PIN_SET(gpio_dev, PIN_USB, GPIO_PIN_HIGH);
168 GPIO_PIN_SET(gpio_dev, PIN_USB, GPIO_PIN_LOW);
171 GPIO_PIN_SETFLAGS(gpio_dev, PIN_USB, GPIO_PIN_OUTPUT);
173 GPIO_PIN_SETFLAGS(gpio_dev, PIN_USB, GPIO_PIN_INPUT);
179 reset_hsic_hub(struct exynos_ehci_softc *esc, phandle_t hub)
184 /* TODO: check that hub is compatible with "smsc,usb3503" */
185 if (!OF_hasprop(hub, "freebsd,reset-gpio")) {
189 if (OF_getencprop(hub, "freebsd,reset-gpio", &pin, sizeof(pin)) < 0) {
190 device_printf(esc->dev,
191 "failed to decode reset GPIO pin number for HSIC hub\n");
195 /* Get the GPIO device, we need this to give power to USB */
196 gpio_dev = devclass_get_device(devclass_find("gpio"), 0);
197 if (gpio_dev == NULL) {
198 device_printf(esc->dev, "Cant find gpio device\n");
202 GPIO_PIN_SET(gpio_dev, pin, GPIO_PIN_LOW);
204 GPIO_PIN_SET(gpio_dev, pin, GPIO_PIN_HIGH);
210 phy_init(struct exynos_ehci_softc *esc)
215 gpio_ctrl(esc, GPIO_INPUT, 1);
217 /* set USB HOST mode */
218 bus_space_write_4(esc->sysreg_bst, esc->sysreg_bsh,
219 EXYNOS5_SYSREG_USB2_PHY, USB2_MODE_HOST);
224 reg = bus_space_read_4(esc->host_bst, esc->host_bsh, 0x0);
225 reg &= ~(HOST_CTRL_CLK_MASK |
226 HOST_CTRL_RESET_PHY |
227 HOST_CTRL_RESET_PHY_ALL |
232 reg |= (HOST_CTRL_CLK_24MHZ |
233 HOST_CTRL_RESET_LINK);
234 bus_space_write_4(esc->host_bst, esc->host_bsh, 0x0, reg);
238 reg = bus_space_read_4(esc->host_bst, esc->host_bsh, 0x0);
239 reg &= ~(HOST_CTRL_RESET_LINK);
240 bus_space_write_4(esc->host_bst, esc->host_bsh, 0x0, reg);
242 if ((hub = OF_finddevice("/hsichub")) != -1) {
243 reset_hsic_hub(esc, hub);
246 gpio_ctrl(esc, GPIO_OUTPUT, 1);
252 exynos_ehci_attach(device_t dev)
254 struct exynos_ehci_softc *esc;
256 bus_space_handle_t bsh;
259 esc = device_get_softc(dev);
262 sc->sc_bus.parent = dev;
263 sc->sc_bus.devices = sc->sc_devices;
264 sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
265 sc->sc_bus.dma_bits = 32;
267 if (bus_alloc_resources(dev, exynos_ehci_spec, esc->res)) {
268 device_printf(dev, "could not allocate resources\n");
273 sc->sc_io_tag = rman_get_bustag(esc->res[0]);
274 bsh = rman_get_bushandle(esc->res[0]);
275 sc->sc_io_size = rman_get_size(esc->res[0]);
277 /* EHCI HOST ctrl registers */
278 esc->host_bst = rman_get_bustag(esc->res[1]);
279 esc->host_bsh = rman_get_bushandle(esc->res[1]);
282 esc->sysreg_bst = rman_get_bustag(esc->res[2]);
283 esc->sysreg_bsh = rman_get_bushandle(esc->res[2]);
285 /* get all DMA memory */
286 if (usb_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(dev),
287 &ehci_iterate_hw_softc))
291 * Set handle to USB related registers subregion used by
292 * generic EHCI driver.
294 err = bus_space_subregion(sc->sc_io_tag, bsh, 0x0,
295 sc->sc_io_size, &sc->sc_io_hdl);
301 /* Setup interrupt handler */
302 err = bus_setup_intr(dev, esc->res[3], INTR_TYPE_BIO | INTR_MPSAFE,
303 NULL, (driver_intr_t *)ehci_interrupt, sc,
306 device_printf(dev, "Could not setup irq, "
312 sc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
313 if (!sc->sc_bus.bdev) {
314 device_printf(dev, "Could not add USB device\n");
315 err = bus_teardown_intr(dev, esc->res[3],
318 device_printf(dev, "Could not tear down irq,"
322 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
324 strlcpy(sc->sc_vendor, "Samsung", sizeof(sc->sc_vendor));
328 sc->sc_flags |= EHCI_SCFLG_DONEINIT;
329 err = device_probe_and_attach(sc->sc_bus.bdev);
331 device_printf(dev, "USB init failed err=%d\n", err);
333 device_delete_child(dev, sc->sc_bus.bdev);
334 sc->sc_bus.bdev = NULL;
336 err = bus_teardown_intr(dev, esc->res[3],
339 device_printf(dev, "Could not tear down irq,"
347 exynos_ehci_detach(device_t dev)
349 struct exynos_ehci_softc *esc;
353 esc = device_get_softc(dev);
356 if (sc->sc_flags & EHCI_SCFLG_DONEINIT)
360 * only call ehci_detach() after ehci_init()
362 if (sc->sc_flags & EHCI_SCFLG_DONEINIT) {
364 sc->sc_flags &= ~EHCI_SCFLG_DONEINIT;
368 * Disable interrupts that might have been switched on in
371 if (sc->sc_io_tag && sc->sc_io_hdl)
372 bus_space_write_4(sc->sc_io_tag, sc->sc_io_hdl,
375 if (esc->res[3] && sc->sc_intr_hdl) {
376 err = bus_teardown_intr(dev, esc->res[3],
379 device_printf(dev, "Could not tear down irq,"
383 sc->sc_intr_hdl = NULL;
386 if (sc->sc_bus.bdev) {
387 device_delete_child(dev, sc->sc_bus.bdev);
388 sc->sc_bus.bdev = NULL;
391 /* During module unload there are lots of children leftover */
392 device_delete_children(dev);
394 bus_release_resources(dev, exynos_ehci_spec, esc->res);