Changeset View
Changeset View
Standalone View
Standalone View
sys/dev/usb/controller/dwc_xhci.c
- This file was added.
| /* | |||||
| * Copyright (c) 2017 Mark kettenis <kettenis@openbsd.org> | |||||
| * | |||||
| * Permission to use, copy, modify, and distribute this software for any | |||||
| * purpose with or without fee is hereby granted, provided that the above | |||||
| * copyright notice and this permission notice appear in all copies. | |||||
| * | |||||
| * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |||||
| * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |||||
| * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |||||
| * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |||||
| * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |||||
| * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |||||
| * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |||||
| */ | |||||
| #include <sys/cdefs.h> | |||||
| __FBSDID("$FreeBSD$"); | |||||
| #include "opt_bus.h" | |||||
| #include <sys/param.h> | |||||
| #include <sys/systm.h> | |||||
| #include <sys/bus.h> | |||||
| #include <sys/condvar.h> | |||||
| #include <sys/kernel.h> | |||||
| #include <sys/module.h> | |||||
| #include <dev/usb/usb.h> | |||||
| #include <dev/usb/usbdi.h> | |||||
| #include <dev/usb/usb_core.h> | |||||
| #include <dev/usb/usb_busdma.h> | |||||
| #include <dev/usb/usb_process.h> | |||||
| #include <dev/usb/usb_controller.h> | |||||
| #include <dev/usb/usb_bus.h> | |||||
| #include <dev/usb/controller/xhci.h> | |||||
| #include <dev/ofw/ofw_bus.h> | |||||
| #include <dev/ofw/ofw_bus_subr.h> | |||||
| #ifdef EXT_RESOURCES | |||||
| #include <dev/extres/phy/phy.h> | |||||
| #endif | |||||
| #include "generic_xhci.h" | |||||
| #define XHCI_DWC3_DEVSTR "Synopsys DesignWare USB 3.0 controller" | |||||
| #define XHCI_DWC3_VENDOR "Synopsys" | |||||
| static struct ofw_compat_data compat_data[] = { | |||||
| {"synopsys,dwc3", true}, | |||||
| {"snps,dwc3", true}, | |||||
| {NULL, false} | |||||
| }; | |||||
| static int | |||||
| dwc_xhci_probe(device_t dev) | |||||
| { | |||||
| if (!ofw_bus_status_okay(dev)) | |||||
| return (ENXIO); | |||||
| if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data) | |||||
| return (ENXIO); | |||||
| device_set_desc(dev, XHCI_DWC3_DEVSTR); | |||||
| return (BUS_PROBE_DEFAULT); | |||||
| } | |||||
| #define USB3_GCTL 0xc110 | |||||
| #define USB3_GCTL_PRTCAPDIR_MASK (0x3 << 12) | |||||
| #define USB3_GCTL_PRTCAPDIR_HOST (0x1 << 12) | |||||
| #define USB3_GCTL_PRTCAPDIR_DEVICE (0x2 << 12) | |||||
| #define USB3_GUCTL1 0xc11c | |||||
| #define USB3_GUCTL1_TX_IPGAP_LINECHECK_DIS (1 << 28) | |||||
| #define USB3_GUSB2PHYCFG0 0xc200 | |||||
| #define USB3_GUSB2PHYCFG0_U2_FREECLK_EXISTS (1 << 30) | |||||
| #define USB3_GUSB2PHYCFG0_USBTRDTIM(n) ((n) << 10) | |||||
| #define USB3_GUSB2PHYCFG0_ENBLSLPM (1 << 8) | |||||
| #define USB3_GUSB2PHYCFG0_SUSPENDUSB20 (1 << 6) | |||||
| #define USB3_GUSB2PHYCFG0_PHYIF (1 << 3) | |||||
| static void | |||||
| dwc3_init(struct xhci_softc *sc, phandle_t node) | |||||
| { | |||||
| char phy_type[16] = { 0 }; | |||||
| uint32_t reg; | |||||
| /* We don't support device mode, so always force host mode. */ | |||||
| reg = bus_space_read_4(sc->sc_io_tag, sc->sc_io_hdl, USB3_GCTL); | |||||
| reg &= ~USB3_GCTL_PRTCAPDIR_MASK; | |||||
| reg |= USB3_GCTL_PRTCAPDIR_HOST; | |||||
| bus_space_write_4(sc->sc_io_tag, sc->sc_io_hdl, USB3_GCTL, reg); | |||||
| /* Configure USB2 PHY type and quirks. */ | |||||
| OF_getprop(node, "phy_type", phy_type, sizeof(phy_type)); | |||||
| reg = bus_space_read_4(sc->sc_io_tag, sc->sc_io_hdl, USB3_GUSB2PHYCFG0); | |||||
| reg &= ~USB3_GUSB2PHYCFG0_USBTRDTIM(0xf); | |||||
| if (strcmp(phy_type, "utmi_wide") == 0) { | |||||
| reg |= USB3_GUSB2PHYCFG0_PHYIF; | |||||
| reg |= USB3_GUSB2PHYCFG0_USBTRDTIM(0x5); | |||||
| } else { | |||||
| reg &= ~USB3_GUSB2PHYCFG0_PHYIF; | |||||
| reg |= USB3_GUSB2PHYCFG0_USBTRDTIM(0x9); | |||||
| } | |||||
| if (OF_getproplen(node, "snps,dis-u2-freeclk-exists-quirk") == 0) | |||||
| reg &= ~USB3_GUSB2PHYCFG0_U2_FREECLK_EXISTS; | |||||
| if (OF_getproplen(node, "snps,dis_enblslpm_quirk") == 0) | |||||
| reg &= ~USB3_GUSB2PHYCFG0_ENBLSLPM; | |||||
| if (OF_getproplen(node, "snps,dis_u2_susphy_quirk") == 0) | |||||
| reg &= ~USB3_GUSB2PHYCFG0_SUSPENDUSB20; | |||||
| bus_space_write_4(sc->sc_io_tag, sc->sc_io_hdl, USB3_GUSB2PHYCFG0, reg); | |||||
| /* Configure USB3 quirks. */ | |||||
| reg = bus_space_read_4(sc->sc_io_tag, sc->sc_io_hdl, USB3_GUCTL1); | |||||
| if (OF_getproplen(node, "snps,dis-tx-ipgap-linecheck-quirk") == 0) | |||||
| reg |= USB3_GUCTL1_TX_IPGAP_LINECHECK_DIS; | |||||
| bus_space_write_4(sc->sc_io_tag, sc->sc_io_hdl, USB3_GUCTL1, reg); | |||||
| } | |||||
| static int | |||||
| dwc_xhci_attach(device_t dev) | |||||
| { | |||||
| int err = 0; | |||||
| struct xhci_softc *sc; | |||||
| phandle_t node; | |||||
| #ifdef EXT_RESOURCES | |||||
| phy_t phy; | |||||
| #endif | |||||
| sc = device_get_softc(dev); | |||||
| node = ofw_bus_get_node(dev); | |||||
| #ifdef EXT_RESOURCES | |||||
| if (phy_get_by_ofw_property(dev, node, "usb2-phy", &phy) == 0) | |||||
| if (phy_enable(phy) != 0) | |||||
| device_printf(dev, "Cannot enable usb2-phy\n"); | |||||
| if (phy_get_by_ofw_property(dev, node, "usb3-phy", &phy) == 0) | |||||
| if (phy_enable(phy) != 0) | |||||
| device_printf(dev, "Cannot enable usb3-phy\n"); | |||||
| #endif | |||||
| err = generic_xhci_init_resources(dev); | |||||
| if (err != 0) | |||||
| return (err); | |||||
| sprintf(sc->sc_vendor, XHCI_DWC3_VENDOR); | |||||
| device_set_desc(sc->sc_bus.bdev, XHCI_DWC3_DEVSTR); | |||||
| dwc3_init(sc, node); | |||||
| return (generic_xhci_init_controller(dev)); | |||||
| } | |||||
| static int | |||||
| dwc_xhci_detach(device_t dev) | |||||
| { | |||||
| int err = 0; | |||||
| phandle_t node; | |||||
| phy_t phy; | |||||
| err = generic_xhci_detach(dev); | |||||
| if (err != 0) | |||||
| return (err); | |||||
| node = ofw_bus_get_node(dev); | |||||
| if (phy_get_by_ofw_property(dev, node, "usb2-phy", &phy) == 0) | |||||
| phy_release(phy); | |||||
| if (phy_get_by_ofw_property(dev, node, "usb3-phy", &phy) == 0) | |||||
| phy_release(phy); | |||||
| return (0); | |||||
| } | |||||
| static device_method_t dwc_xhci_methods[] = { | |||||
| /* Device interface */ | |||||
| DEVMETHOD(device_probe, dwc_xhci_probe), | |||||
| DEVMETHOD(device_attach, dwc_xhci_attach), | |||||
| DEVMETHOD(device_detach, dwc_xhci_detach), | |||||
| DEVMETHOD_END | |||||
| }; | |||||
| DEFINE_CLASS_1(xhci, dwc_xhci_driver, dwc_xhci_methods, | |||||
| sizeof(struct xhci_softc), generic_xhci_driver); | |||||
| static devclass_t dwc_xhci_devclass; | |||||
| DRIVER_MODULE(dwcxhci, simplebus, dwc_xhci_driver, dwc_xhci_devclass, 0, 0); | |||||
| MODULE_DEPEND(dwcxhci, usb, 1, 1, 1); | |||||