1 1.1 matt /*- 2 1.1 matt * Copyright (c) 2012 The NetBSD Foundation, Inc. 3 1.1 matt * All rights reserved. 4 1.1 matt * 5 1.1 matt * This code is derived from software contributed to The NetBSD Foundation 6 1.1 matt * by Matt Thomas of 3am Software Foundry. 7 1.1 matt * 8 1.1 matt * Redistribution and use in source and binary forms, with or without 9 1.1 matt * modification, are permitted provided that the following conditions 10 1.1 matt * are met: 11 1.1 matt * 1. Redistributions of source code must retain the above copyright 12 1.1 matt * notice, this list of conditions and the following disclaimer. 13 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright 14 1.1 matt * notice, this list of conditions and the following disclaimer in the 15 1.1 matt * documentation and/or other materials provided with the distribution. 16 1.1 matt * 17 1.1 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 18 1.1 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19 1.1 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 1.1 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 21 1.1 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 1.1 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 1.1 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 1.1 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 1.1 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 1.1 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 1.1 matt * POSSIBILITY OF SUCH DAMAGE. 28 1.1 matt */ 29 1.3 matt #define USBH_PRIVATE 30 1.1 matt 31 1.1 matt #include "locators.h" 32 1.1 matt 33 1.1 matt #include <sys/cdefs.h> 34 1.1 matt 35 1.11 andvar __KERNEL_RCSID(1, "$NetBSD: bcm53xx_usb.c,v 1.11 2025/07/03 19:09:48 andvar Exp $"); 36 1.1 matt 37 1.5 matt #include <sys/param.h> 38 1.1 matt #include <sys/bus.h> 39 1.1 matt #include <sys/device.h> 40 1.1 matt #include <sys/intr.h> 41 1.1 matt #include <sys/systm.h> 42 1.1 matt 43 1.1 matt #include <arm/broadcom/bcm53xx_reg.h> 44 1.1 matt #include <arm/broadcom/bcm53xx_var.h> 45 1.1 matt 46 1.1 matt #include <dev/usb/usb.h> 47 1.1 matt #include <dev/usb/usbdi.h> 48 1.1 matt #include <dev/usb/usbdivar.h> 49 1.1 matt #include <dev/usb/usb_mem.h> 50 1.1 matt 51 1.1 matt #include <dev/usb/ohcireg.h> 52 1.1 matt #include <dev/usb/ohcivar.h> 53 1.1 matt 54 1.1 matt #include <dev/usb/ehcireg.h> 55 1.1 matt #include <dev/usb/ehcivar.h> 56 1.1 matt 57 1.1 matt struct bcmusb_softc { 58 1.1 matt device_t usbsc_dev; 59 1.1 matt bus_dma_tag_t usbsc_dmat; 60 1.1 matt bus_space_tag_t usbsc_bst; 61 1.1 matt bus_space_handle_t usbsc_ehci_bsh; 62 1.1 matt bus_space_handle_t usbsc_ohci_bsh; 63 1.1 matt 64 1.1 matt device_t usbsc_ohci_dev; 65 1.1 matt device_t usbsc_ehci_dev; 66 1.1 matt void *usbsc_ohci_sc; 67 1.1 matt void *usbsc_ehci_sc; 68 1.1 matt void *usbsc_ih; 69 1.1 matt }; 70 1.1 matt 71 1.1 matt struct bcmusb_attach_args { 72 1.1 matt const char *usbaa_name; 73 1.1 matt bus_dma_tag_t usbaa_dmat; 74 1.1 matt bus_space_tag_t usbaa_bst; 75 1.1 matt bus_space_handle_t usbaa_bsh; 76 1.1 matt bus_size_t usbaa_size; 77 1.1 matt }; 78 1.1 matt 79 1.1 matt #ifdef OHCI_DEBUG 80 1.1 matt #define OHCI_DPRINTF(x) if (ohcidebug) printf x 81 1.1 matt extern int ohcidebug; 82 1.1 matt #else 83 1.1 matt #define OHCI_DPRINTF(x) 84 1.1 matt #endif 85 1.1 matt 86 1.1 matt static int ohci_bcmusb_match(device_t, cfdata_t, void *); 87 1.1 matt static void ohci_bcmusb_attach(device_t, device_t, void *); 88 1.1 matt 89 1.1 matt CFATTACH_DECL_NEW(ohci_bcmusb, sizeof(struct ohci_softc), 90 1.1 matt ohci_bcmusb_match, ohci_bcmusb_attach, NULL, NULL); 91 1.1 matt 92 1.1 matt static int 93 1.1 matt ohci_bcmusb_match(device_t parent, cfdata_t cf, void *aux) 94 1.1 matt { 95 1.1 matt struct bcmusb_attach_args * const usbaa = aux; 96 1.1 matt 97 1.1 matt if (strcmp(cf->cf_name, usbaa->usbaa_name)) 98 1.1 matt return 0; 99 1.1 matt 100 1.1 matt return 1; 101 1.1 matt } 102 1.1 matt 103 1.1 matt static void 104 1.1 matt ohci_bcmusb_attach(device_t parent, device_t self, void *aux) 105 1.1 matt { 106 1.1 matt struct ohci_softc * const sc = device_private(self); 107 1.1 matt struct bcmusb_attach_args * const usbaa = aux; 108 1.1 matt 109 1.1 matt sc->sc_dev = self; 110 1.1 matt 111 1.1 matt sc->iot = usbaa->usbaa_bst; 112 1.1 matt sc->ioh = usbaa->usbaa_bsh; 113 1.1 matt sc->sc_size = usbaa->usbaa_size; 114 1.7 skrll sc->sc_bus.ub_dmatag = usbaa->usbaa_dmat; 115 1.7 skrll sc->sc_bus.ub_hcpriv = sc; 116 1.1 matt 117 1.1 matt aprint_naive(": OHCI USB controller\n"); 118 1.1 matt aprint_normal(": OHCI USB controller\n"); 119 1.1 matt 120 1.1 matt int error = ohci_init(sc); 121 1.7 skrll if (error) { 122 1.1 matt aprint_error_dev(self, "init failed, error=%d\n", error); 123 1.6 skrll return; 124 1.1 matt } 125 1.6 skrll 126 1.6 skrll /* Attach usb device. */ 127 1.10 thorpej sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint, CFARGS_NONE); 128 1.1 matt } 129 1.1 matt 130 1.1 matt #ifdef EHCI_DEBUG 131 1.1 matt #define EHCI_DPRINTF(x) if (ehcidebug) printf x 132 1.1 matt extern int ehcidebug; 133 1.1 matt #else 134 1.1 matt #define EHCI_DPRINTF(x) 135 1.1 matt #endif 136 1.1 matt 137 1.1 matt static int ehci_bcmusb_match(device_t, cfdata_t, void *); 138 1.1 matt static void ehci_bcmusb_attach(device_t, device_t, void *); 139 1.1 matt 140 1.1 matt CFATTACH_DECL_NEW(ehci_bcmusb, sizeof(struct ehci_softc), 141 1.1 matt ehci_bcmusb_match, ehci_bcmusb_attach, NULL, NULL); 142 1.1 matt 143 1.1 matt static int 144 1.1 matt ehci_bcmusb_match(device_t parent, cfdata_t cf, void *aux) 145 1.1 matt { 146 1.1 matt struct bcmusb_attach_args * const usbaa = aux; 147 1.1 matt 148 1.1 matt if (strcmp(cf->cf_name, usbaa->usbaa_name)) 149 1.1 matt return 0; 150 1.1 matt 151 1.1 matt return 1; 152 1.1 matt } 153 1.1 matt 154 1.1 matt static void 155 1.1 matt ehci_bcmusb_attach(device_t parent, device_t self, void *aux) 156 1.1 matt { 157 1.1 matt struct bcmusb_softc * const usbsc = device_private(parent); 158 1.1 matt struct ehci_softc * const sc = device_private(self); 159 1.1 matt struct bcmusb_attach_args * const usbaa = aux; 160 1.1 matt 161 1.1 matt sc->sc_dev = self; 162 1.1 matt 163 1.1 matt sc->iot = usbaa->usbaa_bst; 164 1.1 matt sc->ioh = usbaa->usbaa_bsh; 165 1.1 matt sc->sc_size = usbaa->usbaa_size; 166 1.7 skrll sc->sc_bus.ub_dmatag = usbaa->usbaa_dmat; 167 1.7 skrll sc->sc_bus.ub_hcpriv = sc; 168 1.7 skrll sc->sc_bus.ub_revision = USBREV_2_0; 169 1.1 matt sc->sc_ncomp = 0; 170 1.1 matt if (usbsc->usbsc_ohci_dev != NULL) { 171 1.1 matt sc->sc_comps[sc->sc_ncomp++] = usbsc->usbsc_ohci_dev; 172 1.1 matt } 173 1.1 matt 174 1.1 matt aprint_naive(": EHCI USB controller\n"); 175 1.11 andvar aprint_normal(": EHCI USB controller\n"); 176 1.1 matt 177 1.1 matt int error = ehci_init(sc); 178 1.7 skrll if (error) { 179 1.1 matt aprint_error_dev(self, "init failed, error=%d\n", error); 180 1.6 skrll return; 181 1.1 matt } 182 1.6 skrll /* Attach usb device. */ 183 1.10 thorpej sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint, CFARGS_NONE); 184 1.1 matt } 185 1.1 matt 186 1.1 matt /* 187 1.11 andvar * There's only IRQ shared between both OHCI and EHCI devices. 188 1.1 matt */ 189 1.1 matt static int 190 1.1 matt bcmusb_intr(void *arg) 191 1.1 matt { 192 1.1 matt struct bcmusb_softc * const usbsc = arg; 193 1.1 matt int rv0 = 0, rv1 = 0; 194 1.1 matt 195 1.1 matt if (usbsc->usbsc_ohci_sc) 196 1.1 matt rv0 = ohci_intr(usbsc->usbsc_ohci_sc); 197 1.1 matt 198 1.1 matt if (usbsc->usbsc_ehci_sc) 199 1.1 matt rv1 = ehci_intr(usbsc->usbsc_ehci_sc); 200 1.1 matt 201 1.1 matt return rv0 ? rv0 : rv1; 202 1.1 matt } 203 1.1 matt 204 1.1 matt static int bcmusb_ccb_match(device_t, cfdata_t, void *); 205 1.1 matt static void bcmusb_ccb_attach(device_t, device_t, void *); 206 1.1 matt 207 1.1 matt CFATTACH_DECL_NEW(bcmusb_ccb, sizeof(struct bcmusb_softc), 208 1.1 matt bcmusb_ccb_match, bcmusb_ccb_attach, NULL, NULL); 209 1.1 matt 210 1.1 matt int 211 1.1 matt bcmusb_ccb_match(device_t parent, cfdata_t cf, void *aux) 212 1.1 matt { 213 1.1 matt struct bcmccb_attach_args * const ccbaa = aux; 214 1.1 matt const struct bcm_locators * const loc = &ccbaa->ccbaa_loc; 215 1.1 matt 216 1.1 matt if (strcmp(cf->cf_name, loc->loc_name) != 0) 217 1.1 matt return 0; 218 1.1 matt 219 1.1 matt KASSERT(cf->cf_loc[BCMCCBCF_PORT] == BCMCCBCF_PORT_DEFAULT); 220 1.1 matt 221 1.1 matt return 1; 222 1.1 matt } 223 1.1 matt 224 1.4 matt #define OHCI_OFFSET (OHCI_BASE - EHCI_BASE) 225 1.4 matt 226 1.1 matt void 227 1.1 matt bcmusb_ccb_attach(device_t parent, device_t self, void *aux) 228 1.1 matt { 229 1.1 matt struct bcmusb_softc * const usbsc = device_private(self); 230 1.1 matt const struct bcmccb_attach_args * const ccbaa = aux; 231 1.1 matt const struct bcm_locators * const loc = &ccbaa->ccbaa_loc; 232 1.1 matt 233 1.1 matt usbsc->usbsc_bst = ccbaa->ccbaa_ccb_bst; 234 1.1 matt usbsc->usbsc_dmat = ccbaa->ccbaa_dmat; 235 1.1 matt 236 1.4 matt bus_space_subregion(usbsc->usbsc_bst, ccbaa->ccbaa_ccb_bsh, 237 1.4 matt loc->loc_offset, 0x1000, &usbsc->usbsc_ehci_bsh); 238 1.4 matt bus_space_subregion(usbsc->usbsc_bst, ccbaa->ccbaa_ccb_bsh, 239 1.4 matt loc->loc_offset + OHCI_OFFSET, 0x1000, &usbsc->usbsc_ohci_bsh); 240 1.1 matt 241 1.1 matt /* 242 1.3 matt * Bring the PHYs out of reset. 243 1.3 matt */ 244 1.3 matt bus_space_write_4(usbsc->usbsc_bst, usbsc->usbsc_ehci_bsh, 245 1.3 matt USBH_PHY_CTRL_P0, USBH_PHY_CTRL_INIT); 246 1.3 matt bus_space_write_4(usbsc->usbsc_bst, usbsc->usbsc_ehci_bsh, 247 1.3 matt USBH_PHY_CTRL_P1, USBH_PHY_CTRL_INIT); 248 1.3 matt 249 1.3 matt /* 250 1.1 matt * Disable interrupts 251 1.1 matt */ 252 1.1 matt bus_space_write_4(usbsc->usbsc_bst, usbsc->usbsc_ohci_bsh, 253 1.1 matt OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS); 254 1.1 matt bus_size_t caplength = bus_space_read_1(usbsc->usbsc_bst, 255 1.1 matt usbsc->usbsc_ehci_bsh, EHCI_CAPLENGTH); 256 1.1 matt bus_space_write_4(usbsc->usbsc_bst, usbsc->usbsc_ehci_bsh, 257 1.1 matt caplength + EHCI_USBINTR, 0); 258 1.1 matt 259 1.1 matt aprint_naive("\n"); 260 1.1 matt aprint_normal("\n"); 261 1.1 matt 262 1.1 matt struct bcmusb_attach_args usbaa_ohci = { 263 1.1 matt .usbaa_name = "ohci", 264 1.1 matt .usbaa_dmat = usbsc->usbsc_dmat, 265 1.1 matt .usbaa_bst = usbsc->usbsc_bst, 266 1.1 matt .usbaa_bsh = usbsc->usbsc_ohci_bsh, 267 1.1 matt .usbaa_size = 0x100, 268 1.1 matt }; 269 1.1 matt 270 1.9 thorpej usbsc->usbsc_ohci_dev = config_found(self, &usbaa_ohci, NULL, 271 1.10 thorpej CFARGS_NONE); 272 1.1 matt if (usbsc->usbsc_ohci_dev != NULL) 273 1.1 matt usbsc->usbsc_ohci_sc = device_private(usbsc->usbsc_ohci_dev); 274 1.1 matt 275 1.1 matt struct bcmusb_attach_args usbaa_ehci = { 276 1.1 matt .usbaa_name = "ehci", 277 1.1 matt .usbaa_dmat = usbsc->usbsc_dmat, 278 1.1 matt .usbaa_bst = usbsc->usbsc_bst, 279 1.1 matt .usbaa_bsh = usbsc->usbsc_ehci_bsh, 280 1.1 matt .usbaa_size = 0x100, 281 1.1 matt }; 282 1.1 matt 283 1.9 thorpej usbsc->usbsc_ehci_dev = config_found(self, &usbaa_ehci, NULL, 284 1.10 thorpej CFARGS_NONE); 285 1.1 matt if (usbsc->usbsc_ehci_dev != NULL) 286 1.1 matt usbsc->usbsc_ehci_sc = device_private(usbsc->usbsc_ehci_dev); 287 1.1 matt 288 1.1 matt usbsc->usbsc_ih = intr_establish(loc->loc_intrs[0], IPL_USB, IST_LEVEL, 289 1.1 matt bcmusb_intr, usbsc); 290 1.1 matt if (usbsc->usbsc_ih == NULL) { 291 1.1 matt aprint_error_dev(self, "failed to establish interrupt %d\n", 292 1.1 matt loc->loc_intrs[0]); 293 1.1 matt return; 294 1.1 matt } 295 1.1 matt aprint_normal_dev(self, "interrupting on irq %d\n", loc->loc_intrs[0]); 296 1.1 matt } 297