1 /* $NetBSD: rmixl_usbi.c,v 1.5 2011/07/01 19:01:31 dyoung Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 1999, 2000, 2002, 2003 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Cliff Neighbors 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: rmixl_usbi.c,v 1.5 2011/07/01 19:01:31 dyoung Exp $"); 34 35 #include "locators.h" 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/device.h> 40 41 #include <sys/bus.h> 42 43 #include <mips/rmi/rmixlreg.h> 44 #include <mips/rmi/rmixlvar.h> 45 #include <mips/rmi/rmixl_intr.h> 46 #include <mips/rmi/rmixl_obiovar.h> 47 #include <mips/rmi/rmixl_usbivar.h> 48 49 #include <dev/usb/usb.h> 50 #include <dev/usb/usbdi.h> 51 #include <dev/usb/usbdivar.h> 52 #include <dev/usb/usb_mem.h> 53 54 /* 55 * USB I/O register byte order is 56 * LITTLE ENDIAN regardless of code model 57 */ 58 #define RMIXL_USBI_GEN_VADDR(o) \ 59 (volatile uint32_t *)MIPS_PHYS_TO_KSEG1( \ 60 rmixl_configuration.rc_io_pbase + RMIXL_IO_DEV_USB_B + (o)) 61 #define RMIXL_USBI_GEN_READ(o) le32toh(*RMIXL_USBI_GEN_VADDR(o)) 62 #define RMIXL_USBI_GEN_WRITE(o,v) *RMIXL_USBI_GEN_VADDR(o) = htole32(v) 63 64 static const char rmixl_usbi_intrnames[RMIXL_UB_INTERRUPT_MAX+1][16] = { 65 "int 0 (ohci0)", 66 "int 1 (ohci1)", 67 "int 2 (ehci)", 68 "int 3 (device)", 69 "int 4 (phy)", 70 "int 5 (force)" 71 }; 72 73 static int rmixl_usbi_match(device_t, cfdata_t, void *); 74 static void rmixl_usbi_attach(device_t, device_t, void *); 75 static int rmixl_usbi_print(void *, const char *); 76 static int rmixl_usbi_search(device_t, cfdata_t, const int *, void *); 77 static int rmixl_usbi_intr(void *); 78 79 #ifdef RMIXL_USBI_DEBUG 80 int rmixl_usbi_rdump(void); 81 rmixl_usbi_softc_t *rmixl_usbi_sc; 82 #endif 83 84 85 CFATTACH_DECL_NEW(rmixl_usbi, sizeof (rmixl_usbi_softc_t), 86 rmixl_usbi_match, rmixl_usbi_attach, NULL, NULL); 87 88 int 89 rmixl_usbi_match(device_t parent, cfdata_t match, void *aux) 90 { 91 struct obio_attach_args *obio = aux; 92 93 if (obio->obio_addr == RMIXL_IO_DEV_USB_B) 94 return rmixl_probe_4((volatile uint32_t *)RMIXL_IOREG_VADDR(obio->obio_addr)); 95 96 return 0; 97 } 98 99 void 100 rmixl_usbi_attach(device_t parent, device_t self, void *aux) 101 { 102 rmixl_usbi_softc_t *sc = device_private(self); 103 struct obio_attach_args *obio = aux; 104 uint32_t r; 105 void *ih; 106 107 #ifdef RMIXL_USBI_DEBUG 108 rmixl_usbi_sc = sc; 109 #endif 110 sc->sc_dev = self; 111 sc->sc_eb_bst = obio->obio_eb_bst; 112 sc->sc_el_bst = obio->obio_el_bst; 113 sc->sc_addr = obio->obio_addr; 114 sc->sc_size = obio->obio_size; 115 sc->sc_dmat = obio->obio_32bit_dmat; 116 117 aprint_normal("\n%s", device_xname(self)); 118 119 /* 120 * fail attach if USB interface is disabled GPIO LOW_PWR_DIS reg 121 */ 122 r = RMIXL_IOREG_READ(RMIXL_IO_DEV_GPIO + RMIXL_GPIO_LOW_PWR_DIS); 123 if ((r & RMIXL_GPIO_LOW_PWR_DIS_USB) != 0) { 124 aprint_error(": USB_DIS set in LOW_PWR_DIS, abort attach\n"); 125 return; 126 } 127 128 /* 129 * fail attach if USB interface BIST failed 130 */ 131 r = RMIXL_IOREG_READ(RMIXL_IO_DEV_GPIO + RMIXL_GPIO_BIST_EACH_STS); 132 aprint_normal(": BIST status=%s,", 133 (r & __BIT(18)) ? "OK" : "FAIL"); /* XXX USB_BIST */ 134 135 /* 136 * set BYTESWAP_EN register nonzero when software is little endian 137 */ 138 #if BYTE_ORDER == BIG_ENDIAN 139 r = 0; 140 #else 141 r = 1; 142 #endif 143 RMIXL_USBI_GEN_WRITE(RMIXL_USB_BYTESWAP_EN, r); 144 aprint_normal(" byteswap enable=%d", r); 145 for (int intr=0; intr <= RMIXL_UB_INTERRUPT_MAX; intr++) { 146 evcnt_attach_dynamic(&sc->sc_dispatch[intr].count, 147 EVCNT_TYPE_INTR, NULL, "rmixl_usbi", 148 rmixl_usbi_intrnames[intr]); 149 } 150 151 /* Disable all usb interface interrupts */ 152 RMIXL_USBI_GEN_WRITE(RMIXL_USB_INTERRUPT_ENABLE, 0); 153 154 /* establish interrupt */ 155 if (obio->obio_intr != OBIOCF_INTR_DEFAULT) { 156 ih = rmixl_intr_establish(obio->obio_intr, obio->obio_tmsk, 157 IPL_USB, RMIXL_TRIG_LEVEL, RMIXL_POLR_HIGH, 158 rmixl_usbi_intr, sc, false); 159 if (ih == NULL) 160 panic("%s: couldn't establish interrupt", 161 device_xname(self)); 162 } 163 164 aprint_normal("\n"); 165 166 /* attach any children */ 167 config_search_ia(rmixl_usbi_search, self, "rmixl_usbi", NULL); 168 } 169 170 static int 171 rmixl_usbi_print(void *aux, const char *pnp) 172 { 173 struct rmixl_usbi_attach_args *usbi = aux; 174 175 if (usbi->usbi_addr != RMIXL_USBICF_ADDR_DEFAULT) { 176 aprint_normal(" addr %#"PRIxBUSADDR, usbi->usbi_addr); 177 if (usbi->usbi_size != RMIXL_USBICF_SIZE_DEFAULT) 178 aprint_normal("-%#"PRIxBUSADDR, 179 usbi->usbi_addr + (usbi->usbi_size - 1)); 180 } 181 if (usbi->usbi_intr != RMIXL_USBICF_INTR_DEFAULT) 182 aprint_normal(" intr %d", usbi->usbi_intr); 183 184 aprint_normal("\n"); 185 186 return (UNCONF); 187 } 188 189 static int 190 rmixl_usbi_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux) 191 { 192 struct rmixl_usbi_softc *sc = device_private(parent); 193 struct rmixl_usbi_attach_args usbi; 194 195 usbi.usbi_eb_bst = sc->sc_eb_bst; 196 usbi.usbi_el_bst = sc->sc_el_bst; 197 usbi.usbi_addr = cf->cf_loc[RMIXL_USBICF_ADDR]; 198 usbi.usbi_size = cf->cf_loc[RMIXL_USBICF_SIZE]; 199 usbi.usbi_intr = cf->cf_loc[RMIXL_USBICF_INTR]; 200 usbi.usbi_dmat = sc->sc_dmat; 201 202 if (config_match(parent, cf, &usbi) > 0) 203 config_attach(parent, cf, &usbi, rmixl_usbi_print); 204 205 return 0; 206 } 207 208 209 void 210 rmixl_usbi_intr_disestablish(void *uh, void *ih) 211 { 212 rmixl_usbi_softc_t *sc = uh; 213 u_int intr; 214 215 for (intr=0; intr <= RMIXL_UB_INTERRUPT_MAX; intr++) { 216 if (ih == &sc->sc_dispatch[intr]) { 217 uint32_t r; 218 219 /* disable this interrupt in the usb interface */ 220 r = RMIXL_USBI_GEN_READ(RMIXL_USB_INTERRUPT_ENABLE); 221 r &= 1 << intr; 222 RMIXL_USBI_GEN_WRITE(RMIXL_USB_INTERRUPT_ENABLE, r); 223 224 /* free the dispatch slot */ 225 sc->sc_dispatch[intr].func = NULL; 226 sc->sc_dispatch[intr].arg = NULL; 227 228 break; 229 } 230 } 231 } 232 233 void * 234 rmixl_usbi_intr_establish(void *uh, u_int intr, int (func)(void *), void *arg) 235 { 236 rmixl_usbi_softc_t *sc = uh; 237 uint32_t r; 238 void *ih = NULL; 239 int s; 240 241 s = splusb(); 242 243 if (intr > RMIXL_UB_INTERRUPT_MAX) { 244 aprint_error_dev(sc->sc_dev, "invalid intr %d\n", intr); 245 goto out; 246 } 247 248 if (sc->sc_dispatch[intr].func != NULL) { 249 aprint_error_dev(sc->sc_dev, "intr %dq busy\n", intr); 250 goto out; 251 } 252 253 sc->sc_dispatch[intr].func = func; 254 sc->sc_dispatch[intr].arg = arg; 255 ih = &sc->sc_dispatch[intr]; 256 257 /* enable this interrupt in the usb interface */ 258 r = RMIXL_USBI_GEN_READ(RMIXL_USB_INTERRUPT_ENABLE); 259 r |= 1 << intr; 260 RMIXL_USBI_GEN_WRITE(RMIXL_USB_INTERRUPT_ENABLE, r); 261 262 out: 263 splx(s); 264 return ih; 265 } 266 267 static int 268 rmixl_usbi_intr(void *arg) 269 { 270 rmixl_usbi_softc_t *sc = arg; 271 uint32_t r; 272 int intr; 273 int rv = 0; 274 275 r = RMIXL_USBI_GEN_READ(RMIXL_USB_INTERRUPT_STATUS); 276 if (r != 0) { 277 for (intr=0; intr <= RMIXL_UB_INTERRUPT_MAX; intr++) { 278 uint32_t bit = 1 << intr; 279 if ((r & bit) != 0) { 280 int (*f)(void *) = sc->sc_dispatch[intr].func; 281 void *a = sc->sc_dispatch[intr].arg; 282 if (f != NULL) { 283 (void)(*f)(a); 284 sc->sc_dispatch[intr].count.ev_count++; 285 rv = 1; 286 } 287 } 288 } 289 } 290 291 return rv; 292 } 293 294 #ifdef RMIXL_USBI_DEBUG 295 int 296 rmixl_usbi_rdump(void) 297 { 298 rmixl_usbi_softc_t *sc = rmixl_usbi_sc; 299 uint32_t r; 300 301 if (sc == NULL) 302 return -1; 303 304 printf("\n%s:\n", __func__); 305 r = RMIXL_USBI_GEN_READ(RMIXL_USB_GEN_CTRL1); 306 printf(" USB_GEN_CTRL1 %#x\n", r); 307 r = RMIXL_USBI_GEN_READ(RMIXL_USB_GEN_CTRL2); 308 printf(" USB_GEN_CTRL2 %#x\n", r); 309 r = RMIXL_USBI_GEN_READ(RMIXL_USB_GEN_CTRL3); 310 printf(" USB_GEN_CTRL3 %#x\n", r); 311 r = RMIXL_USBI_GEN_READ(RMIXL_USB_IOBM_TIMER); 312 printf(" USB_IOBM_TIMER %#x\n", r); 313 r = RMIXL_USBI_GEN_READ(RMIXL_USB_VBUS_TIMER); 314 printf(" USB_VBUS_TIMER %#x\n", r); 315 r = RMIXL_USBI_GEN_READ(RMIXL_USB_BYTESWAP_EN); 316 printf(" USB_BYTESWAP_EN %#x\n", r); 317 r = RMIXL_USBI_GEN_READ(RMIXL_USB_COHERENT_MEM_BASE); 318 printf(" USB_COHERENT_MEM_BASE %#x\n", r); 319 r = RMIXL_USBI_GEN_READ(RMIXL_USB_COHERENT_MEM_LIMIT); 320 printf(" USB_COHERENT_MEM_LIMIT %#x\n", r); 321 r = RMIXL_USBI_GEN_READ(RMIXL_USB_L2ALLOC_MEM_BASE); 322 printf(" USB_L2ALLOC_MEM_BASE %#x\n", r); 323 r = RMIXL_USBI_GEN_READ(RMIXL_USB_L2ALLOC_MEM_LIMIT); 324 printf(" USB_L2ALLOC_MEM_LIMIT %#x\n", r); 325 r = RMIXL_USBI_GEN_READ(RMIXL_USB_READEX_MEM_BASE); 326 printf(" USB_READEX_MEM_BASE %#x\n", r); 327 r = RMIXL_USBI_GEN_READ(RMIXL_USB_READEX_MEM_LIMIT); 328 printf(" USB_READEX_MEM_LIMIT %#x\n", r); 329 r = RMIXL_USBI_GEN_READ(RMIXL_USB_PHY_STATUS); 330 printf(" USB_PHY_STATUS %#x\n", r); 331 r = RMIXL_USBI_GEN_READ(RMIXL_USB_INTERRUPT_STATUS); 332 printf(" USB_INTERRUPT_STATUS %#x\n", r); 333 r = RMIXL_USBI_GEN_READ(RMIXL_USB_INTERRUPT_ENABLE); 334 printf(" USB_INTERRUPT_ENABLE %#x\n", r); 335 336 return 0; 337 } 338 #endif 339