11.1Sskrll/* $NetBSD: cdns3_fdt.c,v 1.1 2024/01/18 07:48:57 skrll Exp $ */ 21.1Sskrll 31.1Sskrll/*- 41.1Sskrll * Copyright (c) 2023 The NetBSD Foundation, Inc. 51.1Sskrll * All rights reserved. 61.1Sskrll * 71.1Sskrll * This code is derived from software contributed to The NetBSD Foundation 81.1Sskrll * by Nick Hudson 91.1Sskrll * 101.1Sskrll * Redistribution and use in source and binary forms, with or without 111.1Sskrll * modification, are permitted provided that the following conditions 121.1Sskrll * are met: 131.1Sskrll * 1. Redistributions of source code must retain the above copyright 141.1Sskrll * notice, this list of conditions and the following disclaimer. 151.1Sskrll * 2. Redistributions in binary form must reproduce the above copyright 161.1Sskrll * notice, this list of conditions and the following disclaimer in the 171.1Sskrll * documentation and/or other materials provided with the distribution. 181.1Sskrll * 191.1Sskrll * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 201.1Sskrll * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 211.1Sskrll * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 221.1Sskrll * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 231.1Sskrll * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 241.1Sskrll * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 251.1Sskrll * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 261.1Sskrll * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 271.1Sskrll * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 281.1Sskrll * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 291.1Sskrll * POSSIBILITY OF SUCH DAMAGE. 301.1Sskrll */ 311.1Sskrll 321.1Sskrll#include <sys/cdefs.h> 331.1Sskrll__KERNEL_RCSID(0, "$NetBSD: cdns3_fdt.c,v 1.1 2024/01/18 07:48:57 skrll Exp $"); 341.1Sskrll 351.1Sskrll#include <sys/param.h> 361.1Sskrll 371.1Sskrll#include <sys/bus.h> 381.1Sskrll#include <sys/device.h> 391.1Sskrll#include <sys/intr.h> 401.1Sskrll 411.1Sskrll#include <dev/usb/xhcireg.h> 421.1Sskrll#include <dev/usb/xhcivar.h> 431.1Sskrll 441.1Sskrll#include <dev/fdt/fdtvar.h> 451.1Sskrll 461.1Sskrllstruct cdns3_fdt_softc { 471.1Sskrll struct xhci_softc sc_xhci; 481.1Sskrll bus_space_tag_t sc_otg_bst; 491.1Sskrll bus_space_handle_t sc_otg_bsh; 501.1Sskrll void *sc_ih; 511.1Sskrll}; 521.1Sskrll 531.1Sskrll#define OTGRD4(sc, reg) \ 541.1Sskrll bus_space_read_4((sc)->sc_otg_bst, (sc)->sc_otg_bsh, (reg)) 551.1Sskrll#define OTGWR4(sc, reg, val) \ 561.1Sskrll bus_space_write_4((sc)->sc_otg_bst, (sc)->sc_otg_bsh, (reg), (val)) 571.1Sskrll 581.1Sskrll 591.1Sskrll/* 601.1Sskrll * Cadence USB3 controller. 611.1Sskrll */ 621.1Sskrll 631.1Sskrll#define OTG_DID 0x0000 641.1Sskrll#define OTG_DID_V1 0x4024e 651.1Sskrll#define OTG_CMD 0x10 661.1Sskrll#define OTG_CMD_HOST_BUS_REQ __BIT(1) 671.1Sskrll#define OTG_CMD_OTG_DIS __BIT(3) 681.1Sskrll#define OTG_STS 0x14 691.1Sskrll#define OTG_STS_XHCI_READY __BIT(26) 701.1Sskrll 711.1Sskrll 721.1Sskrllstatic const struct device_compatible_entry compat_data[] = { 731.1Sskrll { .compat = "cdns,usb3" }, 741.1Sskrll DEVICE_COMPAT_EOL 751.1Sskrll}; 761.1Sskrll 771.1Sskrllstatic int 781.1Sskrllcdns3_fdt_match(device_t parent, cfdata_t cf, void *aux) 791.1Sskrll{ 801.1Sskrll struct fdt_attach_args * const faa = aux; 811.1Sskrll 821.1Sskrll return of_compatible_match(faa->faa_phandle, compat_data); 831.1Sskrll} 841.1Sskrll 851.1Sskrllstatic void 861.1Sskrllcdns3_fdt_attach(device_t parent, device_t self, void *aux) 871.1Sskrll{ 881.1Sskrll struct cdns3_fdt_softc * const cfsc = device_private(self); 891.1Sskrll struct xhci_softc * const sc = &cfsc->sc_xhci; 901.1Sskrll struct fdt_attach_args * const faa = aux; 911.1Sskrll const int phandle = faa->faa_phandle; 921.1Sskrll 931.1Sskrll struct fdtbus_phy *phy; 941.1Sskrll char intrstr[128]; 951.1Sskrll bus_addr_t addr; 961.1Sskrll bus_size_t size; 971.1Sskrll int error; 981.1Sskrll 991.1Sskrll /* 1001.1Sskrll * Only host mode is supported, but this includes otg devices 1011.1Sskrll * that have 'usb-role-switch' and 'role-switch-default-mode' of 1021.1Sskrll * 'host' 1031.1Sskrll */ 1041.1Sskrll const char *dr_mode = fdtbus_get_string(phandle, "dr_mode"); 1051.1Sskrll if (dr_mode == NULL || strcmp(dr_mode, "otg") == 0) { 1061.1Sskrll bool ok = false; 1071.1Sskrll if (of_hasprop(phandle, "usb-role-switch")) { 1081.1Sskrll const char *rsdm = fdtbus_get_string(phandle, 1091.1Sskrll "role-switch-default-mode"); 1101.1Sskrll if (rsdm != NULL && strcmp(rsdm, "host") == 0) 1111.1Sskrll ok = true; 1121.1Sskrll 1131.1Sskrll if (!ok) { 1141.1Sskrll aprint_error(": host is not default mode\n"); 1151.1Sskrll return; 1161.1Sskrll } 1171.1Sskrll } 1181.1Sskrll if (!ok) { 1191.1Sskrll aprint_error(": cannot switch 'otg' mode to host\n"); 1201.1Sskrll return; 1211.1Sskrll } 1221.1Sskrll } else if (strcmp(dr_mode, "host") != 0) { 1231.1Sskrll aprint_error(": '%s' not supported\n", dr_mode); 1241.1Sskrll return; 1251.1Sskrll } 1261.1Sskrll 1271.1Sskrll sc->sc_dev = self; 1281.1Sskrll sc->sc_bus.ub_hcpriv = sc; 1291.1Sskrll sc->sc_bus.ub_dmatag = faa->faa_dmat; 1301.1Sskrll sc->sc_iot = faa->faa_bst; 1311.1Sskrll 1321.1Sskrll bus_space_handle_t bsh; 1331.1Sskrll if (fdtbus_get_reg_byname(phandle, "otg", &addr, &size) != 0 || 1341.1Sskrll bus_space_map(faa->faa_bst, addr, size, 0, &bsh) != 0) { 1351.1Sskrll aprint_error(": couldn't map otg registers\n"); 1361.1Sskrll return; 1371.1Sskrll } 1381.1Sskrll cfsc->sc_otg_bst = faa->faa_bst; 1391.1Sskrll cfsc->sc_otg_bsh = bsh; 1401.1Sskrll 1411.1Sskrll if (fdtbus_get_reg_byname(phandle, "xhci", &addr, &size) != 0 || 1421.1Sskrll bus_space_map(faa->faa_bst, addr, size, 0, &bsh) != 0) { 1431.1Sskrll aprint_error(": couldn't map xhci registers\n"); 1441.1Sskrll return; 1451.1Sskrll } 1461.1Sskrll 1471.1Sskrll sc->sc_ios = size; 1481.1Sskrll sc->sc_ioh = bsh; 1491.1Sskrll 1501.1Sskrll uint32_t did = OTGRD4(cfsc, OTG_DID); 1511.1Sskrll if (did != OTG_DID_V1) { 1521.1Sskrll aprint_error(": unsupported IP (%#x)\n", did); 1531.1Sskrll return; 1541.1Sskrll } 1551.1Sskrll OTGWR4(cfsc, OTG_CMD, OTG_CMD_HOST_BUS_REQ | OTG_CMD_OTG_DIS); 1561.1Sskrll int tries; 1571.1Sskrll uint32_t sts; 1581.1Sskrll for (tries = 100; tries > 0; tries--) { 1591.1Sskrll sts = OTGRD4(cfsc, OTG_STS); 1601.1Sskrll if (sts & OTG_STS_XHCI_READY) 1611.1Sskrll break; 1621.1Sskrll delay(1000); 1631.1Sskrll } 1641.1Sskrll if (tries == 0) { 1651.1Sskrll aprint_error(": not ready (%#x)\n", sts); 1661.1Sskrll return; 1671.1Sskrll } 1681.1Sskrll 1691.1Sskrll aprint_naive("\n"); 1701.1Sskrll aprint_normal(": Cadence USB3 XHCI\n"); 1711.1Sskrll 1721.1Sskrll /* Enable PHY devices */ 1731.1Sskrll for (u_int i = 0; ; i++) { 1741.1Sskrll phy = fdtbus_phy_get_index(phandle, i); 1751.1Sskrll if (phy == NULL) 1761.1Sskrll break; 1771.1Sskrll if (fdtbus_phy_enable(phy, true) != 0) 1781.1Sskrll aprint_error_dev(self, "couldn't enable phy #%d\n", i); 1791.1Sskrll } 1801.1Sskrll 1811.1Sskrll if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) { 1821.1Sskrll aprint_error_dev(self, "failed to decode interrupt\n"); 1831.1Sskrll return; 1841.1Sskrll } 1851.1Sskrll 1861.1Sskrll void *ih = fdtbus_intr_establish_xname(phandle, 0, IPL_USB, 1871.1Sskrll FDT_INTR_MPSAFE, xhci_intr, sc, device_xname(self)); 1881.1Sskrll if (ih == NULL) { 1891.1Sskrll aprint_error_dev(self, "couldn't establish interrupt on %s\n", 1901.1Sskrll intrstr); 1911.1Sskrll return; 1921.1Sskrll } 1931.1Sskrll aprint_normal_dev(self, "interrupting on %s\n", intrstr); 1941.1Sskrll 1951.1Sskrll sc->sc_bus.ub_revision = USBREV_3_0; 1961.1Sskrll error = xhci_init(sc); 1971.1Sskrll if (error) { 1981.1Sskrll aprint_error_dev(self, "init failed, error = %d\n", error); 1991.1Sskrll return; 2001.1Sskrll } 2011.1Sskrll 2021.1Sskrll sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint, CFARGS_NONE); 2031.1Sskrll sc->sc_child2 = config_found(self, &sc->sc_bus2, usbctlprint, 2041.1Sskrll CFARGS_NONE); 2051.1Sskrll} 2061.1Sskrll 2071.1Sskrll 2081.1SskrllCFATTACH_DECL2_NEW(cdns3_fdt, sizeof(struct cdns3_fdt_softc), 2091.1Sskrll cdns3_fdt_match, cdns3_fdt_attach, NULL, 2101.1Sskrll xhci_activate, NULL, xhci_childdet); 211