Home | History | Annotate | Line # | Download | only in broadcom
      1  1.12  thorpej /*	$NetBSD: bcm2835_dwctwo.c,v 1.12 2021/08/07 16:18:43 thorpej Exp $	*/
      2   1.1    skrll 
      3   1.1    skrll /*-
      4   1.1    skrll  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5   1.1    skrll  * All rights reserved.
      6   1.1    skrll  *
      7   1.1    skrll  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1    skrll  * by Nick Hudson
      9   1.1    skrll  *
     10   1.1    skrll  * Redistribution and use in source and binary forms, with or without
     11   1.1    skrll  * modification, are permitted provided that the following conditions
     12   1.1    skrll  * are met:
     13   1.1    skrll  * 1. Redistributions of source code must retain the above copyright
     14   1.1    skrll  *    notice, this list of conditions and the following disclaimer.
     15   1.1    skrll  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1    skrll  *    notice, this list of conditions and the following disclaimer in the
     17   1.1    skrll  *    documentation and/or other materials provided with the distribution.
     18   1.1    skrll  *
     19   1.1    skrll  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1    skrll  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1    skrll  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1    skrll  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1    skrll  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1    skrll  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1    skrll  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1    skrll  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1    skrll  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1    skrll  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1    skrll  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1    skrll  */
     31   1.1    skrll 
     32   1.1    skrll #include <sys/cdefs.h>
     33  1.12  thorpej __KERNEL_RCSID(0, "$NetBSD: bcm2835_dwctwo.c,v 1.12 2021/08/07 16:18:43 thorpej Exp $");
     34   1.1    skrll 
     35   1.1    skrll #include <sys/param.h>
     36   1.1    skrll #include <sys/systm.h>
     37   1.1    skrll #include <sys/device.h>
     38   1.1    skrll #include <sys/mutex.h>
     39   1.1    skrll #include <sys/bus.h>
     40   1.1    skrll #include <sys/workqueue.h>
     41   1.1    skrll 
     42   1.1    skrll #include <arm/broadcom/bcm2835reg.h>
     43   1.8    skrll 
     44   1.8    skrll #include <dev/fdt/fdtvar.h>
     45   1.1    skrll 
     46   1.1    skrll #include <dev/usb/usb.h>
     47   1.1    skrll #include <dev/usb/usbdi.h>
     48   1.1    skrll #include <dev/usb/usbdivar.h>
     49   1.1    skrll #include <dev/usb/usb_mem.h>
     50   1.1    skrll 
     51   1.1    skrll #include <dwc2/dwc2var.h>
     52   1.1    skrll 
     53   1.1    skrll #include <dwc2/dwc2.h>
     54   1.1    skrll #include "dwc2_core.h"
     55   1.1    skrll 
     56   1.1    skrll struct bcmdwc2_softc {
     57   1.1    skrll 	struct dwc2_softc	sc_dwc2;
     58   1.1    skrll 
     59   1.1    skrll 	void			*sc_ih;
     60   1.8    skrll 	int			sc_phandle;
     61   1.1    skrll };
     62   1.1    skrll 
     63   1.1    skrll static struct dwc2_core_params bcmdwc2_params = {
     64   1.1    skrll 	.otg_cap			= 0,	/* HNP/SRP capable */
     65   1.1    skrll 	.otg_ver			= 0,	/* 1.3 */
     66   1.1    skrll 	.dma_enable			= 1,
     67   1.1    skrll 	.dma_desc_enable		= 0,
     68   1.1    skrll 	.speed				= 0,	/* High Speed */
     69   1.1    skrll 	.enable_dynamic_fifo		= 1,
     70   1.1    skrll 	.en_multiple_tx_fifo		= 1,
     71   1.1    skrll 	.host_rx_fifo_size		= 774,	/* 774 DWORDs */
     72   1.1    skrll 	.host_nperio_tx_fifo_size	= 256,	/* 256 DWORDs */
     73   1.1    skrll 	.host_perio_tx_fifo_size	= 512,	/* 512 DWORDs */
     74   1.1    skrll 	.max_transfer_size		= 65535,
     75   1.1    skrll 	.max_packet_count		= 511,
     76   1.1    skrll 	.host_channels			= 8,
     77   1.1    skrll 	.phy_type			= 1,	/* UTMI */
     78   1.1    skrll 	.phy_utmi_width			= 8,	/* 8 bits */
     79   1.1    skrll 	.phy_ulpi_ddr			= 0,	/* Single */
     80   1.1    skrll 	.phy_ulpi_ext_vbus		= 0,
     81   1.1    skrll 	.i2c_enable			= 0,
     82   1.1    skrll 	.ulpi_fs_ls			= 0,
     83   1.1    skrll 	.host_support_fs_ls_low_power	= 0,
     84   1.1    skrll 	.host_ls_low_power_phy_clk	= 0,	/* 48 MHz */
     85   1.1    skrll 	.ts_dline			= 0,
     86   1.1    skrll 	.reload_ctl			= 0,
     87   1.1    skrll 	.ahbcfg				= 0x10,
     88   1.1    skrll 	.uframe_sched			= 1,
     89   1.6    skrll 	.external_id_pin_ctl		= -1,
     90   1.6    skrll 	.hibernation			= -1,
     91   1.1    skrll };
     92   1.1    skrll 
     93   1.1    skrll static int bcmdwc2_match(device_t, struct cfdata *, void *);
     94   1.1    skrll static void bcmdwc2_attach(device_t, device_t, void *);
     95   1.1    skrll static void bcmdwc2_deferred(device_t);
     96   1.1    skrll 
     97   1.1    skrll CFATTACH_DECL_NEW(bcmdwctwo, sizeof(struct bcmdwc2_softc),
     98   1.1    skrll     bcmdwc2_match, bcmdwc2_attach, NULL, NULL);
     99   1.1    skrll 
    100   1.9  thorpej static const struct device_compatible_entry compat_data[] = {
    101   1.9  thorpej 	{ .compat = "brcm,bcm2708-usb" },
    102   1.9  thorpej 	{ .compat = "brcm,bcm2835-usb" },
    103   1.9  thorpej 	DEVICE_COMPAT_EOL
    104   1.9  thorpej };
    105   1.9  thorpej 
    106   1.1    skrll /* ARGSUSED */
    107   1.1    skrll static int
    108   1.1    skrll bcmdwc2_match(device_t parent, struct cfdata *match, void *aux)
    109   1.1    skrll {
    110   1.8    skrll 	struct fdt_attach_args * const faa = aux;
    111   1.1    skrll 
    112   1.9  thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
    113   1.1    skrll }
    114   1.1    skrll 
    115   1.1    skrll /* ARGSUSED */
    116   1.1    skrll static void
    117   1.1    skrll bcmdwc2_attach(device_t parent, device_t self, void *aux)
    118   1.1    skrll {
    119   1.1    skrll 	struct bcmdwc2_softc *sc = device_private(self);
    120   1.8    skrll 	struct fdt_attach_args * const faa = aux;
    121   1.8    skrll 	const int phandle = faa->faa_phandle;
    122   1.8    skrll 	bus_addr_t addr;
    123   1.8    skrll 	bus_size_t size;
    124   1.1    skrll 	int error;
    125   1.1    skrll 
    126   1.8    skrll 	aprint_naive(": USB controller\n");
    127   1.8    skrll 	aprint_normal(": USB controller\n");
    128   1.8    skrll 
    129   1.8    skrll 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    130   1.8    skrll 		aprint_error(": couldn't get registers\n");
    131   1.8    skrll 		return;
    132   1.8    skrll 	}
    133   1.8    skrll 
    134   1.8    skrll 	sc->sc_phandle = phandle;
    135   1.1    skrll 	sc->sc_dwc2.sc_dev = self;
    136   1.8    skrll 	sc->sc_dwc2.sc_iot = faa->faa_bst;
    137   1.8    skrll 	sc->sc_dwc2.sc_bus.ub_dmatag = faa->faa_dmat;
    138   1.1    skrll 	sc->sc_dwc2.sc_params = &bcmdwc2_params;
    139   1.1    skrll 
    140   1.8    skrll 	error = bus_space_map(faa->faa_bst, addr, size, 0, &sc->sc_dwc2.sc_ioh);
    141   1.1    skrll 	if (error) {
    142   1.8    skrll 		aprint_error(": couldn't map device\n");
    143   1.1    skrll 		return;
    144   1.1    skrll 	}
    145   1.1    skrll 
    146   1.8    skrll 	char intrstr[128];
    147   1.8    skrll 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    148   1.8    skrll 		aprint_error(": failed to decode interrupt\n");
    149   1.8    skrll 		return;
    150   1.8    skrll 	}
    151   1.1    skrll 
    152  1.10    skrll 	sc->sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_VM, FDT_INTR_MPSAFE,
    153  1.10    skrll 	    dwc2_intr, &sc->sc_dwc2, device_xname(self));
    154   1.1    skrll 
    155   1.1    skrll 	if (sc->sc_ih == NULL) {
    156   1.8    skrll 		aprint_error_dev(self, "failed to establish interrupt %s\n",
    157   1.8    skrll 		    intrstr);
    158   1.1    skrll 		goto fail;
    159   1.1    skrll 	}
    160   1.8    skrll 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    161   1.8    skrll 	config_interrupts(self, bcmdwc2_deferred);
    162   1.1    skrll 
    163   1.1    skrll 	return;
    164   1.1    skrll 
    165   1.1    skrll fail:
    166   1.1    skrll 	if (sc->sc_ih) {
    167   1.8    skrll 		fdtbus_intr_disestablish(sc->sc_phandle, sc->sc_ih);
    168   1.1    skrll 		sc->sc_ih = NULL;
    169   1.1    skrll 	}
    170   1.8    skrll 	bus_space_unmap(sc->sc_dwc2.sc_iot, sc->sc_dwc2.sc_ioh, size);
    171   1.1    skrll }
    172   1.1    skrll 
    173   1.1    skrll static void
    174   1.1    skrll bcmdwc2_deferred(device_t self)
    175   1.1    skrll {
    176   1.1    skrll 	struct bcmdwc2_softc *sc = device_private(self);
    177   1.1    skrll 	int error;
    178   1.1    skrll 
    179   1.1    skrll 	error = dwc2_init(&sc->sc_dwc2);
    180   1.1    skrll 	if (error != 0) {
    181   1.1    skrll 		aprint_error_dev(self, "couldn't initialize host, error=%d\n",
    182   1.1    skrll 		    error);
    183   1.1    skrll 		return;
    184   1.1    skrll 	}
    185   1.1    skrll 	sc->sc_dwc2.sc_child = config_found(sc->sc_dwc2.sc_dev,
    186  1.12  thorpej 	    &sc->sc_dwc2.sc_bus, usbctlprint, CFARGS_NONE);
    187   1.1    skrll }
    188