Changeset View
Standalone View
sys/dev/usb/controller/dwc3_fdt.c
- This file was added.
/*- | |||||
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD | |||||
* | |||||
* Copyright (c) 2019 Emmanuel Vadot <manu@FreeBSD.Org> | |||||
* | |||||
* Redistribution and use in source and binary forms, with or without | |||||
* modification, are permitted provided that the following conditions | |||||
* are met: | |||||
* 1. Redistributions of source code must retain the above copyright | |||||
* notice, this list of conditions and the following disclaimer. | |||||
* 2. Redistributions in binary form must reproduce the above copyright | |||||
* notice, this list of conditions and the following disclaimer in the | |||||
* documentation and/or other materials provided with the distribution. | |||||
* | |||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | |||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||||
* SUCH DAMAGE. | |||||
* | |||||
* $FreeBSD$ | |||||
*/ | |||||
#include <sys/cdefs.h> | |||||
__FBSDID("$FreeBSD$"); | |||||
#include <sys/param.h> | |||||
#include <sys/systm.h> | |||||
#include <sys/bus.h> | |||||
#include <sys/rman.h> | |||||
#include <sys/condvar.h> | |||||
#include <sys/kernel.h> | |||||
#include <sys/module.h> | |||||
#include <sys/gpio.h> | |||||
#include <machine/bus.h> | |||||
#include <dev/fdt/simplebus.h> | |||||
#include <dev/fdt/fdt_common.h> | |||||
#include <dev/ofw/ofw_bus.h> | |||||
#include <dev/ofw/ofw_bus_subr.h> | |||||
#include <dev/ofw/ofw_subr.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/usb/controller/dwc3.h> | |||||
#include <dev/extres/clk/clk.h> | |||||
#include <dev/extres/phy/phy_usb.h> | |||||
#include "generic_xhci.h" | |||||
static struct ofw_compat_data compat_data[] = { | |||||
{ "snps,dwc3", 1 }, | |||||
{ NULL, 0 } | |||||
}; | |||||
struct snps_dwc3_fdt_softc { | |||||
struct snps_dwc3_common_softc csc; | |||||
phandle_t node; | |||||
phy_t usb2_phy; | |||||
phy_t usb3_phy; | |||||
}; | |||||
static void | |||||
snps_dwc3_configure_phy(struct snps_dwc3_fdt_softc *sc) | |||||
{ | |||||
char *phy_type; | |||||
uint32_t reg; | |||||
int nphy_types; | |||||
phy_type = NULL; | |||||
nphy_types = OF_getprop_alloc(sc->node, "phy_type", (void **)&phy_type); | |||||
if (nphy_types <= 0) | |||||
return; | |||||
reg = DWC3_READ(&sc->csc, DWC3_GUSB2PHYCFG0); | |||||
if (strncmp(phy_type, "utmi_wide", 9) == 0) { | |||||
reg &= ~(DWC3_GUSB2PHYCFG0_PHYIF | DWC3_GUSB2PHYCFG0_USBTRDTIM(0xf)); | |||||
reg |= DWC3_GUSB2PHYCFG0_PHYIF | | |||||
DWC3_GUSB2PHYCFG0_USBTRDTIM(DWC3_GUSB2PHYCFG0_USBTRDTIM_16BITS); | |||||
} else { | |||||
reg &= ~(DWC3_GUSB2PHYCFG0_PHYIF | DWC3_GUSB2PHYCFG0_USBTRDTIM(0xf)); | |||||
reg |= DWC3_GUSB2PHYCFG0_PHYIF | | |||||
DWC3_GUSB2PHYCFG0_USBTRDTIM(DWC3_GUSB2PHYCFG0_USBTRDTIM_8BITS); | |||||
} | |||||
DWC3_WRITE(&sc->csc, DWC3_GUSB2PHYCFG0, reg); | |||||
OF_prop_free(phy_type); | |||||
} | |||||
static int | |||||
snps_dwc3_fdt_probe(device_t dev) | |||||
{ | |||||
if (!ofw_bus_status_okay(dev)) | |||||
return (ENXIO); | |||||
if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) | |||||
return (ENXIO); | |||||
return (snps_dwc3_probe_common(dev)); | |||||
} | |||||
static int | |||||
snps_dwc3_fdt_attach(device_t dev) | |||||
{ | |||||
struct snps_dwc3_fdt_softc *sc; | |||||
struct snps_dwc3_common_softc *csc; | |||||
int rid = 0; | |||||
sc = device_get_softc(dev); | |||||
csc = &sc->csc; | |||||
csc->dev = dev; | |||||
csc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, | |||||
RF_ACTIVE); | |||||
if (csc->mem_res == NULL) { | |||||
device_printf(dev, "Failed to map memory\n"); | |||||
return (ENXIO); | |||||
} | |||||
csc->bst = rman_get_bustag(csc->mem_res); | |||||
csc->bsh = rman_get_bushandle(csc->mem_res); | |||||
mw: Please move above block to a common subroutine - it can help avoiding some duplication. | |||||
if (bootverbose) | |||||
device_printf(dev, "snps id: %x\n", DWC3_READ(csc, DWC3_GSNPSID)); | |||||
sc->node = ofw_bus_get_node(dev); | |||||
/* Get the phys */ | |||||
phy_get_by_ofw_name(dev, sc->node, "usb2-phy", &sc->usb2_phy); | |||||
phy_get_by_ofw_name(dev, sc->node, "usb3-phy", &sc->usb3_phy); | |||||
if (sc->usb2_phy) | |||||
phy_enable(sc->usb2_phy); | |||||
if (sc->usb3_phy) | |||||
phy_enable(sc->usb3_phy); | |||||
snps_dwc3_reset(csc); | |||||
snps_dwc3_configure_host(csc); | |||||
snps_dwc3_configure_phy(sc); | |||||
mwUnsubmitted Not Done Inline ActionsI think it should be safe to execute this routine before snps_dwc3_reset (in ACPI world the phy is configured by firmware) - if confirmed, we can move: snps_dwc3_reset(csc); to a common snps_dwc3_attach_xhci. mw: I think it should be safe to execute this routine before snps_dwc3_reset (in ACPI world the phy… | |||||
bzAuthorUnsubmitted Done Inline ActionsHow do you then want to "hide" the phy_get_by_ofw_name() stuff in the ACPI case? That's the thing I couldn't figure out last night at 2AM. bz: How do you then want to "hide" the phy_get_by_ofw_name() stuff in the ACPI case? That's the… | |||||
mwUnsubmitted Not Done Inline ActionsI wrote 2 comments at the same time - in the other I was suggesting to leave obtaining phys from DT in snps_dwc3_fdt_attach and move as much as possible to the common attach routine. In case everything worked as expected, that would allow to shrink snps_dwc3_fdt_attach to:
And snps_dwc3_fdt_attach to:
mw: I wrote 2 comments at the same time - in the other I was suggesting to leave obtaining phys… | |||||
snps_dwc3_do_quirks(csc); | |||||
#if 0 | |||||
mwUnsubmitted Not Done Inline ActionsPlease replace if 0 with a DEBUG flag. mw: Please replace if 0 with a DEBUG flag. | |||||
bzAuthorUnsubmitted Done Inline ActionsYeah I can; or even a tunable with is 0 by default. This is just a copy from the current code. bz: Yeah I can; or even a tunable with is 0 by default. This is just a copy from the current code. | |||||
snsp_dwc3_dump_regs(csc); | |||||
#endif | |||||
snps_dwc3_attach_xhci(csc); | |||||
return (0); | |||||
} | |||||
static device_method_t snps_dwc3_fdt_methods[] = { | |||||
/* Device interface */ | |||||
DEVMETHOD(device_probe, snps_dwc3_fdt_probe), | |||||
DEVMETHOD(device_attach, snps_dwc3_fdt_attach), | |||||
DEVMETHOD_END | |||||
}; | |||||
static driver_t snps_dwc3_fdt_driver = { | |||||
"xhci", | |||||
snps_dwc3_fdt_methods, | |||||
sizeof(struct snps_dwc3_fdt_softc) | |||||
}; | |||||
static devclass_t snps_dwc3_fdt_devclass; | |||||
DRIVER_MODULE(snps_dwc3_fdt, simplebus, snps_dwc3_fdt_driver, | |||||
snps_dwc3_fdt_devclass, 0, 0); | |||||
MODULE_DEPEND(snps_dwc3_fdt, xhci, 1, 1, 1); |
Please move above block to a common subroutine - it can help avoiding some duplication.