ti_motg.c revision 1.4
11.4Sthorpej/* $NetBSD: ti_motg.c,v 1.4 2021/01/27 03:10:20 thorpej Exp $ */ 21.1Sjmcneill/* 31.1Sjmcneill * Copyright (c) 2013 Manuel Bouyer. All rights reserved. 41.1Sjmcneill * 51.1Sjmcneill * Redistribution and use in source and binary forms, with or without 61.1Sjmcneill * modification, are permitted provided that the following conditions 71.1Sjmcneill * are met: 81.1Sjmcneill * 1. Redistributions of source code must retain the above copyright 91.1Sjmcneill * notice, this list of conditions and the following disclaimer. 101.1Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright 111.1Sjmcneill * notice, this list of conditions and the following disclaimer in the 121.1Sjmcneill * documentation and/or other materials provided with the distribution. 131.1Sjmcneill * 141.1Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 151.1Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 161.1Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 171.1Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 181.1Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 191.1Sjmcneill * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 201.1Sjmcneill * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 211.1Sjmcneill * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 221.1Sjmcneill * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 231.1Sjmcneill * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 241.1Sjmcneill */ 251.1Sjmcneill 261.1Sjmcneill#include <sys/cdefs.h> 271.4Sthorpej__KERNEL_RCSID(0, "$NetBSD: ti_motg.c,v 1.4 2021/01/27 03:10:20 thorpej Exp $"); 281.1Sjmcneill 291.1Sjmcneill#include <sys/param.h> 301.1Sjmcneill#include <sys/systm.h> 311.1Sjmcneill#include <sys/device.h> 321.1Sjmcneill#include <sys/conf.h> 331.1Sjmcneill#include <sys/bus.h> 341.1Sjmcneill#include <sys/proc.h> 351.1Sjmcneill#include <sys/kernel.h> 361.1Sjmcneill#include <sys/mutex.h> 371.1Sjmcneill#include <sys/condvar.h> 381.1Sjmcneill 391.1Sjmcneill#include <arm/ti/ti_prcm.h> 401.1Sjmcneill#include <arm/ti/ti_otgreg.h> 411.1Sjmcneill 421.1Sjmcneill#include <dev/usb/usb.h> 431.1Sjmcneill#include <dev/usb/usbdi.h> 441.1Sjmcneill#include <dev/usb/usbdivar.h> 451.1Sjmcneill#include <dev/usb/usb_mem.h> 461.1Sjmcneill#include <dev/usb/motgreg.h> 471.1Sjmcneill#include <dev/usb/motgvar.h> 481.1Sjmcneill#include <dev/usb/usbhist.h> 491.1Sjmcneill 501.1Sjmcneill#include <dev/fdt/fdtvar.h> 511.1Sjmcneill 521.1Sjmcneill#ifdef USB_DEBUG 531.1Sjmcneill#ifndef MOTG_DEBUG 541.1Sjmcneill#define motgdebug 0 551.1Sjmcneill#else 561.1Sjmcneillextern int motgdebug; 571.1Sjmcneill#endif /* MOTG_DEBUG */ 581.1Sjmcneill#endif /* USB_DEBUG */ 591.1Sjmcneill 601.1Sjmcneill#define DPRINTF(FMT,A,B,C,D) USBHIST_LOGN(motgdebug,1,FMT,A,B,C,D) 611.1Sjmcneill#define MOTGHIST_FUNC() USBHIST_FUNC() 621.1Sjmcneill#define MOTGHIST_CALLED(name) USBHIST_CALLED(motgdebug) 631.1Sjmcneill 641.4Sthorpejstatic const struct device_compatible_entry compat_data[] = { 651.4Sthorpej { .compat = "ti,musb-am33xx" }, 661.4Sthorpej DEVICE_COMPAT_EOL 671.1Sjmcneill}; 681.1Sjmcneill 691.1Sjmcneill/* 701.1Sjmcneill * motg device attachement and driver, 711.1Sjmcneill * for the per-port part of the controller: TI-specific part, phy and 721.1Sjmcneill * MI Mentor OTG. 731.1Sjmcneill */ 741.1Sjmcneill 751.1Sjmcneillstruct ti_motg_softc { 761.1Sjmcneill struct motg_softc sc_motg; 771.1Sjmcneill bus_space_tag_t sc_ctrliot; 781.1Sjmcneill bus_space_handle_t sc_ctrlioh; 791.1Sjmcneill void * sc_ctrlih; 801.1Sjmcneill int sc_ctrlport; 811.1Sjmcneill}; 821.1Sjmcneill 831.1Sjmcneillstatic int ti_motg_match(device_t, cfdata_t, void *); 841.1Sjmcneillstatic void ti_motg_attach(device_t, device_t, void *); 851.1Sjmcneillstatic int ti_motg_intr(void *); 861.1Sjmcneillstatic void ti_motg_poll(void *); 871.1Sjmcneill 881.1SjmcneillCFATTACH_DECL_NEW(ti_motg, sizeof(struct ti_motg_softc), 891.1Sjmcneill ti_motg_match, ti_motg_attach, NULL, NULL); 901.1Sjmcneill 911.1Sjmcneillstatic int 921.1Sjmcneillti_motg_match(device_t parent, cfdata_t match, void *aux) 931.1Sjmcneill{ 941.1Sjmcneill struct fdt_attach_args * const faa = aux; 951.1Sjmcneill 961.4Sthorpej return of_compatible_match(faa->faa_phandle, compat_data); 971.1Sjmcneill} 981.1Sjmcneill 991.1Sjmcneillstatic void 1001.1Sjmcneillti_motg_attach(device_t parent, device_t self, void *aux) 1011.1Sjmcneill{ 1021.1Sjmcneill struct ti_motg_softc *sc = device_private(self); 1031.1Sjmcneill struct fdt_attach_args * const faa = aux; 1041.1Sjmcneill const int phandle = faa->faa_phandle; 1051.1Sjmcneill char intrstr[128]; 1061.1Sjmcneill bus_addr_t addr[2]; 1071.1Sjmcneill bus_size_t size[2]; 1081.1Sjmcneill uint32_t val; 1091.1Sjmcneill 1101.1Sjmcneill MOTGHIST_FUNC(); MOTGHIST_CALLED(); 1111.1Sjmcneill 1121.1Sjmcneill if (fdtbus_get_reg_byname(phandle, "mc", &addr[0], &size[0]) != 0 || 1131.1Sjmcneill fdtbus_get_reg_byname(phandle, "control", &addr[1], &size[1])) { 1141.1Sjmcneill aprint_error(": couldn't get registers\n"); 1151.1Sjmcneill return; 1161.1Sjmcneill } 1171.1Sjmcneill 1181.1Sjmcneill if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) { 1191.1Sjmcneill aprint_error(": couldn't decode interrupt\n"); 1201.1Sjmcneill return; 1211.1Sjmcneill } 1221.1Sjmcneill 1231.1Sjmcneill sc->sc_motg.sc_dev = self; 1241.1Sjmcneill sc->sc_ctrliot = faa->faa_bst; 1251.1Sjmcneill if (bus_space_map(sc->sc_ctrliot, addr[1], size[1], 0, &sc->sc_ctrlioh) != 0) { 1261.1Sjmcneill aprint_error(": couldn't map registers\n"); 1271.1Sjmcneill return; 1281.1Sjmcneill } 1291.3Sjmcneill sc->sc_ctrlih = fdtbus_intr_establish_xname(phandle, 0, IPL_USB, 0, 1301.3Sjmcneill ti_motg_intr, sc, device_xname(self)); 1311.1Sjmcneill sc->sc_motg.sc_bus.ub_dmatag = faa->faa_dmat; 1321.1Sjmcneill 1331.1Sjmcneill val = TIOTG_USBC_READ4(sc, USBCTRL_REV); 1341.1Sjmcneill aprint_normal(": 0x%x version v%d.%d.%d", val, 1351.1Sjmcneill (val >> 8) & 7, (val >> 6) & 3, val & 63); 1361.1Sjmcneill 1371.1Sjmcneill /* XXX configure mode */ 1381.1Sjmcneill#if 0 1391.1Sjmcneill if (sc->sc_ctrlport == 0) 1401.1Sjmcneill sc->sc_motg.sc_mode = MOTG_MODE_DEVICE; 1411.1Sjmcneill else 1421.1Sjmcneill sc->sc_motg.sc_mode = MOTG_MODE_HOST; 1431.1Sjmcneill#else 1441.1Sjmcneill /* XXXXX 1451.1Sjmcneill * Both ports always the host mode only. 1461.1Sjmcneill * And motg(4) doesn't supports device and OTG modes. 1471.1Sjmcneill */ 1481.1Sjmcneill sc->sc_motg.sc_mode = MOTG_MODE_HOST; 1491.1Sjmcneill#endif 1501.1Sjmcneill if (sc->sc_motg.sc_mode == MOTG_MODE_HOST) { 1511.1Sjmcneill val = TIOTG_USBC_READ4(sc, USBCTRL_MODE); 1521.1Sjmcneill val |= USBCTRL_MODE_IDDIGMUX; 1531.1Sjmcneill val &= ~USBCTRL_MODE_IDDIG; 1541.1Sjmcneill TIOTG_USBC_WRITE4(sc, USBCTRL_MODE, val); 1551.1Sjmcneill TIOTG_USBC_WRITE4(sc, USBCTRL_UTMI, USBCTRL_UTMI_FSDATAEXT); 1561.1Sjmcneill } else { 1571.1Sjmcneill val = TIOTG_USBC_READ4(sc, USBCTRL_MODE); 1581.1Sjmcneill val |= USBCTRL_MODE_IDDIGMUX; 1591.1Sjmcneill val |= USBCTRL_MODE_IDDIG; 1601.1Sjmcneill TIOTG_USBC_WRITE4(sc, USBCTRL_MODE, val); 1611.1Sjmcneill } 1621.1Sjmcneill 1631.1Sjmcneill aprint_normal("\n"); 1641.1Sjmcneill aprint_naive("\n"); 1651.1Sjmcneill 1661.1Sjmcneill aprint_normal_dev(self, "interrupting on %s\n", intrstr); 1671.1Sjmcneill 1681.1Sjmcneill sc->sc_motg.sc_iot = faa->faa_bst; 1691.1Sjmcneill if (bus_space_map(sc->sc_motg.sc_iot, addr[0], size[0], 0, 1701.1Sjmcneill &sc->sc_motg.sc_ioh) != 0) { 1711.1Sjmcneill aprint_error_dev(self, "couldn't map mc registers\n"); 1721.1Sjmcneill return; 1731.1Sjmcneill } 1741.1Sjmcneill sc->sc_motg.sc_size = size[0]; 1751.1Sjmcneill sc->sc_motg.sc_intr_poll = ti_motg_poll; 1761.1Sjmcneill sc->sc_motg.sc_intr_poll_arg = sc; 1771.1Sjmcneill delay(10); 1781.1Sjmcneill motg_init(&sc->sc_motg); 1791.1Sjmcneill /* enable interrupts */ 1801.1Sjmcneill TIOTG_USBC_WRITE4(sc, USBCTRL_INTEN_SET0, 0xffffffff); 1811.1Sjmcneill TIOTG_USBC_WRITE4(sc, USBCTRL_INTEN_SET1, 1821.1Sjmcneill USBCTRL_INTEN_USB_ALL & ~USBCTRL_INTEN_USB_SOF); 1831.1Sjmcneill} 1841.1Sjmcneill 1851.1Sjmcneillstatic int 1861.1Sjmcneillti_motg_intr(void *v) 1871.1Sjmcneill{ 1881.1Sjmcneill struct ti_motg_softc *sc = v; 1891.1Sjmcneill uint32_t stat, stat0, stat1; 1901.2Sjmcneill int rv; 1911.1Sjmcneill 1921.1Sjmcneill MOTGHIST_FUNC(); MOTGHIST_CALLED(); 1931.1Sjmcneill 1941.1Sjmcneill mutex_spin_enter(&sc->sc_motg.sc_intr_lock); 1951.1Sjmcneill stat = TIOTG_USBC_READ4(sc, USBCTRL_STAT); 1961.1Sjmcneill stat0 = TIOTG_USBC_READ4(sc, USBCTRL_IRQ_STAT0); 1971.1Sjmcneill stat1 = TIOTG_USBC_READ4(sc, USBCTRL_IRQ_STAT1); 1981.1Sjmcneill DPRINTF("USB %jd 0x%jx 0x%jx stat %jd", 1991.1Sjmcneill sc->sc_ctrlport, stat0, stat1, stat); 2001.2Sjmcneill 2011.1Sjmcneill if (stat0) { 2021.1Sjmcneill TIOTG_USBC_WRITE4(sc, USBCTRL_IRQ_STAT0, stat0); 2031.1Sjmcneill } 2041.1Sjmcneill if (stat1) { 2051.1Sjmcneill TIOTG_USBC_WRITE4(sc, USBCTRL_IRQ_STAT1, stat1); 2061.1Sjmcneill } 2071.2Sjmcneill if (stat1 & USBCTRL_IRQ_STAT1_DRVVBUS) { 2081.2Sjmcneill motg_intr_vbus(&sc->sc_motg, stat & 0x1); 2091.1Sjmcneill } 2101.2Sjmcneill rv = motg_intr(&sc->sc_motg, ((stat0 >> 16) & 0xffff), 2111.2Sjmcneill stat0 & 0xffff, stat1 & 0xff); 2121.1Sjmcneill mutex_spin_exit(&sc->sc_motg.sc_intr_lock); 2131.1Sjmcneill return rv; 2141.1Sjmcneill} 2151.1Sjmcneill 2161.1Sjmcneillstatic void 2171.1Sjmcneillti_motg_poll(void *v) 2181.1Sjmcneill{ 2191.1Sjmcneill ti_motg_intr(v); 2201.1Sjmcneill} 221