ti_ehci.c revision 1.2
1/* $NetBSD: ti_ehci.c,v 1.2 2021/01/15 23:19:33 jmcneill Exp $ */
2
3/*-
4 * Copyright (c) 2015-2019 Jared McNeill <jmcneill@invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__KERNEL_RCSID(0, "$NetBSD: ti_ehci.c,v 1.2 2021/01/15 23:19:33 jmcneill Exp $");
31
32#include <sys/param.h>
33#include <sys/bus.h>
34#include <sys/device.h>
35#include <sys/intr.h>
36#include <sys/systm.h>
37#include <sys/kernel.h>
38
39#include <dev/usb/usb.h>
40#include <dev/usb/usbdi.h>
41#include <dev/usb/usbdivar.h>
42#include <dev/usb/usb_mem.h>
43#include <dev/usb/ehcireg.h>
44#include <dev/usb/ehcivar.h>
45
46#include <dev/fdt/fdtvar.h>
47
48#define	TI_EHCI_NPORTS	3
49
50static int	ti_ehci_match(device_t, cfdata_t, void *);
51static void	ti_ehci_attach(device_t, device_t, void *);
52
53CFATTACH_DECL2_NEW(ti_ehci, sizeof(struct ehci_softc),
54	ti_ehci_match, ti_ehci_attach, NULL,
55	ehci_activate, NULL, ehci_childdet);
56
57static int
58ti_ehci_match(device_t parent, cfdata_t cf, void *aux)
59{
60	const char * const compatible[] = {
61		"ti,ehci-omap",
62		NULL
63	};
64	struct fdt_attach_args * const faa = aux;
65
66	return of_match_compatible(faa->faa_phandle, compatible);
67}
68
69static void
70ti_ehci_attach(device_t parent, device_t self, void *aux)
71{
72	struct ehci_softc * const sc = device_private(self);
73	struct fdt_attach_args * const faa = aux;
74	const int phandle = faa->faa_phandle;
75	struct fdtbus_reset *rst;
76	struct fdtbus_phy *phy;
77	struct clk *clk;
78	char intrstr[128];
79	bus_addr_t addr;
80	bus_size_t size;
81	int error;
82	void *ih;
83	u_int n;
84
85	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
86		aprint_error(": couldn't get registers\n");
87		return;
88	}
89
90	/* Enable clocks */
91	for (n = 0; (clk = fdtbus_clock_get_index(phandle, n)) != NULL; n++)
92		if (clk_enable(clk) != 0) {
93			aprint_error(": couldn't enable clock #%d\n", n);
94			return;
95		}
96	/* De-assert resets */
97	for (n = 0; (rst = fdtbus_reset_get_index(phandle, n)) != NULL; n++)
98		if (fdtbus_reset_deassert(rst) != 0) {
99			aprint_error(": couldn't de-assert reset #%d\n", n);
100			return;
101		}
102
103	sc->sc_dev = self;
104	sc->sc_bus.ub_hcpriv = sc;
105	sc->sc_bus.ub_dmatag = faa->faa_dmat;
106	sc->sc_bus.ub_revision = USBREV_2_0;
107	if (of_hasprop(phandle, "has-transaction-translator"))
108		sc->sc_flags |= EHCIF_ETTF;
109	else
110		sc->sc_ncomp = 1;
111	sc->sc_size = size;
112	sc->iot = faa->faa_bst;
113	if (bus_space_map(sc->iot, addr, size, 0, &sc->ioh) != 0) {
114		aprint_error(": couldn't map registers\n");
115		return;
116	}
117
118	aprint_naive("\n");
119	aprint_normal(": EHCI\n");
120
121	/* Enable PHYs */
122	for (n = 0; n < TI_EHCI_NPORTS; n++) {
123		phy = fdtbus_phy_get_index(phandle, n);
124		if (phy && fdtbus_phy_enable(phy, true) != 0) {
125			aprint_error(": couldn't enable phy\n");
126			return;
127		}
128	}
129
130	/* Disable interrupts */
131	sc->sc_offs = EREAD1(sc, EHCI_CAPLENGTH);
132	EOWRITE4(sc, EHCI_USBINTR, 0);
133
134	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
135		aprint_error_dev(self, "failed to decode interrupt\n");
136		return;
137	}
138
139	ih = fdtbus_intr_establish_xname(phandle, 0, IPL_USB, FDT_INTR_MPSAFE,
140	    ehci_intr, sc, device_xname(self));
141	if (ih == NULL) {
142		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
143		    intrstr);
144		return;
145	}
146	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
147
148	error = ehci_init(sc);
149	if (error) {
150		aprint_error_dev(self, "init failed, error = %d\n", error);
151		return;
152	}
153
154	pmf_device_register1(self, NULL, NULL, ehci_shutdown);
155
156	sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint);
157}
158