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