1 /* $NetBSD: ralink_ehci.c,v 1.11 2025/03/31 14:45:57 riastradh Exp $ */ 2 /*- 3 * Copyright (c) 2011 CradlePoint Technology, Inc. 4 * All rights reserved. 5 * 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY CRADLEPOINT TECHNOLOGY, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /* ralink_ehci.c -- Ralink EHCI USB Driver */ 30 31 #include <sys/cdefs.h> 32 __KERNEL_RCSID(0, "$NetBSD: ralink_ehci.c,v 1.11 2025/03/31 14:45:57 riastradh Exp $"); 33 34 #include <sys/param.h> 35 #include <sys/bus.h> 36 37 #include <dev/usb/usb.h> 38 #include <dev/usb/usbdi.h> 39 #include <dev/usb/usbdivar.h> 40 #include <dev/usb/usb_mem.h> 41 42 #include <dev/usb/ehcireg.h> 43 #include <dev/usb/ehcivar.h> 44 45 #include <mips/ralink/ralink_usbhcvar.h> 46 47 #include <mips/ralink/ralink_var.h> 48 #include <mips/ralink/ralink_reg.h> 49 50 struct ralink_ehci_softc { 51 struct ehci_softc sc_ehci; 52 void *sc_ih; 53 }; 54 55 static int ralink_ehci_match(device_t, cfdata_t, void *); 56 static void ralink_ehci_attach(device_t, device_t, void *); 57 static int ralink_ehci_detach(device_t, int); 58 59 CFATTACH_DECL2_NEW(ralink_ehci, sizeof(struct ralink_ehci_softc), 60 ralink_ehci_match, ralink_ehci_attach, ralink_ehci_detach, 61 ehci_activate, NULL, ehci_childdet); 62 63 static TAILQ_HEAD(, ralink_usb_hc) ralink_usb_alldevs = 64 TAILQ_HEAD_INITIALIZER(ralink_usb_alldevs); 65 66 /* 67 * ralink_ehci_match 68 */ 69 static int 70 ralink_ehci_match(device_t parent, cfdata_t cf, void *aux) 71 { 72 73 return 1; 74 } 75 76 /* 77 * ralink_ehci_attach 78 */ 79 static void 80 ralink_ehci_attach(device_t parent, device_t self, void *aux) 81 { 82 struct ralink_ehci_softc * const sc = device_private(self); 83 const struct mainbus_attach_args *ma = aux; 84 struct ralink_usb_hc *ruh; 85 int error; 86 87 aprint_naive(": EHCI USB controller\n"); 88 aprint_normal(": EHCI USB controller\n"); 89 90 sc->sc_ehci.sc_dev = self; 91 sc->sc_ehci.sc_bus.ub_hcpriv = sc; 92 sc->sc_ehci.iot = ma->ma_memt; 93 sc->sc_ehci.sc_bus.ub_dmatag = ma->ma_dmat; 94 95 /* Map EHCI registers */ 96 if ((error = bus_space_map(sc->sc_ehci.iot, RA_USB_EHCI_BASE, 97 RA_USB_BLOCK_SIZE, 0, &sc->sc_ehci.ioh)) != 0) { 98 aprint_error_dev(self, "can't map EHCI registers, " 99 "error=%d\n", error); 100 return; 101 } 102 103 sc->sc_ehci.sc_size = RA_USB_BLOCK_SIZE; 104 sc->sc_ehci.sc_bus.ub_revision = USBREV_2_0; 105 106 #if defined(RALINK_EHCI_DEBUG) 107 aprint_normal_dev(self, "sc %p ma %p\n", sc, ma); 108 aprint_normal_dev(self, "memt %p dmat %p\n", ma->ma_memt, ma->ma_dmat); 109 aprint_normal_dev(self, "EHCI HCCAPBASE=%#x\n", 110 EREAD4(&sc->sc_ehci, EHCI_CAPLENGTH)); 111 aprint_normal_dev(self, "EHCI HCSPARAMS=%#x\n", 112 EREAD4(&sc->sc_ehci, EHCI_HCSPARAMS)); 113 aprint_normal_dev(self, "EHCI HCCPARAMS=%#x\n", 114 EREAD4(&sc->sc_ehci, EHCI_HCCPARAMS)); 115 aprint_normal_dev(self, "EHCI HCSP_PORTROUTE=%#x\n", 116 EREAD4(&sc->sc_ehci, EHCI_HCSP_PORTROUTE)); 117 #endif 118 119 /* Disable EHCI interrupts. */ 120 sc->sc_ehci.sc_offs = EREAD1(&sc->sc_ehci, EHCI_CAPLENGTH); 121 EOWRITE4(&sc->sc_ehci, EHCI_USBINTR, 0); 122 123 #if defined(RALINK_EHCI_DEBUG) 124 aprint_normal_dev(self, "EHCI USBCMD=%#x USBSTS=%#x USBINTR=%#x\n", 125 EOREAD4(&sc->sc_ehci, EHCI_USBCMD), 126 EOREAD4(&sc->sc_ehci, EHCI_USBSTS), 127 EOREAD4(&sc->sc_ehci, EHCI_USBINTR)); 128 #endif 129 130 /* Establish the MIPS level interrupt */ 131 sc->sc_ih = ra_intr_establish(RA_IRQ_USB, ehci_intr, sc, 1); 132 if (sc->sc_ih == NULL) { 133 aprint_error_dev(self, "unable to establish irq %d\n", 134 RA_IRQ_USB); 135 goto fail_0; 136 } 137 138 /* 139 * Find companion controllers. According to the spec they always 140 * have lower function numbers so they should be enumerated already. 141 */ 142 int ncomp = 0; 143 KASSERT(KERNEL_LOCKED_P()); /* XXXSMP ralink_usb_alldevs */ 144 TAILQ_FOREACH(ruh, &ralink_usb_alldevs, next) { 145 aprint_normal_dev(self, "companion %s\n", 146 device_xname(ruh->usb)); 147 sc->sc_ehci.sc_comps[ncomp++] = ruh->usb; 148 if (ncomp >= EHCI_COMPANION_MAX) 149 break; 150 } 151 sc->sc_ehci.sc_ncomp = ncomp; 152 153 /* Initialize EHCI */ 154 int err = ehci_init(&sc->sc_ehci); 155 if (err) { 156 aprint_error_dev(self, "init failed, error=%d\n", err); 157 goto fail_1; 158 } 159 160 if (!pmf_device_register1(self, ehci_suspend, ehci_resume, 161 ehci_shutdown)) 162 aprint_error_dev(self, "couldn't establish power handler\n"); 163 164 /* Attach usb device. */ 165 sc->sc_ehci.sc_child = config_found(self, &sc->sc_ehci.sc_bus, 166 usbctlprint, CFARGS_NONE); 167 168 return; 169 170 fail_1: 171 ra_intr_disestablish(sc->sc_ih); 172 sc->sc_ih = NULL; 173 fail_0: 174 bus_space_unmap(sc->sc_ehci.iot, sc->sc_ehci.ioh, sc->sc_ehci.sc_size); 175 sc->sc_ehci.sc_size = 0; 176 } 177 178 static int 179 ralink_ehci_detach(device_t self, int flags) 180 { 181 struct ralink_ehci_softc * const sc = device_private(self); 182 int rv; 183 184 pmf_device_deregister(self); 185 186 rv = ehci_detach(&sc->sc_ehci, flags); 187 if (rv) 188 return rv; 189 190 if (sc->sc_ih != NULL) { 191 ra_intr_disestablish(sc->sc_ih); 192 sc->sc_ih = NULL; 193 } 194 195 if (sc->sc_ehci.sc_size != 0) { 196 bus_space_unmap(sc->sc_ehci.iot, sc->sc_ehci.ioh, 197 sc->sc_ehci.sc_size); 198 sc->sc_ehci.sc_size = 0; 199 } 200 201 return 0; 202 } 203 204 void 205 ralink_usb_hc_add(struct ralink_usb_hc *ruh, device_t usbp) 206 { 207 208 KASSERT(KERNEL_LOCKED_P()); /* XXXSMP ralink_usb_alldevs */ 209 210 TAILQ_INSERT_TAIL(&ralink_usb_alldevs, ruh, next); 211 ruh->usb = usbp; 212 } 213 214 void 215 ralink_usb_hc_rem(struct ralink_usb_hc *ruh) 216 { 217 218 KASSERT(KERNEL_LOCKED_P()); /* XXXSMP ralink_usb_alldevs */ 219 220 if (ruh->usb) 221 TAILQ_REMOVE(&ralink_usb_alldevs, ruh, next); 222 } 223