Home | History | Annotate | Line # | Download | only in fdt
dwc2_fdt.c revision 1.2.4.1
      1  1.2.4.1  christos /*	$NetBSD: dwc2_fdt.c,v 1.2.4.1 2019/06/10 22:07:07 christos Exp $	*/
      2      1.1  jmcneill 
      3      1.1  jmcneill /*-
      4      1.1  jmcneill  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5      1.1  jmcneill  * All rights reserved.
      6      1.1  jmcneill  *
      7      1.1  jmcneill  * This code is derived from software contributed to The NetBSD Foundation
      8      1.1  jmcneill  * by Nick Hudson
      9      1.1  jmcneill  *
     10      1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
     11      1.1  jmcneill  * modification, are permitted provided that the following conditions
     12      1.1  jmcneill  * are met:
     13      1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     14      1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     15      1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     17      1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     18      1.1  jmcneill  *
     19      1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20      1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21      1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22      1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23      1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24      1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25      1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26      1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27      1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28      1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29      1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     30      1.1  jmcneill  */
     31      1.1  jmcneill 
     32      1.1  jmcneill #include <sys/cdefs.h>
     33  1.2.4.1  christos __KERNEL_RCSID(0, "$NetBSD: dwc2_fdt.c,v 1.2.4.1 2019/06/10 22:07:07 christos Exp $");
     34      1.1  jmcneill 
     35      1.1  jmcneill #include <sys/param.h>
     36      1.1  jmcneill #include <sys/systm.h>
     37      1.1  jmcneill #include <sys/device.h>
     38      1.1  jmcneill #include <sys/mutex.h>
     39      1.1  jmcneill #include <sys/bus.h>
     40      1.1  jmcneill #include <sys/workqueue.h>
     41      1.1  jmcneill 
     42      1.1  jmcneill #include <dev/fdt/fdtvar.h>
     43      1.1  jmcneill 
     44      1.1  jmcneill #include <dev/usb/usb.h>
     45      1.1  jmcneill #include <dev/usb/usbdi.h>
     46      1.1  jmcneill #include <dev/usb/usbdivar.h>
     47      1.1  jmcneill #include <dev/usb/usb_mem.h>
     48      1.1  jmcneill 
     49      1.1  jmcneill #include <dwc2/dwc2var.h>
     50      1.1  jmcneill 
     51      1.1  jmcneill #include <dwc2/dwc2.h>
     52      1.1  jmcneill #include "dwc2_core.h"
     53      1.1  jmcneill 
     54      1.1  jmcneill struct dwc2_fdt_softc {
     55      1.1  jmcneill 	struct dwc2_softc	sc_dwc2;
     56      1.1  jmcneill 
     57      1.1  jmcneill 	struct dwc2_core_params	sc_params;
     58      1.1  jmcneill 
     59      1.1  jmcneill 	void			*sc_ih;
     60      1.1  jmcneill 	int			sc_phandle;
     61      1.1  jmcneill };
     62      1.1  jmcneill 
     63      1.1  jmcneill static int dwc2_fdt_match(device_t, struct cfdata *, void *);
     64      1.1  jmcneill static void dwc2_fdt_attach(device_t, device_t, void *);
     65      1.1  jmcneill static void dwc2_fdt_deferred(device_t);
     66      1.1  jmcneill 
     67  1.2.4.1  christos static void dwc2_fdt_amlogic_params(struct dwc2_fdt_softc *, struct dwc2_core_params *);
     68      1.1  jmcneill static void dwc2_fdt_rockchip_params(struct dwc2_fdt_softc *, struct dwc2_core_params *);
     69      1.1  jmcneill 
     70      1.1  jmcneill struct dwc2_fdt_config {
     71      1.1  jmcneill 	void	(*params)(struct dwc2_fdt_softc *, struct dwc2_core_params *);
     72      1.1  jmcneill };
     73      1.1  jmcneill 
     74      1.1  jmcneill static const struct dwc2_fdt_config dwc2_fdt_rk3066_config = {
     75      1.1  jmcneill 	.params = dwc2_fdt_rockchip_params,
     76      1.1  jmcneill };
     77      1.1  jmcneill 
     78  1.2.4.1  christos static const struct dwc2_fdt_config dwc2_fdt_meson8b_config = {
     79  1.2.4.1  christos 	.params = dwc2_fdt_amlogic_params,
     80  1.2.4.1  christos };
     81  1.2.4.1  christos 
     82      1.2  jmcneill static const struct dwc2_fdt_config dwc2_fdt_generic_config = {
     83      1.2  jmcneill };
     84      1.2  jmcneill 
     85      1.1  jmcneill static const struct of_compat_data compat_data[] = {
     86  1.2.4.1  christos 	{ "amlogic,meson8b-usb",	(uintptr_t)&dwc2_fdt_meson8b_config },
     87  1.2.4.1  christos 	{ "amlogic,meson-gxbb-usb",	(uintptr_t)&dwc2_fdt_meson8b_config },
     88      1.1  jmcneill 	{ "rockchip,rk3066-usb",	(uintptr_t)&dwc2_fdt_rk3066_config },
     89      1.2  jmcneill 	{ "snps,dwc2",			(uintptr_t)&dwc2_fdt_generic_config },
     90      1.1  jmcneill 	{ NULL }
     91      1.1  jmcneill };
     92      1.1  jmcneill 
     93      1.1  jmcneill CFATTACH_DECL_NEW(dwc2_fdt, sizeof(struct dwc2_fdt_softc),
     94      1.1  jmcneill     dwc2_fdt_match, dwc2_fdt_attach, NULL, NULL);
     95      1.1  jmcneill 
     96      1.1  jmcneill /* ARGSUSED */
     97      1.1  jmcneill static int
     98      1.1  jmcneill dwc2_fdt_match(device_t parent, struct cfdata *match, void *aux)
     99      1.1  jmcneill {
    100      1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    101      1.1  jmcneill 
    102      1.1  jmcneill 	return of_match_compat_data(faa->faa_phandle, compat_data);
    103      1.1  jmcneill }
    104      1.1  jmcneill 
    105      1.1  jmcneill /* ARGSUSED */
    106      1.1  jmcneill static void
    107      1.1  jmcneill dwc2_fdt_attach(device_t parent, device_t self, void *aux)
    108      1.1  jmcneill {
    109      1.1  jmcneill 	struct dwc2_fdt_softc *sc = device_private(self);
    110      1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    111      1.1  jmcneill 	const int phandle = faa->faa_phandle;
    112      1.1  jmcneill 	const struct dwc2_fdt_config *conf =
    113      1.1  jmcneill 	    (void *)of_search_compatible(phandle, compat_data)->data;
    114      1.1  jmcneill 	char intrstr[128];
    115      1.1  jmcneill 	struct fdtbus_phy *phy;
    116      1.1  jmcneill 	struct clk *clk;
    117      1.1  jmcneill 	bus_addr_t addr;
    118      1.1  jmcneill 	bus_size_t size;
    119      1.1  jmcneill 	int error;
    120      1.1  jmcneill 
    121      1.1  jmcneill 	const char *dr_mode = fdtbus_get_string(phandle, "dr_mode");
    122      1.1  jmcneill 	if (dr_mode == NULL || strcmp(dr_mode, "host") != 0) {
    123      1.1  jmcneill 		aprint_error(": mode '%s' not supported\n", dr_mode);
    124      1.1  jmcneill 		return;
    125      1.1  jmcneill 	}
    126      1.1  jmcneill 
    127      1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    128      1.1  jmcneill 		aprint_error(": couldn't get registers\n");
    129      1.1  jmcneill 		return;
    130      1.1  jmcneill 	}
    131      1.1  jmcneill 
    132      1.1  jmcneill 	clk = fdtbus_clock_get(phandle, "otg");
    133      1.1  jmcneill 	if (clk == NULL || clk_enable(clk) != 0) {
    134      1.1  jmcneill 		aprint_error(": couldn't enable otg clock\n");
    135      1.1  jmcneill 		return;
    136      1.1  jmcneill 	}
    137      1.1  jmcneill 
    138      1.1  jmcneill 	/* Enable optional phy */
    139      1.1  jmcneill 	phy = fdtbus_phy_get(phandle, "usb2-phy");
    140      1.1  jmcneill 	if (phy && fdtbus_phy_enable(phy, true) != 0) {
    141      1.1  jmcneill 		aprint_error(": couldn't enable phy\n");
    142      1.1  jmcneill 		return;
    143      1.1  jmcneill 	}
    144      1.1  jmcneill 
    145      1.1  jmcneill 	sc->sc_phandle = phandle;
    146      1.1  jmcneill 	sc->sc_dwc2.sc_dev = self;
    147      1.1  jmcneill 	sc->sc_dwc2.sc_iot = faa->faa_bst;
    148      1.1  jmcneill 	sc->sc_dwc2.sc_bus.ub_dmatag = faa->faa_dmat;
    149      1.1  jmcneill 
    150      1.1  jmcneill 	error = bus_space_map(faa->faa_bst, addr, size, 0, &sc->sc_dwc2.sc_ioh);
    151      1.1  jmcneill 	if (error) {
    152      1.1  jmcneill 		aprint_error(": couldn't map device\n");
    153      1.1  jmcneill 		return;
    154      1.1  jmcneill 	}
    155      1.1  jmcneill 
    156      1.1  jmcneill 	if (conf->params) {
    157      1.1  jmcneill 		conf->params(sc, &sc->sc_params);
    158      1.1  jmcneill 		sc->sc_dwc2.sc_params = &sc->sc_params;
    159      1.1  jmcneill 	}
    160      1.1  jmcneill 
    161      1.1  jmcneill 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    162      1.1  jmcneill 		aprint_error(": failed to decode interrupt\n");
    163      1.1  jmcneill 		return;
    164      1.1  jmcneill 	}
    165      1.1  jmcneill 
    166      1.1  jmcneill 	aprint_naive("\n");
    167      1.1  jmcneill 	aprint_normal(": DesignWare USB2 OTG\n");
    168      1.1  jmcneill 
    169      1.1  jmcneill 	sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_VM, FDT_INTR_MPSAFE,
    170      1.1  jmcneill 	    dwc2_intr, &sc->sc_dwc2);
    171      1.1  jmcneill 
    172      1.1  jmcneill 	if (sc->sc_ih == NULL) {
    173      1.1  jmcneill 		aprint_error_dev(self, "failed to establish interrupt %s\n",
    174      1.1  jmcneill 		    intrstr);
    175      1.1  jmcneill 		goto fail;
    176      1.1  jmcneill 	}
    177      1.1  jmcneill 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    178      1.1  jmcneill 	config_interrupts(self, dwc2_fdt_deferred);
    179      1.1  jmcneill 
    180      1.1  jmcneill 	return;
    181      1.1  jmcneill 
    182      1.1  jmcneill fail:
    183      1.1  jmcneill 	if (sc->sc_ih) {
    184      1.1  jmcneill 		fdtbus_intr_disestablish(sc->sc_phandle, sc->sc_ih);
    185      1.1  jmcneill 		sc->sc_ih = NULL;
    186      1.1  jmcneill 	}
    187      1.1  jmcneill 	bus_space_unmap(sc->sc_dwc2.sc_iot, sc->sc_dwc2.sc_ioh, size);
    188      1.1  jmcneill }
    189      1.1  jmcneill 
    190      1.1  jmcneill static void
    191      1.1  jmcneill dwc2_fdt_deferred(device_t self)
    192      1.1  jmcneill {
    193      1.1  jmcneill 	struct dwc2_fdt_softc *sc = device_private(self);
    194      1.1  jmcneill 	int error;
    195      1.1  jmcneill 
    196      1.1  jmcneill 	error = dwc2_init(&sc->sc_dwc2);
    197      1.1  jmcneill 	if (error != 0) {
    198      1.1  jmcneill 		aprint_error_dev(self, "couldn't initialize host, error=%d\n",
    199      1.1  jmcneill 		    error);
    200      1.1  jmcneill 		return;
    201      1.1  jmcneill 	}
    202      1.1  jmcneill 	sc->sc_dwc2.sc_child = config_found(sc->sc_dwc2.sc_dev,
    203      1.1  jmcneill 	    &sc->sc_dwc2.sc_bus, usbctlprint);
    204      1.1  jmcneill }
    205      1.1  jmcneill 
    206      1.1  jmcneill static void
    207  1.2.4.1  christos dwc2_fdt_amlogic_params(struct dwc2_fdt_softc *sc, struct dwc2_core_params *params)
    208  1.2.4.1  christos {
    209  1.2.4.1  christos 	dwc2_set_all_params(params, -1);
    210  1.2.4.1  christos 
    211  1.2.4.1  christos 	params->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
    212  1.2.4.1  christos 	params->speed = DWC2_SPEED_PARAM_HIGH;
    213  1.2.4.1  christos 	params->dma_enable = 1;
    214  1.2.4.1  christos 	params->enable_dynamic_fifo = 1,
    215  1.2.4.1  christos 	params->host_rx_fifo_size = 512;
    216  1.2.4.1  christos 	params->host_nperio_tx_fifo_size = 500;
    217  1.2.4.1  christos 	params->host_perio_tx_fifo_size = 500;
    218  1.2.4.1  christos 	params->host_channels = 16;
    219  1.2.4.1  christos 	params->phy_type = DWC2_PHY_TYPE_PARAM_UTMI;
    220  1.2.4.1  christos 	params->reload_ctl = 1,
    221  1.2.4.1  christos 	params->ahbcfg = GAHBCFG_HBSTLEN_INCR8 << GAHBCFG_HBSTLEN_SHIFT;
    222  1.2.4.1  christos #ifdef DWC2_POWER_DOWN_PARAM_NONE
    223  1.2.4.1  christos 	params->power_down = DWC2_POWER_DOWN_PARAM_NONE;
    224  1.2.4.1  christos #endif
    225  1.2.4.1  christos }
    226  1.2.4.1  christos 
    227  1.2.4.1  christos static void
    228      1.1  jmcneill dwc2_fdt_rockchip_params(struct dwc2_fdt_softc *sc, struct dwc2_core_params *params)
    229      1.1  jmcneill {
    230      1.1  jmcneill 	dwc2_set_all_params(params, -1);
    231      1.1  jmcneill 
    232      1.1  jmcneill 	params->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
    233      1.1  jmcneill 	params->host_rx_fifo_size = 525;
    234      1.1  jmcneill 	params->host_nperio_tx_fifo_size = 128;
    235      1.1  jmcneill 	params->host_perio_tx_fifo_size = 256;
    236      1.1  jmcneill 	params->ahbcfg = GAHBCFG_HBSTLEN_INCR16 << GAHBCFG_HBSTLEN_SHIFT;
    237      1.1  jmcneill }
    238