ralink_ehci.c revision 1.6 1 /* $NetBSD: ralink_ehci.c,v 1.6 2016/04/23 10:15:30 skrll Exp $ */
2 /*-
3 * Copyright (c) 2011 CradlePoint Technology, Inc.
4 * All rights reserved.
5 *
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 CRADLEPOINT TECHNOLOGY, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /* ralink_ehci.c -- Ralink EHCI USB Driver */
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: ralink_ehci.c,v 1.6 2016/04/23 10:15:30 skrll Exp $");
33
34 #include <sys/param.h>
35 #include <sys/bus.h>
36
37 #include <dev/usb/usb.h>
38 #include <dev/usb/usbdi.h>
39 #include <dev/usb/usbdivar.h>
40 #include <dev/usb/usb_mem.h>
41
42 #include <dev/usb/ehcireg.h>
43 #include <dev/usb/ehcivar.h>
44
45 #include <mips/ralink/ralink_usbhcvar.h>
46
47 #include <mips/ralink/ralink_var.h>
48 #include <mips/ralink/ralink_reg.h>
49
50 struct ralink_ehci_softc {
51 struct ehci_softc sc_ehci;
52 void *sc_ih;
53 };
54
55 static int ralink_ehci_match(device_t, cfdata_t, void *);
56 static void ralink_ehci_attach(device_t, device_t, void *);
57 static int ralink_ehci_detach(device_t, int);
58
59 CFATTACH_DECL2_NEW(ralink_ehci, sizeof(struct ralink_ehci_softc),
60 ralink_ehci_match, ralink_ehci_attach, ralink_ehci_detach,
61 ehci_activate, NULL, ehci_childdet);
62
63 static TAILQ_HEAD(, ralink_usb_hc) ralink_usb_alldevs =
64 TAILQ_HEAD_INITIALIZER(ralink_usb_alldevs);
65
66 #if 0
67 struct usb_hc_alldevs ralink_usb_alldevs = TAILQ_HEAD_INITIALIZER(ralink_usb_alldevs);
68 #endif
69
70 /*
71 * ralink_ehci_match
72 */
73 static int
74 ralink_ehci_match(device_t parent, cfdata_t cf, void *aux)
75 {
76
77 return 1;
78 }
79
80 /*
81 * ralink_ehci_attach
82 */
83 static void
84 ralink_ehci_attach(device_t parent, device_t self, void *aux)
85 {
86 struct ralink_ehci_softc * const sc = device_private(self);
87 const struct mainbus_attach_args *ma = aux;
88 struct ralink_usb_hc *ruh;
89 int error;
90
91 aprint_naive(": EHCI USB controller\n");
92 aprint_normal(": EHCI USB controller\n");
93
94 sc->sc_ehci.sc_dev = self;
95 sc->sc_ehci.sc_bus.ub_hcpriv = sc;
96 sc->sc_ehci.iot = ma->ma_memt;
97 sc->sc_ehci.sc_bus.ub_dmatag = ma->ma_dmat;
98
99 /* Map EHCI registers */
100 if ((error = bus_space_map(sc->sc_ehci.iot, RA_USB_EHCI_BASE,
101 RA_USB_BLOCK_SIZE, 0, &sc->sc_ehci.ioh)) != 0) {
102 aprint_error_dev(self, "can't map EHCI registers, "
103 "error=%d\n", error);
104 return;
105 }
106
107 sc->sc_ehci.sc_size = RA_USB_BLOCK_SIZE;
108 sc->sc_ehci.sc_bus.ub_revision = USBREV_2_0;
109
110 #if defined(RALINK_EHCI_DEBUG)
111 aprint_normal_dev(self, "sc %p ma %p\n", sc, ma);
112 aprint_normal_dev(self, "memt %p dmat %p\n", ma->ma_memt, ma->ma_dmat);
113 aprint_normal_dev(self, "EHCI HCCAPBASE=%#x\n",
114 EREAD4(&sc->sc_ehci, EHCI_CAPLENGTH));
115 aprint_normal_dev(self, "EHCI HCSPARAMS=%#x\n",
116 EREAD4(&sc->sc_ehci, EHCI_HCSPARAMS));
117 aprint_normal_dev(self, "EHCI HCCPARAMS=%#x\n",
118 EREAD4(&sc->sc_ehci, EHCI_HCCPARAMS));
119 aprint_normal_dev(self, "EHCI HCSP_PORTROUTE=%#x\n",
120 EREAD4(&sc->sc_ehci, EHCI_HCSP_PORTROUTE));
121 #endif
122
123 /* Disable EHCI interrupts. */
124 sc->sc_ehci.sc_offs = EREAD1(&sc->sc_ehci, EHCI_CAPLENGTH);
125 EOWRITE4(&sc->sc_ehci, EHCI_USBINTR, 0);
126
127 #if defined(RALINK_EHCI_DEBUG)
128 aprint_normal_dev(self, "EHCI USBCMD=%#x USBSTS=%#x USBINTR=%#x\n",
129 EOREAD4(&sc->sc_ehci, EHCI_USBCMD),
130 EOREAD4(&sc->sc_ehci, EHCI_USBSTS),
131 EOREAD4(&sc->sc_ehci, EHCI_USBINTR));
132 #endif
133
134 /* Establish the MIPS level interrupt */
135 sc->sc_ih = ra_intr_establish(RA_IRQ_USB, ehci_intr, sc, 1);
136 if (sc->sc_ih == NULL) {
137 aprint_error_dev(self, "unable to establish irq %d\n",
138 RA_IRQ_USB);
139 goto fail_0;
140 }
141
142 /*
143 * Find companion controllers. According to the spec they always
144 * have lower function numbers so they should be enumerated already.
145 */
146 int ncomp = 0;
147 TAILQ_FOREACH(ruh, &ralink_usb_alldevs, next) {
148 aprint_normal_dev(self, "companion %s\n",
149 device_xname(ruh->usb));
150 sc->sc_ehci.sc_comps[ncomp++] = ruh->usb;
151 if (ncomp >= EHCI_COMPANION_MAX)
152 break;
153 }
154 sc->sc_ehci.sc_ncomp = ncomp;
155
156 /* set vendor for root hub descriptor. */
157 sc->sc_ehci.sc_id_vendor = 0x1814; /* XXX */
158 strlcpy(sc->sc_ehci.sc_vendor, "Ralink", sizeof(sc->sc_ehci.sc_vendor));
159
160 /* Initialize EHCI */
161 int err = ehci_init(&sc->sc_ehci);
162 if (err) {
163 aprint_error_dev(self, "init failed, error=%d\n", err);
164 goto fail_1;
165 }
166
167 if (!pmf_device_register1(self, ehci_suspend, ehci_resume,
168 ehci_shutdown))
169 aprint_error_dev(self, "couldn't establish power handler\n");
170
171 /* Attach usb device. */
172 sc->sc_ehci.sc_child = config_found(self, &sc->sc_ehci.sc_bus,
173 usbctlprint);
174
175 return;
176
177 fail_1:
178 ra_intr_disestablish(sc->sc_ih);
179 sc->sc_ih = NULL;
180 fail_0:
181 bus_space_unmap(sc->sc_ehci.iot, sc->sc_ehci.ioh, sc->sc_ehci.sc_size);
182 sc->sc_ehci.sc_size = 0;
183 }
184
185 static int
186 ralink_ehci_detach(device_t self, int flags)
187 {
188 struct ralink_ehci_softc * const sc = device_private(self);
189 int rv;
190
191 pmf_device_deregister(self);
192
193 rv = ehci_detach(&sc->sc_ehci, flags);
194 if (rv)
195 return rv;
196
197 if (sc->sc_ih != NULL) {
198 ra_intr_disestablish(sc->sc_ih);
199 sc->sc_ih = NULL;
200 }
201
202 if (sc->sc_ehci.sc_size != 0) {
203 bus_space_unmap(sc->sc_ehci.iot, sc->sc_ehci.ioh,
204 sc->sc_ehci.sc_size);
205 sc->sc_ehci.sc_size = 0;
206 }
207
208 return 0;
209 }
210
211 void
212 ralink_usb_hc_add(struct ralink_usb_hc *ruh, device_t usbp)
213 {
214 TAILQ_INSERT_TAIL(&ralink_usb_alldevs, ruh, next);
215 ruh->usb = usbp;
216 }
217
218 void
219 ralink_usb_hc_rem(struct ralink_usb_hc *ruh)
220 {
221 TAILQ_REMOVE(&ralink_usb_alldevs, ruh, next);
222 }
223