ti_ehci.c revision 1.3
11.3Sthorpej/* $NetBSD: ti_ehci.c,v 1.3 2021/01/27 03:10:20 thorpej Exp $ */
21.1Sjmcneill
31.1Sjmcneill/*-
41.1Sjmcneill * Copyright (c) 2015-2019 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.3Sthorpej__KERNEL_RCSID(0, "$NetBSD: ti_ehci.c,v 1.3 2021/01/27 03:10:20 thorpej 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.1Sjmcneill#define	TI_EHCI_NPORTS	3
491.1Sjmcneill
501.1Sjmcneillstatic int	ti_ehci_match(device_t, cfdata_t, void *);
511.1Sjmcneillstatic void	ti_ehci_attach(device_t, device_t, void *);
521.1Sjmcneill
531.1SjmcneillCFATTACH_DECL2_NEW(ti_ehci, sizeof(struct ehci_softc),
541.1Sjmcneill	ti_ehci_match, ti_ehci_attach, NULL,
551.1Sjmcneill	ehci_activate, NULL, ehci_childdet);
561.1Sjmcneill
571.3Sthorpejstatic const struct device_compatible_entry compat_data[] = {
581.3Sthorpej	{ .compat = "ti,ehci-omap" },
591.3Sthorpej	DEVICE_COMPAT_EOL
601.3Sthorpej};
611.3Sthorpej
621.1Sjmcneillstatic int
631.1Sjmcneillti_ehci_match(device_t parent, cfdata_t cf, void *aux)
641.1Sjmcneill{
651.1Sjmcneill	struct fdt_attach_args * const faa = aux;
661.1Sjmcneill
671.3Sthorpej	return of_compatible_match(faa->faa_phandle, compat_data);
681.1Sjmcneill}
691.1Sjmcneill
701.1Sjmcneillstatic void
711.1Sjmcneillti_ehci_attach(device_t parent, device_t self, void *aux)
721.1Sjmcneill{
731.1Sjmcneill	struct ehci_softc * const sc = device_private(self);
741.1Sjmcneill	struct fdt_attach_args * const faa = aux;
751.1Sjmcneill	const int phandle = faa->faa_phandle;
761.1Sjmcneill	struct fdtbus_reset *rst;
771.1Sjmcneill	struct fdtbus_phy *phy;
781.1Sjmcneill	struct clk *clk;
791.1Sjmcneill	char intrstr[128];
801.1Sjmcneill	bus_addr_t addr;
811.1Sjmcneill	bus_size_t size;
821.1Sjmcneill	int error;
831.1Sjmcneill	void *ih;
841.1Sjmcneill	u_int n;
851.1Sjmcneill
861.1Sjmcneill	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
871.1Sjmcneill		aprint_error(": couldn't get registers\n");
881.1Sjmcneill		return;
891.1Sjmcneill	}
901.1Sjmcneill
911.1Sjmcneill	/* Enable clocks */
921.1Sjmcneill	for (n = 0; (clk = fdtbus_clock_get_index(phandle, n)) != NULL; n++)
931.1Sjmcneill		if (clk_enable(clk) != 0) {
941.1Sjmcneill			aprint_error(": couldn't enable clock #%d\n", n);
951.1Sjmcneill			return;
961.1Sjmcneill		}
971.1Sjmcneill	/* De-assert resets */
981.1Sjmcneill	for (n = 0; (rst = fdtbus_reset_get_index(phandle, n)) != NULL; n++)
991.1Sjmcneill		if (fdtbus_reset_deassert(rst) != 0) {
1001.1Sjmcneill			aprint_error(": couldn't de-assert reset #%d\n", n);
1011.1Sjmcneill			return;
1021.1Sjmcneill		}
1031.1Sjmcneill
1041.1Sjmcneill	sc->sc_dev = self;
1051.1Sjmcneill	sc->sc_bus.ub_hcpriv = sc;
1061.1Sjmcneill	sc->sc_bus.ub_dmatag = faa->faa_dmat;
1071.1Sjmcneill	sc->sc_bus.ub_revision = USBREV_2_0;
1081.1Sjmcneill	if (of_hasprop(phandle, "has-transaction-translator"))
1091.1Sjmcneill		sc->sc_flags |= EHCIF_ETTF;
1101.1Sjmcneill	else
1111.1Sjmcneill		sc->sc_ncomp = 1;
1121.1Sjmcneill	sc->sc_size = size;
1131.1Sjmcneill	sc->iot = faa->faa_bst;
1141.1Sjmcneill	if (bus_space_map(sc->iot, addr, size, 0, &sc->ioh) != 0) {
1151.1Sjmcneill		aprint_error(": couldn't map registers\n");
1161.1Sjmcneill		return;
1171.1Sjmcneill	}
1181.1Sjmcneill
1191.1Sjmcneill	aprint_naive("\n");
1201.1Sjmcneill	aprint_normal(": EHCI\n");
1211.1Sjmcneill
1221.1Sjmcneill	/* Enable PHYs */
1231.1Sjmcneill	for (n = 0; n < TI_EHCI_NPORTS; n++) {
1241.1Sjmcneill		phy = fdtbus_phy_get_index(phandle, n);
1251.1Sjmcneill		if (phy && fdtbus_phy_enable(phy, true) != 0) {
1261.1Sjmcneill			aprint_error(": couldn't enable phy\n");
1271.1Sjmcneill			return;
1281.1Sjmcneill		}
1291.1Sjmcneill	}
1301.1Sjmcneill
1311.1Sjmcneill	/* Disable interrupts */
1321.1Sjmcneill	sc->sc_offs = EREAD1(sc, EHCI_CAPLENGTH);
1331.1Sjmcneill	EOWRITE4(sc, EHCI_USBINTR, 0);
1341.1Sjmcneill
1351.1Sjmcneill	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
1361.1Sjmcneill		aprint_error_dev(self, "failed to decode interrupt\n");
1371.1Sjmcneill		return;
1381.1Sjmcneill	}
1391.1Sjmcneill
1401.2Sjmcneill	ih = fdtbus_intr_establish_xname(phandle, 0, IPL_USB, FDT_INTR_MPSAFE,
1411.2Sjmcneill	    ehci_intr, sc, device_xname(self));
1421.1Sjmcneill	if (ih == NULL) {
1431.1Sjmcneill		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
1441.1Sjmcneill		    intrstr);
1451.1Sjmcneill		return;
1461.1Sjmcneill	}
1471.1Sjmcneill	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
1481.1Sjmcneill
1491.1Sjmcneill	error = ehci_init(sc);
1501.1Sjmcneill	if (error) {
1511.1Sjmcneill		aprint_error_dev(self, "init failed, error = %d\n", error);
1521.1Sjmcneill		return;
1531.1Sjmcneill	}
1541.1Sjmcneill
1551.1Sjmcneill	pmf_device_register1(self, NULL, NULL, ehci_shutdown);
1561.1Sjmcneill
1571.1Sjmcneill	sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint);
1581.1Sjmcneill}
159