2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2019 Emmanuel Vadot <manu@FreeBSD.Org>
5 * Copyright (c) 2021-2022 Bjoern A. Zeeb <bz@FreeBSD.ORG>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 #include "opt_platform.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
37 #include <sys/condvar.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
45 #include <machine/bus.h>
47 #include <dev/usb/usb.h>
48 #include <dev/usb/usbdi.h>
50 #include <dev/usb/usb_core.h>
51 #include <dev/usb/usb_busdma.h>
52 #include <dev/usb/usb_process.h>
54 #include <dev/usb/usb_controller.h>
55 #include <dev/usb/usb_bus.h>
56 #include <dev/usb/controller/xhci.h>
57 #include <dev/usb/controller/dwc3.h>
60 #include <dev/fdt/simplebus.h>
62 #include <dev/fdt/fdt_common.h>
63 #include <dev/ofw/ofw_bus.h>
64 #include <dev/ofw/ofw_bus_subr.h>
65 #include <dev/ofw/ofw_subr.h>
67 #include <dev/extres/clk/clk.h>
68 #include <dev/extres/phy/phy_usb.h>
72 #include <contrib/dev/acpica/include/acpi.h>
73 #include <contrib/dev/acpica/include/accommon.h>
74 #include <dev/acpica/acpivar.h>
77 #include "generic_xhci.h"
79 struct snps_dwc3_softc {
82 struct resource * mem_res;
84 bus_space_handle_t bsh;
87 uint32_t snpsrevision;
88 uint32_t snpsversion_type;
96 #define DWC3_WRITE(_sc, _off, _val) \
97 bus_space_write_4(_sc->bst, _sc->bsh, _off, _val)
98 #define DWC3_READ(_sc, _off) \
99 bus_space_read_4(_sc->bst, _sc->bsh, _off)
104 xhci_interrupt_poll(void *_sc)
106 struct xhci_softc *sc = _sc;
108 USB_BUS_UNLOCK(&sc->sc_bus);
110 USB_BUS_LOCK(&sc->sc_bus);
111 usb_callout_reset(&sc->sc_callout, 1, (void *)&xhci_interrupt_poll, sc);
115 snps_dwc3_attach_xhci(device_t dev)
117 struct snps_dwc3_softc *snps_sc = device_get_softc(dev);
118 struct xhci_softc *sc = &snps_sc->sc;
119 int err = 0, rid = 0;
121 sc->sc_io_res = snps_sc->mem_res;
122 sc->sc_io_tag = snps_sc->bst;
123 sc->sc_io_hdl = snps_sc->bsh;
124 sc->sc_io_size = rman_get_size(snps_sc->mem_res);
126 sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
127 RF_SHAREABLE | RF_ACTIVE);
128 if (sc->sc_irq_res == NULL) {
129 device_printf(dev, "Failed to allocate IRQ\n");
133 sc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
134 if (sc->sc_bus.bdev == NULL) {
135 device_printf(dev, "Failed to add USB device\n");
139 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
141 sprintf(sc->sc_vendor, "Synopsys");
142 device_set_desc(sc->sc_bus.bdev, "Synopsys");
144 if (xhci_use_polling() == 0) {
145 err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
146 NULL, (driver_intr_t *)xhci_interrupt, sc, &sc->sc_intr_hdl);
148 device_printf(dev, "Failed to setup IRQ, %d\n", err);
149 sc->sc_intr_hdl = NULL;
154 err = xhci_init(sc, dev, IS_DMA_32B);
156 device_printf(dev, "Failed to init XHCI, with error %d\n", err);
160 usb_callout_init_mtx(&sc->sc_callout, &sc->sc_bus.bus_mtx, 0);
162 if (xhci_use_polling() != 0) {
163 device_printf(dev, "Interrupt polling at %dHz\n", hz);
164 USB_BUS_LOCK(&sc->sc_bus);
165 xhci_interrupt_poll(sc);
166 USB_BUS_UNLOCK(&sc->sc_bus);
169 err = xhci_start_controller(sc);
171 device_printf(dev, "Failed to start XHCI controller, with error %d\n", err);
175 device_printf(sc->sc_bus.bdev, "trying to attach\n");
176 err = device_probe_and_attach(sc->sc_bus.bdev);
178 device_printf(dev, "Failed to initialize USB, with error %d\n", err);
187 snsp_dwc3_dump_regs(struct snps_dwc3_softc *sc, const char *msg)
189 struct xhci_softc *xsc;
195 device_printf(sc->dev, "%s: %s:\n", __func__, msg ? msg : "");
197 reg = DWC3_READ(sc, DWC3_GCTL);
198 device_printf(sc->dev, "GCTL: %#012x\n", reg);
199 reg = DWC3_READ(sc, DWC3_GUCTL);
200 device_printf(sc->dev, "GUCTL: %#012x\n", reg);
201 reg = DWC3_READ(sc, DWC3_GUCTL1);
202 device_printf(sc->dev, "GUCTL1: %#012x\n", reg);
203 reg = DWC3_READ(sc, DWC3_GUSB2PHYCFG0);
204 device_printf(sc->dev, "GUSB2PHYCFG0: %#012x\n", reg);
205 reg = DWC3_READ(sc, DWC3_GUSB3PIPECTL0);
206 device_printf(sc->dev, "GUSB3PIPECTL0: %#012x\n", reg);
207 reg = DWC3_READ(sc, DWC3_DCFG);
208 device_printf(sc->dev, "DCFG: %#012x\n", reg);
211 device_printf(sc->dev, "xhci quirks: %#012x\n", xsc->sc_quirks);
215 snps_dwc3_dump_ctrlparams(struct snps_dwc3_softc *sc)
217 const bus_size_t offs[] = {
218 DWC3_GHWPARAMS0, DWC3_GHWPARAMS1, DWC3_GHWPARAMS2, DWC3_GHWPARAMS3,
219 DWC3_GHWPARAMS4, DWC3_GHWPARAMS5, DWC3_GHWPARAMS6, DWC3_GHWPARAMS7,
225 for (i = 0; i < nitems(offs); i++) {
226 reg = DWC3_READ(sc, offs[i]);
228 device_printf(sc->dev, "hwparams[%d]: %#012x\n", i, reg);
234 snps_dwc3_reset(struct snps_dwc3_softc *sc)
236 uint32_t gctl, ghwp0, phy2, phy3;
238 ghwp0 = DWC3_READ(sc, DWC3_GHWPARAMS0);
240 gctl = DWC3_READ(sc, DWC3_GCTL);
241 gctl |= DWC3_GCTL_CORESOFTRESET;
242 DWC3_WRITE(sc, DWC3_GCTL, gctl);
244 phy2 = DWC3_READ(sc, DWC3_GUSB2PHYCFG0);
245 phy2 |= DWC3_GUSB2PHYCFG0_PHYSOFTRST;
246 if ((ghwp0 & DWC3_GHWPARAMS0_MODE_MASK) ==
247 DWC3_GHWPARAMS0_MODE_DUALROLEDEVICE)
248 phy2 &= ~DWC3_GUSB2PHYCFG0_SUSPENDUSB20;
249 DWC3_WRITE(sc, DWC3_GUSB2PHYCFG0, phy2);
251 phy3 = DWC3_READ(sc, DWC3_GUSB3PIPECTL0);
252 phy3 |= DWC3_GUSB3PIPECTL0_PHYSOFTRST;
253 if ((ghwp0 & DWC3_GHWPARAMS0_MODE_MASK) ==
254 DWC3_GHWPARAMS0_MODE_DUALROLEDEVICE)
255 phy3 &= ~DWC3_GUSB3PIPECTL0_SUSPENDUSB3;
256 DWC3_WRITE(sc, DWC3_GUSB3PIPECTL0, phy3);
260 phy2 &= ~DWC3_GUSB2PHYCFG0_PHYSOFTRST;
261 DWC3_WRITE(sc, DWC3_GUSB2PHYCFG0, phy2);
263 phy3 &= ~DWC3_GUSB3PIPECTL0_PHYSOFTRST;
264 DWC3_WRITE(sc, DWC3_GUSB3PIPECTL0, phy3);
266 gctl &= ~DWC3_GCTL_CORESOFTRESET;
267 DWC3_WRITE(sc, DWC3_GCTL, gctl);
272 snps_dwc3_configure_host(struct snps_dwc3_softc *sc)
276 reg = DWC3_READ(sc, DWC3_GCTL);
277 reg &= ~DWC3_GCTL_PRTCAPDIR_MASK;
278 reg |= DWC3_GCTL_PRTCAPDIR_HOST;
279 DWC3_WRITE(sc, DWC3_GCTL, reg);
282 * Enable the Host IN Auto Retry feature, making the
283 * host respond with a non-terminating retry ACK.
284 * XXX If we ever support more than host mode this needs a dr_mode check.
286 reg = DWC3_READ(sc, DWC3_GUCTL);
287 reg |= DWC3_GUCTL_HOST_AUTO_RETRY;
288 DWC3_WRITE(sc, DWC3_GUCTL, reg);
293 snps_dwc3_configure_phy(struct snps_dwc3_softc *sc, phandle_t node)
300 nphy_types = OF_getprop_alloc(node, "phy_type", (void **)&phy_type);
304 reg = DWC3_READ(sc, DWC3_GUSB2PHYCFG0);
305 if (strncmp(phy_type, "utmi_wide", 9) == 0) {
306 reg &= ~(DWC3_GUSB2PHYCFG0_PHYIF | DWC3_GUSB2PHYCFG0_USBTRDTIM(0xf));
307 reg |= DWC3_GUSB2PHYCFG0_PHYIF |
308 DWC3_GUSB2PHYCFG0_USBTRDTIM(DWC3_GUSB2PHYCFG0_USBTRDTIM_16BITS);
310 reg &= ~(DWC3_GUSB2PHYCFG0_PHYIF | DWC3_GUSB2PHYCFG0_USBTRDTIM(0xf));
311 reg |= DWC3_GUSB2PHYCFG0_PHYIF |
312 DWC3_GUSB2PHYCFG0_USBTRDTIM(DWC3_GUSB2PHYCFG0_USBTRDTIM_8BITS);
314 DWC3_WRITE(sc, DWC3_GUSB2PHYCFG0, reg);
315 OF_prop_free(phy_type);
320 snps_dwc3_do_quirks(struct snps_dwc3_softc *sc)
322 struct xhci_softc *xsc;
325 ghwp0 = DWC3_READ(sc, DWC3_GHWPARAMS0);
326 reg = DWC3_READ(sc, DWC3_GUSB2PHYCFG0);
327 if (device_has_property(sc->dev, "snps,dis-u2-freeclk-exists-quirk"))
328 reg &= ~DWC3_GUSB2PHYCFG0_U2_FREECLK_EXISTS;
330 reg |= DWC3_GUSB2PHYCFG0_U2_FREECLK_EXISTS;
331 if (device_has_property(sc->dev, "snps,dis_u2_susphy_quirk"))
332 reg &= ~DWC3_GUSB2PHYCFG0_SUSPENDUSB20;
333 else if ((ghwp0 & DWC3_GHWPARAMS0_MODE_MASK) ==
334 DWC3_GHWPARAMS0_MODE_DUALROLEDEVICE)
335 reg |= DWC3_GUSB2PHYCFG0_SUSPENDUSB20;
336 if (device_has_property(sc->dev, "snps,dis_enblslpm_quirk"))
337 reg &= ~DWC3_GUSB2PHYCFG0_ENBLSLPM;
339 reg |= DWC3_GUSB2PHYCFG0_ENBLSLPM;
340 DWC3_WRITE(sc, DWC3_GUSB2PHYCFG0, reg);
342 reg = DWC3_READ(sc, DWC3_GUCTL1);
343 if (device_has_property(sc->dev, "snps,dis-tx-ipgap-linecheck-quirk"))
344 reg |= DWC3_GUCTL1_TX_IPGAP_LINECHECK_DIS;
345 DWC3_WRITE(sc, DWC3_GUCTL1, reg);
347 reg = DWC3_READ(sc, DWC3_GUSB3PIPECTL0);
348 if (device_has_property(sc->dev, "snps,dis-del-phy-power-chg-quirk"))
349 reg &= ~DWC3_GUSB3PIPECTL0_DELAYP1TRANS;
350 if (device_has_property(sc->dev, "snps,dis_rxdet_inp3_quirk"))
351 reg |= DWC3_GUSB3PIPECTL0_DISRXDETINP3;
352 if (device_has_property(sc->dev, "snps,dis_u3_susphy_quirk"))
353 reg &= ~DWC3_GUSB3PIPECTL0_SUSPENDUSB3;
354 else if ((ghwp0 & DWC3_GHWPARAMS0_MODE_MASK) ==
355 DWC3_GHWPARAMS0_MODE_DUALROLEDEVICE)
356 reg |= DWC3_GUSB3PIPECTL0_SUSPENDUSB3;
357 DWC3_WRITE(sc, DWC3_GUSB3PIPECTL0, reg);
359 /* Port Disable does not work on <= 3.00a. Disable PORT_PED. */
360 if ((sc->snpsid & 0xffff) <= 0x300a) {
362 xsc->sc_quirks |= XHCI_QUIRK_DISABLE_PORT_PED;
367 snps_dwc3_probe_common(device_t dev)
369 char dr_mode[16] = { 0 };
372 s = device_get_property(dev, "dr_mode", dr_mode, sizeof(dr_mode),
375 device_printf(dev, "Cannot determine dr_mode\n");
378 if (strcmp(dr_mode, "host") != 0) {
380 "Found dr_mode '%s' but only 'host' supported. s=%zd\n",
385 device_set_desc(dev, "Synopsys Designware DWC3");
386 return (BUS_PROBE_DEFAULT);
390 snps_dwc3_common_attach(device_t dev, bool is_fdt)
392 struct snps_dwc3_softc *sc;
395 phy_t usb2_phy, usb3_phy;
400 sc = device_get_softc(dev);
404 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
406 if (sc->mem_res == NULL) {
407 device_printf(dev, "Failed to map memory\n");
410 sc->bst = rman_get_bustag(sc->mem_res);
411 sc->bsh = rman_get_bushandle(sc->mem_res);
413 sc->snpsid = DWC3_READ(sc, DWC3_GSNPSID);
414 sc->snpsversion = DWC3_VERSION(sc->snpsid);
415 sc->snpsrevision = DWC3_REVISION(sc->snpsid);
416 if (sc->snpsversion == DWC3_1_IP_ID ||
417 sc->snpsversion == DWC3_2_IP_ID) {
418 sc->snpsrevision = DWC3_READ(sc, DWC3_1_VER_NUMBER);
419 sc->snpsversion_type = DWC3_READ(sc, DWC3_1_VER_TYPE);
422 switch (sc->snpsversion) {
424 device_printf(sc->dev, "SNPS Version: DWC3 (%x %x)\n",
425 sc->snpsversion, sc->snpsrevision);
428 device_printf(sc->dev, "SNPS Version: DWC3.1 (%x %x %x)\n",
429 sc->snpsversion, sc->snpsrevision,
430 sc->snpsversion_type);
433 device_printf(sc->dev, "SNPS Version: DWC3.2 (%x %x %x)\n",
434 sc->snpsversion, sc->snpsrevision,
435 sc->snpsversion_type);
440 snps_dwc3_dump_ctrlparams(sc);
447 node = ofw_bus_get_node(dev);
449 /* Get the clocks if any */
450 if (ofw_bus_is_compatible(dev, "rockchip,rk3328-dwc3") == 1 ||
451 ofw_bus_is_compatible(dev, "rockchip,rk3568-dwc3") == 1) {
452 if (clk_get_by_ofw_name(dev, node, "ref_clk", &sc->clk_ref) != 0)
453 device_printf(dev, "Cannot get ref_clk\n");
454 if (clk_get_by_ofw_name(dev, node, "suspend_clk", &sc->clk_suspend) != 0)
455 device_printf(dev, "Cannot get suspend_clk\n");
456 if (clk_get_by_ofw_name(dev, node, "bus_clk", &sc->clk_bus) != 0)
457 device_printf(dev, "Cannot get bus_clk\n");
460 if (sc->clk_ref != NULL) {
461 if (clk_enable(sc->clk_ref) != 0)
462 device_printf(dev, "Cannot enable ref_clk\n");
464 if (sc->clk_suspend != NULL) {
465 if (clk_enable(sc->clk_suspend) != 0)
466 device_printf(dev, "Cannot enable suspend_clk\n");
468 if (sc->clk_bus != NULL) {
469 if (clk_enable(sc->clk_bus) != 0)
470 device_printf(dev, "Cannot enable bus_clk\n");
474 usb2_phy = usb3_phy = NULL;
475 error = phy_get_by_ofw_name(dev, node, "usb2-phy", &usb2_phy);
476 if (error == 0 && usb2_phy != NULL)
477 phy_enable(usb2_phy);
478 error = phy_get_by_ofw_name(dev, node, "usb3-phy", &usb3_phy);
479 if (error == 0 && usb3_phy != NULL)
480 phy_enable(usb3_phy);
481 if (sc->snpsversion == DWC3_IP_ID) {
482 if (sc->snpsrevision >= 0x290A) {
485 hwparams3 = DWC3_READ(sc, DWC3_GHWPARAMS3);
486 if (DWC3_HWPARAMS3_SSPHY(hwparams3) == DWC3_HWPARAMS3_SSPHY_DISABLE) {
487 reg = DWC3_READ(sc, DWC3_GUCTL1);
489 device_printf(dev, "Forcing USB2 clock only\n");
490 reg |= DWC3_GUCTL1_DEV_FORCE_20_CLK_FOR_30_CLK;
491 DWC3_WRITE(sc, DWC3_GUCTL1, reg);
495 snps_dwc3_configure_phy(sc, node);
500 snps_dwc3_configure_host(sc);
501 snps_dwc3_do_quirks(sc);
504 snsp_dwc3_dump_regs(sc, "Pre XHCI init");
506 error = snps_dwc3_attach_xhci(dev);
508 snsp_dwc3_dump_regs(sc, "Post XHCI init");
513 if (sc->clk_ref != NULL)
514 clk_disable(sc->clk_ref);
515 if (sc->clk_suspend != NULL)
516 clk_disable(sc->clk_suspend);
517 if (sc->clk_bus != NULL)
518 clk_disable(sc->clk_bus);
525 static struct ofw_compat_data compat_data[] = {
531 snps_dwc3_fdt_probe(device_t dev)
534 if (!ofw_bus_status_okay(dev))
537 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
540 return (snps_dwc3_probe_common(dev));
544 snps_dwc3_fdt_attach(device_t dev)
547 return (snps_dwc3_common_attach(dev, true));
550 static device_method_t snps_dwc3_fdt_methods[] = {
551 /* Device interface */
552 DEVMETHOD(device_probe, snps_dwc3_fdt_probe),
553 DEVMETHOD(device_attach, snps_dwc3_fdt_attach),
558 DEFINE_CLASS_1(snps_dwc3_fdt, snps_dwc3_fdt_driver, snps_dwc3_fdt_methods,
559 sizeof(struct snps_dwc3_softc), generic_xhci_driver);
561 DRIVER_MODULE(snps_dwc3_fdt, simplebus, snps_dwc3_fdt_driver, 0, 0);
562 MODULE_DEPEND(snps_dwc3_fdt, xhci, 1, 1, 1);
566 static char *dwc3_acpi_ids[] = {
567 "808622B7", /* This was an Intel PCI Vendor/Device ID used. */
568 "PNP0D10", /* The generic XHCI PNP ID needing extra probe checks. */
573 snps_dwc3_acpi_probe(device_t dev)
578 if (acpi_disabled("snps_dwc3"))
581 error = ACPI_ID_PROBE(device_get_parent(dev), dev, dwc3_acpi_ids, &match);
586 * If we found the Generic XHCI PNP ID we can only attach if we have
587 * some other means to identify the device as dwc3.
589 if (strcmp(match, "PNP0D10") == 0) {
590 /* This is needed in SolidRun's HoneyComb. */
591 if (device_has_property(dev, "snps,dis_rxdet_inp3_quirk"))
598 return (snps_dwc3_probe_common(dev));
602 snps_dwc3_acpi_attach(device_t dev)
605 return (snps_dwc3_common_attach(dev, false));
608 static device_method_t snps_dwc3_acpi_methods[] = {
609 /* Device interface */
610 DEVMETHOD(device_probe, snps_dwc3_acpi_probe),
611 DEVMETHOD(device_attach, snps_dwc3_acpi_attach),
616 DEFINE_CLASS_1(snps_dwc3_acpi, snps_dwc3_acpi_driver, snps_dwc3_acpi_methods,
617 sizeof(struct snps_dwc3_softc), generic_xhci_driver);
619 DRIVER_MODULE(snps_dwc3_acpi, acpi, snps_dwc3_acpi_driver, 0, 0);
620 MODULE_DEPEND(snps_dwc3_acpi, usb, 1, 1, 1);