ehci_fdt.c revision 1.1
11.1Sjmcneill/* $NetBSD: ehci_fdt.c,v 1.1 2017/06/29 17:04:53 jmcneill Exp $ */ 21.1Sjmcneill 31.1Sjmcneill/*- 41.1Sjmcneill * Copyright (c) 2015-2017 Jared McNeill <jmcneill@invisible.ca> 51.1Sjmcneill * All rights reserved. 61.1Sjmcneill * 71.1Sjmcneill * Redistribution and use in source and binary forms, with or without 81.1Sjmcneill * modification, are permitted provided that the following conditions 91.1Sjmcneill * are met: 101.1Sjmcneill * 1. Redistributions of source code must retain the above copyright 111.1Sjmcneill * notice, this list of conditions and the following disclaimer. 121.1Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright 131.1Sjmcneill * notice, this list of conditions and the following disclaimer in the 141.1Sjmcneill * documentation and/or other materials provided with the distribution. 151.1Sjmcneill * 161.1Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 171.1Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 181.1Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 191.1Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 201.1Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 211.1Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 221.1Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 231.1Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 241.1Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 251.1Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 261.1Sjmcneill * SUCH DAMAGE. 271.1Sjmcneill */ 281.1Sjmcneill 291.1Sjmcneill#include <sys/cdefs.h> 301.1Sjmcneill__KERNEL_RCSID(0, "$NetBSD: ehci_fdt.c,v 1.1 2017/06/29 17:04:53 jmcneill Exp $"); 311.1Sjmcneill 321.1Sjmcneill#include <sys/param.h> 331.1Sjmcneill#include <sys/bus.h> 341.1Sjmcneill#include <sys/device.h> 351.1Sjmcneill#include <sys/intr.h> 361.1Sjmcneill#include <sys/systm.h> 371.1Sjmcneill#include <sys/kernel.h> 381.1Sjmcneill 391.1Sjmcneill#include <dev/usb/usb.h> 401.1Sjmcneill#include <dev/usb/usbdi.h> 411.1Sjmcneill#include <dev/usb/usbdivar.h> 421.1Sjmcneill#include <dev/usb/usb_mem.h> 431.1Sjmcneill#include <dev/usb/ehcireg.h> 441.1Sjmcneill#include <dev/usb/ehcivar.h> 451.1Sjmcneill 461.1Sjmcneill#include <dev/fdt/fdtvar.h> 471.1Sjmcneill 481.1Sjmcneillstatic int ehci_fdt_match(device_t, cfdata_t, void *); 491.1Sjmcneillstatic void ehci_fdt_attach(device_t, device_t, void *); 501.1Sjmcneill 511.1SjmcneillCFATTACH_DECL2_NEW(ehci_fdt, sizeof(struct ehci_softc), 521.1Sjmcneill ehci_fdt_match, ehci_fdt_attach, NULL, 531.1Sjmcneill ehci_activate, NULL, ehci_childdet); 541.1Sjmcneill 551.1Sjmcneillstatic void 561.1Sjmcneillehci_fdt_find_companions(struct ehci_softc *sc) 571.1Sjmcneill{ 581.1Sjmcneill const char * drivers[] = { "ohci", "uhci", NULL }; 591.1Sjmcneill device_t comp_dev; 601.1Sjmcneill int unit, n; 611.1Sjmcneill 621.1Sjmcneill /* XXX find an ohci or uhci with the same unit as us */ 631.1Sjmcneill unit = device_unit(sc->sc_dev); 641.1Sjmcneill 651.1Sjmcneill for (n = 0; drivers[n] != NULL; n++) { 661.1Sjmcneill comp_dev = device_find_by_driver_unit(drivers[n], unit); 671.1Sjmcneill if (comp_dev != NULL) 681.1Sjmcneill sc->sc_comps[sc->sc_ncomp++] = comp_dev; 691.1Sjmcneill } 701.1Sjmcneill} 711.1Sjmcneill 721.1Sjmcneillstatic int 731.1Sjmcneillehci_fdt_match(device_t parent, cfdata_t cf, void *aux) 741.1Sjmcneill{ 751.1Sjmcneill const char * const compatible[] = { 761.1Sjmcneill "generic-ehci", 771.1Sjmcneill NULL 781.1Sjmcneill }; 791.1Sjmcneill struct fdt_attach_args * const faa = aux; 801.1Sjmcneill 811.1Sjmcneill return of_match_compatible(faa->faa_phandle, compatible); 821.1Sjmcneill} 831.1Sjmcneill 841.1Sjmcneillstatic void 851.1Sjmcneillehci_fdt_attach(device_t parent, device_t self, void *aux) 861.1Sjmcneill{ 871.1Sjmcneill struct ehci_softc * const sc = device_private(self); 881.1Sjmcneill struct fdt_attach_args * const faa = aux; 891.1Sjmcneill const int phandle = faa->faa_phandle; 901.1Sjmcneill struct fdtbus_reset *rst; 911.1Sjmcneill struct fdtbus_phy *phy; 921.1Sjmcneill struct clk *clk; 931.1Sjmcneill char intrstr[128]; 941.1Sjmcneill bus_addr_t addr; 951.1Sjmcneill bus_size_t size; 961.1Sjmcneill int error; 971.1Sjmcneill void *ih; 981.1Sjmcneill u_int n; 991.1Sjmcneill 1001.1Sjmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 1011.1Sjmcneill aprint_error(": couldn't get registers\n"); 1021.1Sjmcneill return; 1031.1Sjmcneill } 1041.1Sjmcneill 1051.1Sjmcneill /* Enable clocks */ 1061.1Sjmcneill for (n = 0; (clk = fdtbus_clock_get_index(phandle, n)) != NULL; n++) 1071.1Sjmcneill if (clk_enable(clk) != 0) { 1081.1Sjmcneill aprint_error(": couldn't enable clock #%d\n", n); 1091.1Sjmcneill return; 1101.1Sjmcneill } 1111.1Sjmcneill /* De-assert resets */ 1121.1Sjmcneill for (n = 0; (rst = fdtbus_reset_get_index(phandle, n)) != NULL; n++) 1131.1Sjmcneill if (fdtbus_reset_deassert(rst) != 0) { 1141.1Sjmcneill aprint_error(": couldn't de-assert reset #%d\n", n); 1151.1Sjmcneill return; 1161.1Sjmcneill } 1171.1Sjmcneill 1181.1Sjmcneill /* Enable optional phy */ 1191.1Sjmcneill phy = fdtbus_phy_get(phandle, "usb"); 1201.1Sjmcneill if (phy && fdtbus_phy_enable(phy, true) != 0) { 1211.1Sjmcneill aprint_error(": couldn't enable phy\n"); 1221.1Sjmcneill return; 1231.1Sjmcneill } 1241.1Sjmcneill 1251.1Sjmcneill sc->sc_dev = self; 1261.1Sjmcneill sc->sc_bus.ub_hcpriv = sc; 1271.1Sjmcneill sc->sc_bus.ub_dmatag = faa->faa_dmat; 1281.1Sjmcneill sc->sc_bus.ub_revision = USBREV_2_0; 1291.1Sjmcneill if (of_hasprop(phandle, "has-transaction-translator")) 1301.1Sjmcneill sc->sc_flags |= EHCIF_ETTF; 1311.1Sjmcneill else 1321.1Sjmcneill ehci_fdt_find_companions(sc); 1331.1Sjmcneill sc->sc_id_vendor = 0; 1341.1Sjmcneill strlcpy(sc->sc_vendor, "Generic", sizeof(sc->sc_vendor)); 1351.1Sjmcneill sc->sc_size = size; 1361.1Sjmcneill sc->iot = faa->faa_bst; 1371.1Sjmcneill if (bus_space_map(sc->iot, addr, size, 0, &sc->ioh) != 0) { 1381.1Sjmcneill aprint_error(": couldn't map registers\n"); 1391.1Sjmcneill return; 1401.1Sjmcneill } 1411.1Sjmcneill 1421.1Sjmcneill aprint_naive("\n"); 1431.1Sjmcneill aprint_normal(": EHCI\n"); 1441.1Sjmcneill 1451.1Sjmcneill /* Disable interrupts */ 1461.1Sjmcneill sc->sc_offs = EREAD1(sc, EHCI_CAPLENGTH); 1471.1Sjmcneill EOWRITE4(sc, EHCI_USBINTR, 0); 1481.1Sjmcneill 1491.1Sjmcneill if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) { 1501.1Sjmcneill aprint_error_dev(self, "failed to decode interrupt\n"); 1511.1Sjmcneill return; 1521.1Sjmcneill } 1531.1Sjmcneill 1541.1Sjmcneill ih = fdtbus_intr_establish(phandle, 0, IPL_USB, FDT_INTR_MPSAFE, 1551.1Sjmcneill ehci_intr, sc); 1561.1Sjmcneill if (ih == NULL) { 1571.1Sjmcneill aprint_error_dev(self, "couldn't establish interrupt on %s\n", 1581.1Sjmcneill intrstr); 1591.1Sjmcneill return; 1601.1Sjmcneill } 1611.1Sjmcneill aprint_normal_dev(self, "interrupting on %s\n", intrstr); 1621.1Sjmcneill 1631.1Sjmcneill error = ehci_init(sc); 1641.1Sjmcneill if (error) { 1651.1Sjmcneill aprint_error_dev(self, "init failed, error = %d\n", error); 1661.1Sjmcneill return; 1671.1Sjmcneill } 1681.1Sjmcneill 1691.1Sjmcneill sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint); 1701.1Sjmcneill} 171