Home | History | Annotate | Line # | Download | only in fdt
dwc3_fdt.c revision 1.1
      1  1.1  jmcneill /* $NetBSD: dwc3_fdt.c,v 1.1 2018/05/01 23:59:15 jmcneill 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.1  jmcneill __KERNEL_RCSID(0, "$NetBSD: dwc3_fdt.c,v 1.1 2018/05/01 23:59:15 jmcneill 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.1  jmcneill #define	DWC3_GUSB2PHYCFG(n)		(0xc200 + ((n) * 4))
     56  1.1  jmcneill #define	 GUSB2PHYCFG_PHYSOFTRST		__BIT(31)
     57  1.1  jmcneill 
     58  1.1  jmcneill #define	DWC3_GUSB3PIPECTL(n)		(0xc2c0 + ((n) * 4))
     59  1.1  jmcneill #define	 GUSB3PIPECTL_PHYSOFTRST	__BIT(31)
     60  1.1  jmcneill 
     61  1.1  jmcneill static int	dwc3_fdt_match(device_t, cfdata_t, void *);
     62  1.1  jmcneill static void	dwc3_fdt_attach(device_t, device_t, void *);
     63  1.1  jmcneill 
     64  1.1  jmcneill CFATTACH_DECL2_NEW(dwc3_fdt, sizeof(struct xhci_softc),
     65  1.1  jmcneill 	dwc3_fdt_match, dwc3_fdt_attach, NULL,
     66  1.1  jmcneill 	xhci_activate, NULL, xhci_childdet);
     67  1.1  jmcneill 
     68  1.1  jmcneill #define	RD4(sc, reg)				\
     69  1.1  jmcneill 	bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg))
     70  1.1  jmcneill #define	WR4(sc, reg, val)			\
     71  1.1  jmcneill 	bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
     72  1.1  jmcneill #define	SET4(sc, reg, mask)			\
     73  1.1  jmcneill 	WR4((sc), (reg), RD4((sc), (reg)) | (mask))
     74  1.1  jmcneill #define	CLR4(sc, reg, mask)			\
     75  1.1  jmcneill 	WR4((sc), (reg), RD4((sc), (reg)) & ~(mask))
     76  1.1  jmcneill 
     77  1.1  jmcneill static void
     78  1.1  jmcneill dwc3_fdt_soft_reset(struct xhci_softc *sc)
     79  1.1  jmcneill {
     80  1.1  jmcneill 	/* Put core in reset */
     81  1.1  jmcneill 	SET4(sc, DWC3_GCTL, GCTL_CORESOFTRESET);
     82  1.1  jmcneill 
     83  1.1  jmcneill 	/* Assert USB3 PHY reset */
     84  1.1  jmcneill 	SET4(sc, DWC3_GUSB3PIPECTL(0), GUSB3PIPECTL_PHYSOFTRST);
     85  1.1  jmcneill 
     86  1.1  jmcneill 	/* Assert USB2 PHY reset */
     87  1.1  jmcneill 	SET4(sc, DWC3_GUSB2PHYCFG(0), GUSB2PHYCFG_PHYSOFTRST);
     88  1.1  jmcneill 
     89  1.1  jmcneill 	delay(100000);
     90  1.1  jmcneill 
     91  1.1  jmcneill 	/* Clear USB3 PHY reset */
     92  1.1  jmcneill 	CLR4(sc, DWC3_GUSB3PIPECTL(0), GUSB3PIPECTL_PHYSOFTRST);
     93  1.1  jmcneill 
     94  1.1  jmcneill 	/* Clear USB2 PHY reset */
     95  1.1  jmcneill 	CLR4(sc, DWC3_GUSB2PHYCFG(0), GUSB2PHYCFG_PHYSOFTRST);
     96  1.1  jmcneill 
     97  1.1  jmcneill 	delay(100000);
     98  1.1  jmcneill 
     99  1.1  jmcneill 	/* Take core out of reset */
    100  1.1  jmcneill 	CLR4(sc, DWC3_GCTL, GCTL_CORESOFTRESET);
    101  1.1  jmcneill }
    102  1.1  jmcneill 
    103  1.1  jmcneill static void
    104  1.1  jmcneill dwc3_fdt_set_mode(struct xhci_softc *sc, u_int mode)
    105  1.1  jmcneill {
    106  1.1  jmcneill 	uint32_t val;
    107  1.1  jmcneill 
    108  1.1  jmcneill 	val = RD4(sc, DWC3_GCTL);
    109  1.1  jmcneill 	val &= ~GCTL_PRTCAP;
    110  1.1  jmcneill 	val |= __SHIFTIN(mode, GCTL_PRTCAP);
    111  1.1  jmcneill 	WR4(sc, DWC3_GCTL, val);
    112  1.1  jmcneill }
    113  1.1  jmcneill 
    114  1.1  jmcneill static int
    115  1.1  jmcneill dwc3_fdt_match(device_t parent, cfdata_t cf, void *aux)
    116  1.1  jmcneill {
    117  1.1  jmcneill 	const char * const compatible[] = {
    118  1.1  jmcneill 		"allwinner,sun50i-h6-dwc3",
    119  1.1  jmcneill 		NULL
    120  1.1  jmcneill 	};
    121  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    122  1.1  jmcneill 
    123  1.1  jmcneill 	return of_match_compatible(faa->faa_phandle, compatible);
    124  1.1  jmcneill }
    125  1.1  jmcneill 
    126  1.1  jmcneill static void
    127  1.1  jmcneill dwc3_fdt_attach(device_t parent, device_t self, void *aux)
    128  1.1  jmcneill {
    129  1.1  jmcneill 	struct xhci_softc * const sc = device_private(self);
    130  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    131  1.1  jmcneill 	const int phandle = faa->faa_phandle;
    132  1.1  jmcneill 	struct fdtbus_reset *rst;
    133  1.1  jmcneill 	struct fdtbus_phy *phy;
    134  1.1  jmcneill 	struct clk *clk;
    135  1.1  jmcneill 	char intrstr[128];
    136  1.1  jmcneill 	bus_addr_t addr;
    137  1.1  jmcneill 	bus_size_t size;
    138  1.1  jmcneill 	int error;
    139  1.1  jmcneill 	void *ih;
    140  1.1  jmcneill 	u_int n;
    141  1.1  jmcneill 
    142  1.1  jmcneill 	/* Find dwc3 sub-node */
    143  1.1  jmcneill 	const int dwc3_phandle = of_find_firstchild_byname(phandle, "dwc3");
    144  1.1  jmcneill 	if (dwc3_phandle <= 0) {
    145  1.1  jmcneill 		aprint_error(": couldn't find dwc3 child node\n");
    146  1.1  jmcneill 		return;
    147  1.1  jmcneill 	}
    148  1.1  jmcneill 
    149  1.1  jmcneill 	/* Only host mode is supported */
    150  1.1  jmcneill 	const char *dr_mode = fdtbus_get_string(dwc3_phandle, "dr_mode");
    151  1.1  jmcneill 	if (dr_mode == NULL || strcmp(dr_mode, "host") != 0) {
    152  1.1  jmcneill 		aprint_error(": '%s' not supported\n", dr_mode);
    153  1.1  jmcneill 		return;
    154  1.1  jmcneill 	}
    155  1.1  jmcneill 
    156  1.1  jmcneill 	/* Enable clocks */
    157  1.1  jmcneill 	for (n = 0; (clk = fdtbus_clock_get_index(phandle, n)) != NULL; n++)
    158  1.1  jmcneill 		if (clk_enable(clk) != 0) {
    159  1.1  jmcneill 			aprint_error(": couldn't enable clock #%d\n", n);
    160  1.1  jmcneill 			return;
    161  1.1  jmcneill 		}
    162  1.1  jmcneill 	/* De-assert resets */
    163  1.1  jmcneill 	for (n = 0; (rst = fdtbus_reset_get_index(phandle, n)) != NULL; n++)
    164  1.1  jmcneill 		if (fdtbus_reset_deassert(rst) != 0) {
    165  1.1  jmcneill 			aprint_error(": couldn't de-assert reset #%d\n", n);
    166  1.1  jmcneill 			return;
    167  1.1  jmcneill 		}
    168  1.1  jmcneill 
    169  1.1  jmcneill 	/* Enable phy */
    170  1.1  jmcneill 	phy = fdtbus_phy_get(dwc3_phandle, "usb3-phy");
    171  1.1  jmcneill 	if (!phy || fdtbus_phy_enable(phy, true) != 0) {
    172  1.1  jmcneill 		aprint_error(": couldn't enable phy\n");
    173  1.1  jmcneill 		return;
    174  1.1  jmcneill 	}
    175  1.1  jmcneill 
    176  1.1  jmcneill 	/* Get resources */
    177  1.1  jmcneill 	if (fdtbus_get_reg(dwc3_phandle, 0, &addr, &size) != 0) {
    178  1.1  jmcneill 		aprint_error(": couldn't get registers\n");
    179  1.1  jmcneill 		return;
    180  1.1  jmcneill 	}
    181  1.1  jmcneill 
    182  1.1  jmcneill 	sc->sc_dev = self;
    183  1.1  jmcneill 	sc->sc_bus.ub_hcpriv = sc;
    184  1.1  jmcneill 	sc->sc_bus.ub_dmatag = faa->faa_dmat;
    185  1.1  jmcneill 	sc->sc_iot = faa->faa_bst;
    186  1.1  jmcneill 	if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh) != 0) {
    187  1.1  jmcneill 		aprint_error(": couldn't map registers\n");
    188  1.1  jmcneill 		return;
    189  1.1  jmcneill 	}
    190  1.1  jmcneill 
    191  1.1  jmcneill 	aprint_naive("\n");
    192  1.1  jmcneill 	aprint_normal(": DesignWare USB3 XHCI\n");
    193  1.1  jmcneill 
    194  1.1  jmcneill 	dwc3_fdt_soft_reset(sc);
    195  1.1  jmcneill 	dwc3_fdt_set_mode(sc, GCTL_PRTCAP_HOST);
    196  1.1  jmcneill 
    197  1.1  jmcneill 	if (!fdtbus_intr_str(dwc3_phandle, 0, intrstr, sizeof(intrstr))) {
    198  1.1  jmcneill 		aprint_error_dev(self, "failed to decode interrupt\n");
    199  1.1  jmcneill 		return;
    200  1.1  jmcneill 	}
    201  1.1  jmcneill 
    202  1.1  jmcneill 	ih = fdtbus_intr_establish(dwc3_phandle, 0, IPL_USB, FDT_INTR_MPSAFE,
    203  1.1  jmcneill 	    xhci_intr, sc);
    204  1.1  jmcneill 	if (ih == NULL) {
    205  1.1  jmcneill 		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
    206  1.1  jmcneill 		    intrstr);
    207  1.1  jmcneill 		return;
    208  1.1  jmcneill 	}
    209  1.1  jmcneill 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    210  1.1  jmcneill 
    211  1.1  jmcneill 	error = xhci_init(sc);
    212  1.1  jmcneill 	if (error) {
    213  1.1  jmcneill 		aprint_error_dev(self, "init failed, error = %d\n", error);
    214  1.1  jmcneill 		return;
    215  1.1  jmcneill 	}
    216  1.1  jmcneill 
    217  1.1  jmcneill 	sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint);
    218  1.1  jmcneill 	sc->sc_child2 = config_found(self, &sc->sc_bus2, usbctlprint);
    219  1.1  jmcneill }
    220