1 /* $NetBSD: dwc2_fdt.c,v 1.13 2023/07/31 04:59:47 rin 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: dwc2_fdt.c,v 1.13 2023/07/31 04:59:47 rin 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 <dev/fdt/fdtvar.h> 43 44 #include <dev/usb/usb.h> 45 #include <dev/usb/usbdi.h> 46 #include <dev/usb/usbdivar.h> 47 #include <dev/usb/usb_mem.h> 48 49 #include <dwc2/dwc2var.h> 50 51 #include <dwc2/dwc2.h> 52 #include "dwc2_core.h" 53 54 struct dwc2_fdt_softc { 55 struct dwc2_softc sc_dwc2; 56 57 struct dwc2_core_params sc_params; 58 59 void *sc_ih; 60 int sc_phandle; 61 }; 62 63 static int dwc2_fdt_match(device_t, struct cfdata *, void *); 64 static void dwc2_fdt_attach(device_t, device_t, void *); 65 static void dwc2_fdt_deferred(device_t); 66 67 static void dwc2_fdt_amlogic_params(struct dwc2_fdt_softc *, struct dwc2_core_params *); 68 static void dwc2_fdt_rockchip_params(struct dwc2_fdt_softc *, struct dwc2_core_params *); 69 70 struct dwc2_fdt_config { 71 void (*params)(struct dwc2_fdt_softc *, struct dwc2_core_params *); 72 }; 73 74 static const struct dwc2_fdt_config dwc2_fdt_rk3066_config = { 75 .params = dwc2_fdt_rockchip_params, 76 }; 77 78 static const struct dwc2_fdt_config dwc2_fdt_meson8b_config = { 79 .params = dwc2_fdt_amlogic_params, 80 }; 81 82 static const struct dwc2_fdt_config dwc2_fdt_generic_config = { 83 }; 84 85 static const struct device_compatible_entry compat_data[] = { 86 { .compat = "amlogic,meson8b-usb", 87 .data = &dwc2_fdt_meson8b_config }, 88 { .compat = "amlogic,meson-gxbb-usb", 89 .data = &dwc2_fdt_meson8b_config }, 90 { .compat = "rockchip,rk3066-usb", 91 .data = &dwc2_fdt_rk3066_config }, 92 { .compat = "snps,dwc2", 93 .data = &dwc2_fdt_generic_config }, 94 95 DEVICE_COMPAT_EOL 96 }; 97 98 CFATTACH_DECL_NEW(dwc2_fdt, sizeof(struct dwc2_fdt_softc), 99 dwc2_fdt_match, dwc2_fdt_attach, NULL, NULL); 100 101 /* ARGSUSED */ 102 static int 103 dwc2_fdt_match(device_t parent, struct cfdata *match, void *aux) 104 { 105 struct fdt_attach_args * const faa = aux; 106 107 return of_compatible_match(faa->faa_phandle, compat_data); 108 } 109 110 /* ARGSUSED */ 111 static void 112 dwc2_fdt_attach(device_t parent, device_t self, void *aux) 113 { 114 struct dwc2_fdt_softc *sc = device_private(self); 115 struct fdt_attach_args * const faa = aux; 116 const int phandle = faa->faa_phandle; 117 const struct dwc2_fdt_config *conf = 118 of_compatible_lookup(phandle, compat_data)->data; 119 char intrstr[128]; 120 struct fdtbus_phy *phy; 121 struct clk *clk; 122 bus_addr_t addr; 123 bus_size_t size; 124 int error; 125 126 const char *dr_mode = fdtbus_get_string(phandle, "dr_mode"); 127 if (dr_mode == NULL || strcmp(dr_mode, "host") != 0) { 128 aprint_error(": mode '%s' not supported\n", dr_mode); 129 return; 130 } 131 132 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 133 aprint_error(": couldn't get registers\n"); 134 return; 135 } 136 137 clk = fdtbus_clock_get(phandle, "otg"); 138 if (clk == NULL || clk_enable(clk) != 0) { 139 aprint_error(": couldn't enable otg clock\n"); 140 return; 141 } 142 143 /* Enable optional phy */ 144 phy = fdtbus_phy_get(phandle, "usb2-phy"); 145 if (phy && fdtbus_phy_enable(phy, true) != 0) { 146 aprint_error(": couldn't enable phy\n"); 147 return; 148 } 149 150 sc->sc_phandle = phandle; 151 sc->sc_dwc2.sc_dev = self; 152 sc->sc_dwc2.sc_iot = faa->faa_bst; 153 sc->sc_dwc2.sc_bus.ub_dmatag = faa->faa_dmat; 154 155 error = bus_space_map(faa->faa_bst, addr, size, 0, &sc->sc_dwc2.sc_ioh); 156 if (error) { 157 aprint_error(": couldn't map device\n"); 158 return; 159 } 160 161 if (conf->params) { 162 conf->params(sc, &sc->sc_params); 163 sc->sc_dwc2.sc_params = &sc->sc_params; 164 } 165 166 if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) { 167 aprint_error(": failed to decode interrupt\n"); 168 return; 169 } 170 171 aprint_naive("\n"); 172 aprint_normal(": DesignWare USB2 OTG\n"); 173 174 sc->sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_VM, 175 FDT_INTR_MPSAFE, dwc2_intr, &sc->sc_dwc2, device_xname(self)); 176 if (sc->sc_ih == NULL) { 177 aprint_error_dev(self, "failed to establish interrupt %s\n", 178 intrstr); 179 goto fail; 180 } 181 aprint_normal_dev(self, "interrupting on %s\n", intrstr); 182 config_interrupts(self, dwc2_fdt_deferred); 183 184 return; 185 186 fail: 187 if (sc->sc_ih) { 188 fdtbus_intr_disestablish(sc->sc_phandle, sc->sc_ih); 189 sc->sc_ih = NULL; 190 } 191 bus_space_unmap(sc->sc_dwc2.sc_iot, sc->sc_dwc2.sc_ioh, size); 192 } 193 194 static void 195 dwc2_fdt_deferred(device_t self) 196 { 197 struct dwc2_fdt_softc *sc = device_private(self); 198 int error; 199 200 error = dwc2_init(&sc->sc_dwc2); 201 if (error != 0) { 202 aprint_error_dev(self, "couldn't initialize host, error=%d\n", 203 error); 204 return; 205 } 206 sc->sc_dwc2.sc_child = config_found(sc->sc_dwc2.sc_dev, 207 &sc->sc_dwc2.sc_bus, usbctlprint, CFARGS_NONE); 208 } 209 210 static void 211 dwc2_fdt_amlogic_params(struct dwc2_fdt_softc *sc, struct dwc2_core_params *params) 212 { 213 dwc2_set_all_params(params, -1); 214 215 params->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE; 216 params->speed = DWC2_SPEED_PARAM_HIGH; 217 params->dma_enable = 1; 218 params->enable_dynamic_fifo = 1; 219 params->host_rx_fifo_size = 512; 220 params->host_nperio_tx_fifo_size = 500; 221 params->host_perio_tx_fifo_size = 500; 222 params->host_channels = 16; 223 params->phy_type = DWC2_PHY_TYPE_PARAM_UTMI; 224 params->reload_ctl = 1; 225 params->ahbcfg = GAHBCFG_HBSTLEN_INCR8 << GAHBCFG_HBSTLEN_SHIFT; 226 #ifdef DWC2_POWER_DOWN_PARAM_NONE 227 params->power_down = DWC2_POWER_DOWN_PARAM_NONE; 228 #endif 229 } 230 231 static void 232 dwc2_fdt_rockchip_params(struct dwc2_fdt_softc *sc, struct dwc2_core_params *params) 233 { 234 dwc2_set_all_params(params, -1); 235 236 params->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE; 237 params->host_rx_fifo_size = 525; 238 params->host_nperio_tx_fifo_size = 128; 239 params->host_perio_tx_fifo_size = 256; 240 params->ahbcfg = GAHBCFG_HBSTLEN_INCR16 << GAHBCFG_HBSTLEN_SHIFT; 241 } 242