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