ehci_fdt.c revision 1.1
1/* $NetBSD: ehci_fdt.c,v 1.1 2017/06/29 17:04:53 jmcneill Exp $ */
2
3/*-
4 * Copyright (c) 2015-2017 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: ehci_fdt.c,v 1.1 2017/06/29 17:04:53 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
48static int	ehci_fdt_match(device_t, cfdata_t, void *);
49static void	ehci_fdt_attach(device_t, device_t, void *);
50
51CFATTACH_DECL2_NEW(ehci_fdt, sizeof(struct ehci_softc),
52	ehci_fdt_match, ehci_fdt_attach, NULL,
53	ehci_activate, NULL, ehci_childdet);
54
55static void
56ehci_fdt_find_companions(struct ehci_softc *sc)
57{
58	const char * drivers[] = { "ohci", "uhci", NULL };
59	device_t comp_dev;
60	int unit, n;
61
62	/* XXX find an ohci or uhci with the same unit as us */
63	unit = device_unit(sc->sc_dev);
64
65	for (n = 0; drivers[n] != NULL; n++) {
66		comp_dev = device_find_by_driver_unit(drivers[n], unit);
67		if (comp_dev != NULL)
68			sc->sc_comps[sc->sc_ncomp++] = comp_dev;
69	}
70}
71
72static int
73ehci_fdt_match(device_t parent, cfdata_t cf, void *aux)
74{
75	const char * const compatible[] = {
76		"generic-ehci",
77		NULL
78	};
79	struct fdt_attach_args * const faa = aux;
80
81	return of_match_compatible(faa->faa_phandle, compatible);
82}
83
84static void
85ehci_fdt_attach(device_t parent, device_t self, void *aux)
86{
87	struct ehci_softc * const sc = device_private(self);
88	struct fdt_attach_args * const faa = aux;
89	const int phandle = faa->faa_phandle;
90	struct fdtbus_reset *rst;
91	struct fdtbus_phy *phy;
92	struct clk *clk;
93	char intrstr[128];
94	bus_addr_t addr;
95	bus_size_t size;
96	int error;
97	void *ih;
98	u_int n;
99
100	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
101		aprint_error(": couldn't get registers\n");
102		return;
103	}
104
105	/* Enable clocks */
106	for (n = 0; (clk = fdtbus_clock_get_index(phandle, n)) != NULL; n++)
107		if (clk_enable(clk) != 0) {
108			aprint_error(": couldn't enable clock #%d\n", n);
109			return;
110		}
111	/* De-assert resets */
112	for (n = 0; (rst = fdtbus_reset_get_index(phandle, n)) != NULL; n++)
113		if (fdtbus_reset_deassert(rst) != 0) {
114			aprint_error(": couldn't de-assert reset #%d\n", n);
115			return;
116		}
117
118	/* Enable optional phy */
119	phy = fdtbus_phy_get(phandle, "usb");
120	if (phy && fdtbus_phy_enable(phy, true) != 0) {
121		aprint_error(": couldn't enable phy\n");
122		return;
123	}
124
125	sc->sc_dev = self;
126	sc->sc_bus.ub_hcpriv = sc;
127	sc->sc_bus.ub_dmatag = faa->faa_dmat;
128	sc->sc_bus.ub_revision = USBREV_2_0;
129	if (of_hasprop(phandle, "has-transaction-translator"))
130		sc->sc_flags |= EHCIF_ETTF;
131	else
132		ehci_fdt_find_companions(sc);
133	sc->sc_id_vendor = 0;
134	strlcpy(sc->sc_vendor, "Generic", sizeof(sc->sc_vendor));
135	sc->sc_size = size;
136	sc->iot = faa->faa_bst;
137	if (bus_space_map(sc->iot, addr, size, 0, &sc->ioh) != 0) {
138		aprint_error(": couldn't map registers\n");
139		return;
140	}
141
142	aprint_naive("\n");
143	aprint_normal(": EHCI\n");
144
145	/* Disable interrupts */
146	sc->sc_offs = EREAD1(sc, EHCI_CAPLENGTH);
147	EOWRITE4(sc, EHCI_USBINTR, 0);
148
149	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
150		aprint_error_dev(self, "failed to decode interrupt\n");
151		return;
152	}
153
154	ih = fdtbus_intr_establish(phandle, 0, IPL_USB, FDT_INTR_MPSAFE,
155	    ehci_intr, sc);
156	if (ih == NULL) {
157		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
158		    intrstr);
159		return;
160	}
161	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
162
163	error = ehci_init(sc);
164	if (error) {
165		aprint_error_dev(self, "init failed, error = %d\n", error);
166		return;
167	}
168
169	sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint);
170}
171