ehci_fdt.c revision 1.6
11.6Sthorpej/* $NetBSD: ehci_fdt.c,v 1.6 2021/01/27 03:10:21 thorpej 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.6Sthorpej__KERNEL_RCSID(0, "$NetBSD: ehci_fdt.c,v 1.6 2021/01/27 03:10:21 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.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.6Sthorpejstatic const struct device_compatible_entry compat_data[] = {
561.6Sthorpej	{ .compat = "generic-ehci" },
571.6Sthorpej	DEVICE_COMPAT_EOL
581.6Sthorpej};
591.6Sthorpej
601.1Sjmcneillstatic int
611.1Sjmcneillehci_fdt_match(device_t parent, cfdata_t cf, void *aux)
621.1Sjmcneill{
631.1Sjmcneill	struct fdt_attach_args * const faa = aux;
641.1Sjmcneill
651.6Sthorpej	return of_compatible_match(faa->faa_phandle, compat_data);
661.1Sjmcneill}
671.1Sjmcneill
681.1Sjmcneillstatic void
691.1Sjmcneillehci_fdt_attach(device_t parent, device_t self, void *aux)
701.1Sjmcneill{
711.1Sjmcneill	struct ehci_softc * const sc = device_private(self);
721.1Sjmcneill	struct fdt_attach_args * const faa = aux;
731.1Sjmcneill	const int phandle = faa->faa_phandle;
741.1Sjmcneill	struct fdtbus_reset *rst;
751.1Sjmcneill	struct fdtbus_phy *phy;
761.1Sjmcneill	struct clk *clk;
771.1Sjmcneill	char intrstr[128];
781.1Sjmcneill	bus_addr_t addr;
791.1Sjmcneill	bus_size_t size;
801.1Sjmcneill	int error;
811.1Sjmcneill	void *ih;
821.1Sjmcneill	u_int n;
831.1Sjmcneill
841.1Sjmcneill	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
851.1Sjmcneill		aprint_error(": couldn't get registers\n");
861.1Sjmcneill		return;
871.1Sjmcneill	}
881.1Sjmcneill
891.1Sjmcneill	/* Enable clocks */
901.1Sjmcneill	for (n = 0; (clk = fdtbus_clock_get_index(phandle, n)) != NULL; n++)
911.1Sjmcneill		if (clk_enable(clk) != 0) {
921.1Sjmcneill			aprint_error(": couldn't enable clock #%d\n", n);
931.1Sjmcneill			return;
941.1Sjmcneill		}
951.1Sjmcneill	/* De-assert resets */
961.1Sjmcneill	for (n = 0; (rst = fdtbus_reset_get_index(phandle, n)) != NULL; n++)
971.1Sjmcneill		if (fdtbus_reset_deassert(rst) != 0) {
981.1Sjmcneill			aprint_error(": couldn't de-assert reset #%d\n", n);
991.1Sjmcneill			return;
1001.1Sjmcneill		}
1011.1Sjmcneill
1021.1Sjmcneill	/* Enable optional phy */
1031.1Sjmcneill	phy = fdtbus_phy_get(phandle, "usb");
1041.1Sjmcneill	if (phy && fdtbus_phy_enable(phy, true) != 0) {
1051.1Sjmcneill		aprint_error(": couldn't enable phy\n");
1061.1Sjmcneill		return;
1071.1Sjmcneill	}
1081.1Sjmcneill
1091.1Sjmcneill	sc->sc_dev = self;
1101.1Sjmcneill	sc->sc_bus.ub_hcpriv = sc;
1111.1Sjmcneill	sc->sc_bus.ub_dmatag = faa->faa_dmat;
1121.1Sjmcneill	sc->sc_bus.ub_revision = USBREV_2_0;
1131.1Sjmcneill	if (of_hasprop(phandle, "has-transaction-translator"))
1141.1Sjmcneill		sc->sc_flags |= EHCIF_ETTF;
1151.1Sjmcneill	else
1161.2Sjmcneill		sc->sc_ncomp = 1;
1171.1Sjmcneill	sc->sc_size = size;
1181.1Sjmcneill	sc->iot = faa->faa_bst;
1191.1Sjmcneill	if (bus_space_map(sc->iot, addr, size, 0, &sc->ioh) != 0) {
1201.1Sjmcneill		aprint_error(": couldn't map registers\n");
1211.1Sjmcneill		return;
1221.1Sjmcneill	}
1231.1Sjmcneill
1241.1Sjmcneill	aprint_naive("\n");
1251.1Sjmcneill	aprint_normal(": EHCI\n");
1261.1Sjmcneill
1271.1Sjmcneill	/* Disable interrupts */
1281.1Sjmcneill	sc->sc_offs = EREAD1(sc, EHCI_CAPLENGTH);
1291.1Sjmcneill	EOWRITE4(sc, EHCI_USBINTR, 0);
1301.1Sjmcneill
1311.1Sjmcneill	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
1321.1Sjmcneill		aprint_error_dev(self, "failed to decode interrupt\n");
1331.1Sjmcneill		return;
1341.1Sjmcneill	}
1351.1Sjmcneill
1361.5Sryo	ih = fdtbus_intr_establish_xname(phandle, 0, IPL_USB, FDT_INTR_MPSAFE,
1371.5Sryo	    ehci_intr, sc, device_xname(self));
1381.1Sjmcneill	if (ih == NULL) {
1391.1Sjmcneill		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
1401.1Sjmcneill		    intrstr);
1411.1Sjmcneill		return;
1421.1Sjmcneill	}
1431.1Sjmcneill	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
1441.1Sjmcneill
1451.1Sjmcneill	error = ehci_init(sc);
1461.1Sjmcneill	if (error) {
1471.1Sjmcneill		aprint_error_dev(self, "init failed, error = %d\n", error);
1481.1Sjmcneill		return;
1491.1Sjmcneill	}
1501.1Sjmcneill
1511.4Sjmcneill	pmf_device_register1(self, NULL, NULL, ehci_shutdown);
1521.4Sjmcneill
1531.1Sjmcneill	sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint);
1541.1Sjmcneill}
155