2 * Copyright (c) 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>
40 #include <dev/ofw/ofw_bus.h>
41 #include <dev/ofw/ofw_bus_subr.h>
43 #include <dev/usb/usb.h>
44 #include <dev/usb/usbdi.h>
45 #include <dev/usb/usb_busdma.h>
46 #include <dev/usb/usb_process.h>
47 #include <dev/usb/usb_controller.h>
48 #include <dev/usb/usb_bus.h>
49 #include <dev/usb/controller/xhci.h>
50 #include <dev/usb/controller/xhcireg.h>
52 #include <machine/bus.h>
53 #include <machine/resource.h>
55 #include <arm/samsung/exynos/exynos5_common.h>
57 #include "opt_platform.h"
60 #define GSNPSID_MASK 0xffff0000
61 #define REVISION_MASK 0xffff
63 #define GCTL_PWRDNSCALE(n) ((n) << 19)
64 #define GCTL_U2RSTECN (1 << 16)
65 #define GCTL_CLK_BUS (0)
66 #define GCTL_CLK_PIPE (1)
67 #define GCTL_CLK_PIPEHALF (2)
68 #define GCTL_CLK_M (3)
69 #define GCTL_CLK_S (6)
70 #define GCTL_PRTCAP(n) (((n) & (3 << 12)) >> 12)
71 #define GCTL_PRTCAPDIR(n) ((n) << 12)
72 #define GCTL_PRTCAP_HOST 1
73 #define GCTL_PRTCAP_DEVICE 2
74 #define GCTL_PRTCAP_OTG 3
75 #define GCTL_CORESOFTRESET (1 << 11)
76 #define GCTL_SCALEDOWN_MASK 3
77 #define GCTL_SCALEDOWN_SHIFT 4
78 #define GCTL_DISSCRAMBLE (1 << 3)
79 #define GCTL_DSBLCLKGTNG (1 << 0)
80 #define GHWPARAMS1 0x3c
81 #define GHWPARAMS1_EN_PWROPT(n) (((n) & (3 << 24)) >> 24)
82 #define GHWPARAMS1_EN_PWROPT_NO 0
83 #define GHWPARAMS1_EN_PWROPT_CLK 1
84 #define GUSB2PHYCFG(n) (0x100 + (n * 0x04))
85 #define GUSB2PHYCFG_PHYSOFTRST (1 << 31)
86 #define GUSB2PHYCFG_SUSPHY (1 << 6)
87 #define GUSB3PIPECTL(n) (0x1c0 + (n * 0x04))
88 #define GUSB3PIPECTL_PHYSOFTRST (1 << 31)
89 #define GUSB3PIPECTL_SUSPHY (1 << 17)
91 /* Forward declarations */
92 static device_attach_t exynos_xhci_attach;
93 static device_detach_t exynos_xhci_detach;
94 static device_probe_t exynos_xhci_probe;
96 struct exynos_xhci_softc {
98 struct xhci_softc base;
99 struct resource *res[3];
101 bus_space_handle_t bsh;
104 static struct resource_spec exynos_xhci_spec[] = {
105 { SYS_RES_MEMORY, 0, RF_ACTIVE },
106 { SYS_RES_MEMORY, 1, RF_ACTIVE },
107 { SYS_RES_IRQ, 0, RF_ACTIVE },
111 static device_method_t xhci_methods[] = {
112 /* Device interface */
113 DEVMETHOD(device_probe, exynos_xhci_probe),
114 DEVMETHOD(device_attach, exynos_xhci_attach),
115 DEVMETHOD(device_detach, exynos_xhci_detach),
116 DEVMETHOD(device_suspend, bus_generic_suspend),
117 DEVMETHOD(device_resume, bus_generic_resume),
118 DEVMETHOD(device_shutdown, bus_generic_shutdown),
123 /* kobj_class definition */
124 static driver_t xhci_driver = {
127 sizeof(struct xhci_softc)
130 static devclass_t xhci_devclass;
132 DRIVER_MODULE(xhci, simplebus, xhci_driver, xhci_devclass, 0, 0);
133 MODULE_DEPEND(xhci, usb, 1, 1, 1);
139 exynos_xhci_probe(device_t dev)
142 if (!ofw_bus_status_okay(dev))
145 if (ofw_bus_is_compatible(dev, "samsung,exynos5250-dwusb3") == 0)
148 device_set_desc(dev, "Exynos USB 3.0 controller");
149 return (BUS_PROBE_DEFAULT);
153 dwc3_init(struct exynos_xhci_softc *esc)
159 rev = READ4(esc, GSNPSID);
160 if ((rev & GSNPSID_MASK) != 0x55330000) {
161 printf("It is not DWC3 controller\n");
165 /* Reset controller */
166 WRITE4(esc, GCTL, GCTL_CORESOFTRESET);
167 WRITE4(esc, GUSB3PIPECTL(0), GUSB3PIPECTL_PHYSOFTRST);
168 WRITE4(esc, GUSB2PHYCFG(0), GUSB2PHYCFG_PHYSOFTRST);
172 reg = READ4(esc, GUSB3PIPECTL(0));
173 reg &= ~(GUSB3PIPECTL_PHYSOFTRST);
174 WRITE4(esc, GUSB3PIPECTL(0), reg);
176 reg = READ4(esc, GUSB2PHYCFG(0));
177 reg &= ~(GUSB2PHYCFG_PHYSOFTRST);
178 WRITE4(esc, GUSB2PHYCFG(0), reg);
180 reg = READ4(esc, GCTL);
181 reg &= ~GCTL_CORESOFTRESET;
182 WRITE4(esc, GCTL, reg);
184 hwparams1 = READ4(esc, GHWPARAMS1);
186 reg = READ4(esc, GCTL);
187 reg &= ~(GCTL_SCALEDOWN_MASK << GCTL_SCALEDOWN_SHIFT);
188 reg &= ~(GCTL_DISSCRAMBLE);
190 if (GHWPARAMS1_EN_PWROPT(hwparams1) == \
191 GHWPARAMS1_EN_PWROPT_CLK)
192 reg &= ~(GCTL_DSBLCLKGTNG);
194 if ((rev & REVISION_MASK) < 0x190a)
195 reg |= (GCTL_U2RSTECN);
196 WRITE4(esc, GCTL, reg);
199 reg = READ4(esc, GCTL);
200 reg &= ~(GCTL_PRTCAPDIR(GCTL_PRTCAP_OTG));
201 reg |= GCTL_PRTCAPDIR(GCTL_PRTCAP_HOST);
202 WRITE4(esc, GCTL, reg);
208 exynos_xhci_attach(device_t dev)
210 struct exynos_xhci_softc *esc = device_get_softc(dev);
211 bus_space_handle_t bsh;
216 if (bus_alloc_resources(dev, exynos_xhci_spec, esc->res)) {
217 device_printf(dev, "could not allocate resources\n");
222 esc->base.sc_io_tag = rman_get_bustag(esc->res[0]);
223 bsh = rman_get_bushandle(esc->res[0]);
224 esc->base.sc_io_size = rman_get_size(esc->res[0]);
226 /* DWC3 ctrl registers */
227 esc->bst = rman_get_bustag(esc->res[1]);
228 esc->bsh = rman_get_bushandle(esc->res[1]);
231 * Set handle to USB related registers subregion used by
232 * generic XHCI driver.
234 err = bus_space_subregion(esc->base.sc_io_tag, bsh, 0x0,
235 esc->base.sc_io_size, &esc->base.sc_io_hdl);
237 device_printf(dev, "Subregion failed\n");
238 bus_release_resources(dev, exynos_xhci_spec, esc->res);
242 if (xhci_init(&esc->base, dev, 0)) {
243 device_printf(dev, "Could not initialize softc\n");
244 bus_release_resources(dev, exynos_xhci_spec, esc->res);
248 /* Setup interrupt handler */
249 err = bus_setup_intr(dev, esc->res[2], INTR_TYPE_BIO | INTR_MPSAFE,
250 NULL, (driver_intr_t *)xhci_interrupt, &esc->base,
251 &esc->base.sc_intr_hdl);
253 device_printf(dev, "Could not setup irq, %d\n", err);
254 esc->base.sc_intr_hdl = NULL;
259 esc->base.sc_bus.bdev = device_add_child(dev, "usbus", -1);
260 if (esc->base.sc_bus.bdev == NULL) {
261 device_printf(dev, "Could not add USB device\n");
264 device_set_ivars(esc->base.sc_bus.bdev, &esc->base.sc_bus);
265 strlcpy(esc->base.sc_vendor, "Samsung", sizeof(esc->base.sc_vendor));
269 err = xhci_halt_controller(&esc->base);
271 device_printf(dev, "Starting controller\n");
272 err = xhci_start_controller(&esc->base);
275 device_printf(dev, "Controller started\n");
276 err = device_probe_and_attach(esc->base.sc_bus.bdev);
283 exynos_xhci_detach(dev);
288 exynos_xhci_detach(device_t dev)
290 struct exynos_xhci_softc *esc = device_get_softc(dev);
293 /* During module unload there are lots of children leftover */
294 device_delete_children(dev);
296 xhci_halt_controller(&esc->base);
298 if (esc->res[2] && esc->base.sc_intr_hdl) {
299 err = bus_teardown_intr(dev, esc->res[2],
300 esc->base.sc_intr_hdl);
302 device_printf(dev, "Could not tear down IRQ,"
308 bus_release_resources(dev, exynos_xhci_spec, esc->res);
310 xhci_uninit(&esc->base);