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 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
37 #include <sys/condvar.h>
41 #include <dev/ofw/ofw_bus.h>
42 #include <dev/ofw/ofw_bus_subr.h>
44 #include <dev/usb/usb.h>
45 #include <dev/usb/usbdi.h>
46 #include <dev/usb/usb_busdma.h>
47 #include <dev/usb/usb_process.h>
48 #include <dev/usb/usb_controller.h>
49 #include <dev/usb/usb_bus.h>
50 #include <dev/usb/controller/ehci.h>
51 #include <dev/usb/controller/ehcireg.h>
53 #include <machine/bus.h>
54 #include <machine/resource.h>
56 #include <arm/samsung/exynos/exynos5_common.h>
57 #include <arm/samsung/exynos/exynos5_pmu.h>
61 #include "opt_platform.h"
69 #define EXYNOS5_SYSREG_USB2_PHY 0x0
70 #define USB2_MODE_HOST 0x1
73 #define HOST_CTRL_CLK_24MHZ (5 << 16)
74 #define HOST_CTRL_CLK_MASK (7 << 16)
75 #define HOST_CTRL_SIDDQ (1 << 6)
76 #define HOST_CTRL_SLEEP (1 << 5)
77 #define HOST_CTRL_SUSPEND (1 << 4)
78 #define HOST_CTRL_RESET_LINK (1 << 1)
79 #define HOST_CTRL_RESET_PHY (1 << 0)
80 #define HOST_CTRL_RESET_PHY_ALL (1U << 31)
82 /* Forward declarations */
83 static int exynos_ehci_attach(device_t dev);
84 static int exynos_ehci_detach(device_t dev);
85 static int exynos_ehci_probe(device_t dev);
87 struct exynos_ehci_softc {
90 struct resource *res[4];
91 bus_space_tag_t host_bst;
92 bus_space_tag_t sysreg_bst;
93 bus_space_handle_t host_bsh;
94 bus_space_handle_t sysreg_bsh;
98 static struct resource_spec exynos_ehci_spec[] = {
99 { SYS_RES_MEMORY, 0, RF_ACTIVE },
100 { SYS_RES_MEMORY, 1, RF_ACTIVE },
101 { SYS_RES_MEMORY, 2, RF_ACTIVE },
102 { SYS_RES_IRQ, 0, RF_ACTIVE },
106 static device_method_t ehci_methods[] = {
107 /* Device interface */
108 DEVMETHOD(device_probe, exynos_ehci_probe),
109 DEVMETHOD(device_attach, exynos_ehci_attach),
110 DEVMETHOD(device_detach, exynos_ehci_detach),
111 DEVMETHOD(device_suspend, bus_generic_suspend),
112 DEVMETHOD(device_resume, bus_generic_resume),
113 DEVMETHOD(device_shutdown, bus_generic_shutdown),
116 DEVMETHOD(bus_print_child, bus_generic_print_child),
121 /* kobj_class definition */
122 static driver_t ehci_driver = {
128 static devclass_t ehci_devclass;
130 DRIVER_MODULE(ehci, simplebus, ehci_driver, ehci_devclass, 0, 0);
131 MODULE_DEPEND(ehci, usb, 1, 1, 1);
137 exynos_ehci_probe(device_t dev)
140 if (!ofw_bus_status_okay(dev))
143 if (ofw_bus_is_compatible(dev, "exynos,usb-ehci") == 0)
146 device_set_desc(dev, "Exynos integrated USB controller");
147 return (BUS_PROBE_DEFAULT);
151 gpio_ctrl(struct exynos_ehci_softc *esc, int dir, int power)
155 /* Get the GPIO device, we need this to give power to USB */
156 gpio_dev = devclass_get_device(devclass_find("gpio"), 0);
157 if (gpio_dev == NULL) {
158 device_printf(esc->dev, "cant find gpio_dev\n");
163 GPIO_PIN_SET(gpio_dev, PIN_USB, GPIO_PIN_HIGH);
165 GPIO_PIN_SET(gpio_dev, PIN_USB, GPIO_PIN_LOW);
168 GPIO_PIN_SETFLAGS(gpio_dev, PIN_USB, GPIO_PIN_OUTPUT);
170 GPIO_PIN_SETFLAGS(gpio_dev, PIN_USB, GPIO_PIN_INPUT);
176 reset_hsic_hub(struct exynos_ehci_softc *esc, phandle_t hub)
181 /* TODO: check that hub is compatible with "smsc,usb3503" */
182 if (!OF_hasprop(hub, "freebsd,reset-gpio")) {
186 if (OF_getencprop(hub, "freebsd,reset-gpio", &pin, sizeof(pin)) < 0) {
187 device_printf(esc->dev,
188 "failed to decode reset GPIO pin number for HSIC hub\n");
192 /* Get the GPIO device, we need this to give power to USB */
193 gpio_dev = devclass_get_device(devclass_find("gpio"), 0);
194 if (gpio_dev == NULL) {
195 device_printf(esc->dev, "Cant find gpio device\n");
199 GPIO_PIN_SET(gpio_dev, pin, GPIO_PIN_LOW);
201 GPIO_PIN_SET(gpio_dev, pin, GPIO_PIN_HIGH);
207 phy_init(struct exynos_ehci_softc *esc)
212 gpio_ctrl(esc, GPIO_INPUT, 1);
214 /* set USB HOST mode */
215 bus_space_write_4(esc->sysreg_bst, esc->sysreg_bsh,
216 EXYNOS5_SYSREG_USB2_PHY, USB2_MODE_HOST);
221 reg = bus_space_read_4(esc->host_bst, esc->host_bsh, 0x0);
222 reg &= ~(HOST_CTRL_CLK_MASK |
223 HOST_CTRL_RESET_PHY |
224 HOST_CTRL_RESET_PHY_ALL |
229 reg |= (HOST_CTRL_CLK_24MHZ |
230 HOST_CTRL_RESET_LINK);
231 bus_space_write_4(esc->host_bst, esc->host_bsh, 0x0, reg);
235 reg = bus_space_read_4(esc->host_bst, esc->host_bsh, 0x0);
236 reg &= ~(HOST_CTRL_RESET_LINK);
237 bus_space_write_4(esc->host_bst, esc->host_bsh, 0x0, reg);
239 if ((hub = OF_finddevice("/hsichub")) != 0) {
240 reset_hsic_hub(esc, hub);
243 gpio_ctrl(esc, GPIO_OUTPUT, 1);
249 exynos_ehci_attach(device_t dev)
251 struct exynos_ehci_softc *esc;
253 bus_space_handle_t bsh;
256 esc = device_get_softc(dev);
259 sc->sc_bus.parent = dev;
260 sc->sc_bus.devices = sc->sc_devices;
261 sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
262 sc->sc_bus.dma_bits = 32;
264 if (bus_alloc_resources(dev, exynos_ehci_spec, esc->res)) {
265 device_printf(dev, "could not allocate resources\n");
270 sc->sc_io_tag = rman_get_bustag(esc->res[0]);
271 bsh = rman_get_bushandle(esc->res[0]);
272 sc->sc_io_size = rman_get_size(esc->res[0]);
274 /* EHCI HOST ctrl registers */
275 esc->host_bst = rman_get_bustag(esc->res[1]);
276 esc->host_bsh = rman_get_bushandle(esc->res[1]);
279 esc->sysreg_bst = rman_get_bustag(esc->res[2]);
280 esc->sysreg_bsh = rman_get_bushandle(esc->res[2]);
282 /* get all DMA memory */
283 if (usb_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(dev),
284 &ehci_iterate_hw_softc))
288 * Set handle to USB related registers subregion used by
289 * generic EHCI driver.
291 err = bus_space_subregion(sc->sc_io_tag, bsh, 0x0,
292 sc->sc_io_size, &sc->sc_io_hdl);
298 /* Setup interrupt handler */
299 err = bus_setup_intr(dev, esc->res[3], INTR_TYPE_BIO | INTR_MPSAFE,
300 NULL, (driver_intr_t *)ehci_interrupt, sc,
303 device_printf(dev, "Could not setup irq, "
309 sc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
310 if (!sc->sc_bus.bdev) {
311 device_printf(dev, "Could not add USB device\n");
312 err = bus_teardown_intr(dev, esc->res[3],
315 device_printf(dev, "Could not tear down irq,"
319 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
321 strlcpy(sc->sc_vendor, "Samsung", sizeof(sc->sc_vendor));
325 sc->sc_flags |= EHCI_SCFLG_DONEINIT;
326 err = device_probe_and_attach(sc->sc_bus.bdev);
328 device_printf(dev, "USB init failed err=%d\n", err);
330 device_delete_child(dev, sc->sc_bus.bdev);
331 sc->sc_bus.bdev = NULL;
333 err = bus_teardown_intr(dev, esc->res[3],
336 device_printf(dev, "Could not tear down irq,"
344 exynos_ehci_detach(device_t dev)
346 struct exynos_ehci_softc *esc;
350 esc = device_get_softc(dev);
353 if (sc->sc_flags & EHCI_SCFLG_DONEINIT)
357 * only call ehci_detach() after ehci_init()
359 if (sc->sc_flags & EHCI_SCFLG_DONEINIT) {
361 sc->sc_flags &= ~EHCI_SCFLG_DONEINIT;
365 * Disable interrupts that might have been switched on in
368 if (sc->sc_io_tag && sc->sc_io_hdl)
369 bus_space_write_4(sc->sc_io_tag, sc->sc_io_hdl,
372 if (esc->res[3] && sc->sc_intr_hdl) {
373 err = bus_teardown_intr(dev, esc->res[3],
376 device_printf(dev, "Could not tear down irq,"
380 sc->sc_intr_hdl = NULL;
383 if (sc->sc_bus.bdev) {
384 device_delete_child(dev, sc->sc_bus.bdev);
385 sc->sc_bus.bdev = NULL;
388 /* During module unload there are lots of children leftover */
389 device_delete_children(dev);
391 bus_release_resources(dev, exynos_ehci_spec, esc->res);