Home | History | Annotate | Line # | Download | only in fdt
dwc3_fdt.c revision 1.2.2.1
      1  1.2.2.1  christos /* $NetBSD: dwc3_fdt.c,v 1.2.2.1 2019/06/10 22:07:07 christos Exp $ */
      2      1.1  jmcneill 
      3      1.1  jmcneill /*-
      4      1.1  jmcneill  * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
      5      1.1  jmcneill  * All rights reserved.
      6      1.1  jmcneill  *
      7      1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8      1.1  jmcneill  * modification, are permitted provided that the following conditions
      9      1.1  jmcneill  * are met:
     10      1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11      1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12      1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14      1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15      1.1  jmcneill  *
     16      1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17      1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18      1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19      1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20      1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21      1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22      1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23      1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24      1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25      1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26      1.1  jmcneill  * SUCH DAMAGE.
     27      1.1  jmcneill  */
     28      1.1  jmcneill 
     29      1.1  jmcneill #include <sys/cdefs.h>
     30  1.2.2.1  christos __KERNEL_RCSID(0, "$NetBSD: dwc3_fdt.c,v 1.2.2.1 2019/06/10 22:07:07 christos Exp $");
     31      1.1  jmcneill 
     32      1.1  jmcneill #include <sys/param.h>
     33      1.1  jmcneill #include <sys/bus.h>
     34      1.1  jmcneill #include <sys/device.h>
     35      1.1  jmcneill #include <sys/intr.h>
     36      1.1  jmcneill #include <sys/systm.h>
     37      1.1  jmcneill #include <sys/kernel.h>
     38      1.1  jmcneill 
     39      1.1  jmcneill #include <dev/usb/usb.h>
     40      1.1  jmcneill #include <dev/usb/usbdi.h>
     41      1.1  jmcneill #include <dev/usb/usbdivar.h>
     42      1.1  jmcneill #include <dev/usb/usb_mem.h>
     43      1.1  jmcneill #include <dev/usb/xhcireg.h>
     44      1.1  jmcneill #include <dev/usb/xhcivar.h>
     45      1.1  jmcneill 
     46      1.1  jmcneill #include <dev/fdt/fdtvar.h>
     47      1.1  jmcneill 
     48      1.1  jmcneill #define	DWC3_GCTL			0xc110
     49      1.1  jmcneill #define	 GCTL_PRTCAP			__BITS(13,12)
     50      1.1  jmcneill #define	  GCTL_PRTCAP_HOST		1
     51      1.1  jmcneill #define	  GCTL_PRTCAP_DEVICE		2
     52      1.1  jmcneill #define	  GCTL_PRTCAP_OTG		3
     53      1.1  jmcneill #define	 GCTL_CORESOFTRESET		__BIT(11)
     54      1.1  jmcneill 
     55  1.2.2.1  christos #define	DWC3_SNPSID			0xc120
     56  1.2.2.1  christos #define	 DWC3_SNPSID_REV		__BITS(15,0)
     57  1.2.2.1  christos 
     58      1.1  jmcneill #define	DWC3_GUSB2PHYCFG(n)		(0xc200 + ((n) * 4))
     59      1.1  jmcneill #define	 GUSB2PHYCFG_PHYSOFTRST		__BIT(31)
     60      1.2  jmcneill #define	 GUSB2PHYCFG_U2_FREECLK_EXISTS	__BIT(30)
     61      1.2  jmcneill #define	 GUSB2PHYCFG_USBTRDTIM		__BITS(13,10)
     62      1.2  jmcneill #define	 GUSB2PHYCFG_SUSPHY		__BIT(6)
     63      1.2  jmcneill #define	 GUSB2PHYCFG_PHYIF		__BIT(3)
     64      1.2  jmcneill #define	 GUSB2PHYCFG_ENBLSLPM		__BIT(0)
     65      1.1  jmcneill 
     66      1.1  jmcneill #define	DWC3_GUSB3PIPECTL(n)		(0xc2c0 + ((n) * 4))
     67      1.1  jmcneill #define	 GUSB3PIPECTL_PHYSOFTRST	__BIT(31)
     68  1.2.2.1  christos #define	 GUSB3PIPECTL_UX_EXIT_PX	__BIT(27)
     69  1.2.2.1  christos #define	 GUSB3PIPECTL_DEPOCHANGE	__BIT(18)
     70  1.2.2.1  christos #define	 GUSB3PIPECTL_SUSPHY		__BIT(17)
     71      1.1  jmcneill 
     72      1.2  jmcneill #define	DWC3_DCFG			0xc700
     73      1.2  jmcneill #define	 DCFG_SPEED			__BITS(2,0)
     74      1.2  jmcneill #define	  DCFG_SPEED_HS			0
     75      1.2  jmcneill #define	  DCFG_SPEED_FS			1
     76      1.2  jmcneill #define	  DCFG_SPEED_LS			2
     77      1.2  jmcneill #define	  DCFG_SPEED_SS			4
     78      1.2  jmcneill #define	  DCFG_SPEED_SS_PLUS		5
     79      1.2  jmcneill 
     80      1.1  jmcneill static int	dwc3_fdt_match(device_t, cfdata_t, void *);
     81      1.1  jmcneill static void	dwc3_fdt_attach(device_t, device_t, void *);
     82      1.1  jmcneill 
     83      1.1  jmcneill CFATTACH_DECL2_NEW(dwc3_fdt, sizeof(struct xhci_softc),
     84      1.1  jmcneill 	dwc3_fdt_match, dwc3_fdt_attach, NULL,
     85      1.1  jmcneill 	xhci_activate, NULL, xhci_childdet);
     86      1.1  jmcneill 
     87      1.1  jmcneill #define	RD4(sc, reg)				\
     88      1.1  jmcneill 	bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg))
     89      1.1  jmcneill #define	WR4(sc, reg, val)			\
     90      1.1  jmcneill 	bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
     91      1.1  jmcneill #define	SET4(sc, reg, mask)			\
     92      1.1  jmcneill 	WR4((sc), (reg), RD4((sc), (reg)) | (mask))
     93      1.1  jmcneill #define	CLR4(sc, reg, mask)			\
     94      1.1  jmcneill 	WR4((sc), (reg), RD4((sc), (reg)) & ~(mask))
     95      1.1  jmcneill 
     96      1.1  jmcneill static void
     97      1.1  jmcneill dwc3_fdt_soft_reset(struct xhci_softc *sc)
     98      1.1  jmcneill {
     99      1.1  jmcneill 	/* Put core in reset */
    100      1.1  jmcneill 	SET4(sc, DWC3_GCTL, GCTL_CORESOFTRESET);
    101      1.1  jmcneill 
    102      1.1  jmcneill 	/* Assert USB3 PHY reset */
    103      1.1  jmcneill 	SET4(sc, DWC3_GUSB3PIPECTL(0), GUSB3PIPECTL_PHYSOFTRST);
    104      1.1  jmcneill 
    105      1.1  jmcneill 	/* Assert USB2 PHY reset */
    106      1.1  jmcneill 	SET4(sc, DWC3_GUSB2PHYCFG(0), GUSB2PHYCFG_PHYSOFTRST);
    107      1.1  jmcneill 
    108      1.1  jmcneill 	delay(100000);
    109      1.1  jmcneill 
    110      1.1  jmcneill 	/* Clear USB3 PHY reset */
    111      1.1  jmcneill 	CLR4(sc, DWC3_GUSB3PIPECTL(0), GUSB3PIPECTL_PHYSOFTRST);
    112      1.1  jmcneill 
    113      1.1  jmcneill 	/* Clear USB2 PHY reset */
    114      1.1  jmcneill 	CLR4(sc, DWC3_GUSB2PHYCFG(0), GUSB2PHYCFG_PHYSOFTRST);
    115      1.1  jmcneill 
    116      1.1  jmcneill 	delay(100000);
    117      1.1  jmcneill 
    118      1.1  jmcneill 	/* Take core out of reset */
    119      1.1  jmcneill 	CLR4(sc, DWC3_GCTL, GCTL_CORESOFTRESET);
    120      1.1  jmcneill }
    121      1.1  jmcneill 
    122      1.1  jmcneill static void
    123      1.2  jmcneill dwc3_fdt_enable_phy(struct xhci_softc *sc, const int phandle)
    124      1.2  jmcneill {
    125  1.2.2.1  christos 	const char *max_speed, *phy_type;
    126      1.2  jmcneill 	u_int phyif_utmi_bits;
    127      1.2  jmcneill 	uint32_t val;
    128      1.2  jmcneill 
    129      1.2  jmcneill 	val = RD4(sc, DWC3_GUSB2PHYCFG(0));
    130  1.2.2.1  christos 	if (of_getprop_uint32(phandle, "snps,phyif-utmi-bits", &phyif_utmi_bits) != 0) {
    131  1.2.2.1  christos 		phy_type = fdtbus_get_string(phandle, "phy_type");
    132  1.2.2.1  christos 		if (phy_type && strcmp(phy_type, "utmi_wide") == 0)
    133  1.2.2.1  christos 			phyif_utmi_bits = 16;
    134  1.2.2.1  christos 		else if (phy_type && strcmp(phy_type, "utmi") == 0)
    135  1.2.2.1  christos 			phyif_utmi_bits = 8;
    136  1.2.2.1  christos 		else
    137  1.2.2.1  christos 			phyif_utmi_bits = 0;
    138  1.2.2.1  christos 	}
    139  1.2.2.1  christos 	if (phyif_utmi_bits == 16) {
    140  1.2.2.1  christos 		val |= GUSB2PHYCFG_PHYIF;
    141  1.2.2.1  christos 		val &= ~GUSB2PHYCFG_USBTRDTIM;
    142  1.2.2.1  christos 		val |= __SHIFTIN(5, GUSB2PHYCFG_USBTRDTIM);
    143  1.2.2.1  christos 	} else if (phyif_utmi_bits == 8) {
    144  1.2.2.1  christos 		val &= ~GUSB2PHYCFG_PHYIF;
    145  1.2.2.1  christos 		val &= ~GUSB2PHYCFG_USBTRDTIM;
    146  1.2.2.1  christos 		val |= __SHIFTIN(9, GUSB2PHYCFG_USBTRDTIM);
    147      1.2  jmcneill 	}
    148  1.2.2.1  christos 	if (of_hasprop(phandle, "snps,dis-enblslpm-quirk") ||
    149  1.2.2.1  christos 	    of_hasprop(phandle, "snps,dis_enblslpm_quirk"))
    150      1.2  jmcneill 		val &= ~GUSB2PHYCFG_ENBLSLPM;
    151      1.2  jmcneill 	if (of_hasprop(phandle, "snps,dis-u2-freeclk-exists-quirk"))
    152      1.2  jmcneill 		val &= ~GUSB2PHYCFG_U2_FREECLK_EXISTS;
    153  1.2.2.1  christos 	if (of_hasprop(phandle, "snps,dis_u2_susphy_quirk"))
    154      1.2  jmcneill 		val &= ~GUSB2PHYCFG_SUSPHY;
    155      1.2  jmcneill 	WR4(sc, DWC3_GUSB2PHYCFG(0), val);
    156      1.2  jmcneill 
    157  1.2.2.1  christos 	val = RD4(sc, DWC3_GUSB3PIPECTL(0));
    158  1.2.2.1  christos 	val &= ~GUSB3PIPECTL_UX_EXIT_PX;
    159  1.2.2.1  christos 	if (of_hasprop(phandle, "snps,dis_u3_susphy_quirk"))
    160  1.2.2.1  christos 		val &= ~GUSB3PIPECTL_SUSPHY;
    161  1.2.2.1  christos 	if (of_hasprop(phandle, "snps,dis-del-phy-power-chg-quirk"))
    162  1.2.2.1  christos 		val &= ~GUSB3PIPECTL_DEPOCHANGE;
    163  1.2.2.1  christos 	WR4(sc, DWC3_GUSB3PIPECTL(0), val);
    164  1.2.2.1  christos 
    165      1.2  jmcneill 	max_speed = fdtbus_get_string(phandle, "maximum-speed");
    166      1.2  jmcneill 	if (max_speed == NULL)
    167      1.2  jmcneill 		max_speed = "super-speed";
    168      1.2  jmcneill 
    169      1.2  jmcneill 	val = RD4(sc, DWC3_DCFG);
    170      1.2  jmcneill 	val &= ~DCFG_SPEED;
    171      1.2  jmcneill 	if (strcmp(max_speed, "low-speed") == 0)
    172      1.2  jmcneill 		val |= __SHIFTIN(DCFG_SPEED_LS, DCFG_SPEED);
    173      1.2  jmcneill 	else if (strcmp(max_speed, "full-speed") == 0)
    174      1.2  jmcneill 		val |= __SHIFTIN(DCFG_SPEED_FS, DCFG_SPEED);
    175      1.2  jmcneill 	else if (strcmp(max_speed, "high-speed") == 0)
    176      1.2  jmcneill 		val |= __SHIFTIN(DCFG_SPEED_HS, DCFG_SPEED);
    177      1.2  jmcneill 	else if (strcmp(max_speed, "super-speed") == 0)
    178      1.2  jmcneill 		val |= __SHIFTIN(DCFG_SPEED_SS, DCFG_SPEED);
    179      1.2  jmcneill 	else
    180      1.2  jmcneill 		val |= __SHIFTIN(DCFG_SPEED_SS, DCFG_SPEED);	/* default to super speed */
    181      1.2  jmcneill 	WR4(sc, DWC3_DCFG, val);
    182      1.2  jmcneill }
    183      1.2  jmcneill 
    184      1.2  jmcneill static void
    185      1.1  jmcneill dwc3_fdt_set_mode(struct xhci_softc *sc, u_int mode)
    186      1.1  jmcneill {
    187      1.1  jmcneill 	uint32_t val;
    188      1.1  jmcneill 
    189      1.1  jmcneill 	val = RD4(sc, DWC3_GCTL);
    190      1.1  jmcneill 	val &= ~GCTL_PRTCAP;
    191      1.1  jmcneill 	val |= __SHIFTIN(mode, GCTL_PRTCAP);
    192      1.1  jmcneill 	WR4(sc, DWC3_GCTL, val);
    193      1.1  jmcneill }
    194      1.1  jmcneill 
    195      1.1  jmcneill static int
    196      1.1  jmcneill dwc3_fdt_match(device_t parent, cfdata_t cf, void *aux)
    197      1.1  jmcneill {
    198      1.1  jmcneill 	const char * const compatible[] = {
    199      1.1  jmcneill 		"allwinner,sun50i-h6-dwc3",
    200  1.2.2.1  christos 		"amlogic,meson-gxl-dwc3",
    201      1.2  jmcneill 		"rockchip,rk3328-dwc3",
    202  1.2.2.1  christos 		"rockchip,rk3399-dwc3",
    203  1.2.2.1  christos 		"samsung,exynos5250-dwusb3",
    204      1.1  jmcneill 		NULL
    205      1.1  jmcneill 	};
    206      1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    207      1.1  jmcneill 
    208      1.1  jmcneill 	return of_match_compatible(faa->faa_phandle, compatible);
    209      1.1  jmcneill }
    210      1.1  jmcneill 
    211      1.1  jmcneill static void
    212      1.1  jmcneill dwc3_fdt_attach(device_t parent, device_t self, void *aux)
    213      1.1  jmcneill {
    214      1.1  jmcneill 	struct xhci_softc * const sc = device_private(self);
    215      1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    216      1.1  jmcneill 	const int phandle = faa->faa_phandle;
    217      1.1  jmcneill 	struct fdtbus_reset *rst;
    218      1.1  jmcneill 	struct fdtbus_phy *phy;
    219      1.1  jmcneill 	struct clk *clk;
    220      1.1  jmcneill 	char intrstr[128];
    221      1.1  jmcneill 	bus_addr_t addr;
    222      1.1  jmcneill 	bus_size_t size;
    223      1.1  jmcneill 	int error;
    224      1.1  jmcneill 	void *ih;
    225      1.1  jmcneill 	u_int n;
    226      1.1  jmcneill 
    227      1.1  jmcneill 	/* Find dwc3 sub-node */
    228      1.1  jmcneill 	const int dwc3_phandle = of_find_firstchild_byname(phandle, "dwc3");
    229      1.1  jmcneill 	if (dwc3_phandle <= 0) {
    230      1.1  jmcneill 		aprint_error(": couldn't find dwc3 child node\n");
    231      1.1  jmcneill 		return;
    232      1.1  jmcneill 	}
    233      1.1  jmcneill 
    234      1.1  jmcneill 	/* Only host mode is supported */
    235      1.1  jmcneill 	const char *dr_mode = fdtbus_get_string(dwc3_phandle, "dr_mode");
    236      1.1  jmcneill 	if (dr_mode == NULL || strcmp(dr_mode, "host") != 0) {
    237      1.1  jmcneill 		aprint_error(": '%s' not supported\n", dr_mode);
    238      1.1  jmcneill 		return;
    239      1.1  jmcneill 	}
    240      1.1  jmcneill 
    241      1.1  jmcneill 	/* Enable clocks */
    242      1.1  jmcneill 	for (n = 0; (clk = fdtbus_clock_get_index(phandle, n)) != NULL; n++)
    243      1.1  jmcneill 		if (clk_enable(clk) != 0) {
    244      1.1  jmcneill 			aprint_error(": couldn't enable clock #%d\n", n);
    245      1.1  jmcneill 			return;
    246      1.1  jmcneill 		}
    247      1.1  jmcneill 	/* De-assert resets */
    248      1.1  jmcneill 	for (n = 0; (rst = fdtbus_reset_get_index(phandle, n)) != NULL; n++)
    249      1.1  jmcneill 		if (fdtbus_reset_deassert(rst) != 0) {
    250      1.1  jmcneill 			aprint_error(": couldn't de-assert reset #%d\n", n);
    251      1.1  jmcneill 			return;
    252      1.1  jmcneill 		}
    253      1.1  jmcneill 
    254      1.1  jmcneill 	/* Get resources */
    255      1.1  jmcneill 	if (fdtbus_get_reg(dwc3_phandle, 0, &addr, &size) != 0) {
    256      1.1  jmcneill 		aprint_error(": couldn't get registers\n");
    257      1.1  jmcneill 		return;
    258      1.1  jmcneill 	}
    259      1.1  jmcneill 
    260      1.1  jmcneill 	sc->sc_dev = self;
    261      1.1  jmcneill 	sc->sc_bus.ub_hcpriv = sc;
    262      1.1  jmcneill 	sc->sc_bus.ub_dmatag = faa->faa_dmat;
    263      1.1  jmcneill 	sc->sc_iot = faa->faa_bst;
    264      1.1  jmcneill 	if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh) != 0) {
    265      1.1  jmcneill 		aprint_error(": couldn't map registers\n");
    266      1.1  jmcneill 		return;
    267      1.1  jmcneill 	}
    268      1.1  jmcneill 
    269      1.1  jmcneill 	aprint_naive("\n");
    270  1.2.2.1  christos 	aprint_normal(": DesignWare USB3 XHCI");
    271  1.2.2.1  christos 	const uint32_t snpsid = RD4(sc, DWC3_SNPSID);
    272  1.2.2.1  christos 	const u_int rev = __SHIFTOUT(snpsid, DWC3_SNPSID_REV);
    273  1.2.2.1  christos 	aprint_normal(" (rev. %d.%03x)\n", rev >> 12, rev & 0xfff);
    274  1.2.2.1  christos 
    275  1.2.2.1  christos 	/* Enable PHY devices */
    276  1.2.2.1  christos 	for (n = 0; (phy = fdtbus_phy_get_index(dwc3_phandle, n)) != NULL; n++) {
    277  1.2.2.1  christos 		if (fdtbus_phy_enable(phy, true) != 0)
    278  1.2.2.1  christos 			aprint_error_dev(self, "couldn't enable phy #%d\n", n);
    279      1.2  jmcneill 	}
    280      1.2  jmcneill 
    281      1.1  jmcneill 	dwc3_fdt_soft_reset(sc);
    282  1.2.2.1  christos 	dwc3_fdt_enable_phy(sc, dwc3_phandle);
    283      1.1  jmcneill 	dwc3_fdt_set_mode(sc, GCTL_PRTCAP_HOST);
    284      1.1  jmcneill 
    285      1.1  jmcneill 	if (!fdtbus_intr_str(dwc3_phandle, 0, intrstr, sizeof(intrstr))) {
    286      1.1  jmcneill 		aprint_error_dev(self, "failed to decode interrupt\n");
    287      1.1  jmcneill 		return;
    288      1.1  jmcneill 	}
    289      1.1  jmcneill 
    290      1.1  jmcneill 	ih = fdtbus_intr_establish(dwc3_phandle, 0, IPL_USB, FDT_INTR_MPSAFE,
    291      1.1  jmcneill 	    xhci_intr, sc);
    292      1.1  jmcneill 	if (ih == NULL) {
    293      1.1  jmcneill 		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
    294      1.1  jmcneill 		    intrstr);
    295      1.1  jmcneill 		return;
    296      1.1  jmcneill 	}
    297      1.1  jmcneill 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    298      1.1  jmcneill 
    299  1.2.2.1  christos 	sc->sc_bus.ub_revision = USBREV_3_0;
    300      1.1  jmcneill 	error = xhci_init(sc);
    301      1.1  jmcneill 	if (error) {
    302      1.1  jmcneill 		aprint_error_dev(self, "init failed, error = %d\n", error);
    303      1.1  jmcneill 		return;
    304      1.1  jmcneill 	}
    305      1.1  jmcneill 
    306      1.1  jmcneill 	sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint);
    307      1.1  jmcneill 	sc->sc_child2 = config_found(self, &sc->sc_bus2, usbctlprint);
    308      1.1  jmcneill }
    309